From 77661127043b6f558cfadbf78e37e535753087ea Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Thu, 10 Jan 2008 17:07:23 -0800 Subject: [PATCH] New DHT method 'join' like 'ping' but returns our IP and port. --- apt_dht_Khashmir/DHT.py | 16 +++++++++++----- apt_dht_Khashmir/khashmir.py | 15 ++++++++++----- apt_dht_Khashmir/knode.py | 7 +++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/apt_dht_Khashmir/DHT.py b/apt_dht_Khashmir/DHT.py index f77962e..933c353 100644 --- a/apt_dht_Khashmir/DHT.py +++ b/apt_dht_Khashmir/DHT.py @@ -3,6 +3,7 @@ import os, sha, random from twisted.internet import defer, reactor from twisted.internet.abstract import isIPAddress +from twisted.python import log from twisted.trial import unittest from zope.interface import implements @@ -23,6 +24,7 @@ class DHT: self.bootstrap_node = False self.joining = None self.joined = False + self.foundAddrs = [] self.storing = {} self.retrieving = {} self.retrieved = {} @@ -71,23 +73,27 @@ class DHT: """Called after an IP address has been found for a single bootstrap node.""" self.khashmir.addContact(ip, port, self._join_single) - def _join_single(self): + def _join_single(self, addr): """Called when a single bootstrap node has been added.""" + if addr: + self.foundAddrs.append(addr) + log.msg('Got back from bootstrap node: %r' % (addr,)) self.khashmir.findCloseNodes(self._join_complete) def _join_complete(self, result): """Called when the tables have been initialized with nodes.""" if not self.joined: self.joined = True + df = self.joining + self.joining = None if len(result) > 0 or self.bootstrap_node: - df = self.joining - self.joining = None df.callback(result) else: - df = self.joining - self.joining = None df.errback(DHTError('could not find any nodes to bootstrap to')) + def getAddrs(self): + return self.foundAddrs + def leave(self): """See L{apt_dht.interfaces.IDHT}.""" if self.config is None: diff --git a/apt_dht_Khashmir/khashmir.py b/apt_dht_Khashmir/khashmir.py index 1cb4a3c..80d8118 100644 --- a/apt_dht_Khashmir/khashmir.py +++ b/apt_dht_Khashmir/khashmir.py @@ -85,7 +85,7 @@ class KhashmirBase(protocol.Factory): ping this node and add the contact info to the table on pong! """ n = self.Node(NULL_ID, host, port) - self.sendPing(n, callback=callback) + self.sendJoin(n, callback=callback) ## this call is async! def findNode(self, id, callback, errback=None): @@ -133,21 +133,21 @@ class KhashmirBase(protocol.Factory): df = old.ping(self.node.id) df.addCallbacks(_notStaleNodeHandler, _staleNodeHandler) - def sendPing(self, node, callback=None): + def sendJoin(self, node, callback=None): """ ping a node """ - df = node.ping(self.node.id) + df = node.join(self.node.id) ## these are the callbacks we use when we issue a PING def _pongHandler(dict, node=node, self=self, callback=callback): n = self.Node(dict['rsp']['id'], dict['_krpc_sender'][0], dict['_krpc_sender'][1]) self.insertNode(n) if callback: - callback() + callback((dict['rsp']['ip_addr'], dict['rsp']['port'])) def _defaultPong(err, node=node, table=self.table, callback=callback): table.nodeFailed(node) if callback: - callback() + callback(None) df.addCallbacks(_pongHandler,_defaultPong) @@ -199,6 +199,11 @@ class KhashmirBase(protocol.Factory): self.insertNode(n, contacted=0) return {"id" : self.node.id} + def krpc_join(self, id, _krpc_sender): + n = self.Node(id, _krpc_sender[0], _krpc_sender[1]) + self.insertNode(n, contacted=0) + return {"ip_addr" : _krpc_sender[0], "port" : _krpc_sender[1], "id" : self.node.id} + def krpc_find_node(self, target, id, _krpc_sender): n = self.Node(id, _krpc_sender[0], _krpc_sender[1]) self.insertNode(n, contacted=0) diff --git a/apt_dht_Khashmir/knode.py b/apt_dht_Khashmir/knode.py index 1e0b299..5a9d8f4 100644 --- a/apt_dht_Khashmir/knode.py +++ b/apt_dht_Khashmir/knode.py @@ -26,6 +26,13 @@ class KNodeBase(Node): df.addErrback(self.errBack) df.addCallback(self.checkSender) return df + + def join(self, id): + df = self.conn.sendRequest('join', {"id":id}) + df.addErrback(self.errBack) + df.addCallback(self.checkSender) + return df + def findNode(self, target, id): df = self.conn.sendRequest('find_node', {"target" : target, "id": id}) df.addErrback(self.errBack) -- 2.39.5