2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
from zope.interface import implements
|
2007-03-23 23:15:57 +00:00
|
|
|
from twisted.application import service
|
|
|
|
from twisted.python import log
|
2007-03-27 23:12:11 +00:00
|
|
|
from foolscap import Referenceable
|
|
|
|
from allmydata.interfaces import RIIntroducer, RIIntroducerClient
|
|
|
|
from allmydata.util import idlib, observer
|
2007-03-23 23:15:57 +00:00
|
|
|
|
|
|
|
class Introducer(service.MultiService, Referenceable):
|
|
|
|
implements(RIIntroducer)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
service.MultiService.__init__(self)
|
|
|
|
self.nodes = set()
|
2007-05-22 21:08:30 +00:00
|
|
|
self.furls = set()
|
2007-03-23 23:15:57 +00:00
|
|
|
|
2007-05-22 21:08:30 +00:00
|
|
|
def remote_hello(self, node, furl):
|
|
|
|
log.msg("introducer: new contact at %s, node is %s" % (furl, node))
|
2007-03-23 23:15:57 +00:00
|
|
|
def _remove():
|
2007-05-22 21:08:30 +00:00
|
|
|
log.msg(" introducer: removing %s %s" % (node, furl))
|
2007-03-23 23:15:57 +00:00
|
|
|
self.nodes.remove(node)
|
2007-05-22 21:08:30 +00:00
|
|
|
self.furls.remove(furl)
|
2007-03-23 23:15:57 +00:00
|
|
|
node.notifyOnDisconnect(_remove)
|
2007-05-22 21:08:30 +00:00
|
|
|
self.furls.add(furl)
|
|
|
|
node.callRemote("new_peers", self.furls)
|
2007-03-23 23:15:57 +00:00
|
|
|
for othernode in self.nodes:
|
2007-05-22 21:08:30 +00:00
|
|
|
othernode.callRemote("new_peers", set([furl]))
|
2007-03-23 23:15:57 +00:00
|
|
|
self.nodes.add(node)
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IntroducerClient(service.Service, Referenceable):
|
|
|
|
implements(RIIntroducerClient)
|
|
|
|
|
2007-05-22 21:08:30 +00:00
|
|
|
def __init__(self, tub, introducer_furl, my_furl):
|
2007-03-27 23:12:11 +00:00
|
|
|
self.tub = tub
|
2007-05-22 21:08:30 +00:00
|
|
|
self.introducer_furl = introducer_furl
|
|
|
|
self.my_furl = my_furl
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
self.connections = {} # k: nodeid, v: ref
|
2007-05-22 21:08:30 +00:00
|
|
|
self.reconnectors = {} # k: FURL, v: reconnector
|
2007-06-10 04:03:57 +00:00
|
|
|
self._connected = False
|
2007-03-27 23:12:11 +00:00
|
|
|
|
2007-05-08 02:10:24 +00:00
|
|
|
self.connection_observers = observer.ObserverList()
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
def startService(self):
|
2007-06-05 01:45:40 +00:00
|
|
|
service.Service.startService(self)
|
2007-05-22 21:08:30 +00:00
|
|
|
self.introducer_reconnector = self.tub.connectTo(self.introducer_furl,
|
2007-03-27 23:12:11 +00:00
|
|
|
self._got_introducer)
|
2007-06-05 01:48:53 +00:00
|
|
|
def connect_failed(failure):
|
|
|
|
self.log("\n\nInitial Introducer connection failed: perhaps it's down\n")
|
|
|
|
log.err(failure)
|
|
|
|
d = self.tub.getReference(self.introducer_furl)
|
|
|
|
d.addErrback(connect_failed)
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
self.parent.log(msg)
|
|
|
|
|
2007-05-22 21:08:30 +00:00
|
|
|
def remote_new_peers(self, furls):
|
|
|
|
for furl in furls:
|
|
|
|
self._new_peer(furl)
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
def stopService(self):
|
|
|
|
service.Service.stopService(self)
|
|
|
|
self.introducer_reconnector.stopConnecting()
|
|
|
|
for reconnector in self.reconnectors.itervalues():
|
|
|
|
reconnector.stopConnecting()
|
|
|
|
|
2007-05-22 21:08:30 +00:00
|
|
|
def _new_peer(self, furl):
|
|
|
|
if furl in self.reconnectors:
|
2007-03-27 23:12:11 +00:00
|
|
|
return
|
2007-03-29 18:16:29 +00:00
|
|
|
# TODO: rather than using the TubID as a nodeid, we should use
|
|
|
|
# something else. The thing that requires the least additional
|
|
|
|
# mappings is to use the foolscap "identifier" (the last component of
|
2007-05-22 21:08:30 +00:00
|
|
|
# the furl), since these are unguessable. Before we can do that,
|
2007-03-29 18:16:29 +00:00
|
|
|
# though, we need a way to conveniently make these identifiers
|
|
|
|
# persist from one run of the client program to the next. Also, using
|
|
|
|
# the foolscap identifier would mean that anyone who knows the name
|
|
|
|
# of the node also has all the secrets they need to contact and use
|
|
|
|
# them, which may or may not be what we want.
|
2007-05-22 21:08:30 +00:00
|
|
|
m = re.match(r'pb://(\w+)@', furl)
|
2007-03-27 23:12:11 +00:00
|
|
|
assert m
|
|
|
|
nodeid = idlib.a2b(m.group(1))
|
|
|
|
def _got_peer(rref):
|
|
|
|
self.log(" connected to(%s)" % idlib.b2a(nodeid))
|
2007-05-08 02:10:24 +00:00
|
|
|
self.connection_observers.notify(nodeid, rref)
|
2007-03-27 23:12:11 +00:00
|
|
|
self.connections[nodeid] = rref
|
|
|
|
def _lost():
|
|
|
|
# TODO: notifyOnDisconnect uses eventually(), but connects do
|
|
|
|
# not. Could this cause a problem?
|
|
|
|
del self.connections[nodeid]
|
|
|
|
rref.notifyOnDisconnect(_lost)
|
2007-05-22 21:08:30 +00:00
|
|
|
self.log(" connecting to(%s)" % furl)
|
|
|
|
self.reconnectors[furl] = self.tub.connectTo(furl, _got_peer)
|
2007-03-27 23:12:11 +00:00
|
|
|
|
|
|
|
def _got_introducer(self, introducer):
|
2007-05-22 21:08:30 +00:00
|
|
|
self.log(" introducing ourselves: %s, %s" % (self, self.my_furl))
|
2007-06-10 04:03:57 +00:00
|
|
|
self._connected = True
|
2007-03-27 23:12:11 +00:00
|
|
|
d = introducer.callRemote("hello",
|
|
|
|
node=self,
|
2007-05-22 21:08:30 +00:00
|
|
|
furl=self.my_furl)
|
2007-06-10 04:03:57 +00:00
|
|
|
introducer.notifyOnDisconnect(self._disconnected)
|
|
|
|
|
|
|
|
def _disconnected(self):
|
|
|
|
self._connected = False
|
2007-03-27 23:12:11 +00:00
|
|
|
|
2007-05-08 02:10:24 +00:00
|
|
|
def notify_on_new_connection(self, cb):
|
|
|
|
"""Register a callback that will be fired (with nodeid, rref) when
|
|
|
|
a new connection is established."""
|
|
|
|
self.connection_observers.subscribe(cb)
|
2007-03-27 23:12:11 +00:00
|
|
|
|
2007-06-10 04:03:57 +00:00
|
|
|
def connected_to_introducer(self):
|
|
|
|
return self._connected
|