Add request/reply event support to support communication between apps

What most of applications need to do is:

1) register a handler to catch a specific event
2) The handler does multiple asynchronous tasks (query dbs, configures
switches, etc).

2) can be implemented in an asynchronous way, that is sending multiple
requests at the same time. However, I don't think most of developers
need to do or want to do. The API to handle multiple asynchrnous tasks
in a synchronous way is more handy.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2013-03-05 07:34:27 +09:00
parent 99755c2123
commit d8625d4f8f
2 changed files with 22 additions and 0 deletions

View File

@ -62,6 +62,7 @@ class RyuApp(object):
self.observers = {}
self.threads = []
self.events = Queue()
self.replies = Queue()
self.threads.append(gevent.spawn(self._event_loop))
def register_handler(self, ev_cls, handler):
@ -84,6 +85,15 @@ class RyuApp(object):
return observers
def send_reply(self, rep):
SERVICE_BRICKS[rep.dst].replies.put(rep)
def send_request(self, req):
req.src = self.name
self.send_event(req.dst, req)
# going to sleep for the reply
return self.replies.get()
def _event_loop(self):
while True:
ev = self.events.get()

View File

@ -18,3 +18,15 @@
class EventBase(object):
# Nothing yet
pass
class EventRequestBase(EventBase):
def __init__(self, dst):
super(EventRequestBase, self).__init__()
self.dst = dst
class EventReplyBase(EventBase):
def __init__(self, dst):
super(EventReplyBase, self).__init__()
self.dst = dst