1
0

node.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. import khash
  11. from BTL.platform import bttime as time
  12. from types import *
  13. class Node(object):
  14. """encapsulate contact info"""
  15. __slots__ = ('fails','lastSeen','invalid','id','host','port','age')
  16. def __init__(self):
  17. self.fails = 0
  18. self.lastSeen = 0
  19. self.invalid = True
  20. self.id = self.host = self.port = ''
  21. self.age = time()
  22. def init(self, id, host, port):
  23. self.id = id
  24. self.num = khash.intify(id)
  25. self.host = host
  26. self.port = port
  27. self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host}
  28. return self
  29. def initWithDict(self, dict):
  30. self._senderDict = dict
  31. self.id = dict['id']
  32. self.num = khash.intify(self.id)
  33. self.port = dict['port']
  34. self.host = dict['host']
  35. self.age = dict.get('age', self.age)
  36. return self
  37. def updateLastSeen(self):
  38. self.lastSeen = time()
  39. self.fails = 0
  40. self.invalid = False
  41. def msgFailed(self):
  42. self.fails = self.fails + 1
  43. return self.fails
  44. def senderDict(self):
  45. return self._senderDict
  46. def __hash__(self):
  47. return self.id.__hash__()
  48. def __repr__(self):
  49. return ">node <%s> %s<" % (self.id.encode('base64')[:4], (self.host, self.port))
  50. ## these comparators let us bisect/index a list full of nodes with either a node or an int/long
  51. def __lt__(self, a):
  52. if type(a) == InstanceType:
  53. a = a.num
  54. return self.num < a
  55. def __le__(self, a):
  56. if type(a) == InstanceType:
  57. a = a.num
  58. return self.num <= a
  59. def __gt__(self, a):
  60. if type(a) == InstanceType:
  61. a = a.num
  62. return self.num > a
  63. def __ge__(self, a):
  64. if type(a) == InstanceType:
  65. a = a.num
  66. return self.num >= a
  67. def __eq__(self, a):
  68. if type(a) == InstanceType:
  69. a = a.num
  70. return self.num == a
  71. def __ne__(self, a):
  72. if type(a) == InstanceType:
  73. a = a.num
  74. return self.num != a
  75. import unittest
  76. class TestNode(unittest.TestCase):
  77. def setUp(self):
  78. self.node = Node().init(khash.newID(), 'localhost', 2002)
  79. def testUpdateLastSeen(self):
  80. t = self.node.lastSeen
  81. self.node.updateLastSeen()
  82. assert t < self.node.lastSeen