def start(self): if self.peer_manager is None: self.peer_manager = PeerManager() def match_port(h, p): return h, p def join_resolved_addresses(result): addresses = [] for success, value in result: if success is True: addresses.append(value) return addresses def start_dht(addresses): log.info("Starting the dht") log.info("lbry id: %s", base64.encodestring(self.lbryid).strip("\n")) self.dht_node.joinNetwork(addresses) self.peer_finder.run_manage_loop() self.hash_announcer.run_manage_loop() ds = [] for host, port in self.known_dht_nodes: d = reactor.resolve(host) d.addCallback(match_port, port) ds.append(d) if self.dht_node is None: self.dht_node = Node(udpPort=self.dht_node_port, lbryid=self.lbryid, externalIP=self.external_ip) if self.peer_finder is None: self.peer_finder = DHTPeerFinder(self.dht_node, self.peer_manager) if self.hash_announcer is None: self.hash_announcer = DHTHashAnnouncer(self.dht_node, self.peer_port) if self.blob_manager is None: self.blob_manager = DiskBlobManager(self.hash_announcer, self.blob_dir, self.blob_data_dir) d1 = defer.DeferredList(ds) d1.addCallback(join_resolved_addresses) d1.addCallback(start_dht) d2 = self.blob_manager.setup() dl = defer.DeferredList([d1, d2], fireOnOneErrback=True, consumeErrors=True) return dl
def _setup_dht(self): log.info("Starting DHT") def start_dht(join_network_result): self.hash_announcer.run_manage_loop() return True self.dht_node = self.dht_node_class(udpPort=self.dht_node_port, node_id=self.node_id, externalIP=self.external_ip, peerPort=self.peer_port) self.peer_finder = DHTPeerFinder(self.dht_node, self.peer_manager) if self.hash_announcer is None: self.hash_announcer = DHTHashAnnouncer(self.dht_node, self.peer_port) self.dht_node.startNetwork() # pass start_dht() as callback to start the remaining components after joining the DHT return self.join_dht(start_dht)
def _setup_dht(self): from twisted.internet import reactor log.debug("Starting the dht") def match_port(h, p): return h, p def join_resolved_addresses(result): addresses = [] for success, value in result: if success is True: addresses.append(value) return addresses def start_dht(addresses): self.dht_node.joinNetwork(addresses) self.peer_finder.run_manage_loop() self.hash_announcer.run_manage_loop() return True ds = [] for host, port in self.known_dht_nodes: d = reactor.resolve(host) d.addCallback(match_port, port) ds.append(d) self.dht_node = self.dht_node_class( udpPort=self.dht_node_port, lbryid=self.lbryid, externalIP=self.external_ip ) self.peer_finder = DHTPeerFinder(self.dht_node, self.peer_manager) if self.hash_announcer is None: self.hash_announcer = DHTHashAnnouncer(self.dht_node, self.peer_port) dl = defer.DeferredList(ds) dl.addCallback(join_resolved_addresses) dl.addCallback(start_dht) return dl
def _setup_dht(self): """DHT相关功能入口""" from twisted.internet import reactor log.info("Starting DHT") def join_resolved_addresses(result): addresses = [] for success, value in result: if success is True: addresses.append(value) return addresses def start_dht(join_network_result): self.peer_finder.run_manage_loop() self.hash_announcer.run_manage_loop() return True ds = [] for host, port in self.known_dht_nodes: d = reactor.resolve(host) #reactor.resolve函数负责将将主机名转化为IP地址,该方法返回了一个已经激活的deferred d.addCallback(lambda h: (h, port)) # match host to port ds.append(d) self.dht_node = self.dht_node_class( udpPort=self.dht_node_port, node_id=self.node_id, externalIP=self.external_ip, peerPort=self.peer_port ) self.peer_finder = DHTPeerFinder(self.dht_node, self.peer_manager) if self.hash_announcer is None: self.hash_announcer = DHTHashAnnouncer(self.dht_node, self.peer_port) # 这个类告诉DHT网络, 这个peer有哪些blobs dl = defer.DeferredList(ds) dl.addCallback(join_resolved_addresses) dl.addCallback(self.dht_node.joinNetwork) dl.addCallback(start_dht) return dl
class StreamAvailabilityManager(object): def __init__(self, storage): self.storage = storage self.lbryid = base64.decodestring(LBRYID) self.peer_manager = None self.peer_finder = None self.dht_node = None self.hash_announcer = None self.blob_manager = None self.dht_node_port = conf.settings['dht_node_port'] self.blob_data_dir = conf.settings['data_dir'] self.blob_dir = os.path.join(self.blob_data_dir, conf.settings['BLOBFILES_DIR']) self.peer_port = conf.settings['peer_port'] self.known_dht_nodes = conf.settings['known_dht_nodes'] self.external_ip = '127.0.0.1' def start(self): if self.peer_manager is None: self.peer_manager = PeerManager() def match_port(h, p): return h, p def join_resolved_addresses(result): addresses = [] for success, value in result: if success is True: addresses.append(value) return addresses def start_dht(addresses): log.info("Starting the dht") log.info("lbry id: %s", base64.encodestring(self.lbryid).strip("\n")) self.dht_node.joinNetwork(addresses) self.peer_finder.run_manage_loop() self.hash_announcer.run_manage_loop() ds = [] for host, port in self.known_dht_nodes: d = reactor.resolve(host) d.addCallback(match_port, port) ds.append(d) if self.dht_node is None: self.dht_node = Node(udpPort=self.dht_node_port, lbryid=self.lbryid, externalIP=self.external_ip) if self.peer_finder is None: self.peer_finder = DHTPeerFinder(self.dht_node, self.peer_manager) if self.hash_announcer is None: self.hash_announcer = DHTHashAnnouncer(self.dht_node, self.peer_port) if self.blob_manager is None: self.blob_manager = DiskBlobManager(self.hash_announcer, self.blob_dir, self.blob_data_dir) d1 = defer.DeferredList(ds) d1.addCallback(join_resolved_addresses) d1.addCallback(start_dht) d2 = self.blob_manager.setup() dl = defer.DeferredList([d1, d2], fireOnOneErrback=True, consumeErrors=True) return dl def stop(self): log.info("Shutting down availability manager") ds = [] if self.blob_manager is not None: ds.append(defer.maybeDeferred(self.blob_manager.stop)) if self.dht_node is not None: ds.append(defer.maybeDeferred(self.dht_node.stop)) if self.peer_finder is not None: ds.append(defer.maybeDeferred(self.peer_finder.stop)) if self.hash_announcer is not None: ds.append(defer.maybeDeferred(self.hash_announcer.stop)) return defer.DeferredList(ds) def get_peers_for_hash(self, blob_hash): return self.peer_finder.find_peers_for_blob(blob_hash)