]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
rearrange node startup again, allowing Tub.registerReference to happen in startServic...
authorBrian Warner <warner@lothar.com>
Sun, 3 Dec 2006 02:37:31 +0000 (19:37 -0700)
committerBrian Warner <warner@lothar.com>
Sun, 3 Dec 2006 02:37:31 +0000 (19:37 -0700)
allmydata/client.py
allmydata/node.py
allmydata/queen.py
allmydata/test/test_system.py
allmydata/util/iputil.py

index df17ac7240433a41705619872b1be2030ba47f3d..641b2e433d653e26d2a841aa1e023d0e15d0cc06 100644 (file)
@@ -7,12 +7,7 @@ from zope.interface import implements
 from allmydata.interfaces import RIClient
 from allmydata import node
 
-from twisted.internet import defer, reactor
-# this BlockingResolver is because otherwise unit tests must sometimes deal
-# with a leftover DNS lookup thread. I'd prefer to not do this, and use the
-# default ThreadedResolver
-from twisted.internet.base import BlockingResolver
-reactor.installResolver(BlockingResolver())
+from twisted.internet import defer
 
 from allmydata.storageserver import StorageServer
 from allmydata.upload import Uploader
@@ -34,7 +29,10 @@ class Client(node.Node, Referenceable):
         self.add_service(Uploader())
         self.queen_pburl = None
         self.queen_connector = None
-        self.my_pburl = None
+
+    def tub_ready(self):
+        self.my_pburl = self.tub.registerReference(self)
+        self.maybe_connect_to_queen()
 
     def set_queen_pburl(self, queen_pburl):
         self.queen_pburl = queen_pburl
@@ -53,10 +51,6 @@ class Client(node.Node, Referenceable):
         self.queen_connector = self.tub.connectTo(self.queen_pburl,
                                                   self._got_queen)
 
-    def tub_ready(self, tub):
-        self.my_pburl = self.tub.registerReference(self)
-        self.maybe_connect_to_queen()
-
     def stopService(self):
         if self.queen_connector:
             self.queen_connector.stopConnecting()
index 201b75401e3fb044ba6ce75e711871d95c8d73b5..7a0d28ad83b2f01a27bd6d3861eee42417444625 100644 (file)
@@ -12,6 +12,7 @@ class Node(service.MultiService):
     NODETYPE = "unknown NODETYPE"
     PORTNUMFILE = None
     CERTFILE = None
+    LOCAL_IP_FILE = "local_ip"
 
     def __init__(self, basedir="."):
         service.MultiService.__init__(self)
@@ -50,8 +51,15 @@ class Node(service.MultiService):
                 log.msg("AuthorizedKeysManhole listening on %d" % portnum)
 
     def _setup_tub(self, local_ip):
+        # we can't get a dynamically-assigned portnum until our Tub is
+        # running, which means after startService.
         l = self.tub.getListeners()[0]
         portnum = l.getPortnum()
+        local_ip_filename = os.path.join(self.basedir, self.LOCAL_IP_FILE)
+        if os.path.exists(local_ip_filename):
+            f = open(local_ip_filename, "r")
+            local_ip = f.read()
+            f.close()
         self.tub.setLocation("%s:%d" % (local_ip, portnum))
         if not os.path.exists(self._portnumfile):
             # record which port we're listening on, so we can grab the same
@@ -62,8 +70,8 @@ class Node(service.MultiService):
         self.tub.setLocation("%s:%d" % (local_ip, l.getPortnum()))
         return self.tub
 
-    def tub_ready(self, tub):
-        # this is called when the Tub has a location
+    def tub_ready(self):
+        # called when the Tub is available for registerReference
         pass
 
     def add_service(self, s):
@@ -73,8 +81,7 @@ class Node(service.MultiService):
     def startService(self):
         # note: this class can only be started and stopped once.
         service.MultiService.startService(self)
-        d = get_local_ip_for()
-        d.addCallback(self._setup_tub)
-        d.addCallback(self.tub_ready)
-        d.addCallback(lambda res: log.msg("%s running" % self.NODETYPE))
-
+        local_ip = get_local_ip_for()
+        self._setup_tub(local_ip)
+        self.tub_ready()
+        log.msg("%s running" % self.NODETYPE)
index 5db057881da5b3385114bc7e123aa41ed7dd6077..dfb299d01eb23a315055e40f7bcb45582393aa39 100644 (file)
@@ -1,4 +1,5 @@
 
+import os.path
 from foolscap import Referenceable
 from foolscap.eventual import eventually
 from twisted.application import service
@@ -53,8 +54,12 @@ class Queen(node.Node):
         node.Node.__init__(self, basedir)
         self.urls = {}
 
-    def tub_ready(self, tub):
+    def tub_ready(self):
         r = self.add_service(Roster())
         self.urls["roster"] = self.tub.registerReference(r, "roster")
         log.msg(" roster is at %s" % self.urls["roster"])
+        f = open(os.path.join(self.basedir, "roster_pburl"), "w")
+        f.write(self.urls["roster"] + "\n")
+        f.close()
+
 
index 41a236a2936277eacafda59198f0a3b534d6126e..060ed5a7aba5ec16f3d10408e2af4f63a2b56bcd 100644 (file)
@@ -1,21 +1,25 @@
 
 from twisted.trial import unittest
+from twisted.internet import defer, reactor
 from twisted.application import service
 from allmydata import upload, client, queen
 import os
+from foolscap.eventual import flushEventualQueue
 
 class SystemTest(unittest.TestCase):
     def setUp(self):
         self.sparent = service.MultiService()
         self.sparent.startService()
     def tearDown(self):
-        return self.sparent.stopService()
+        d = self.sparent.stopService()
+        d.addCallback(lambda res: flushEventualQueue())
+        return d
 
     def addService(self, s):
         s.setServiceParent(self.sparent)
         return s
 
-    def test_it(self):
+    def setUpNodes(self):
         os.mkdir("queen")
         q = self.addService(queen.Queen(basedir="queen"))
         clients = []
@@ -23,5 +27,17 @@ class SystemTest(unittest.TestCase):
         for i in range(NUMCLIENTS):
             basedir = "client%d" % i
             os.mkdir(basedir)
-            clients.append(self.addService(client.Client(basedir=basedir)))
+            c = self.addService(client.Client(basedir=basedir))
+            clients.append(c)
+
 
+    def waitForConnections(self):
+        # the cheap way: time
+        d = defer.Deferred()
+        reactor.callLater(1, d.callback, None)
+        return d
+
+    def test_it(self):
+        self.setUpNodes()
+        d = self.waitForConnections()
+        return d
index 602d5f1a09a93c22a16df9e6ed70ea3702430caa..9b87b1c00012e486cd46145a9de774f5811de0d5 100644 (file)
@@ -1,48 +1,21 @@
 
 # adapted from nattraverso.ipdiscover
 
+import socket
 from twisted.internet import reactor
 from twisted.internet.protocol import DatagramProtocol
-#from twisted.internet.error import CannotListenError
-#from twisted.internet.interfaces import IReactorMulticast
-#from amdlib.util.nattraverso.utils import is_rfc1918_ip, is_bogus_ip
 
 def get_local_ip_for(target='A.ROOT-SERVERS.NET'):
     """Find out what our IP address is for use by a given target.
 
-    Returns a Deferred which will be fired with a string that holds the IP
-    address which could be used by 'target' to connect to us. It might work
-    for them, it might not.
-
-    The reactor must be running before you can call this, because we must
-    perform a DNS lookup on the target.
-
-    """
-    d = reactor.resolve(target)
-    def _resolved(target_ipaddr):
-        udpprot = DatagramProtocol()
-        port = reactor.listenUDP(0, udpprot)
-        udpprot.transport.connect(target_ipaddr, 7)
-        localip = udpprot.transport.getHost().host
-        port.stopListening()
-        return localip
-    d.addCallback(_resolved)
-    return d
-
-
-
-def BROKEN_get_local_ip_for(target_ipaddr):
-    """Find out what our IP address is for use by a given target.
-
-    Returns a Deferred which will be fired with a string that holds the IP
-    address which could be used by 'target' to connect to us. It might work
-    for them, it might not. 'target' must be an IP address.
-
+    Returns a string that holds the IP address which could be used by
+    'target' to connect to us. It might work for them, it might not.
     """
+    target_ipaddr = socket.gethostbyname(target)
     udpprot = DatagramProtocol()
     port = reactor.listenUDP(0, udpprot)
     udpprot.transport.connect(target_ipaddr, 7)
     localip = udpprot.transport.getHost().host
-    port.stopListening()
-
+    port.stopListening() # note, this returns a Deferred
     return localip
+