1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
4 from datetime import datetime, MINYEAR
5 from types import InstanceType
7 from twisted.trial import unittest
10 from util import compact
12 # magic id to use before we know a peer's id
16 """encapsulate contact info"""
17 def __init__(self, id, host = None, port = None):
19 self.lastSeen = datetime(MINYEAR, 1, 1)
21 # Alternate method, init Node from dictionary
22 if isinstance(id, dict):
27 assert isinstance(id, str)
28 assert isinstance(host, str)
30 self.num = khash.intify(id)
35 self._contactInfo = None
37 def updateLastSeen(self):
38 self.lastSeen = datetime.now()
41 def updateToken(self, token):
44 def updateNumValues(self, num_values):
45 self.num_values = num_values
48 self.fails = self.fails + 1
51 def contactInfo(self):
52 if self._contactInfo is None:
53 self._contactInfo = compact(self.id, self.host, self.port)
54 return self._contactInfo
57 return `(self.id, self.host, self.port)`
59 ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
61 if type(a) == InstanceType:
65 if type(a) == InstanceType:
69 if type(a) == InstanceType:
73 if type(a) == InstanceType:
77 if type(a) == InstanceType:
81 if type(a) == InstanceType:
86 class TestNode(unittest.TestCase):
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)