getPeersForBlob inlinecallbacks refactor

This commit is contained in:
Jack Robison 2017-10-10 13:12:47 -04:00
parent 8e9f3c90a1
commit 46e31d5b45
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -204,28 +204,21 @@ class Node(object):
def announceHaveBlob(self, key, port): def announceHaveBlob(self, key, port):
return self.iterativeAnnounceHaveBlob(key, {'port': port, 'lbryid': self.lbryid}) return self.iterativeAnnounceHaveBlob(key, {'port': port, 'lbryid': self.lbryid})
@defer.inlineCallbacks
def getPeersForBlob(self, blob_hash): def getPeersForBlob(self, blob_hash):
def expand_and_filter(result): result = yield self.iterativeFindValue(blob_hash)
expanded_peers = [] expanded_peers = []
if isinstance(result, dict): if result:
if blob_hash in result: if blob_hash in result:
for peer in result[blob_hash]: for peer in result[blob_hash]:
if self.lbryid != peer[6:]: if self.lbryid != peer[6:]:
host = ".".join([str(ord(d)) for d in peer[:4]]) host = ".".join([str(ord(d)) for d in peer[:4]])
if host == "127.0.0.1": if host == "127.0.0.1" and "from_peer" in result and result["from_peer"] != "self":
if "from_peer" in result: host = result["from_peer"]
if result["from_peer"] != "self": port, = struct.unpack('>H', peer[4:6])
host = result["from_peer"] if (host, port) not in expanded_peers:
port, = struct.unpack('>H', peer[4:6])
expanded_peers.append((host, port)) expanded_peers.append((host, port))
return expanded_peers defer.returnValue(expanded_peers)
def find_failed(err):
return []
d = self.iterativeFindValue(blob_hash)
d.addCallbacks(expand_and_filter, find_failed)
return d
def get_most_popular_hashes(self, num_to_return): def get_most_popular_hashes(self, num_to_return):
return self.hash_watcher.most_popular_hashes(num_to_return) return self.hash_watcher.most_popular_hashes(num_to_return)