]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - apt_dht_Khashmir/node.py
99d95fb1b67c2793f709e99b9d221dee2d69541b
[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 """Represents a node in the DHT.
5
6 @type NULL_ID: C{string}
7 @var NULL_ID: the node ID to use until one is known
8 """
9
10 from datetime import datetime, MINYEAR
11 from types import InstanceType
12
13 from twisted.trial import unittest
14
15 import khash
16 from util import compact
17
18 # magic id to use before we know a peer's id
19 NULL_ID = 20 * '\0'
20
21 class Node:
22     """Encapsulate a node's contact info.
23     
24     @type fails: C{int}
25     @ivar fails: number of times this node has failed in a row
26     @type lastSeen: C{datetime.datetime}
27     @ivar lastSeen: the last time a response was received from this node
28     @type id: C{string}
29     @ivar id: the node's ID in the DHT
30     @type num: C{long}
31     @ivar num: the node's ID in number form
32     @type host: C{string}
33     @ivar host: the IP address of the node
34     @type port: C{int}
35     @ivar port: the port of the node
36     @type token: C{string}
37     @ivar token: the last received token from the node
38     @type num_values: C{int}
39     @ivar num_values: the number of values the node has for the key in the
40         currently executing action
41     """
42     
43     def __init__(self, id, host = None, port = None):
44         """Initialize the node.
45         
46         @type id: C{string} or C{dictionary}
47         @param id: the node's ID in the DHT, or a dictionary containing the
48             node's id, host and port
49         @type host: C{string}
50         @param host: the IP address of the node
51             (optional, but must be specified if id is not a dictionary)
52         @type port: C{int}
53         @param port: the port of the node
54             (optional, but must be specified if id is not a dictionary)
55         """
56         self.fails = 0
57         self.lastSeen = datetime(MINYEAR, 1, 1)
58
59         # Alternate method, init Node from dictionary
60         if isinstance(id, dict):
61             host = id['host']
62             port = id['port']
63             id = id['id']
64
65         assert isinstance(id, str)
66         assert isinstance(host, str)
67         self.id = id
68         self.num = khash.intify(id)
69         self.host = host
70         self.port = int(port)
71         self.token = ''
72         self.num_values = 0
73         self._contactInfo = None
74     
75     def updateLastSeen(self):
76         """Updates the last contact time of the node and resets the number of failures."""
77         self.lastSeen = datetime.now()
78         self.fails = 0
79         
80     def updateToken(self, token):
81         """Update the token for the node."""
82         self.token = token
83     
84     def updateNumValues(self, num_values):
85         """Update how many values the node has in the current search for a value."""
86         self.num_values = num_values
87     
88     def msgFailed(self):
89         """Log a failed attempt to contact this node.
90         
91         @rtype: C{int}
92         @return: the number of consecutive failures this node has
93         """
94         self.fails = self.fails + 1
95         return self.fails
96     
97     def contactInfo(self):
98         """Get the compact contact info for the node."""
99         if self._contactInfo is None:
100             self._contactInfo = compact(self.id, self.host, self.port)
101         return self._contactInfo
102     
103     def __repr__(self):
104         return `(self.id, self.host, self.port)`
105     
106     #{ Comparators to bisect/index a list of nodes with either a node or a long
107     def __lt__(self, a):
108         if type(a) == InstanceType:
109             a = a.num
110         return self.num < a
111     def __le__(self, a):
112         if type(a) == InstanceType:
113             a = a.num
114         return self.num <= a
115     def __gt__(self, a):
116         if type(a) == InstanceType:
117             a = a.num
118         return self.num > a
119     def __ge__(self, a):
120         if type(a) == InstanceType:
121             a = a.num
122         return self.num >= a
123     def __eq__(self, a):
124         if type(a) == InstanceType:
125             a = a.num
126         return self.num == a
127     def __ne__(self, a):
128         if type(a) == InstanceType:
129             a = a.num
130         return self.num != a
131
132
133 class TestNode(unittest.TestCase):
134     """Unit tests for the node implementation."""
135     def setUp(self):
136         self.node = Node(khash.newID(), '127.0.0.1', 2002)
137     def testUpdateLastSeen(self):
138         t = self.node.lastSeen
139         self.node.updateLastSeen()
140         self.failUnless(t < self.node.lastSeen)
141