diff options
author | YAMAMOTO Takashi <yamamoto@valinux.co.jp> | 2014-03-26 13:04:18 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-04-10 07:09:56 +0900 |
commit | a4a77546d85f2f6218f55a5382d8005c88801f98 (patch) | |
tree | 0f60a54ccd608b59948e16b6a6e404be46785efc | |
parent | 5b34514c994fcb10964db350609999498f1ea90b (diff) |
RyuApp: add methods to observe/unobserve specific event dynamically
Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/base/app_manager.py | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/ryu/base/app_manager.py b/ryu/base/app_manager.py index 7d00e2e7..bdba1ec6 100644 --- a/ryu/base/app_manager.py +++ b/ryu/base/app_manager.py @@ -37,6 +37,14 @@ def lookup_service_brick(name): return SERVICE_BRICKS.get(name) +def _lookup_service_brick_by_ev_cls(ev_cls): + return _lookup_service_brick_by_mod_name(ev_cls.__module__) + + +def _lookup_service_brick_by_mod_name(mod_name): + return lookup_service_brick(mod_name.split('.')[-1]) + + def register_app(app): assert isinstance(app, RyuApp) assert not app.name in SERVICE_BRICKS @@ -175,6 +183,16 @@ class RyuApp(object): for observers in self.observers.values(): observers.pop(name, None) + def observe_event(self, ev_cls, states=None): + brick = _lookup_service_brick_by_ev_cls(ev_cls) + if brick is not None: + brick.register_observer(ev_cls, self.name, states) + + def unobserve_event(self, ev_cls): + brick = _lookup_service_brick_by_ev_cls(ev_cls) + if brick is not None: + brick.unregister_observer(ev_cls, self.name) + def get_handlers(self, ev, state=None): """Returns a list of handlers for the specific event. @@ -361,12 +379,11 @@ class AppManager(object): for ev_cls, c in m.callers.iteritems(): if not c.ev_source: continue - # name is module name of ev_cls - name = c.ev_source.split('.')[-1] - if name in SERVICE_BRICKS: - brick = SERVICE_BRICKS[name] - brick.register_observer( - ev_cls, i.name, c.dispatchers) + + brick = _lookup_service_brick_by_mod_name(c.ev_source) + if brick: + brick.register_observer(ev_cls, i.name, + c.dispatchers) # allow RyuApp and Event class are in different module for brick in SERVICE_BRICKS.itervalues(): |