From 315725926f19740350d3057795a4da630b1c7e89 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Tue, 5 Feb 2008 19:58:38 -0700
Subject: [PATCH] refactor node startup, remove tub_ready()

---
 src/allmydata/client.py     | 64 +++++++++++++++++--------------------
 src/allmydata/introducer.py | 18 ++++++++---
 src/allmydata/node.py       |  5 ---
 3 files changed, 43 insertions(+), 44 deletions(-)

diff --git a/src/allmydata/client.py b/src/allmydata/client.py
index 38fd4c75..f485a4d5 100644
--- a/src/allmydata/client.py
+++ b/src/allmydata/client.py
@@ -59,7 +59,10 @@ class Client(node.Node, testutil.PollMixin):
         self.init_stats_provider()
         self.init_lease_secret()
         self.init_storage()
-        self.init_options()
+        self.init_control()
+        run_helper = self.get_config("run_helper")
+        if run_helper:
+            self.init_helper()
         helper_furl = self.get_config("helper.furl")
         self.add_service(Uploader(helper_furl))
         self.add_service(Downloader())
@@ -142,11 +145,33 @@ class Client(node.Node, testutil.PollMixin):
             ri_name = RIStorageServer.__remote_name__
             self.introducer_client.publish(furl, "storage", ri_name)
         d.addCallback(_publish)
-        d.addErrback(log.err, facility="tahoe.storage", level=log.BAD)
+        d.addErrback(log.err, facility="tahoe.init", level=log.BAD)
 
+    def init_control(self):
+        d = self.when_tub_ready()
+        def _publish(res):
+            c = ControlServer()
+            c.setServiceParent(self)
+            control_url = self.tub.registerReference(c)
+            self.write_private_config("control.furl", control_url + "\n")
+        d.addCallback(_publish)
+        d.addErrback(log.err, facility="tahoe.init", level=log.BAD)
 
-    def init_options(self):
-        pass
+    def init_helper(self):
+        d = self.when_tub_ready()
+        def _publish(self):
+            h = Helper(os.path.join(self.basedir, "helper"))
+            h.setServiceParent(self)
+            # TODO: this is confusing. BASEDIR/private/helper.furl is created
+            # by the helper. BASEDIR/helper.furl is consumed by the client
+            # who wants to use the helper. I like having the filename be the
+            # same, since that makes 'cp' work smoothly, but the difference
+            # between config inputs and generated outputs is hard to see.
+            helper_furlfile = os.path.join(self.basedir,
+                                           "private", "helper.furl")
+            self.tub.registerReference(h, furlFile=helper_furlfile)
+        d.addCallback(_publish)
+        d.addErrback(log.err, facility="tahoe.init", level=log.BAD)
 
     def init_web(self, webport):
         self.log("init_web(webport=%s)", args=(webport,))
@@ -169,37 +194,6 @@ class Client(node.Node, testutil.PollMixin):
             self.log("hotline file missing, shutting down")
         reactor.stop()
 
-    def tub_ready(self):
-        self.log("tub_ready")
-        node.Node.tub_ready(self)
-
-        # TODO: replace register_control() with an init_control() that
-        # internally uses self.when_tub_ready() to stall registerReference.
-        # Do the same for register_helper(). That will remove the need for
-        # this tub_ready() method.
-        self.register_control()
-        self.register_helper()
-
-    def register_control(self):
-        c = ControlServer()
-        c.setServiceParent(self)
-        control_url = self.tub.registerReference(c)
-        self.write_private_config("control.furl", control_url + "\n")
-
-    def register_helper(self):
-        run_helper = self.get_config("run_helper")
-        if not run_helper:
-            return
-        h = Helper(os.path.join(self.basedir, "helper"))
-        h.setServiceParent(self)
-        # TODO: this is confusing. BASEDIR/private/helper.furl is created by
-        # the helper. BASEDIR/helper.furl is consumed by the client who wants
-        # to use the helper. I like having the filename be the same, since
-        # that makes 'cp' work smoothly, but the difference between config
-        # inputs and generated outputs is hard to see.
-        helper_furlfile = os.path.join(self.basedir, "private", "helper.furl")
-        self.tub.registerReference(h, furlFile=helper_furlfile)
-
     def get_all_peerids(self):
         return self.introducer_client.get_all_peerids()
 
diff --git a/src/allmydata/introducer.py b/src/allmydata/introducer.py
index 45168ad7..62172157 100644
--- a/src/allmydata/introducer.py
+++ b/src/allmydata/introducer.py
@@ -15,7 +15,11 @@ class IntroducerNode(node.Node):
     ENCODING_PARAMETERS_FILE = "encoding_parameters"
     DEFAULT_K, DEFAULT_DESIRED, DEFAULT_N = 3, 7, 10
 
-    def tub_ready(self):
+    def __init__(self, basedir="."):
+        node.Node.__init__(self, basedir)
+        self.init_introducer()
+
+    def init_introducer(self):
         k, desired, n = self.DEFAULT_K, self.DEFAULT_DESIRED, self.DEFAULT_N
         data = self.get_config("encoding_parameters")
         if data is not None:
@@ -23,9 +27,15 @@ class IntroducerNode(node.Node):
             k = int(k); desired = int(desired); n = int(n)
         introducerservice = IntroducerService(self.basedir, (k, desired, n))
         self.add_service(introducerservice)
-        self.introducer_url = self.tub.registerReference(introducerservice, "introducer")
-        self.log(" introducer is at %s" % self.introducer_url)
-        self.write_config("introducer.furl", self.introducer_url + "\n")
+
+        d = self.when_tub_ready()
+        def _publish(res):
+            self.introducer_url = self.tub.registerReference(introducerservice,
+                                                             "introducer")
+            self.log(" introducer is at %s" % self.introducer_url)
+            self.write_config("introducer.furl", self.introducer_url + "\n")
+        d.addCallback(_publish)
+        d.addErrback(log.err, facility="tahoe.init", level=log.BAD)
 
 class IntroducerService(service.MultiService, Referenceable):
     implements(RIIntroducerPublisherAndSubscriberService)
diff --git a/src/allmydata/node.py b/src/allmydata/node.py
index 56b63cce..ae1f9229 100644
--- a/src/allmydata/node.py
+++ b/src/allmydata/node.py
@@ -161,7 +161,6 @@ class Node(service.MultiService):
         d = defer.succeed(None)
         d.addCallback(lambda res: iputil.get_local_addresses_async())
         d.addCallback(self._setup_tub)
-        d.addCallback(lambda res: self.tub_ready())
         def _ready(res):
             self.log("%s running" % self.NODETYPE)
             self._tub_ready_observerlist.fire(self)
@@ -257,10 +256,6 @@ class Node(service.MultiService):
         self.tub.setLocation(location)
         return self.tub
 
-    def tub_ready(self):
-        # called when the Tub is available for registerReference
-        pass
-
     def when_tub_ready(self):
         return self._tub_ready_observerlist.when_fired()
 
-- 
2.45.2