From 4860bd484f9bd22f049205e5932c95f13d172816 Mon Sep 17 00:00:00 2001 From: burris Date: Mon, 14 Jun 2004 02:32:02 +0000 Subject: [PATCH] always use the socket's idea of host and port --- khash.py | 2 +- khashmir.py | 5 ++--- krpc.py | 33 +++++++++++++++++---------------- test_khashmir.py | 2 +- test_krpc.py | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/khash.py b/khash.py index deb8d35..f750dea 100644 --- a/khash.py +++ b/khash.py @@ -12,7 +12,7 @@ except ImportError: s = '' for i in range(n): s += chr(whrandom.randint(0,255)) - return s + return s def intify(hstr): """20 bit hash, big-endian -> long python integer""" diff --git a/khashmir.py b/khashmir.py index e9e53a7..7091f80 100644 --- a/khashmir.py +++ b/khashmir.py @@ -222,7 +222,6 @@ class Khashmir(protocol.Factory): def _notStaleNodeHandler(dict, old=old): """ called when we get a pong from the old node """ - _krpc_sender = dict['_krpc_sender'] dict = dict['rsp'] sender = dict['sender'] if sender['id'] == old.id: @@ -245,8 +244,8 @@ class Khashmir(protocol.Factory): # whoah, got response from different peer than we were expecting self.table.invalidateNode(node) else: - sender['host'] = node.host - sender['port'] = node.port + sender['host'] = _krpc_sender[0] + sender['port'] = _krpc_sender[1] n = Node().initWithDict(sender) n.conn = self.udp.connectionForAddr((n.host, n.port)) table.insertNode(n) diff --git a/krpc.py b/krpc.py index 24a0f16..7e64544 100644 --- a/krpc.py +++ b/krpc.py @@ -20,16 +20,15 @@ KRPC_ERROR_RECEIVED_UNKNOWN = 3 KRPC_ERROR_TIMEOUT = 4 # commands -TID = 'tid' -REQ = 'req' -RSP = 'rsp' -TYP = 'typ' -ARG = 'arg' -ERR = 'err' +TID = 't' +REQ = 'q' +RSP = 'r' +TYP = 'y' +ARG = 'a' +ERR = 'e' class hostbroker(protocol.DatagramProtocol): def __init__(self, server): - self.noisy = 0 self.server = server # this should be changed to storage that drops old entries self.connections = {} @@ -38,7 +37,7 @@ class hostbroker(protocol.DatagramProtocol): #print `addr`, `datagram` #if addr != self.addr: c = self.connectionForAddr(addr) - c.datagramReceived(datagram) + c.datagramReceived(datagram, addr) #if c.idle(): # del self.connections[addr] @@ -65,8 +64,9 @@ class KRPC: self.factory = server self.addr = addr self.tids = {} + self.mtid = 0 - def datagramReceived(self, str): + def datagramReceived(self, str, addr): # bdecode try: msg = bdecode(str) @@ -83,15 +83,15 @@ class KRPC: # if request # tell factory to handle f = getattr(self.factory ,"krpc_" + msg[REQ], None) + msg[ARG]['_krpc_sender'] = self.addr if f and callable(f): - msg[ARG]['_krpc_sender'] = self.addr try: ret = apply(f, (), msg[ARG]) except Exception, e: ## send error out = bencode({TID:msg[TID], TYP:ERR, ERR :`e`}) olen = len(out) - self.transport.write(out, self.addr) + self.transport.write(out, addr) else: if ret: # make response @@ -100,7 +100,7 @@ class KRPC: out = bencode({TID : msg[TID], TYP : RSP, RSP : {}}) # send response olen = len(out) - self.transport.write(out, self.addr) + self.transport.write(out, addr) else: if self.noisy: @@ -108,9 +108,9 @@ class KRPC: # unknown method out = bencode({TID:msg[TID], TYP:ERR, ERR : KRPC_ERROR_METHOD_UNKNOWN}) olen = len(out) - self.transport.write(out, self.addr) + self.transport.write(out, addr) if self.noisy: - print "%s %s >>> %s - %s %s %s" % (time.asctime(), self.addr, self.factory.node.port, + print "%s %s >>> %s - %s %s %s" % (time.asctime(), addr, self.factory.node.port, ilen, msg[REQ], olen) elif msg[TYP] == RSP: # if response @@ -119,7 +119,7 @@ class KRPC: df = self.tids[msg[TID]] # callback del(self.tids[msg[TID]]) - df.callback({RSP : msg[RSP], '_krpc_sender': self.addr}) + df.callback({'rsp' : msg[RSP], '_krpc_sender': addr}) else: print 'timeout ' + `msg[RSP]['sender']` # no tid, this transaction timed out already... @@ -145,7 +145,8 @@ class KRPC: def sendRequest(self, method, args): # make message # send it - msg = {TID : hash.newTID(), TYP : REQ, REQ : method, ARG : args} + msg = {TID : chr(self.mtid), TYP : REQ, REQ : method, ARG : args} + self.mtid = (self.mtid + 1) % 256 str = bencode(msg) d = Deferred() self.tids[msg[TID]] = d diff --git a/test_khashmir.py b/test_khashmir.py index 7dcc247..d0dfc82 100644 --- a/test_khashmir.py +++ b/test_khashmir.py @@ -71,7 +71,7 @@ class SimpleTests(TestCase): class MultiTest(TestCase): - num = 30 + num = 5 def _done(self, val): self.done = 1 diff --git a/test_krpc.py b/test_krpc.py index a7bef9d..7dd4bac 100644 --- a/test_krpc.py +++ b/test_krpc.py @@ -79,7 +79,7 @@ class KRPCTests(TestCase): def gotMsg(self, dict): _krpc_sender = dict['_krpc_sender'] - msg = dict[RSP] + msg = dict['rsp'] self.msg = msg def testManyEcho(self): -- 2.39.5