changeset 845:a5b5178ef6b9

browser (json): display a warning when connection with server is lost, and count down until the next try
author Goffi <goffi@goffi.org>
date Fri, 15 Jan 2016 15:42:37 +0100
parents 2ef71ec07d87
children c1907a460f6a
files src/browser/sat_browser/json.py
diffstat 1 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/json.py	Fri Jan 15 15:42:37 2016 +0100
+++ b/src/browser/sat_browser/json.py	Fri Jan 15 15:42:37 2016 +0100
@@ -26,6 +26,8 @@
 from pyjamas.Timer import Timer
 from pyjamas import Window
 from pyjamas import JSONService
+import time
+from sat_browser import main_panel
 
 from sat_browser.constants import Const as C
 
@@ -203,19 +205,22 @@
 
 
 class BridgeSignals(LiberviaJsonProxy):
-    RETRY_BASE_DELAY = 1000
 
     def __init__(self, host):
         self.host = host
-        self.retry_delay = self.RETRY_BASE_DELAY
+        self.retry_time = None
+        self.retry_nb = 0
+        self.retry_warning = None
+        self.retry_timer = None
         LiberviaJsonProxy.__init__(self, "/json_signal_api",
                         ["getSignals"])
         self._signals = {} # key: signal name, value: callback
 
     def onRemoteResponse(self, response, request_info):
-        if self.retry_delay != self.RETRY_BASE_DELAY:
+        if self.retry_time:
             log.info("Connection with server restablished")
-        self.retry_delay = self.RETRY_BASE_DELAY
+            self.retry_nb = 0
+            self.retry_time = None
         LiberviaJsonProxy.onRemoteResponse(self, response, request_info)
 
     def onRemoteError(self, code, errobj, request_info):
@@ -229,13 +234,28 @@
         if isinstance(errobj['message'], dict) and errobj['message']['faultCode'] == 0:
             Window.alert('You are not allowed to connect to server')
         else:
-            def _timerCb(timer):
-                log.info("Trying to reconnect to server...")
-                self.getSignals(callback=self.signalHandler, profile=None)
-            # TODO: display a notification to user
-            log.warning(u"Lost connection, trying to reconnect in {} s".format(self.retry_delay / 1000))
-            Timer(notify=_timerCb).schedule(self.retry_delay)
-            self.retry_delay *= 2
+            def _timerCb(dummy):
+                current = time.time()
+                if current > self.retry_time:
+                    msg = "Trying to reconnect to server..."
+                    log.info(msg)
+                    self.retry_warning.showWarning("INFO", msg)
+                    self.retry_timer.cancel()
+                    self.retry_warning = self.retry_timer = None
+                    self.getSignals(callback=self.signalHandler, profile=None)
+                else:
+                    remaining = int(self.retry_time - current)
+                    msg_html = u"Connection with server lost. Retrying in <strong>{}</strong> s".format(remaining)
+                    self.retry_warning.showWarning("WARNING", msg_html, None)
+
+            retry_delay = 2**self.retry_nb
+            self.retry_nb += 1
+            log.warning(u"Lost connection, trying to reconnect in {} s".format(retry_delay))
+            self.retry_time = time.time() + retry_delay
+            self.retry_warning = main_panel.WarningPopup()
+            self.retry_timer = Timer(notify=_timerCb)
+            self.retry_timer.scheduleRepeating(1000)
+            _timerCb(None)
 
     def register(self, name, callback, with_profile=True):
         """Register a signal