]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
New DHT method 'join' like 'ping' but returns our IP and port.
authorCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 01:07:23 +0000 (17:07 -0800)
committerCameron Dale <camrdale@gmail.com>
Fri, 11 Jan 2008 01:07:23 +0000 (17:07 -0800)
apt_dht_Khashmir/DHT.py
apt_dht_Khashmir/khashmir.py
apt_dht_Khashmir/knode.py

index f77962e427416ac6f90f2258a67852390efe0ba3..933c35300a58b93a77de0574fbffa6bf5833af2b 100644 (file)
@@ -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:
index 1cb4a3c187e0e50b313c57d1b5ae4400abcd7295..80d811878260d54ac3f76bc69ce2150fb0063bf8 100644 (file)
@@ -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)
index 1e0b299cd6cdd90b6669ef76fcbc464dfdea3162..5a9d8f43898e1997c137585d49363d14ef468620 100644 (file)
@@ -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)