Code and docstring style fixes
[ganeti-local] / lib / daemon.py
index 9467bb9..809e538 100644 (file)
 
 import asyncore
 import os
-import select
 import signal
 import errno
 import logging
 import sched
 import time
 import socket
+import sys
 
 from ganeti import utils
 from ganeti import constants
@@ -95,7 +95,7 @@ class AsyncUDPSocket(asyncore.dispatcher):
   def handle_read(self):
     try:
       try:
-        payload, address = self.recvfrom(4096)
+        payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE)
       except socket.error, err:
         if err.errno == errno.EINTR:
           # we got a signal while trying to read. no need to do anything,
@@ -148,31 +148,31 @@ class AsyncUDPSocket(asyncore.dispatcher):
     """Enqueue a datagram to be sent when possible
 
     """
+    if len(payload) > constants.MAX_UDP_DATA_SIZE:
+      raise errors.UdpDataSizeError('Packet too big: %s > %s' % (len(payload),
+                                    constants.MAX_UDP_DATA_SIZE))
     self._out_queue.append((ip, port, payload))
 
 
 class Mainloop(object):
   """Generic mainloop for daemons
 
+  @ivar scheduler: A sched.scheduler object, which can be used to register
+    timed events
+
   """
   def __init__(self):
     """Constructs a new Mainloop instance.
 
-    @ivar scheduler: A L{sched.scheduler} object, which can be used to register
-    timed events
-
     """
     self._signal_wait = []
     self.scheduler = AsyncoreScheduler(time.time)
 
   @utils.SignalHandled([signal.SIGCHLD])
   @utils.SignalHandled([signal.SIGTERM])
-  def Run(self, stop_on_empty=False, signal_handlers=None):
+  def Run(self, signal_handlers=None):
     """Runs the mainloop.
 
-    @type stop_on_empty: bool
-    @param stop_on_empty: Whether to stop mainloop once all I/O waiters
-                          unregistered
     @type signal_handlers: dict
     @param signal_handlers: signal->L{utils.SignalHandler} passed by decorator
 
@@ -183,10 +183,6 @@ class Mainloop(object):
     running = True
     # Start actual main loop
     while running:
-      # Stop if nothing is listening anymore
-      if stop_on_empty and not (self._io_wait):
-        break
-
       if not self.scheduler.empty():
         try:
           self.scheduler.run()
@@ -230,11 +226,9 @@ def GenericMain(daemon_name, optionparser, dirs, check_fn, exec_fn):
 
   @type daemon_name: string
   @param daemon_name: daemon name
-  @type optionparser: L{optparse.OptionParser}
+  @type optionparser: optparse.OptionParser
   @param optionparser: initialized optionparser with daemon-specific options
                        (common -f -d options will be handled by this module)
-  @type options: object @param options: OptionParser result, should contain at
-                 least the fork and the debug options
   @type dirs: list of strings
   @param dirs: list of directories that must exist for this daemon to work
   @type check_fn: function which accepts (options, args)
@@ -307,4 +301,3 @@ def GenericMain(daemon_name, optionparser, dirs, check_fn, exec_fn):
     exec_fn(options, args)
   finally:
     utils.RemovePidFile(daemon_name)
-