From a39e01929cd4732488311facd245c282621a9d5f Mon Sep 17 00:00:00 2001 From: Felician Nemeth Date: Thu, 28 Nov 2013 09:39:17 -0800 Subject: [PATCH] core: Add RereadConfiguration event. Signal SIGHUP fires it. --- pox/boot.py | 4 +++- pox/core.py | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/pox/boot.py b/pox/boot.py index f411c741e..94d43a279 100755 --- a/pox/boot.py +++ b/pox/boot.py @@ -178,7 +178,8 @@ def _do_launch (argv): core = pox.core.core core.getLogger('boot').debug('Using existing POX core') else: - core = pox.core.initialize(_options.threaded_selecthub) + core = pox.core.initialize(_options.threaded_selecthub, + _options.handle_signals) _pre_startup() modules = _do_imports(n.split(':')[0] for n in component_order) @@ -378,6 +379,7 @@ def __init__ (self): self.enable_openflow = True self.log_config = None self.threaded_selecthub = True + self.handle_signals = True def _set_h (self, given_name, name, value): self._set_help(given_name, name, value) diff --git a/pox/core.py b/pox/core.py index 5fd052773..7579b74a0 100644 --- a/pox/core.py +++ b/pox/core.py @@ -28,6 +28,7 @@ class (an instance of which is available as pox.core.core). import inspect import time import os +import signal _path = inspect.stack()[0][1] _ext_path = _path[0:_path.rindex(os.sep)] @@ -152,6 +153,10 @@ def __init__ (self, name, component): self.name = name self.component = component +class RereadConfiguration (Event): + """ Fired when modules should reread their configuration files. """ + pass + import pox.lib.recoco as recoco class POXCore (EventMixin): @@ -179,19 +184,21 @@ class POXCore (EventMixin): DownEvent, GoingUpEvent, GoingDownEvent, - ComponentRegistered + ComponentRegistered, + RereadConfiguration, ]) version = (0,3,0) version_name = "dart" - def __init__ (self, threaded_selecthub=True): + def __init__ (self, threaded_selecthub=True, handle_signals=True): self.debug = False self.running = True self.starting_up = True self.components = {'core':self} self._openflow_wanted = False + self._handle_signals = handle_signals import threading self.quit_condition = threading.Condition() @@ -317,6 +324,28 @@ def _get_platform_info (self): except: return "Unknown Platform" + def _add_signal_handlers (self): + if not self._handle_signals: + return + + import threading + # Note, python 3.4 will have threading.main_thread() + # http://bugs.python.org/issue18882 + if not isinstance(threading.current_thread(), threading._MainThread): + raise RuntimeError("add_signal_handers must be called from MainThread") + + try: + previous = signal.getsignal(signal.SIGHUP) + signal.signal(signal.SIGHUP, self._signal_handler_SIGHUP) + if previous != signal.SIG_DFL: + log.warn('Redefined signal handler for SIGHUP') + except (AttributeError, ValueError): + # SIGHUP is not supported on some systems (e.g., Windows) + log.debug("Didn't install handler for SIGHUP") + + def _signal_handler_SIGHUP (self, signal, frame): + self.raiseLater(core, RereadConfiguration) + def goUp (self): log.debug(self.version_string + " going up...") @@ -337,6 +366,8 @@ def goUp (self): self.starting_up = False self.raiseEvent(GoingUpEvent()) + self._add_signal_handlers() + self.raiseEvent(UpEvent()) self._waiter_notify() @@ -564,9 +595,10 @@ def __getattr__ (self, name): core = None -def initialize (threaded_selecthub=True): +def initialize (threaded_selecthub=True, handle_signals=True): global core - core = POXCore(threaded_selecthub=threaded_selecthub) + core = POXCore(threaded_selecthub=threaded_selecthub, + handle_signals=handle_signals) return core # The below is a big hack to make tests and doc tools work.