X-Git-Url: https://git.mxchange.org/?p=quix0rs-apt-p2p.git;a=blobdiff_plain;f=apt_p2p_Khashmir%2Fktable.py;h=201d494d927392bca58157857a11a5154e39daa2;hp=24ea93fc2ecce34a22cbef71d1e4d1b876a6c0ed;hb=8490561a10b2681635b573b5c28179dff297550c;hpb=58467fc71f79725ccf83764f8859f0f82174d4cc diff --git a/apt_p2p_Khashmir/ktable.py b/apt_p2p_Khashmir/ktable.py index 24ea93f..201d494 100644 --- a/apt_p2p_Khashmir/ktable.py +++ b/apt_p2p_Khashmir/ktable.py @@ -95,6 +95,18 @@ class KTable: nodes.sort(lambda a, b, num=num: cmp(num ^ a.num, num ^ b.num)) return nodes[:K] + def touch(self, id): + """Mark a bucket as having been looked up. + + @type id: C{string} or C{int} or L{node.Node} + @param id: the ID in the bucket that was accessed + """ + # Get the bucket number from the input + num = self._nodeNum(id) + i = self._bucketIndexForInt(num) + + self.buckets[i].touch() + def _mergeBucket(self, i): """Merge unneeded buckets after removing a node. @@ -190,7 +202,6 @@ class KTable: # note that we removed the original and replaced it with the new one # utilizing this nodes new contact info self.buckets[i].add(node) - self.buckets[i].touch() return True # We don't have this node, check to see if the bucket is full @@ -199,7 +210,6 @@ class KTable: if contacted: node.updateLastSeen() self.buckets[i].add(node) - self.buckets[i].touch() log.msg('Added node to routing table: %s/%s' % (node.host, node.port)) return True return False @@ -242,7 +252,6 @@ class KTable: except ValueError: return None else: - self.buckets[i].touch() return tstamp def invalidateNode(self, n): @@ -253,7 +262,10 @@ class KTable: self.replaceStaleNode(n) def nodeFailed(self, node): - """Mark a node as having failed once, and remove it if it has failed too much.""" + """Mark a node as having failed once, and remove it if it has failed too much. + + @return: whether the node is in the routing table + """ # Get the bucket number num = self._nodeNum(node) i = self._bucketIndexForInt(num) @@ -262,11 +274,13 @@ class KTable: try: n = self.buckets[i].node(num) except ValueError: - return None + return False else: # The node is in the bucket if n.msgFailed() >= self.config['MAX_FAILURES']: self.invalidateNode(n) + return False + return True class KBucket: """Single bucket of nodes in a kademlia-like routing table.