2006-11-30 22:39:24 +00:00
|
|
|
|
2006-12-03 02:37:31 +00:00
|
|
|
import os.path
|
2006-12-03 01:27:18 +00:00
|
|
|
from foolscap import Referenceable
|
2006-12-01 01:09:57 +00:00
|
|
|
from foolscap.eventual import eventually
|
2006-11-30 22:39:24 +00:00
|
|
|
from twisted.application import service
|
|
|
|
from twisted.python import log
|
2006-12-02 23:26:26 +00:00
|
|
|
from allmydata.util import idlib
|
2006-12-02 02:17:50 +00:00
|
|
|
from zope.interface import implements
|
|
|
|
from allmydata.interfaces import RIQueenRoster
|
2006-12-03 01:27:18 +00:00
|
|
|
from allmydata import node
|
2006-12-04 02:07:41 +00:00
|
|
|
from allmydata.filetable import GlobalVirtualDrive
|
2006-11-30 22:39:24 +00:00
|
|
|
|
|
|
|
class Roster(service.MultiService, Referenceable):
|
2006-12-02 02:17:50 +00:00
|
|
|
implements(RIQueenRoster)
|
|
|
|
|
2006-12-01 00:43:15 +00:00
|
|
|
def __init__(self):
|
|
|
|
service.MultiService.__init__(self)
|
2006-12-01 01:09:57 +00:00
|
|
|
self.phonebook = {}
|
|
|
|
self.connections = {}
|
2006-12-04 02:07:41 +00:00
|
|
|
self.gvd_root = None
|
|
|
|
|
|
|
|
def set_gvd_root(self, root):
|
|
|
|
self.gvd_root = root
|
2006-12-01 00:43:15 +00:00
|
|
|
|
2006-12-01 01:09:57 +00:00
|
|
|
def remote_hello(self, nodeid, node, pburl):
|
2006-12-03 03:29:09 +00:00
|
|
|
log.msg("roster: contact from %s" % idlib.b2a(nodeid))
|
|
|
|
eventually(self._educate_the_new_peer,
|
2006-12-03 07:53:53 +00:00
|
|
|
nodeid, node, list(self.phonebook.items()))
|
2006-12-03 03:29:09 +00:00
|
|
|
eventually(self._announce_new_peer,
|
|
|
|
nodeid, pburl, list(self.connections.values()))
|
2006-12-01 01:09:57 +00:00
|
|
|
self.phonebook[nodeid] = pburl
|
|
|
|
self.connections[nodeid] = node
|
2006-12-01 00:43:15 +00:00
|
|
|
node.notifyOnDisconnect(self._lost_node, nodeid)
|
2006-12-04 02:07:41 +00:00
|
|
|
return self.gvd_root
|
2006-12-01 00:43:15 +00:00
|
|
|
|
2006-12-03 07:53:53 +00:00
|
|
|
def _educate_the_new_peer(self, nodeid, node, new_peers):
|
|
|
|
log.msg("roster: educating %s (%d)" % (idlib.b2a(nodeid)[:4], len(new_peers)))
|
2006-12-03 03:29:09 +00:00
|
|
|
node.callRemote("add_peers", new_peers=new_peers)
|
2006-12-01 01:09:57 +00:00
|
|
|
|
2006-12-03 03:29:09 +00:00
|
|
|
def _announce_new_peer(self, new_nodeid, new_node_pburl, peers):
|
2006-12-03 07:53:53 +00:00
|
|
|
log.msg("roster: announcing %s to everybody (%d)" % (idlib.b2a(new_nodeid)[:4], len(peers)))
|
2006-12-03 03:29:09 +00:00
|
|
|
for targetnode in peers:
|
2006-12-01 01:09:57 +00:00
|
|
|
targetnode.callRemote("add_peers",
|
|
|
|
new_peers=[(new_nodeid, new_node_pburl)])
|
|
|
|
|
2006-12-01 00:43:15 +00:00
|
|
|
def _lost_node(self, nodeid):
|
2006-12-03 03:27:50 +00:00
|
|
|
log.msg("roster: lost contact with %s" % idlib.b2a(nodeid))
|
2006-12-01 01:09:57 +00:00
|
|
|
del self.phonebook[nodeid]
|
|
|
|
del self.connections[nodeid]
|
|
|
|
eventually(self._announce_lost_peer, nodeid)
|
|
|
|
|
|
|
|
def _announce_lost_peer(self, lost_nodeid):
|
|
|
|
for targetnode in self.connections.values():
|
|
|
|
targetnode.callRemote("lost_peers", lost_peers=[lost_nodeid])
|
|
|
|
|
2006-12-01 00:43:15 +00:00
|
|
|
|
2006-11-30 22:39:24 +00:00
|
|
|
|
2006-12-03 01:27:18 +00:00
|
|
|
class Queen(node.Node):
|
2006-11-30 22:39:24 +00:00
|
|
|
CERTFILE = "queen.pem"
|
2006-11-30 23:39:38 +00:00
|
|
|
PORTNUMFILE = "queen.port"
|
2006-12-03 01:27:18 +00:00
|
|
|
NODETYPE = "queen"
|
2006-11-30 22:39:24 +00:00
|
|
|
|
2006-12-03 01:27:18 +00:00
|
|
|
def __init__(self, basedir="."):
|
|
|
|
node.Node.__init__(self, basedir)
|
2006-12-04 02:07:41 +00:00
|
|
|
self.gvd = self.add_service(GlobalVirtualDrive(basedir))
|
2006-11-30 22:39:24 +00:00
|
|
|
self.urls = {}
|
2006-12-01 02:38:38 +00:00
|
|
|
|
2006-12-03 02:37:31 +00:00
|
|
|
def tub_ready(self):
|
2006-12-03 01:27:18 +00:00
|
|
|
r = self.add_service(Roster())
|
2006-11-30 23:39:38 +00:00
|
|
|
self.urls["roster"] = self.tub.registerReference(r, "roster")
|
2006-12-03 03:27:50 +00:00
|
|
|
self.log(" roster is at %s" % self.urls["roster"])
|
2006-12-03 02:37:31 +00:00
|
|
|
f = open(os.path.join(self.basedir, "roster_pburl"), "w")
|
|
|
|
f.write(self.urls["roster"] + "\n")
|
|
|
|
f.close()
|
2006-12-04 02:07:41 +00:00
|
|
|
r.set_gvd_root(self.gvd.get_root())
|
2006-11-30 22:39:24 +00:00
|
|
|
|