From 7c7c2d49b172d08e106e297101142747eaf78944 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Mon, 14 Jul 2014 16:23:20 -0300 Subject: Use polling to prevent communication issues. --- src/leap/bitmask/backend/signaler.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src/leap/bitmask/backend') diff --git a/src/leap/bitmask/backend/signaler.py b/src/leap/bitmask/backend/signaler.py index f21f0998..7401f3a0 100644 --- a/src/leap/bitmask/backend/signaler.py +++ b/src/leap/bitmask/backend/signaler.py @@ -38,6 +38,8 @@ class Signaler(object): """ PORT = "5667" SERVER = "tcp://localhost:%s" % PORT + POLL_TIMEOUT = 1000 # ms + POLL_TRIES = 3 def __init__(self): """ @@ -148,13 +150,23 @@ class Signaler(object): # logger.debug("Signaling '{0}'".format(request)) self._socket.send(request) - # Get the reply. - try: - self._socket.recv() - # response = self._socket.recv() - # msg = "Received reply for '{0}' -> '{1}'" - # msg = msg.format(request, response) - # logger.debug(msg) - except zmq.error.Again as e: - msg = "Timeout error contacting signaler. {0!r}".format(e) + poll = zmq.Poller() + poll.register(self._socket, zmq.POLLIN) + + reply = None + tries = 0 + + while tries < self.POLL_TRIES: + socks = dict(poll.poll(self.POLL_TIMEOUT)) + if socks.get(self._socket) == zmq.POLLIN: + reply = self._socket.recv() + break + + tries += 1 + + if reply is None: + msg = "Timeout error contacting backend." logger.critical(msg) + # else: + # msg = "Received reply for '{0}' -> '{1}'".format(request, reply) + # logger.debug(msg) -- cgit v1.2.3