From f3c0856dc76910bf6af8919b34880121f6418ea0 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Thu, 30 Nov 2006 16:39:38 -0700
Subject: [PATCH] help the queen have a persistent PBURL, have the client
 connect to it

---
 allmydata/queen.py       | 37 ++++++++++++++++++++++++++++++++++---
 allmydata/util/iputil.py |  2 +-
 client.tac               |  2 +-
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/allmydata/queen.py b/allmydata/queen.py
index ca667178..79d7db4a 100644
--- a/allmydata/queen.py
+++ b/allmydata/queen.py
@@ -3,12 +3,15 @@ from foolscap import Tub, Referenceable
 from twisted.application import service
 from twisted.python import log
 import os.path
+from allmydata.util.iputil import get_local_ip_for
 
 class Roster(service.MultiService, Referenceable):
-    pass
+    def remote_hello(self, urls):
+        print "contact from %s" % urls
 
 class Queen(service.MultiService):
     CERTFILE = "queen.pem"
+    PORTNUMFILE = "queen.port"
 
     def __init__(self):
         service.MultiService.__init__(self)
@@ -19,12 +22,40 @@ class Queen(service.MultiService):
             f = open(self.CERTFILE, "wb")
             f.write(self.tub.getCertData())
             f.close()
+        portnum = 0
+        if os.path.exists(self.PORTNUMFILE):
+            portnum = int(open(self.PORTNUMFILE, "r").read())
+        self.tub.listenOn("tcp:%d" % portnum)
+        # we must wait until our service has started before we can find out
+        # our IP address and thus do tub.setLocation, and we can't register
+        # any services with the Tub until after that point
+        self.tub.setServiceParent(self)
         self.urls = {}
+
+    def _setup_tub(self, local_ip):
+        l = self.tub.getListeners()[0]
+        portnum = l.getPortnum()
+        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
+            # one next time
+            f = open(self.PORTNUMFILE, "w")
+            f.write("%d\n" % portnum)
+            f.close()
+        self.tub.setLocation("%s:%d" % (local_ip, l.getPortnum()))
+        return local_ip
+
+    def _setup_services(self, local_ip):
         r = Roster()
         r.setServiceParent(self)
-        #self.urls["roster"] = self.tub.registerReference(r, "roster")
+        self.urls["roster"] = self.tub.registerReference(r, "roster")
+        log.msg(" roster is at %s" % self.urls["roster"])
 
     def startService(self):
+        # note: this class can only be started and stopped once.
         service.MultiService.startService(self)
         log.msg("queen running")
-        #log.msg(" roster is at %s" % self.urls["roster"])
+        d = get_local_ip_for()
+        d.addCallback(self._setup_tub)
+        d.addCallback(self._setup_services)
+
diff --git a/allmydata/util/iputil.py b/allmydata/util/iputil.py
index a8daa01d..602d5f1a 100644
--- a/allmydata/util/iputil.py
+++ b/allmydata/util/iputil.py
@@ -7,7 +7,7 @@ from twisted.internet.protocol import DatagramProtocol
 #from twisted.internet.interfaces import IReactorMulticast
 #from amdlib.util.nattraverso.utils import is_rfc1918_ip, is_bogus_ip
 
-def get_local_ip_for(target):
+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
diff --git a/client.tac b/client.tac
index 035156d8..7d987783 100644
--- a/client.tac
+++ b/client.tac
@@ -4,7 +4,7 @@ from allmydata import client
 from twisted.application import service
 
 queen_host = "yumyum"
-queen_pburl = ""
+queen_pburl = "pb://jekyv6ghn7zinppk7wcvfmk7o4gw76hb@192.168.1.101:42552/roster"
 c = client.Client(queen_host, queen_pburl)
 
 application = service.Application("allmydata_client")
-- 
2.45.2