X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=node.py;h=f8d2801c1d69e5b9fde13bca5d1d1d95a48726a0;hb=2852f1c4e2cc9afb3723ff04f4586e35281420fc;hp=cb1940e3cd0b5f6fed8f6fb58307ee18d17ab725;hpb=1abfc43747d252f5d0bf11f119e0daabecc4f3a6;p=quix0rs-apt-p2p.git diff --git a/node.py b/node.py index cb1940e..f8d2801 100644 --- a/node.py +++ b/node.py @@ -1,56 +1,81 @@ -import hash +## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved +# see LICENSE.txt for license information + +import khash import time from types import * class Node: """encapsulate contact info""" - def __init__(self, id, host, port): - self.id = id - self.int = hash.intify(id) - self.host = host - self.port = port - self.lastSeen = time.time() - + def __init__(self): + self.fails = 0 + self.lastSeen = 0 + self.id = self.host = self.port = '' + + def init(self, id, host, port): + self.id = id + self.num = khash.intify(id) + self.host = host + self.port = port + self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host} + return self + + def initWithDict(self, dict): + self._senderDict = dict + self.id = dict['id'] + self.num = khash.intify(self.id) + self.port = dict['port'] + self.host = dict['host'] + return self + def updateLastSeen(self): - self.lastSeen = time.time() - + self.lastSeen = time.time() + self.fails = 0 + + def msgFailed(self): + self.fails = self.fails + 1 + return self.fails + + def senderDict(self): + return self._senderDict + def __repr__(self): - return `(self.id, self.host, self.port)` - + return `(self.id, self.host, self.port)` + ## these comparators let us bisect/index a list full of nodes with either a node or an int/long def __lt__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int < a + if type(a) == InstanceType: + a = a.num + return self.num < a def __le__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int <= a + if type(a) == InstanceType: + a = a.num + return self.num <= a def __gt__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int > a + if type(a) == InstanceType: + a = a.num + return self.num > a def __ge__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int >= a + if type(a) == InstanceType: + a = a.num + return self.num >= a def __eq__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int == a + if type(a) == InstanceType: + a = a.num + return self.num == a def __ne__(self, a): - if type(a) == InstanceType: - a = a.int - return self.int != a + if type(a) == InstanceType: + a = a.num + return self.num != a import unittest class TestNode(unittest.TestCase): def setUp(self): - self.node = Node(hash.newID(), 'localhost', 2002) + self.node = Node().init(khash.newID(), 'localhost', 2002) def testUpdateLastSeen(self): - t = self.node.lastSeen - self.node.updateLastSeen() - assert t < self.node.lastSeen - \ No newline at end of file + t = self.node.lastSeen + self.node.updateLastSeen() + assert t < self.node.lastSeen + \ No newline at end of file