X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/026f5f3a6274709b7863c619c979dd32f0ee686f..6f9f028c43f77c76a0ac6d4f4f7223e5c6e2ba64:/pymindaemon.py diff --git a/pymindaemon.py b/pymindaemon.py index 1868ae2..f3f45fb 100644 --- a/pymindaemon.py +++ b/pymindaemon.py @@ -10,10 +10,12 @@ command-line. import signal import socket -from dispatcher import Dispatcher -from eventloop import EventLoop -class PyminDaemon(EventLoop): +import dispatcher +import eventloop +import serializer + +class PyminDaemon(eventloop.EventLoop): r"""PyminDaemon(bind_addr, routes) -> PyminDaemon instance This class is well suited to run as a single process. It handles @@ -31,7 +33,7 @@ class PyminDaemon(EventLoop): >>> PyminDaemon(('', 9999), dict(test=test_handler)).run() """ - def __init__(self, bind_addr, routes): + def __init__(self, routes=dict(), bind_addr=('', 9999)): r"""Initialize the PyminDaemon object. See PyminDaemon class documentation for more info. @@ -41,13 +43,13 @@ class PyminDaemon(EventLoop): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(bind_addr) # Create EventLoop - EventLoop.__init__(self, sock) + eventloop.EventLoop.__init__(self, sock) # Create Dispatcher - self.dispatcher = Dispatcher(routes) + self.dispatcher = dispatcher.Dispatcher(routes) # Signal handling def quit(signum, frame): print "Shuting down ..." - loop.stop() # tell main event loop to stop + self.stop() # tell main event loop to stop def reload_config(signum, frame): print "Reloading configuration..." # TODO iterate handlers list propagating reload action @@ -58,22 +60,42 @@ class PyminDaemon(EventLoop): def handle(self): r"handle() -> None :: Handle incoming events using the dispatcher." (msg, addr) = self.file.recvfrom(65535) - self.dispatcher.dispatch(msg) - #try: - # d.dispatch(msg) - #except dis.BadRouteError, inst: - # sock.sendto('Bad route from : ' + inst.cmd + '\n', addr) - #except dis.CommandNotFoundError, inst: - # sock.sendto('Command not found : ' + inst.cmd + '\n', addr) + try: + result = self.dispatcher.dispatch(msg) + if result is not None: + result = serializer.serialize(result) + response = u'OK ' + except dispatcher.Error, e: + result = unicode(e) + u'\n' + response = u'ERROR ' + except Exception, e: + import traceback + result = u'Internal server error\n' + traceback.print_exc() # TODO logging! + response = u'ERROR ' + if result is None: + response += u'0' + else: + response += u'%d\n%s' % (len(result), result) + self.file.sendto(response, addr) def run(self): r"run() -> None :: Run the event loop (shortcut to loop())" - return self.loop() + try: + return self.loop() + except eventloop.LoopInterruptedError, e: + pass if __name__ == '__main__': + @handler(u"Print all the arguments, return nothing.") def test_handler(*args): print 'test:', args - PyminDaemon(('', 9999), dict(test=test_handler)).run() + @handler(u"Echo the message passed as argument.") + def echo_handler(message): + print 'echo:', message + return message + + PyminDaemon(dict(test=test_handler, echo=echo_handler)).run()