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
11 # magic id to use before we know a peer's id
15 """encapsulate contact info"""
16 def __init__(self, id, host = None, port = None):
18 self.lastSeen = datetime(MINYEAR, 1, 1)
20 # Alternate method, init Node from dictionary
21 if isinstance(id, dict):
26 assert isinstance(id, str)
27 assert isinstance(host, str)
29 self.num = khash.intify(id)
32 self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host}
34 def updateLastSeen(self):
35 self.lastSeen = datetime.now()
39 self.fails = self.fails + 1
43 return self._senderDict
46 return `(self.id, self.host, self.port)`
48 ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
50 if type(a) == InstanceType:
54 if type(a) == InstanceType:
58 if type(a) == InstanceType:
62 if type(a) == InstanceType:
66 if type(a) == InstanceType:
70 if type(a) == InstanceType:
75 class TestNode(unittest.TestCase):
77 self.node = Node(khash.newID(), 'localhost', 2002)
78 def testUpdateLastSeen(self):
79 t = self.node.lastSeen
80 self.node.updateLastSeen()
81 self.failUnless(t < self.node.lastSeen)