]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
automatically check responses to make sure it's from the node
authorburris <burris>
Tue, 1 Oct 2002 05:07:14 +0000 (05:07 +0000)
committerburris <burris>
Tue, 1 Oct 2002 05:07:14 +0000 (05:07 +0000)
we are expecting it to be from, else errback

knode.py

index 3e32ce83409aebffe04eec98f17b5dec1533e508..30d19fb7ca369ee1154d6a7f90b94d65e2b5bfba 100644 (file)
--- a/knode.py
+++ b/knode.py
@@ -1,28 +1,40 @@
 from node import Node
 from twisted.internet.defer import Deferred
 from xmlrpcclient import XMLRPCClientFactory as factory
-from const import reactor
+from const import reactor, NULL_ID
 from xmlrpclib import Binary
 
 
-class KNode(Node):     
+class KNode(Node):
+    def makeResponse(self, df):
+       def _callback(args, d=df):
+           try:
+               l, sender = args
+           except:
+               d.callback(args)
+           else:
+               if self.id != NULL_ID and sender['id'].data != self.id:
+                   d.errback()
+               else:
+                   d.callback(args)
+       return _callback
     def ping(self, sender):
        df = Deferred()
-       f = factory('ping', (sender,), df.callback, df.errback)
+       f = factory('ping', (sender,), self.makeResponse(df), df.errback)
        reactor.connectTCP(self.host, self.port, f)
        return df
     def findNode(self, target, sender):
        df = Deferred()
-       f = factory('find_node', (Binary(target), sender), df.callback, df.errback)
+       f = factory('find_node', (Binary(target), sender), self.makeResponse(df), df.errback)
        reactor.connectTCP(self.host, self.port, f)
        return df
     def storeValue(self, key, value, sender):
        df = Deferred()
-       f = factory('store_value', (Binary(key), Binary(value), sender), df.callback, df.errback)
+       f = factory('store_value', (Binary(key), Binary(value), sender), self.makeResponse(df), df.errback)
        reactor.connectTCP(self.host, self.port, f)
        return df
     def findValue(self, key, sender):
        df = Deferred()
-       f = factory('find_value', (Binary(key), sender), df.callback, df.errback)
+       f = factory('find_value', (Binary(key), sender), self.makeResponse(df), df.errback)
        reactor.connectTCP(self.host, self.port, f)
        return df