]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
always use the socket's idea of host and port
authorburris <burris>
Mon, 14 Jun 2004 02:32:02 +0000 (02:32 +0000)
committerburris <burris>
Mon, 14 Jun 2004 02:32:02 +0000 (02:32 +0000)
khash.py
khashmir.py
krpc.py
test_khashmir.py
test_krpc.py

index deb8d35b19bcbe3e2e1a1f91d66571e1a2bf55c6..f750deaf7126247dac716725da7ba89a61417bd5 100644 (file)
--- 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"""
index e9e53a76ebd60a2f574f38c743e5dd95ced378f0..7091f805f1069cda6ff47a282c84d2436e289299 100644 (file)
@@ -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 24a0f16c9a06f4c703f401bda85f473f2f4fd318..7e64544f4103b26e8b0ed925d6144e5c40a8561b 100644 (file)
--- 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
index 7dcc247dde9344725ffd9c602d325db8e7e1d1a0..d0dfc82bc1eb0e08eb6ae64dba3f393af9bd288a 100644 (file)
@@ -71,7 +71,7 @@ class SimpleTests(TestCase):
 
 
 class MultiTest(TestCase):
-    num = 30
+    num = 5
     def _done(self, val):
         self.done = 1
         
index a7bef9d54b49dfda756886565ee0f2748e2a20fb..7dd4bacc6c0fe4ed4559a25dbbc9dfe141ef8b41 100644 (file)
@@ -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):