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)
34 self._contactInfo = None
36 def updateLastSeen(self):
37 self.lastSeen = datetime.now()
40 def updateToken(self, token):
44 self.fails = self.fails + 1
47 def contactInfo(self):
48 if self._contactInfo is None:
49 self._contactInfo = compact(self.id, self.host, self.port)
50 return self._contactInfo
53 return `(self.id, self.host, self.port)`
55 ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
57 if type(a) == InstanceType:
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:
82 class TestNode(unittest.TestCase):
84 self.node = Node(khash.newID(), '127.0.0.1', 2002)
85 def testUpdateLastSeen(self):
86 t = self.node.lastSeen
87 self.node.updateLastSeen()
88 self.failUnless(t < self.node.lastSeen)