mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-10 11:00:22 +00:00
Added support for module-leve setup and start tags.
This commit is contained in:
parent
1ce3bb1a96
commit
5979987c45
@ -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):
|
||||
|
||||
@ -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.
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user