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
self.bootstrap_node = False
self.joining = None
self.joined = False
+ self.foundAddrs = []
self.storing = {}
self.retrieving = {}
self.retrieved = {}
"""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:
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):
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)
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)
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)