2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
2016-11-03 20:42:45 +01:00
|
|
|
import logging
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
from zope.interface import implements
|
2017-02-16 05:38:33 +01:00
|
|
|
from twisted.internet import defer, reactor
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.interfaces import IPeerFinder
|
2017-02-16 05:38:33 +01:00
|
|
|
from lbrynet.core.utils import short_hash
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2016-11-03 20:42:45 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class DHTPeerFinder(object):
|
|
|
|
"""This class finds peers which have announced to the DHT that they have certain blobs"""
|
|
|
|
implements(IPeerFinder)
|
|
|
|
|
|
|
|
def __init__(self, dht_node, peer_manager):
|
2017-10-27 19:31:42 +02:00
|
|
|
"""
|
|
|
|
dht_node - an instance of dht.Node class
|
|
|
|
peer_manager - an instance of PeerManager class
|
|
|
|
"""
|
2015-08-20 17:27:15 +02:00
|
|
|
self.dht_node = dht_node
|
|
|
|
self.peer_manager = peer_manager
|
|
|
|
self.peers = []
|
|
|
|
|
|
|
|
def stop(self):
|
2018-02-27 21:45:12 +01:00
|
|
|
pass
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-16 05:38:33 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-10-27 19:31:42 +02:00
|
|
|
def find_peers_for_blob(self, blob_hash, timeout=None, filter_self=False):
|
|
|
|
"""
|
|
|
|
Find peers for blob in the DHT
|
|
|
|
blob_hash (str): blob hash to look for
|
|
|
|
timeout (int): seconds to timeout after
|
|
|
|
filter_self (bool): if True, and if a peer for a blob is itself, filter it
|
|
|
|
from the result
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list of peers for the blob
|
|
|
|
"""
|
2017-02-16 05:38:33 +01:00
|
|
|
def _trigger_timeout():
|
|
|
|
if not finished_deferred.called:
|
2017-02-20 01:27:01 +01:00
|
|
|
log.debug("Peer search for %s timed out", short_hash(blob_hash))
|
2017-02-16 05:38:33 +01:00
|
|
|
finished_deferred.cancel()
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
bin_hash = binascii.unhexlify(blob_hash)
|
2017-02-16 05:38:33 +01:00
|
|
|
finished_deferred = self.dht_node.getPeersForBlob(bin_hash)
|
|
|
|
|
|
|
|
if timeout is not None:
|
|
|
|
reactor.callLater(timeout, _trigger_timeout)
|
|
|
|
|
2017-10-11 20:05:03 +02:00
|
|
|
try:
|
|
|
|
peer_list = yield finished_deferred
|
|
|
|
except defer.CancelledError:
|
|
|
|
peer_list = []
|
2017-02-16 05:38:33 +01:00
|
|
|
|
|
|
|
peers = set(peer_list)
|
|
|
|
good_peers = []
|
|
|
|
for host, port in peers:
|
2017-10-27 19:31:42 +02:00
|
|
|
if filter_self and (host, port) == (self.dht_node.externalIP, self.dht_node.peerPort):
|
|
|
|
continue
|
2017-02-16 05:38:33 +01:00
|
|
|
peer = self.peer_manager.get_peer(host, port)
|
|
|
|
if peer.is_available() is True:
|
|
|
|
good_peers.append(peer)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-16 05:38:33 +01:00
|
|
|
defer.returnValue(good_peers)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def get_most_popular_hashes(self, num_to_return):
|
2016-11-03 20:42:57 +01:00
|
|
|
return self.dht_node.get_most_popular_hashes(num_to_return)
|