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
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
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):
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)