]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_dht_Khashmir/node.py
Remove some extra logging that shouldn't have been in the last commit.
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / node.py
index 5f11e51453acf06322b6d17485cb8489143eb5e4..580ad9c84dc5226f04b222b21ea6ee30a7a66f23 100644 (file)
@@ -7,32 +7,30 @@ from types import InstanceType
 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()
@@ -42,8 +40,10 @@ class Node:
         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)`
@@ -77,9 +77,9 @@ class Node:
 
 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