3 from twisted.python import log
4 from twisted.application import service
5 from twisted.internet import defer
6 from foolscap import Tub
7 from allmydata.util import idlib, iputil, observer
10 # Just to get their versions:
15 class Node(service.MultiService):
16 # this implements common functionality of both Client nodes, Introducer
17 # nodes, and Vdrive nodes
18 NODETYPE = "unknown NODETYPE"
21 LOCAL_IP_FILE = "local_ip"
22 NODEIDFILE = "my_nodeid"
24 def __init__(self, basedir="."):
25 service.MultiService.__init__(self)
26 self.basedir = os.path.abspath(basedir)
27 self._tub_ready_observerlist = observer.OneShotObserverList()
28 assert self.CERTFILE, "Your node.Node subclass must provide CERTFILE"
29 certfile = os.path.join(self.basedir, self.CERTFILE)
30 if os.path.exists(certfile):
31 f = open(certfile, "rb")
32 self.tub = Tub(certData=f.read())
36 f = open(certfile, "wb")
37 f.write(self.tub.getCertData())
39 self.tub.setOption("logLocalFailures", True)
40 self.tub.setOption("logRemoteFailures", True)
41 self.nodeid = idlib.a2b(self.tub.tubID)
42 f = open(os.path.join(self.basedir, self.NODEIDFILE), "w")
43 f.write(idlib.b2a(self.nodeid) + "\n")
45 self.short_nodeid = self.tub.tubID[:4] # ready for printing
47 assert self.PORTNUMFILE, "Your node.Node subclass must provide PORTNUMFILE"
48 self._portnumfile = os.path.join(self.basedir, self.PORTNUMFILE)
49 if os.path.exists(self._portnumfile):
50 portnum = int(open(self._portnumfile, "r").read())
51 self.tub.listenOn("tcp:%d" % portnum)
52 # we must wait until our service has started before we can find out
53 # our IP address and thus do tub.setLocation, and we can't register
54 # any services with the Tub until after that point
55 self.tub.setServiceParent(self)
57 AUTHKEYSFILEBASE = "authorized_keys."
58 for f in os.listdir(self.basedir):
59 if f.startswith(AUTHKEYSFILEBASE):
60 keyfile = os.path.join(self.basedir, f)
61 portnum = int(f[len(AUTHKEYSFILEBASE):])
62 from allmydata import manhole
63 m = manhole.AuthorizedKeysManhole(portnum, keyfile)
64 m.setServiceParent(self)
65 self.log("AuthorizedKeysManhole listening on %d" % portnum)
67 self.log("Node constructed. tahoe version: %s, foolscap version: %s, zfec version: %s" % (allmydata.__version__, foolscap.__version__, zfec.__version__,))
69 def startService(self):
70 """Start the node. Returns a Deferred that fires (with self) when it
73 Many callers don't pay attention to the return value from
74 startService, since they aren't going to do anything special when it
75 finishes. If they are (for example unit tests which need to wait for
76 the node to fully start up before it gets shut down), they can wait
77 for the Deferred I return to fire. In particular, you should wait for
78 my startService() Deferred to fire before you call my stopService()
82 # note: this class can only be started and stopped once.
83 service.MultiService.startService(self)
84 d = defer.succeed(None)
85 d.addCallback(lambda res: iputil.get_local_addresses_async())
86 d.addCallback(self._setup_tub)
87 d.addCallback(lambda res: self.tub_ready())
89 self.log("%s running" % self.NODETYPE)
90 self._tub_ready_observerlist.fire(self)
96 """Shut down the node. Returns a Deferred that fires (with None) when
97 it finally stops kicking."""
98 return self.stopService()
101 log.msg(self.short_nodeid + ": " + msg)
103 def _setup_tub(self, local_addresses):
104 # we can't get a dynamically-assigned portnum until our Tub is
105 # running, which means after startService.
106 l = self.tub.getListeners()[0]
107 portnum = l.getPortnum()
108 local_ip_filename = os.path.join(self.basedir, self.LOCAL_IP_FILE)
109 if os.path.exists(local_ip_filename):
110 f = open(local_ip_filename, "r")
113 if local_ip not in local_addresses:
114 local_addresses.append(local_ip)
115 if not os.path.exists(self._portnumfile):
116 # record which port we're listening on, so we can grab the same
118 f = open(self._portnumfile, "w")
119 f.write("%d\n" % portnum)
121 location = ",".join(["%s:%d" % (ip, portnum)
122 for ip in local_addresses])
123 self.log("Tub location set to %s" % location)
124 self.tub.setLocation(location)
128 # called when the Tub is available for registerReference
131 def when_tub_ready(self):
132 return self._tub_ready_observerlist.when_fired()
134 def add_service(self, s):
135 s.setServiceParent(self)