lbry-sdk/lbrynet/core/client/DHTPeerFinder.py
2015-08-20 11:27:15 -04:00

47 lines
No EOL
1.4 KiB
Python

import binascii
from zope.interface import implements
from lbrynet.interfaces import IPeerFinder
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):
self.dht_node = dht_node
self.peer_manager = peer_manager
self.peers = []
self.next_manage_call = None
def run_manage_loop(self):
from twisted.internet import reactor
self._manage_peers()
self.next_manage_call = reactor.callLater(60, self.run_manage_loop)
def stop(self):
if self.next_manage_call is not None and self.next_manage_call.active():
self.next_manage_call.cancel()
self.next_manage_call = None
def _manage_peers(self):
pass
def find_peers_for_blob(self, blob_hash):
bin_hash = binascii.unhexlify(blob_hash)
def filter_peers(peer_list):
good_peers = []
for host, port in peer_list:
peer = self.peer_manager.get_peer(host, port)
if peer.is_available() is True:
good_peers.append(peer)
return good_peers
d = self.dht_node.getPeersForBlob(bin_hash)
d.addCallback(filter_peers)
return d
def get_most_popular_hashes(self, num_to_return):
return self.dht_node.get_most_popular_hashes(num_to_return)