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