aboutsummaryrefslogtreecommitdiffstats
path: root/gui
diff options
context:
space:
mode:
authorYoann QUERET <yoann@queret.net>2015-10-02 15:37:25 +0200
committerYoann QUERET <yoann@queret.net>2015-10-02 15:37:25 +0200
commit89f5a6aa4f6827399db78e7850020626fbac06bb (patch)
tree098665b4ec8a16bfe23d6d9aab5da2704a227d5a /gui
parent3c51aa309f6ff0c38ee9d86c8a35b38cd4f6ac72 (diff)
downloaddabmux-89f5a6aa4f6827399db78e7850020626fbac06bb.tar.gz
dabmux-89f5a6aa4f6827399db78e7850020626fbac06bb.tar.bz2
dabmux-89f5a6aa4f6827399db78e7850020626fbac06bb.zip
Add timeout on ZMQ socket connection
Diffstat (limited to 'gui')
-rw-r--r--gui/muxconfig.py39
1 files changed, 25 insertions, 14 deletions
diff --git a/gui/muxconfig.py b/gui/muxconfig.py
index 4b307fd..8358118 100644
--- a/gui/muxconfig.py
+++ b/gui/muxconfig.py
@@ -97,18 +97,33 @@ class ConfigurationHandler(object):
self._config = None
self._statistics = None
- self._ctx = zmq.Context()
+ #self._ctx = zmq.Context()
+ #self.sock = zmq.Socket(self._ctx, zmq.REQ)
+ #self.sock.setsockopt(zmq.LINGER, 0)
+ #self.sock.connect("tcp://{}:{}".format(self._host, self._port))
+
+ def zRead(self, key):
+ self._ctx = zmq.Context()
self.sock = zmq.Socket(self._ctx, zmq.REQ)
+ self.sock.setsockopt(zmq.LINGER, 0)
self.sock.connect("tcp://{}:{}".format(self._host, self._port))
+ self.sock.send(key)
+
+ # use poll for timeouts:
+ poller = zmq.Poller()
+ poller.register(self.sock, zmq.POLLIN)
+ if poller.poll(5*1000): # 5s timeout in milliseconds
+ recv = self.sock.recv()
+ self.sock.close()
+ self._ctx.term()
+ return recv
+ else:
+ raise IOError("Timeout processing ZMQ request")
def load(self):
- """Load the configuration from the multiplexer and
- save it locally"""
- self.sock.send(b'info')
- server_info = self.sock.recv()
-
- self.sock.send(b'getptree')
- config_info = self.sock.recv()
+ """Load the configuration from the multiplexer and save it locally"""
+ server_info = self.zRead(b'info')
+ config_info = self.zRead(b'getptree')
print("Config '%r'" % config_info)
@@ -118,12 +133,8 @@ class ConfigurationHandler(object):
def update_stats(self):
"""Load the statistics from the multiplexer and
save them locally"""
-
- self.sock.send(b'info')
- server_info = self.sock.recv()
-
- self.sock.send(b'values')
- stats_info = self.sock.recv()
+ server_info = self.zRead(b'info')
+ stats_info = self.zRead(b'values')
self._statistics = json.loads(stats_info)['values']