fix findValue result parsing

This commit is contained in:
Victor Shyba 2018-08-10 18:23:07 -03:00 committed by Jack Robison
parent a32a0c6401
commit 3f6e928cc2
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 12 additions and 27 deletions

View file

@ -16,7 +16,7 @@ def get_contact(contact_list, node_id, address, port):
def expand_peer(compact_peer_info): def expand_peer(compact_peer_info):
host = ".".join([str(ord(d)) for d in compact_peer_info[:4]]) host = ".".join([str(d) for d in compact_peer_info[:4]])
port, = struct.unpack('>H', compact_peer_info[4:6]) port, = struct.unpack('>H', compact_peer_info[4:6])
peer_node_id = compact_peer_info[6:] peer_node_id = compact_peer_info[6:]
return (peer_node_id, host, port) return (peer_node_id, host, port)
@ -103,9 +103,9 @@ class _IterativeFind:
if self.is_find_value_request and self.key in result: if self.is_find_value_request and self.key in result:
# We have found the value # We have found the value
for peer in result[self.key]: for peer in result[self.key]:
_, host, port = expand_peer(peer) node_id, host, port = expand_peer(peer)
if (host, port) not in self.exclude: if (host, port) not in self.exclude:
self.find_value_result.setdefault(self.key, []).append(peer) self.find_value_result.setdefault(self.key, []).append((node_id, host, port))
if self.find_value_result: if self.find_value_result:
self.finished_deferred.callback(self.find_value_result) self.finished_deferred.callback(self.find_value_result)
else: else:

View file

@ -18,17 +18,9 @@ from .peerfinder import DHTPeerFinder
from .contact import ContactManager from .contact import ContactManager
from .iterativefind import iterativeFind from .iterativefind import iterativeFind
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def expand_peer(compact_peer_info):
host = ".".join([str(ord(d)) for d in compact_peer_info[:4]])
port, = struct.unpack('>H', compact_peer_info[4:6])
peer_node_id = compact_peer_info[6:]
return peer_node_id, host, port
def rpcmethod(func): def rpcmethod(func):
""" Decorator to expose Node methods as remote procedure calls """ Decorator to expose Node methods as remote procedure calls
@ -422,13 +414,7 @@ class Node(MockKademliaHelper):
else: else:
pass pass
expanded_peers = [] defer.returnValue(list(set(find_result.get(key, []) if find_result else [])))
if find_result:
if key in find_result:
for peer in find_result[key]:
expanded = expand_peer(peer)
if expanded not in expanded_peers:
expanded_peers.append(expanded)
# TODO: get this working # TODO: get this working
# if 'closestNodeNoValue' in find_result: # if 'closestNodeNoValue' in find_result:
# closest_node_without_value = find_result['closestNodeNoValue'] # closest_node_without_value = find_result['closestNodeNoValue']
@ -437,7 +423,6 @@ class Node(MockKademliaHelper):
# yield closest_node_without_value.store(key, response.response['token'], self.peerPort) # yield closest_node_without_value.store(key, response.response['token'], self.peerPort)
# except TimeoutError: # except TimeoutError:
# pass # pass
defer.returnValue(expanded_peers)
def addContact(self, contact): def addContact(self, contact):
""" Add/update the given contact; simple wrapper for the same method """ Add/update the given contact; simple wrapper for the same method