X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=ktable.py;h=c34f3630410c44941ee4f27329305001e977481b;hb=2a3391d7352ad0a364c27f06f0a9339abd312bd1;hp=9cd373246ab1ea23cf556e9e939d1128bef272cf;hpb=1abfc43747d252f5d0bf11f119e0daabecc4f3a6;p=quix0rs-apt-p2p.git diff --git a/ktable.py b/ktable.py index 9cd3732..c34f363 100644 --- a/ktable.py +++ b/ktable.py @@ -5,10 +5,11 @@ from bisect import * import time from types import * +import const from node import Node # The all-powerful, magical Kademlia "k" constant, bucket depth -K = 20 +K = 8 # how many bits wide is our hash? HASH_LENGTH = 160 @@ -104,13 +105,15 @@ class KTable: return del(self.buckets[i].l[it]) - self.buckets[i].l.append(new) + if new: + self.buckets[i].l.append(new) def insertNode(self, node): """ this insert the node, returning None if successful, returns the oldest node in the bucket if it's full the caller responsible for pinging the returned node and calling replaceStaleNode if it is found to be stale!! """ + assert(node.id != " "*20) # get the bucket for this node i = self. _bucketIndexForInt(node.int) ## check to see if node is in the bucket already @@ -162,7 +165,16 @@ class KTable: n.updateLastSeen() return tstamp - + def nodeFailed(self, node): + """ call this when a node fails to respond to a message, to invalidate that node """ + try: + n = self.findNodes(node.int)[0] + except IndexError: + return None + else: + if(n.msgFailed() >= const.MAX_FAILURES): + self.replaceStaleNode(n, None) + class KBucket: __slots = ['min', 'max', 'lastAccessed'] def __init__(self, contents, min, max): @@ -218,11 +230,11 @@ import unittest class TestKTable(unittest.TestCase): def setUp(self): - self.a = Node(hash.newID(), 'localhost', 2002) + self.a = Node().init(hash.newID(), 'localhost', 2002) self.t = KTable(self.a) def test_replace_stale_node(self): - self.b = Node(hash.newID(), 'localhost', 2003) + self.b = Node().init(hash.newID(), 'localhost', 2003) self.t.replaceStaleNode(self.a, self.b) assert(len(self.t.buckets[0].l) == 1) assert(self.t.buckets[0].l[0].id == self.b.id)