Maybe transfer key, values on incoming request.

The existing code only checks if we should call transferKeyValues when we
receive a response to one of our RPCs. We also need to check when receive a
new request from a new node.

This commit adds a new method that is called each time we receive a new request.
This commit is contained in:
Chris 2015-07-14 21:29:48 -04:00
parent f5b2c13d93
commit 2f363cba84

View File

@ -32,11 +32,13 @@ class KademliaProtocol(RPCProtocol):
def rpc_ping(self, sender, nodeid):
source = Node(nodeid, sender[0], sender[1])
self.maybeTransferKeyValues(source)
self.router.addContact(source)
return self.sourceNode.id
def rpc_store(self, sender, nodeid, key, value):
source = Node(nodeid, sender[0], sender[1])
self.maybeTransferKeyValues(source)
self.router.addContact(source)
self.log.debug("got a store request from %s, storing value" % str(sender))
self.storage[key] = value
@ -45,12 +47,14 @@ class KademliaProtocol(RPCProtocol):
def rpc_find_node(self, sender, nodeid, key):
self.log.info("finding neighbors of %i in local table" % long(nodeid.encode('hex'), 16))
source = Node(nodeid, sender[0], sender[1])
self.maybeTransferKeyValues(source)
self.router.addContact(source)
node = Node(key)
return map(tuple, self.router.findNeighbors(node, exclude=source))
def rpc_find_value(self, sender, nodeid, key):
source = Node(nodeid, sender[0], sender[1])
self.maybeTransferKeyValues(source)
self.router.addContact(source)
value = self.storage.get(key, None)
if value is None:
@ -101,6 +105,10 @@ class KademliaProtocol(RPCProtocol):
ds.append(self.callStore(node, key, value))
return defer.gatherResults(ds)
def maybeTransferKeyValues(self, node):
if self.router.isNewNode(node):
self.transferKeyValues(node)
def handleCallResponse(self, result, node):
"""
If we get a response, add the node to the routing table. If