def _got_my_address(self, value, key): if not isinstance(value, dict): lg.warn('can not read my address') self.automat('dht-write-failed') return try: addr = value[dht_service.key_to_hash(key)].strip('\n').strip() except: if _Debug: lg.out( 4, 'udp_node._got_my_address ERROR wrong key in response: %r' % value) lg.exc() self.automat('dht-write-failed') return if addr != '%s:%d' % (self.my_address[0], self.my_address[1]): if _Debug: lg.out( 4, 'udp_node._got_my_address ERROR value not fit: %r' % value) self.automat('dht-write-failed') return self.automat('dht-write-success')
def _got_my_incoming(self, value, key, position): if not isinstance(value, dict): if _Debug: lg.out( _DebugLevel, 'udp_node._got_my_incoming no incoming at position: %d' % position) self.automat('dht-read-result', None) return try: myincoming = value[dht_service.key_to_hash(key)] except: if _Debug: lg.out( _DebugLevel, 'udp_node._got_my_incoming ERROR reading my incoming at position: %d\n%r' % (position, value)) self.automat('dht-read-result', None) return if _Debug: lg.out( _DebugLevel, 'udp_node._got_my_incoming found one: %r' % myincoming) self.automat('dht-read-result', myincoming)
def _got_peer_incoming(self, value, key, position): if _Debug: lg.out( _DebugLevel, 'udp_connector._got_peer_incoming at position %d: %d' % (position, len( str(value)))) self.working_deferred = None incoming = None if not isinstance(value, dict): self.automat('dht-read-failed') return try: # incoming = value.values()[0] incoming = value[dht_service.key_to_hash(key)] except: lg.out(2, '%r' % value) lg.exc() self.automat('dht-read-failed') return try: incoming_peer_id, incoming_user_address, _ = incoming.split(b' ') incoming_user_address = incoming_user_address.split(b':') incoming_user_address = (incoming_user_address[0], int(incoming_user_address[1])) except: lg.out(2, '%r' % incoming) lg.exc() self.automat('dht-read-failed') return self.automat('dht-read-success', (incoming_peer_id, incoming_user_address))
def _got_peer_incoming(self, value, key, position): if _Debug: lg.out( _DebugLevel, 'udp_connector._got_peer_incoming at position %d: %d' % (position, len( str(value)))) self.working_deferred = None incoming = None if not isinstance(value, dict): self.automat('dht-read-failed') return try: # incoming = value.values()[0] incoming = value[dht_service.key_to_hash(key)] except: lg.out(2, '%r' % value) lg.exc() self.automat('dht-read-failed') return try: incoming_peer_id, incoming_user_address, time_placed = incoming.split( ' ') incoming_user_address = incoming_user_address.split(':') incoming_user_address = ( incoming_user_address[0], int( incoming_user_address[1])) except: lg.out(2, '%r' % incoming) lg.exc() self.automat('dht-read-failed') return self.automat( 'dht-read-success', (incoming_peer_id, incoming_user_address))
def do_verify(self, dht_value): try: json_value = dht_value[dht_service.key_to_hash( make_dht_key(self.customer_idurl, self._index, self._prefix))] record = json.loads(json_value) record['customer_idurl'] = str(record['customer_idurl']) record['supplier_idurl'] = str(record['supplier_idurl']) record['time'] = int(record['time']) record['signature'] = str(record['signature']) except: record = None if not record: if _Debug: lg.out( _DebugLevel + 6, 'dht_relations.do_verify MISSED %s: empty record found at pos %s' % (self.customer_idurl, self._index)) # record not exist or invalid return self.do_process(record, -1) if record['customer_idurl'] != self.customer_idurl: if _Debug: lg.out( _DebugLevel + 6, 'dht_relations.do_verify ERROR, found invalid record %s at %s' % (self.customer_idurl, self._index)) # record exist but stored for another customer - can be overwritten return self.do_process(record, -1) if record['supplier_idurl'] == my_id.getLocalID(): # TODO: verify signature # TODO: check expiration time if _Debug: lg.out( _DebugLevel + 6, 'dht_relations.do_verify SUCCESS, found own data %s at %s' % (self.customer_idurl, self._index)) # record exist and store relation to me return self.do_process(record, 1) if record['supplier_idurl'] in self._result.values(): lg.out( _DebugLevel + 6, 'dht_relations.do_verify DUPLICATED %s, found second record for supplier %s at %s' % (self.customer_idurl, record['supplier_idurl'], self._index)) # this record from another supplier is duplicated - we can overwrite it return self.do_process(record, -1) if _Debug: lg.out( _DebugLevel + 6, 'dht_relations.do_verify NEXT %s, found another supplier record %s at %s' % (record['supplier_idurl'], self.customer_idurl, self._index)) # this is a correct record from another supplier return self.do_process(record, 0)
def _dht_read_result(self, value, key): self.dht_read_defer = None if not isinstance(value, dict): self.automat('dht-read-failed') return try: v = value[dht_service.key_to_hash(key)] except: lg.out(8, '%r' % value) lg.exc() self.automat('dht-read-failed') return self.automat('dht-read-success', v)
def _got_peer_address(self, value, key): if not isinstance(value, dict): self.automat('dht-read-failed') return try: peer_ip, peer_port = value[dht_service.key_to_hash(key)].split(b':') peer_port = int(peer_port) except: lg.exc() self.automat('dht-read-failed') return if _Debug: lg.out( _DebugLevel, 'udp_connector._got_peer_address %s:%d ~ %s' % (peer_ip, peer_port, self.peer_id)) self.automat('dht-read-success', (peer_ip, peer_port))
def _got_peer_address(self, value, key): if not isinstance(value, dict): self.automat('dht-read-failed') return try: peer_ip, peer_port = value[dht_service.key_to_hash(key)].split(':') peer_port = int(peer_port) except: lg.exc() self.automat('dht-read-failed') return if _Debug: lg.out( _DebugLevel, 'udp_connector._got_peer_address %s:%d ~ %s' % (peer_ip, peer_port, self.peer_id)) self.automat('dht-read-success', (peer_ip, peer_port))