]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - node.py
fix tests
[quix0rs-apt-p2p.git] / node.py
1 import hash
2 import time
3 from types import *
4 from xmlrpclib import Binary
5
6 class Node:
7     """encapsulate contact info"""
8     def init(self, id, host, port):
9         self.id = id
10         self.int = hash.intify(id)
11         self.host = host
12         self.port = port
13         self.lastSeen = time.time()
14         self._senderDict = {'id': Binary(self.id), 'port' : self.port, 'host' : self.host}
15         return self
16         
17     def initWithDict(self, dict):
18         self._senderDict = dict
19         self.id = dict['id'].data
20         self.int = hash.intify(self.id)
21         self.port = dict['port']
22         self.host = dict['host']
23         self.lastSeen = time.time()
24         return self
25         
26     def updateLastSeen(self):
27         self.lastSeen = time.time()
28
29     def senderDict(self):
30         return self._senderDict
31         
32     def __repr__(self):
33         return `(self.id, self.host, self.port)`
34         
35     ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
36     def __lt__(self, a):
37         if type(a) == InstanceType:
38             a = a.int
39         return self.int < a
40     def __le__(self, a):
41         if type(a) == InstanceType:
42             a = a.int
43         return self.int <= a
44     def __gt__(self, a):
45         if type(a) == InstanceType:
46             a = a.int
47         return self.int > a
48     def __ge__(self, a):
49         if type(a) == InstanceType:
50             a = a.int
51         return self.int >= a
52     def __eq__(self, a):
53         if type(a) == InstanceType:
54             a = a.int
55         return self.int == a
56     def __ne__(self, a):
57         if type(a) == InstanceType:
58             a = a.int
59         return self.int != a
60
61
62 import unittest
63
64 class TestNode(unittest.TestCase):
65     def setUp(self):
66         self.node = Node().init(hash.newID(), 'localhost', 2002)
67     def testUpdateLastSeen(self):
68         t = self.node.lastSeen
69         self.node.updateLastSeen()
70         assert t < self.node.lastSeen
71