ripped out xmlrpc, experimented with xmlrpc but with bencode, finally
[quix0rs-apt-p2p.git] / node.py
diff --git a/node.py b/node.py
index 7128ecb493312877ba8e7e0ad1ac7cc807786df9..93e16052c2e9d92ddc9f45f8a89c80e4f4d08767 100644 (file)
--- a/node.py
+++ b/node.py
@@ -1,71 +1,78 @@
 import hash
 import time
 from types import *
-from xmlrpclib import Binary
 
 class Node:
     """encapsulate contact info"""
+    def __init__(self):
+        self.fails = 0
+        self.lastSeen = 0
+        self.id = self.host = self.port = ''
+    
     def init(self, id, host, port):
-       self.id = id
-       self.int = hash.intify(id)
-       self.host = host
-       self.port = port
-       self.lastSeen = time.time()
-       self._senderDict = {'id': Binary(self.id), 'port' : self.port, 'host' : self.host}
-       return self
-       
+        self.id = id
+        self.num = hash.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'].data
-       self.int = hash.intify(self.id)
-       self.port = dict['port']
-       self.host = dict['host']
-       self.lastSeen = time.time()
-       return self
-       
+        self._senderDict = dict
+        self.id = dict['id']
+        self.num = hash.intify(self.id)
+        self.port = dict['port']
+        self.host = dict['host']
+        return self
+    
     def updateLastSeen(self):
-       self.lastSeen = time.time()
-
+        self.lastSeen = time.time()
+        self.fails = 0
+    
+    def msgFailed(self):
+        self.fails = self.fails + 1
+        return self.fails
+    
     def senderDict(self):
-       return self._senderDict
-       
+        return self._senderDict
+    
     def __repr__(self):
-       return `(self.id, self.host, self.port)`
-       
+        return `(self.id, self.host, self.port)`
+    
     ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
     def __lt__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int < a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num < a
     def __le__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int <= a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num <= a
     def __gt__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int > a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num > a
     def __ge__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int >= a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num >= a
     def __eq__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int == a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num == a
     def __ne__(self, a):
-       if type(a) == InstanceType:
-           a = a.int
-       return self.int != a
+        if type(a) == InstanceType:
+            a = a.num
+        return self.num != a
 
 
 import unittest
 
 class TestNode(unittest.TestCase):
     def setUp(self):
-       self.node = Node().init(hash.newID(), 'localhost', 2002)
+        self.node = Node().init(hash.newID(), 'localhost', 2002)
     def testUpdateLastSeen(self):
-       t = self.node.lastSeen
-       self.node.updateLastSeen()
-       assert t < self.node.lastSeen
-       
\ No newline at end of file
+        t = self.node.lastSeen
+        self.node.updateLastSeen()
+        assert t < self.node.lastSeen
+    
\ No newline at end of file