from twisted.trial import unittest
import khash
+from util import compact
# magic id to use before we know a peer's id
NULL_ID = 20 * '\0'
class Node:
"""encapsulate contact info"""
- def __init__(self):
+ def __init__(self, id, host = None, port = None):
self.fails = 0
self.lastSeen = datetime(MINYEAR, 1, 1)
- self.id = self.host = self.port = ''
-
- def init(self, id, host, port):
+
+ # Alternate method, init Node from dictionary
+ if isinstance(id, dict):
+ host = id['host']
+ port = id['port']
+ id = id['id']
+
+ assert isinstance(id, str)
+ assert isinstance(host, str)
self.id = id
self.num = khash.intify(id)
self.host = host
- self.port = port
- self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host}
- return self
-
- def initWithDict(self, dict):
- self._senderDict = dict
- self.id = dict['id']
- self.num = khash.intify(self.id)
- self.port = dict['port']
- self.host = dict['host']
- return self
+ self.port = int(port)
+ self._contactInfo = None
def updateLastSeen(self):
self.lastSeen = datetime.now()
self.fails = self.fails + 1
return self.fails
- def senderDict(self):
- return self._senderDict
+ def contactInfo(self):
+ if self._contactInfo is None:
+ self._contactInfo = compact(self.id, self.host, self.port)
+ return self._contactInfo
def __repr__(self):
return `(self.id, self.host, self.port)`
class TestNode(unittest.TestCase):
def setUp(self):
- self.node = Node().init(khash.newID(), 'localhost', 2002)
+ self.node = Node(khash.newID(), '127.0.0.1', 2002)
def testUpdateLastSeen(self):
t = self.node.lastSeen
self.node.updateLastSeen()
- assert t < self.node.lastSeen
+ self.failUnless(t < self.node.lastSeen)
\ No newline at end of file