From 5979987c456e0fd95963e3f07354e5247cebcae5 Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Tue, 29 May 2012 17:45:57 -0500 Subject: [PATCH] Added support for module-leve setup and start tags. --- .../santiago/protocols/https/controller.py | 24 ++++++--- ugly_hacks/santiago/santiago.py | 50 +++++++++++++++++-- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/ugly_hacks/santiago/protocols/https/controller.py b/ugly_hacks/santiago/protocols/https/controller.py index 350e621c3..b9f46498a 100644 --- a/ugly_hacks/santiago/protocols/https/controller.py +++ b/ugly_hacks/santiago/protocols/https/controller.py @@ -12,9 +12,25 @@ import logging def setup(santiago): + """Module-level setup function. + + Called after listener and senders are set up, before they're started. + # TODO call this bugger to prep the dispatcher, objects, etc. + + """ pass +def start(): + """Module-level start function, called after listener and sender started. + + TODO: integrate multiple servers: + + http://docs.cherrypy.org/dev/refman/process/servers.html + + """ + cherrypy.engine.start() + class Listener(SantiagoListener): def __init__(self, santiago, socket_port=0, @@ -25,15 +41,11 @@ class Listener(SantiagoListener): cherrypy.server.socket_port = socket_port cherrypy.server.ssl_certificate = ssl_certificate cherrypy.server.ssl_private_key = ssl_private_key - cherrypy.Application(self, "/") + cherrypy.tree.mount(cherrypy.Application(self, "/")) def start(self): """Starts the listener.""" - - # TODO: integrate multiple servers: - # http://docs.cherrypy.org/dev/refman/process/servers.html - # cherrypy.engine.start() - cherrypy.quickstart(self) + pass @cherrypy.expose def index(self, **kwargs): diff --git a/ugly_hacks/santiago/santiago.py b/ugly_hacks/santiago/santiago.py index 4b022a6c4..95bc4a59d 100644 --- a/ugly_hacks/santiago/santiago.py +++ b/ugly_hacks/santiago/santiago.py @@ -61,6 +61,7 @@ import logging import re import shelve import sys +import time import pgpprocessor import utilities @@ -90,6 +91,7 @@ class Santiago(object): "request_version", "reply_versions")) OPTIONAL_KEYS = ALL_KEYS ^ REQUIRED_KEYS LIST_KEYS = set(("reply_to", "locations", "reply_versions")) + CONTROLLER_MODULE = "protocols.{0}.controller" def __init__(self, listeners = None, senders = None, hosting = None, consuming = None, me = 0): @@ -116,6 +118,7 @@ class Santiago(object): hosts are unreachable from some points. """ + self.live = 1 self.requests = DefaultDict(set) self.me = me self.gpg = gnupg.GPG(use_agent = True) @@ -161,7 +164,7 @@ class Santiago(object): FIXME: Assumes the current directory is in sys.path """ - import_name = "protocols.{0}.controller".format(protocol) + import_name = cls.CONTROLLER_MODULE.format(protocol) if not import_name in sys.modules: __import__(import_name) @@ -174,15 +177,44 @@ class Santiago(object): When this has finished, the Santiago will be ready to go. """ + debug_log("Setting up protocols.") + + find_protocol = (lambda protocol: sys.modules[ + self.__class__.CONTROLLER_MODULE.format(protocol)]) + + [find_protocol(protocol).setup(None) for protocol in self.protocols] + + debug_log("Starting connectors.") + for connector in (list(self.listeners.itervalues()) + list(self.senders.itervalues())): connector.start() + # debug_log("Starting monitors.") + + # for monitor in list(self.monitors.itervalues()): + # monitor.start() + + debug_log("Starting protocols.") + + [find_protocol(protocol).start() for protocol in self.protocols] + debug_log("Santiago started!") + count = 0 + try: + while self.live: + time.sleep(5) + except KeyboardInterrupt: + pass + def __exit__(self, exc_type, exc_value, traceback): """Clean up and save all data to shut down the service.""" + for connector in (list(self.listeners.itervalues()) + + list(self.senders.itervalues())): + connector.stop() + santiago.save_data("hosting") santiago.save_data("consuming") debug_log([key for key in santiago.shelf]) @@ -590,17 +622,29 @@ class SantiagoConnector(object): "controllers" in the MVC paradigm. """ - def __init__(self, santiago): + def __init__(self, santiago=None, *args, **kwargs): + super(SantiagoConnector, self).__init__(*args, **kwargs) self.santiago = santiago + def setup(self): + """Initialize the connector. + + """ + pass + def start(self): - """Called when initialization is complete. + """Starts the connector, called when initialization is complete. Cannot block. """ pass + def stop(self): + """Shuts down the connector.""" + + pass + class SantiagoListener(SantiagoConnector): """Generic Santiago Listener superclass.