base/app_manager: teach event dispatcher state

When independent methods for different dispatchers defined, all methods
are called independent of state. This patch teach event dispatcher state.

The following class

class PacketTesting(app_manager.RyuApp):
@set_ev_cls(ofp_event.EventOFPEchoRequest, MAIN_DISPATCHER)
def _echo_rep_main(self, ev):
    print "DEBUG: In Handler for Echo Request (Main)"

@set_ev_cls(ofp_event.EventOFPEchoRequest, CONFIG_DISPATCHER)
def _echo_rep_conf(self, ev):
    print "DEBUG: In Handler for Echo Request (Config)"

@set_ev_cls(ofp_event.EventOFPEchoRequest, HANDSHAKE_DISPATCHER)
def _echo_rep_hand(self, ev):
    print "DEBUG: In Handler for Echo Request (Handshake)"

output
> EVENT ofp_event->PacketTesting EventOFPEchoRequest
> DEBUG: In Handler for Echo Request (Config)
> DEBUG: In Handler for Echo Request (Dead)
> DEBUG: In Handler for Echo Request (Handshake)
> DEBUG: In Handler for Echo Request (Main)

Reported-by: Alan Barr <alanbarr2002@gmail.com>
Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Isaku Yamahata 2013-07-04 22:04:38 +09:00 committed by FUJITA Tomonori
parent b2636e0cc1
commit 1ed59505a8

View File

@ -57,8 +57,8 @@ class RyuApp(object):
def __init__(self, *_args, **_kwargs):
super(RyuApp, self).__init__()
self.name = self.__class__.__name__
self.event_handlers = {}
self.observers = {}
self.event_handlers = {} # ev_cls -> handlers:list
self.observers = {} # ev_cls -> observer-name -> states:set
self.threads = []
self.events = hub.Queue(128)
self.replies = hub.Queue()
@ -71,11 +71,17 @@ class RyuApp(object):
self.event_handlers[ev_cls].append(handler)
def register_observer(self, ev_cls, name, states=None):
states = states or []
self.observers.setdefault(ev_cls, {})[name] = states
states = states or set()
ev_cls_observers = self.observers.setdefault(ev_cls, {})
ev_cls_observers.setdefault(name, set()).update(states)
def get_handlers(self, ev):
return self.event_handlers.get(ev.__class__, [])
def get_handlers(self, ev, state=None):
handlers = self.event_handlers.get(ev.__class__, [])
if state is None:
return handlers
return [handler for handler in handlers
if not handler.dispatchers or state in handler.dispatchers]
def get_observers(self, ev, state):
observers = []
@ -98,28 +104,28 @@ class RyuApp(object):
def _event_loop(self):
while True:
ev = self.events.get()
handlers = self.get_handlers(ev)
ev, state = self.events.get()
handlers = self.get_handlers(ev, state)
for handler in handlers:
handler(ev)
def _send_event(self, ev):
self.events.put(ev)
def _send_event(self, ev, state):
self.events.put((ev, state))
def send_event(self, name, ev):
def send_event(self, name, ev, state=None):
if name in SERVICE_BRICKS:
if isinstance(ev, EventRequestBase):
ev.src = self.name
LOG.debug("EVENT %s->%s %s" %
(self.name, name, ev.__class__.__name__))
SERVICE_BRICKS[name]._send_event(ev)
SERVICE_BRICKS[name]._send_event(ev, state)
else:
LOG.debug("EVENT LOST %s->%s %s" %
(self.name, name, ev.__class__.__name__))
def send_event_to_observers(self, ev, state=None):
for observer in self.get_observers(ev, state):
self.send_event(observer, ev)
self.send_event(observer, ev, state)
def reply_to_request(self, req, rep):
rep.dst = req.src