]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/pymindaemon.py
Add a global ProcessManager instance and functions to procman module.
[software/pymin.git] / pymin / pymindaemon.py
index ee5f80544837d41a85b657ed252a4e4438f49656..ccb943dd48d4ebcd030c5a12aa94b9f3ebb853a1 100644 (file)
@@ -37,35 +37,43 @@ class PyminDaemon(eventloop.EventLoop):
     >>> PyminDaemon(Root(), ('', 9999)).run()
     """
 
     >>> PyminDaemon(Root(), ('', 9999)).run()
     """
 
-    def __init__(self, root, bind_addr=('', 9999)):
+    def __init__(self, root, bind_addr=('', 9999), timer=1):
         r"""Initialize the PyminDaemon object.
 
         See PyminDaemon class documentation for more info.
         """
         r"""Initialize the PyminDaemon object.
 
         See PyminDaemon class documentation for more info.
         """
+        # Timer timeout time
+        self.timer = timer
         # Create and bind socket
         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         sock.bind(bind_addr)
         # Create and bind socket
         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         sock.bind(bind_addr)
-        # Create EventLoop
-        eventloop.EventLoop.__init__(self, sock)
-        # Create Dispatcher
-        self.dispatcher = dispatcher.Dispatcher(root)
         # Signal handling
         # Signal handling
-        def quit(signum, frame):
+        def quit(loop, signum):
             print "Shuting down ..."
             print "Shuting down ..."
-            self.stop() # tell main event loop to stop
-        def reload_config(signum, frame):
+            loop.stop() # tell main event loop to stop
+        def reload_config(loop, signum):
             print "Reloading configuration..."
             # TODO iterate handlers list propagating reload action
             print "Reloading configuration..."
             # TODO iterate handlers list propagating reload action
-        signal.signal(signal.SIGINT, quit)
-        signal.signal(signal.SIGTERM, quit)
-        signal.signal(signal.SIGUSR1, reload_config)
+        def timer(loop, signum):
+            loop.handle_timer()
+            signal.alarm(loop.timer)
+        # Create EventLoop
+        eventloop.EventLoop.__init__(self, sock, signals={
+                signal.SIGINT: quit,
+                signal.SIGTERM: quit,
+                signal.SIGUSR1: reload_config,
+                signal.SIGALRM: timer,
+            })
+        # Create Dispatcher
+        #TODO root.pymin = PyminHandler()
+        self.dispatcher = dispatcher.Dispatcher(root)
 
     def handle(self):
         r"handle() -> None :: Handle incoming events using the dispatcher."
         (msg, addr) = self.file.recvfrom(65535)
         try:
 
     def handle(self):
         r"handle() -> None :: Handle incoming events using the dispatcher."
         (msg, addr) = self.file.recvfrom(65535)
         try:
-            result = self.dispatcher.dispatch(msg)
+            result = self.dispatcher.dispatch(unicode(msg, 'utf-8'))
             if result is not None:
                 result = serializer.serialize(result)
             response = u'OK '
             if result is not None:
                 result = serializer.serialize(result)
             response = u'OK '
@@ -81,10 +89,18 @@ class PyminDaemon(eventloop.EventLoop):
             response += u'0\n'
         else:
             response += u'%d\n%s' % (len(result), result)
             response += u'0\n'
         else:
             response += u'%d\n%s' % (len(result), result)
-        self.file.sendto(response, addr)
+        self.file.sendto(response.encode('utf-8'), addr)
+
+    def handle_timer(self):
+        r"handle_timer() -> None :: Call handle_timer() on handlers."
+        self.dispatcher.root.handle_timer()
 
     def run(self):
         r"run() -> None :: Run the event loop (shortcut to loop())"
 
     def run(self):
         r"run() -> None :: Run the event loop (shortcut to loop())"
+        # Start the timer
+        self.handle_timer()
+        signal.alarm(self.timer)
+        # Loop
         try:
             return self.loop()
         except eventloop.LoopInterruptedError, e:
         try:
             return self.loop()
         except eventloop.LoopInterruptedError, e: