X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/026f5f3a6274709b7863c619c979dd32f0ee686f..2269d334bc9d3ae1c0de8229875b068e4cb29ac9:/pymindaemon.py?ds=inline diff --git a/pymindaemon.py b/pymindaemon.py index 1868ae2..b8369b9 100644 --- a/pymindaemon.py +++ b/pymindaemon.py @@ -11,7 +11,7 @@ command-line. import signal import socket from dispatcher import Dispatcher -from eventloop import EventLoop +from eventloop import EventLoop, LoopInterruptedError class PyminDaemon(EventLoop): r"""PyminDaemon(bind_addr, routes) -> PyminDaemon instance @@ -47,7 +47,7 @@ class PyminDaemon(EventLoop): # 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,20 +58,28 @@ 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) + response = 'OK ' + except Exception, e: + result = str(e) + response = 'ERROR ' + if result is None: + response += '0' + else: + response += '%d\n%s' % (len(str(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 LoopInterruptedError, e: + pass if __name__ == '__main__': + @handler def test_handler(*args): print 'test:', args