580ad9c84dc5226f04b222b21ea6ee30a7a66f23
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / node.py
1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 from datetime import datetime, MINYEAR
5 from types import InstanceType
6
7 from twisted.trial import unittest
8
9 import khash
10 from util import compact
11
12 # magic id to use before we know a peer's id
13 NULL_ID = 20 * '\0'
14
15 class Node:
16     """encapsulate contact info"""
17     def __init__(self, id, host = None, port = None):
18         self.fails = 0
19         self.lastSeen = datetime(MINYEAR, 1, 1)
20
21         # Alternate method, init Node from dictionary
22         if isinstance(id, dict):
23             host = id['host']
24             port = id['port']
25             id = id['id']
26
27         assert isinstance(id, str)
28         assert isinstance(host, str)
29         self.id = id
30         self.num = khash.intify(id)
31         self.host = host
32         self.port = int(port)
33         self._contactInfo = None
34     
35     def updateLastSeen(self):
36         self.lastSeen = datetime.now()
37         self.fails = 0
38     
39     def msgFailed(self):
40         self.fails = self.fails + 1
41         return self.fails
42     
43     def contactInfo(self):
44         if self._contactInfo is None:
45             self._contactInfo = compact(self.id, self.host, self.port)
46         return self._contactInfo
47     
48     def __repr__(self):
49         return `(self.id, self.host, self.port)`
50     
51     ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
52     def __lt__(self, a):
53         if type(a) == InstanceType:
54             a = a.num
55         return self.num < a
56     def __le__(self, a):
57         if type(a) == InstanceType:
58             a = a.num
59         return self.num <= a
60     def __gt__(self, a):
61         if type(a) == InstanceType:
62             a = a.num
63         return self.num > a
64     def __ge__(self, a):
65         if type(a) == InstanceType:
66             a = a.num
67         return self.num >= a
68     def __eq__(self, a):
69         if type(a) == InstanceType:
70             a = a.num
71         return self.num == a
72     def __ne__(self, a):
73         if type(a) == InstanceType:
74             a = a.num
75         return self.num != a
76
77
78 class TestNode(unittest.TestCase):
79     def setUp(self):
80         self.node = Node(khash.newID(), '127.0.0.1', 2002)
81     def testUpdateLastSeen(self):
82         t = self.node.lastSeen
83         self.node.updateLastSeen()
84         self.failUnless(t < self.node.lastSeen)
85