def join_network(self, seeds=None, callback=None): if seeds is None: seeds = [] self.log.info('Joining network') # Connect up through seed servers for idx, seed in enumerate(seeds): seeds[idx] = network_util.get_peer_url(seed, "12345") # Connect to persisted peers db_peers = self.get_past_peers() known_peers = list(set(seeds).union(db_peers)) for known_peer in known_peers: self.dht.add_peer(self, known_peer) # Populate routing table by searching for self if known_peers: # Check every one second if we are connected # We could use a PeriodicCallback but I think this is simpler # since this will be repeated in most cases less than 10 times def join_callback(): # If we are not connected to any node, reschedule a check if not self.dht.activePeers: ioloop.IOLoop.instance().call_later(1, join_callback) else: self.search_for_my_node() join_callback() if callback is not None: callback('Joined')
def __init__(self, ob_ctx, db): self.ob_ctx = ob_ctx self.log = logging.getLogger( '[%s] %s' % (ob_ctx.market_id, self.__class__.__name__)) requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) self.db = db self.bitmessage_api = None if (ob_ctx.bm_user, ob_ctx.bm_pass, ob_ctx.bm_port) != (None, None, -1): if not self._connect_to_bitmessage(): self.log.info('Bitmessage not installed or started') self.market_id = ob_ctx.market_id self.nick_mapping = {} self.uri = network_util.get_peer_url(ob_ctx.server_ip, ob_ctx.server_port) self.ip = ob_ctx.server_ip self.nickname = "" self.dev_mode = ob_ctx.dev_mode self.all_messages = ('hello', 'findNode', 'findNodeResponse', 'store') self._setup_settings() ob_ctx.market_id = self.market_id self.dht = DHT(self, self.market_id, self.settings, self.db) TransportLayer.__init__(self, ob_ctx, self.guid, self.nickname) self.start_listener() if ob_ctx.enable_ip_checker and not ob_ctx.seed_mode and not ob_ctx.dev_mode: self.start_ip_address_checker()
def set_ip_address(self, new_ip): self.ip = new_ip self.uri = network_util.get_peer_url(self.ip, self.port) if not self.is_listening: return try: self.stream.close() self.listen() except Exception as e: self.log.error('[Requests] error: %s', e)
def __init__(self, ip, port, ctx, guid, data_cb): super(PeerListener, self).__init__(guid) self.ip = ip self.port = port self._data_cb = data_cb self.uri = network_util.get_peer_url(self.ip, self.port) self.is_listening = False self.ctx = ctx self.socket = None self.stream = None self._ok_msg = None self.log = logging.getLogger(self.__class__.__name__)
def __init__(self, ob_ctx, guid, nickname=None): self.peers = {} self.callbacks = defaultdict(list) self.timeouts = [] self.port = ob_ctx.server_port self.ip = ob_ctx.server_ip self.guid = guid self.market_id = ob_ctx.market_id self.nickname = nickname self.handler = None self.uri = network_util.get_peer_url(self.ip, self.port) self.listener = None # Create one ZeroMQ context to be reused and reduce overhead self.ctx = zmq.Context.instance() self.log = logging.getLogger( '[%s] %s' % (ob_ctx.market_id, self.__class__.__name__))
def __init__(self, ob_ctx, guid, nickname=None): self.peers = {} self.callbacks = defaultdict(list) self.timeouts = [] self.port = ob_ctx.server_port self.ip = ob_ctx.server_ip self.guid = guid self.market_id = ob_ctx.market_id self.nickname = nickname self.handler = None self.uri = network_util.get_peer_url(self.ip, self.port) self.listener = None # Create one ZeroMQ context to be reused and reduce overhead self.ctx = zmq.Context.instance() self.log = logging.getLogger( '[%s] %s' % (ob_ctx.market_id, self.__class__.__name__) )
def __init__(self, ob_ctx, db): self.ob_ctx = ob_ctx self.log = logging.getLogger( '[%s] %s' % (ob_ctx.market_id, self.__class__.__name__) ) requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) self.db = db self.bitmessage_api = None if (ob_ctx.bm_user, ob_ctx.bm_pass, ob_ctx.bm_port) != (None, None, -1): if not self._connect_to_bitmessage(): self.log.info('Bitmessage not installed or started') self.market_id = ob_ctx.market_id self.nick_mapping = {} self.uri = network_util.get_peer_url(ob_ctx.server_ip, ob_ctx.server_port) self.ip = ob_ctx.server_ip self.nickname = "" self.dev_mode = ob_ctx.dev_mode self.all_messages = ( 'hello', 'findNode', 'findNodeResponse', 'store' ) self._setup_settings() ob_ctx.market_id = self.market_id self.dht = DHT(self, self.market_id, self.settings, self.db) TransportLayer.__init__(self, ob_ctx, self.guid, self.nickname) self.start_listener() if ob_ctx.enable_ip_checker and not ob_ctx.seed_mode and not ob_ctx.dev_mode: self.start_ip_address_checker()
def storeKeyValue(self, nodes, key, value, originalPublisherID, age): self.log.debug('Store Key Value: (%s, %s %s)', nodes, key, type(value)) try: value_json = json.loads(value) # Add Notary GUID to index if 'notary_index_add' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if not value_json['notary_index_add'] in existing_index[ 'notaries']: existing_index['notaries'].append( value_json['notary_index_add']) value = existing_index else: value = {'notaries': [value_json['notary_index_add']]} self.log.info('Notaries: %s', existing_index) if 'notary_index_remove' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if value_json['notary_index_remove'] in existing_index[ 'notaries']: existing_index['notaries'].remove( value_json['notary_index_remove']) value = existing_index else: return else: return # Add listing to keyword index if 'keyword_index_add' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if not value_json['keyword_index_add'] in existing_index[ 'listings']: existing_index['listings'].append( value_json['keyword_index_add']) value = existing_index else: value = {'listings': [value_json['keyword_index_add']]} self.log.info('Keyword Index: %s', value) if 'keyword_index_remove' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if value_json['keyword_index_remove'] in existing_index[ 'listings']: existing_index['listings'].remove( value_json['keyword_index_remove']) value = existing_index else: return else: # Not in keyword index anyways return except Exception as e: self.log.debug('Value is not a JSON array: %s', e) now = int(time.time()) originallyPublished = now - age # Store it in your own node self.dataStore.setItem(key, value, now, originallyPublished, originalPublisherID, market_id=self.market_id) for node in nodes: self.log.debug('Sending data to store in DHT: %s', node) uri = network_util.get_peer_url(node[0], node[1]) guid = node[2] peer = self.routingTable.getContact(guid) if guid == self.transport.guid: break if not peer: peer = self.transport.get_crypto_peer(guid, uri) peer.start_handshake() peer.send(proto_store(key, value, originalPublisherID, age))
def storeKeyValue(self, nodes, key, value, originalPublisherID, age): self.log.debug('Store Key Value: (%s, %s %s)', nodes, key, type(value)) try: value_json = json.loads(value) # Add Notary GUID to index if 'notary_index_add' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if not value_json['notary_index_add'] in existing_index['notaries']: existing_index['notaries'].append(value_json['notary_index_add']) value = existing_index else: value = {'notaries': [value_json['notary_index_add']]} self.log.info('Notaries: %s', existing_index) if 'notary_index_remove' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if value_json['notary_index_remove'] in existing_index['notaries']: existing_index['notaries'].remove(value_json['notary_index_remove']) value = existing_index else: return else: return # Add listing to keyword index if 'keyword_index_add' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if not value_json['keyword_index_add'] in existing_index['listings']: existing_index['listings'].append(value_json['keyword_index_add']) value = existing_index else: value = {'listings': [value_json['keyword_index_add']]} self.log.info('Keyword Index: %s', value) if 'keyword_index_remove' in value_json: existing_index = self.dataStore[key] if existing_index is not None: if value_json['keyword_index_remove'] in existing_index['listings']: existing_index['listings'].remove(value_json['keyword_index_remove']) value = existing_index else: return else: # Not in keyword index anyways return except Exception as e: self.log.debug('Value is not a JSON array: %s', e) now = int(time.time()) originallyPublished = now - age # Store it in your own node self.dataStore.setItem( key, value, now, originallyPublished, originalPublisherID, market_id=self.market_id ) for node in nodes: self.log.debug('Sending data to store in DHT: %s', node) uri = network_util.get_peer_url(node[0], node[1]) guid = node[2] peer = self.routingTable.getContact(guid) if guid == self.transport.guid: break if not peer: peer = self.transport.get_crypto_peer(guid, uri) peer.start_handshake() peer.send(proto_store(key, value, originalPublisherID, age))