summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/backend/backend_proxy.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-07-24 12:54:12 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-12-19 17:15:55 -0300
commitdf160c0d44e8d0439d54313f097b2a4d9ada7357 (patch)
treec3d2cb9f68f696bc9377dbaf20897cf0a560afde /src/leap/bitmask/backend/backend_proxy.py
parent30b02e9153b21d177bf0f79e7132157bf25b636d (diff)
Allow frontend and backend to be run separately.
Add the 'check_online' method to check whether the backend is accessible or not. Reduce the wait for running threads timeout on quit. Add retry feature to the backend requests send.
Diffstat (limited to 'src/leap/bitmask/backend/backend_proxy.py')
-rw-r--r--src/leap/bitmask/backend/backend_proxy.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/leap/bitmask/backend/backend_proxy.py b/src/leap/bitmask/backend/backend_proxy.py
index e2611251..9de3501e 100644
--- a/src/leap/bitmask/backend/backend_proxy.py
+++ b/src/leap/bitmask/backend/backend_proxy.py
@@ -67,6 +67,7 @@ class BackendProxy(object):
socket.curve_serverkey = public
socket.setsockopt(zmq.RCVTIMEO, 1000)
+ socket.setsockopt(zmq.LINGER, 0) # Terminate early
socket.connect(self.SERVER)
self._socket = socket
@@ -75,8 +76,23 @@ class BackendProxy(object):
self._call_queue = Queue.Queue()
self._worker_caller = threading.Thread(target=self._worker)
+
+ def start(self):
self._worker_caller.start()
+ def check_online(self):
+ """
+ Return whether the backend is accessible or not.
+ You don't need to do `run` in order to use this.
+
+ :rtype: bool
+ """
+ # we use a small timeout in order to response quickly if the backend is
+ # offline
+ self._send_request(PING_REQUEST, retry=False, timeout=500)
+ self._socket.close()
+ return self.online
+
def _worker(self):
"""
Worker loop that processes the Queue of pending requests to do.
@@ -150,7 +166,7 @@ class BackendProxy(object):
if api_method == STOP_REQUEST:
self._call_queue.put(STOP_REQUEST)
- def _send_request(self, request):
+ def _send_request(self, request, retry=True, timeout=None):
"""
Send the given request to the server.
This is used from a thread safe loop in order to avoid sending a
@@ -158,6 +174,10 @@ class BackendProxy(object):
:param request: the request to send.
:type request: str
+ :param retry: whether we should retry or not in case of timeout.
+ :type retry: bool
+ :param timeout: a custom timeout (milliseconds) to wait for a response.
+ :type timeout: int
"""
# logger.debug("Sending request to backend: {0}".format(request))
self._socket.send(request)
@@ -166,10 +186,16 @@ class BackendProxy(object):
poll.register(self._socket, zmq.POLLIN)
reply = None
+
tries = 0
+ if not retry:
+ tries = self.POLL_TRIES + 1 # this means: no retries left
+
+ if timeout is None:
+ timeout = self.POLL_TIMEOUT
while True:
- socks = dict(poll.poll(self.POLL_TIMEOUT))
+ socks = dict(poll.poll(timeout))
if socks.get(self._socket) == zmq.POLLIN:
reply = self._socket.recv()
break