Break up the find_value into 2 parts (with get_value).
[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.token = ''
34         self.num_values = 0
35         self._contactInfo = None
36     
37     def updateLastSeen(self):
38         self.lastSeen = datetime.now()
39         self.fails = 0
40         
41     def updateToken(self, token):
42         self.token = token
43     
44     def updateNumValues(self, num_values):
45         self.num_values = num_values
46     
47     def msgFailed(self):
48         self.fails = self.fails + 1
49         return self.fails
50     
51     def contactInfo(self):
52         if self._contactInfo is None:
53             self._contactInfo = compact(self.id, self.host, self.port)
54         return self._contactInfo
55     
56     def __repr__(self):
57         return `(self.id, self.host, self.port)`
58     
59     ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
60     def __lt__(self, a):
61         if type(a) == InstanceType:
62             a = a.num
63         return self.num < a
64     def __le__(self, a):
65         if type(a) == InstanceType:
66             a = a.num
67         return self.num <= a
68     def __gt__(self, a):
69         if type(a) == InstanceType:
70             a = a.num
71         return self.num > a
72     def __ge__(self, a):
73         if type(a) == InstanceType:
74             a = a.num
75         return self.num >= a
76     def __eq__(self, a):
77         if type(a) == InstanceType:
78             a = a.num
79         return self.num == a
80     def __ne__(self, a):
81         if type(a) == InstanceType:
82             a = a.num
83         return self.num != a
84
85
86 class TestNode(unittest.TestCase):
87     def setUp(self):
88         self.node = Node(khash.newID(), '127.0.0.1', 2002)
89     def testUpdateLastSeen(self):
90         t = self.node.lastSeen
91         self.node.updateLastSeen()
92         self.failUnless(t < self.node.lastSeen)
93