Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit cbdd554

Browse files
committed
End find_node crawl early if node is found
closes #281
1 parent d527462 commit cbdd554

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

dht/crawling.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ def _handleFoundValues(self, values):
160160

161161
class NodeSpiderCrawl(SpiderCrawl):
162162

163+
def __init__(self, protocol, node, peers, ksize, alpha, find_exact=False):
164+
SpiderCrawl.__init__(self, protocol, node, peers, ksize, alpha)
165+
self.find_exact = find_exact
166+
163167
def find(self):
164168
"""
165169
Find the closest nodes.
@@ -176,7 +180,12 @@ def _nodesFound(self, responses):
176180
if not response.happened():
177181
toremove.append(peerid)
178182
else:
179-
self.nearest.push(response.getNodeList())
183+
node_list = response.getNodeList()
184+
self.nearest.push(node_list)
185+
if self.find_exact:
186+
for node in node_list:
187+
if node.id == self.node.id:
188+
return [node]
180189
self.nearest.remove(toremove)
181190

182191
if self.nearest.allBeenContacted():

dht/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def check_for_node(nodes):
354354
self.log.warning("there are no known neighbors to find node %s" % node_to_find.id.encode("hex"))
355355
return defer.succeed(None)
356356

357-
spider = NodeSpiderCrawl(self.protocol, node_to_find, nearest, self.ksize, self.alpha)
357+
spider = NodeSpiderCrawl(self.protocol, node_to_find, nearest, self.ksize, self.alpha, True)
358358
return spider.find().addCallback(check_for_node)
359359

360360
def saveState(self, fname):

0 commit comments

Comments
 (0)