From f57f914f82d213e3f4e2e1bd5b5c8daab8e99c0b Mon Sep 17 00:00:00 2001
From: Stefano Borini <sborini@enthought.com>
Date: Wed, 9 Aug 2017 13:08:56 +0100
Subject: [PATCH] Made small protocol

---
 .../dummy/ui_notification/ui_notification.py  | 21 ++++++++++++++++---
 utils/zmq_client.py                           | 14 +++++--------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py b/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py
index eb419db..bbf7218 100644
--- a/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py
+++ b/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py
@@ -1,3 +1,5 @@
+import errno
+import logging
 from traits.api import Any, List
 
 from force_bdss.api import BaseNotificationListener
@@ -5,17 +7,30 @@ import zmq
 
 
 class UINotification(BaseNotificationListener):
+    #: The ZMQ context.
     _context = Any()
+
+    #: The pubsub socket.
     _pub_socket = Any()
+
+    #: The synchronization socket to recover already sent information at a
+    #: later stage
     _rep_socket = Any()
+
+    #: The cache of messages as they are sent out.
     _msg_cache = List()
 
     def deliver(self, model, message):
         try:
             data = self._rep_socket.recv(flags=zmq.NOBLOCK)
-        except zmq.ZMQError:
-            pass
-        else:
+        except zmq.ZMQError as e:
+            if e.errno == errno.EAGAIN:
+                data = None
+            else:
+                logging.error("Error while receiving data from "
+                              "reply socket: {}".format(str(e)))
+
+        if data and data[0:4] == "SYNC".encode("utf-8"):
             self._rep_socket.send_multipart(self._msg_cache)
 
         msg = "ACTION {}".format(message).encode("utf-8")
diff --git a/utils/zmq_client.py b/utils/zmq_client.py
index 03a2b44..a78f368 100644
--- a/utils/zmq_client.py
+++ b/utils/zmq_client.py
@@ -9,16 +9,12 @@ socket.connect("tcp://localhost:12345")
 socket.setsockopt(zmq.SUBSCRIBE, "".encode("utf-8"))
 send_socket = context.socket(zmq.REQ)
 send_socket.connect("tcp://localhost:12346")
-send_socket.send("hello".encode("utf-8"))
-while True:
-    try:
-        data = send_socket.recv_multipart(flags=zmq.NOBLOCK)
-    except zmq.ZMQError:
-        pass
-    else:
-        print("RECOVERING CACHE", data)
-        break
 
+send_socket.send("SYNC".encode("utf-8"))
+data = send_socket.recv_multipart()
+for d in data:
+    topic, messagedata = d.split()
+    print("SYNCED ", topic, messagedata)
 
 while True:
     string = socket.recv()
-- 
GitLab