def findNode(self, id, callback, errback=None): """ returns the contact info for node, or the k closest nodes, from the global table """ # get K nodes out of local table/cache, or the node we want nodes = self.table.findNodes(id, invalid=True) l = [x for x in nodes if x.invalid] if len(l) > 4: nodes = sample(l, 4) + self.table.findNodes(id, invalid=False)[:4] d = Deferred() if errback: d.addCallbacks(callback, errback) else: d.addCallback(callback) if len(nodes) == 1 and nodes[0].id == id: d.callback(nodes) else: # create our search state state = FindNode(self, id, d.callback, self.rawserver.add_task) self.rawserver.external_add_task(0, state.goWithNodes, nodes)
def findNode(self, id, callback, errback=None): """ returns the contact info for node, or the k closest nodes, from the global table """ # get K nodes out of local table/cache, or the node we want nodes = self.table.findNodes(id, invalid=True) l = [x for x in nodes if x.invalid] if len(l) > 4: nodes = sample(l , 4) + self.table.findNodes(id, invalid=False)[:4] d = Deferred() if errback: d.addCallbacks(callback, errback) else: d.addCallback(callback) if len(nodes) == 1 and nodes[0].id == id : d.callback(nodes) else: # create our search state state = FindNode(self, id, d.callback, self.rawserver.add_task) self.rawserver.external_add_task(state.goWithNodes, 0, (nodes,))
def getPeers(self, info_hash, callback, searchlocal = 1): """ returns the values found for key in global table callback will be called with a list of values for each peer that returns unique values final callback will be an empty list - probably should change to 'more coming' arg """ nodes = self.table.findNodes(info_hash, invalid=True) l = [x for x in nodes if x.invalid] if len(l) > 4: nodes = sample(l , 4) + self.table.findNodes(info_hash, invalid=False)[:4] # get locals if searchlocal: l = self.retrieveValues(info_hash) if len(l) > 0: self.rawserver.external_add_task(callback, 0, ([reducePeers(l)],)) else: l = [] # create our search state state = GetValue(self, info_hash, callback, self.rawserver.add_task, 'getPeers') self.rawserver.external_add_task(state.goWithNodes, 0, (nodes, l))
def getPeers(self, info_hash, callback, searchlocal = 1): """ returns the values found for key in global table callback will be called with a list of values for each peer that returns unique values final callback will be an empty list - probably should change to 'more coming' arg """ print "getPeers" nodes = self.table.findNodes(info_hash, invalid=True) l = [x for x in nodes if x.invalid] if len(l) > 4: nodes = sample(l , 4) + self.table.findNodes(info_hash, invalid=False)[:4] # get locals if searchlocal: l = self.retrieveValues(info_hash) if len(l) > 0: self.rawserver.external_add_task(0, callback, [reducePeers(l)]) else: l = [] # create our search state state = GetValue(self, info_hash, callback, self.rawserver.add_task, 'getPeers') self.rawserver.external_add_task(0, state.goWithNodes, nodes, l)