findValue inlinecallbacks refactor

This commit is contained in:
Jack Robison 2018-02-20 13:39:14 -05:00
parent 43896c8d17
commit 16fcc3f5c1
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -418,14 +418,13 @@ class Node(object):
to the specified key to the specified key
@rtype: twisted.internet.defer.Deferred @rtype: twisted.internet.defer.Deferred
""" """
# Prepare a callback for this operation
outerDf = defer.Deferred()
def checkResult(result): # Execute the search
if isinstance(result, dict): iterative_find_result = yield self._iterativeFind(key, rpc='findValue')
if isinstance(iterative_find_result, dict):
# We have found the value; now see who was the closest contact without it... # We have found the value; now see who was the closest contact without it...
# ...and store the key/value pair # ...and store the key/value pair
outerDf.callback(result) defer.returnValue(iterative_find_result)
else: else:
# The value wasn't found, but a list of contacts was returned # The value wasn't found, but a list of contacts was returned
# Now, see if we have the value (it might seem wasteful to search on the network # Now, see if we have the value (it might seem wasteful to search on the network
@ -433,18 +432,12 @@ class Node(object):
# network # network
if self._dataStore.hasPeersForBlob(key): if self._dataStore.hasPeersForBlob(key):
# Ok, we have the value locally, so use that # Ok, we have the value locally, so use that
peers = self._dataStore.getPeersForBlob(key)
# Send this value to the closest node without it # Send this value to the closest node without it
outerDf.callback({key: peers}) peers = self._dataStore.getPeersForBlob(key)
defer.returnValue({key: peers})
else: else:
# Ok, value does not exist in DHT at all # Ok, value does not exist in DHT at all
outerDf.callback(result) defer.returnValue(iterative_find_result)
# Execute the search
iterative_find_result = yield self._iterativeFind(key, rpc='findValue')
checkResult(iterative_find_result)
result = yield outerDf
defer.returnValue(result)
def addContact(self, contact): def addContact(self, contact):
""" Add/update the given contact; simple wrapper for the same method """ Add/update the given contact; simple wrapper for the same method