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
8 from twisted.python import log
11 from util import compact
13 # magic id to use before we know a peer's id
17 """encapsulate contact info"""
18 def __init__(self, id, host = None, port = None):
19 log.msg('New node: id=%r, host=%r, port=%r' % (id, host, port))
21 self.lastSeen = datetime(MINYEAR, 1, 1)
23 # Alternate method, init Node from dictionary
24 if isinstance(id, dict):
29 assert isinstance(id, str)
30 assert isinstance(host, str)
32 self.num = khash.intify(id)
35 self._contactInfo = None
37 def updateLastSeen(self):
38 self.lastSeen = datetime.now()
42 self.fails = self.fails + 1
45 def contactInfo(self):
46 if self._contactInfo is None:
47 self._contactInfo = compact(self.id, self.host, self.port)
48 return self._contactInfo
51 return `(self.id, self.host, self.port)`
53 ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
55 if type(a) == InstanceType:
59 if type(a) == InstanceType:
63 if type(a) == InstanceType:
67 if type(a) == InstanceType:
71 if type(a) == InstanceType:
75 if type(a) == InstanceType:
80 class TestNode(unittest.TestCase):
82 self.node = Node(khash.newID(), '127.0.0.1', 2002)
83 def testUpdateLastSeen(self):
84 t = self.node.lastSeen
85 self.node.updateLastSeen()
86 self.failUnless(t < self.node.lastSeen)