## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-import time
-from bisect import *
-from types import *
+from time import time
+from bisect import bisect_left
-import khash as hash
-import const
-from const import K, HASH_LENGTH, NULL_ID
+from const import K, HASH_LENGTH, NULL_ID, MAX_FAILURES
+import khash
from node import Node
class KTable:
"""
if isinstance(id, str):
- num = hash.intify(id)
+ num = khash.intify(id)
elif isinstance(id, Node):
num = id.num
elif isinstance(id, int) or isinstance(id, long):
except IndexError:
return None
else:
- if n.msgFailed() >= const.MAX_FAILURES:
+ if n.msgFailed() >= MAX_FAILURES:
self.invalidateNode(n)
class KBucket:
self.l = contents
self.min = min
self.max = max
- self.lastAccessed = time.time()
+ self.lastAccessed = time()
def touch(self):
- self.lastAccessed = time.time()
+ self.lastAccessed = time()
def getNodeWithInt(self, num):
if num in self.l: return num
class TestKTable(unittest.TestCase):
def setUp(self):
- self.a = Node().init(hash.newID(), 'localhost', 2002)
+ self.a = Node().init(khash.newID(), 'localhost', 2002)
self.t = KTable(self.a)
def testAddNode(self):
- self.b = Node().init(hash.newID(), 'localhost', 2003)
+ self.b = Node().init(khash.newID(), 'localhost', 2003)
self.t.insertNode(self.b)
self.assertEqual(len(self.t.buckets[0].l), 1)
self.assertEqual(self.t.buckets[0].l[0], self.b)
def testFail(self):
self.testAddNode()
- for i in range(const.MAX_FAILURES - 1):
+ for i in range(MAX_FAILURES - 1):
self.t.nodeFailed(self.b)
self.assertEqual(len(self.t.buckets[0].l), 1)
self.assertEqual(self.t.buckets[0].l[0], self.b)