From d8625d4f8fc59a41650c0088a160fb490ef389b1 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Tue, 5 Mar 2013 07:34:27 +0900 Subject: [PATCH] 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 --- ryu/base/app_manager.py | 10 ++++++++++ ryu/controller/event.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ryu/base/app_manager.py b/ryu/base/app_manager.py index d977b409..f20b87cc 100644 --- a/ryu/base/app_manager.py +++ b/ryu/base/app_manager.py @@ -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() diff --git a/ryu/controller/event.py b/ryu/controller/event.py index 59740f51..4d583d9d 100644 --- a/ryu/controller/event.py +++ b/ryu/controller/event.py @@ -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