def schedule(self): """ send messages to new peers, if necessary """ if self.finished: return l = self.found.values() l.sort(self.sort) for node in l[:K]: if node.id == self.target: self.finished=1 return self.callback([node]) if (not self.queried.has_key(node.id)) and node.id != self.table.node.id: #xxxx t.timeout = time.time() + FIND_NODE_TIMEOUT df = node.findNode(self.target, self.table.node.id) df.addCallbacks(self.handleGotNodes, self.makeMsgFailed(node)) self.outstanding = self.outstanding + 1 self.queried[node.id] = 1 if self.outstanding >= const.CONCURRENT_REQS: break assert(self.outstanding) >=0 if self.outstanding == 0: ## all done!! self.finished=1 reactor.callFromThread(self.callback, l[:K])
def schedule(self): if self.finished: return l = self.found.values() l.sort(self.sort) for node in l[:K]: if (not self.queried.has_key(node.id)) and node.id != self.table.node.id: #xxx t.timeout = time.time() + GET_VALUE_TIMEOUT try: f = getattr(node, self.findValue) except AttributeError: print ">>> findValue %s doesn't have a %s method!" % (node, self.findValue) else: df = f(self.target, self.table.node.id) df.addCallback(self.handleGotNodes) df.addErrback(self.makeMsgFailed(node)) self.outstanding = self.outstanding + 1 self.queried[node.id] = 1 if self.outstanding >= const.CONCURRENT_REQS: break assert(self.outstanding) >=0 if self.outstanding == 0: ## all done, didn't find it!! self.finished=1 reactor.callFromThread(self.callback,[])
def handleGotNodes(self, dict): _krpc_sender = dict['_krpc_sender'] dict = dict['rsp'] sender = {'id' : dict["id"]} sender['port'] = _krpc_sender[1] sender['host'] = _krpc_sender[0] sender = self.table.Node().initWithDict(sender) sender.conn = self.table.udp.connectionForAddr((sender.host, sender.port)) self.table.table.insertNode(sender) if self.finished or self.answered.has_key(sender.id): # a day late and a dollar short return self.outstanding = self.outstanding - 1 self.answered[sender.id] = 1 # go through nodes # if we have any closer than what we already got, query them if dict.has_key('nodes'): for node in dict['nodes']: n = self.table.Node().initWithDict(node) n.conn = self.table.udp.connectionForAddr((n.host, n.port)) if not self.found.has_key(n.id): self.found[n.id] = n elif dict.has_key('values'): def x(y, z=self.results): if not z.has_key(y): z[y] = 1 return y else: return None z = len(dict['values']) v = filter(None, map(x, dict['values'])) if(len(v)): reactor.callFromThread(self.callback, v) self.schedule()
def _storeValueForKey(nodes, key=key, value=value, response=callback , table=self.table): if not response: # default callback def _storedValueHandler(sender): pass response=_storedValueHandler action = StoreValue(self.table, key, value, response) reactor.callFromThread(action.goWithNodes, nodes)
def _storeValueForKey(nodes, key=key, value=value, response=callback, table=self.table): if not response: # default callback def _storedValueHandler(sender): pass response = _storedValueHandler action = StoreValue(self.table, key, value, response) reactor.callFromThread(action.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) 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) reactor.callFromThread(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) 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) reactor.callFromThread(state.goWithNodes, nodes)
def valueForKey(self, key, 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(key) # get locals if searchlocal: l = self.retrieveValues(key) if len(l) > 0: reactor.callLater(0, callback, (l)) else: l = [] # create our search state state = GetValue(self, key, callback) reactor.callFromThread(state.goWithNodes, nodes, l)
def valueForKey(self, key, 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(key) # get locals if searchlocal: l = self.retrieveValues(key) if len(l) > 0: reactor.callLater(0, callback, (l)) else: l = [] # create our search state state = GetValue(self, key, callback) reactor.callFromThread(state.goWithNodes, nodes, l)