Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Add RereadConfiguration event. Signal SIGHUP fires it. (v2) #106

Merged
merged 1 commit into from
Mar 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pox/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 36 additions & 4 deletions pox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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...")

Expand All @@ -337,6 +366,8 @@ def goUp (self):
self.starting_up = False
self.raiseEvent(GoingUpEvent())

self._add_signal_handlers()

self.raiseEvent(UpEvent())

self._waiter_notify()
Expand Down Expand Up @@ -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.
Expand Down