manage the pair of a request/reply

Signed-off-by: Satoshi Kobayashi <[email protected]>
---
Changes v1 -> v2:
  - stop using uuid
  - refactoring

 ryu/base/app_manager.py |   17 ++++++++++++++---
 ryu/controller/event.py |    1 +
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/ryu/base/app_manager.py b/ryu/base/app_manager.py
index ebf2196..dddf56f 100644
--- a/ryu/base/app_manager.py
+++ b/ryu/base/app_manager.py
@@ -67,7 +67,7 @@ class RyuApp(object):
         self.observers = {}     # ev_cls -> observer-name -> states:set
         self.threads = []
         self.events = hub.Queue(128)
-        self.replies = hub.Queue()
+        self.replies = {}  # request:EventRequestBase -> reply-queue:hub.Queue
         self.logger = logging.getLogger(self.name)
 
         # prevent accidental creation of instances of this class outside RyuApp
@@ -123,14 +123,24 @@ class RyuApp(object):
 
     def send_reply(self, rep):
         assert isinstance(rep, EventReplyBase)
-        SERVICE_BRICKS[rep.dst].replies.put(rep)
+        # take the application that the reply should be sent
+        rep_dst_app = SERVICE_BRICKS[rep.dst]
+        reply_q = rep_dst_app.replies[rep.request]
+        # send the response
+        reply_q.put(rep)
+        # cleanup
+        del rep_dst_app.replies[rep.request]
 
     def send_request(self, req):
         assert isinstance(req, EventRequestBase)
         req.sync = True
+        # prepare for receiving the reply
+        reply_q = hub.Queue()
+        self.replies[req] = reply_q
+        # send the request
         self.send_event(req.dst, req)
         # going to sleep for the reply
-        return self.replies.get()
+        return reply_q.get()
 
     def _event_loop(self):
         while self.is_active or not self.events.empty():
@@ -161,6 +171,7 @@ class RyuApp(object):
 
     def reply_to_request(self, req, rep):
         rep.dst = req.src
+        rep.request = req
         if req.sync:
             self.send_reply(rep)
         else:
diff --git a/ryu/controller/event.py b/ryu/controller/event.py
index 8191710..a2e24e2 100644
--- a/ryu/controller/event.py
+++ b/ryu/controller/event.py
@@ -31,4 +31,5 @@ class EventRequestBase(EventBase):
 class EventReplyBase(EventBase):
     def __init__(self, dst):
         super(EventReplyBase, self).__init__()
+        self.request = None
         self.dst = dst
-- 
1.7.1


------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today. 
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to