From: burris Date: Tue, 1 Oct 2002 05:07:14 +0000 (+0000) Subject: automatically check responses to make sure it's from the node X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7d427317cab577b54ab377100fdce5dc7d5366dc;p=quix0rs-apt-p2p.git automatically check responses to make sure it's from the node we are expecting it to be from, else errback --- diff --git a/knode.py b/knode.py index 3e32ce8..30d19fb 100644 --- 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