def _findBucket(self, node): """ Find the appropriate bucket for the given node """ for bucket in buckets: if bucket.inRange(node): return bucket #if bucket.low <= node and node <= bucket.high: # return bucket return None
def findNode(self, target: hash.hash.Hash): """ Find a node with the given ID in the routing table. """ for bucket in self.buckets: if bucket.inRange(nodeID): for node in bucket: if node.hash == target: return node return None return None
def addNode(self, node: dht.node.Node): """ Attempt to add the given node to the routing table. """ bucket = self._findBucket(node) if bucket == None: raise Exception("Found no bucket for given id") if not node in bucket: # We do not have this node on our routing table yet; # attempt to add it. if len(bucket) < MAX_NODES_PER_BUCKET: bucket.append(node) else: if bucket.inRange(myID): # Our own node's ID is in the appropriate bucket's range, # split the bucket and recursively attempt to add the node. self._splitBucket(bucket) self.addNode(node) else: # TODO: handle this pass
def addNode(self, node: dht.node.Node): """ Attempt to add the given node to the routing table. """ bucket = self._findBucket(node) if bucket == None: raise Exception("Found no bucket for given id") if not node in bucket: # We do not have this node on our routing table yet; # attempt to add it. if len(bucket) < MAX_NODES_PER_BUCKET: bucket.append(node) else: if bucket.inRange(myID): # Our own node's ID is in the appropriate bucket's range, # split the bucket and recursively attempt to add the node. self._splitBucket(bucket) self.addNode(node) else: # TODO: handle this pass