added stun rpc method

This commit is contained in:
Brian Muller 2014-01-13 22:25:51 -05:00
parent abf5fe19ad
commit 10925520c4
2 changed files with 20 additions and 1 deletions

View File

@ -172,7 +172,7 @@ class Server(object):
are acceptable - hostnames will cause an error.
"""
# if the transport hasn't been initialized yet, wait a second
if self.protocol.transport == None:
if self.protocol.transport is None:
reactor.callLater(1, self.bootstrap, addrs)
return
@ -189,6 +189,22 @@ class Server(object):
ds[addr] = self.protocol.ping(addr, self.node.id)
return deferredDict(ds).addCallback(initTable)
def inetVisibleIP(self):
"""
Get the internet visible IP's of this node as other nodes see it.
@return: An C{list} of IP's. If no one can be contacted, then the
C{list} will be empty.
"""
def handle(results):
ips = [ result[0] for result in results if result is not None ]
self.log.debug("other nodes think our ip is %s" % str(ips))
return ips
ds = []
for neighbor in self.bootstrappableNeighbors():
ds.append(self.protocol.router.stun(neighbor))
return defer.gatherResults(ds).addCallback(handle)
def get(self, key):
"""
Get a key if the network has it.

View File

@ -23,6 +23,9 @@ class KademliaProtocol(RPCProtocol):
ids.append(random.randint(*bucket.range))
return ids
def rpc_stun(self, sender):
return sender
def rpc_ping(self, sender, nodeid):
source = Node(nodeid, sender[0], sender[1])
self.router.addContact(source)