Mercurial > libervia-web
comparison src/browser/sat_browser/json.py @ 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 | 83a40d786e7a |
children | c1907a460f6a |
comparison
equal
deleted
inserted
replaced
844:2ef71ec07d87 | 845:a5b5178ef6b9 |
---|---|
24 ### | 24 ### |
25 | 25 |
26 from pyjamas.Timer import Timer | 26 from pyjamas.Timer import Timer |
27 from pyjamas import Window | 27 from pyjamas import Window |
28 from pyjamas import JSONService | 28 from pyjamas import JSONService |
29 import time | |
30 from sat_browser import main_panel | |
29 | 31 |
30 from sat_browser.constants import Const as C | 32 from sat_browser.constants import Const as C |
31 | 33 |
32 | 34 |
33 class LiberviaMethodProxy(object): | 35 class LiberviaMethodProxy(object): |
201 callback(hash_) | 203 callback(hash_) |
202 return hash_ | 204 return hash_ |
203 | 205 |
204 | 206 |
205 class BridgeSignals(LiberviaJsonProxy): | 207 class BridgeSignals(LiberviaJsonProxy): |
206 RETRY_BASE_DELAY = 1000 | |
207 | 208 |
208 def __init__(self, host): | 209 def __init__(self, host): |
209 self.host = host | 210 self.host = host |
210 self.retry_delay = self.RETRY_BASE_DELAY | 211 self.retry_time = None |
212 self.retry_nb = 0 | |
213 self.retry_warning = None | |
214 self.retry_timer = None | |
211 LiberviaJsonProxy.__init__(self, "/json_signal_api", | 215 LiberviaJsonProxy.__init__(self, "/json_signal_api", |
212 ["getSignals"]) | 216 ["getSignals"]) |
213 self._signals = {} # key: signal name, value: callback | 217 self._signals = {} # key: signal name, value: callback |
214 | 218 |
215 def onRemoteResponse(self, response, request_info): | 219 def onRemoteResponse(self, response, request_info): |
216 if self.retry_delay != self.RETRY_BASE_DELAY: | 220 if self.retry_time: |
217 log.info("Connection with server restablished") | 221 log.info("Connection with server restablished") |
218 self.retry_delay = self.RETRY_BASE_DELAY | 222 self.retry_nb = 0 |
223 self.retry_time = None | |
219 LiberviaJsonProxy.onRemoteResponse(self, response, request_info) | 224 LiberviaJsonProxy.onRemoteResponse(self, response, request_info) |
220 | 225 |
221 def onRemoteError(self, code, errobj, request_info): | 226 def onRemoteError(self, code, errobj, request_info): |
222 if errobj['message'] == 'Empty Response': | 227 if errobj['message'] == 'Empty Response': |
223 log.warning(u"Empty reponse bridgeSignal\ncode={}\nrequest_info: id={} method={} handler={}".format(code, request_info.id, request_info.method, request_info.handler)) | 228 log.warning(u"Empty reponse bridgeSignal\ncode={}\nrequest_info: id={} method={} handler={}".format(code, request_info.id, request_info.method, request_info.handler)) |
227 LiberviaJsonProxy.onRemoteError(self, code, errobj, request_info) | 232 LiberviaJsonProxy.onRemoteError(self, code, errobj, request_info) |
228 #we now try to reconnect | 233 #we now try to reconnect |
229 if isinstance(errobj['message'], dict) and errobj['message']['faultCode'] == 0: | 234 if isinstance(errobj['message'], dict) and errobj['message']['faultCode'] == 0: |
230 Window.alert('You are not allowed to connect to server') | 235 Window.alert('You are not allowed to connect to server') |
231 else: | 236 else: |
232 def _timerCb(timer): | 237 def _timerCb(dummy): |
233 log.info("Trying to reconnect to server...") | 238 current = time.time() |
234 self.getSignals(callback=self.signalHandler, profile=None) | 239 if current > self.retry_time: |
235 # TODO: display a notification to user | 240 msg = "Trying to reconnect to server..." |
236 log.warning(u"Lost connection, trying to reconnect in {} s".format(self.retry_delay / 1000)) | 241 log.info(msg) |
237 Timer(notify=_timerCb).schedule(self.retry_delay) | 242 self.retry_warning.showWarning("INFO", msg) |
238 self.retry_delay *= 2 | 243 self.retry_timer.cancel() |
244 self.retry_warning = self.retry_timer = None | |
245 self.getSignals(callback=self.signalHandler, profile=None) | |
246 else: | |
247 remaining = int(self.retry_time - current) | |
248 msg_html = u"Connection with server lost. Retrying in <strong>{}</strong> s".format(remaining) | |
249 self.retry_warning.showWarning("WARNING", msg_html, None) | |
250 | |
251 retry_delay = 2**self.retry_nb | |
252 self.retry_nb += 1 | |
253 log.warning(u"Lost connection, trying to reconnect in {} s".format(retry_delay)) | |
254 self.retry_time = time.time() + retry_delay | |
255 self.retry_warning = main_panel.WarningPopup() | |
256 self.retry_timer = Timer(notify=_timerCb) | |
257 self.retry_timer.scheduleRepeating(1000) | |
258 _timerCb(None) | |
239 | 259 |
240 def register(self, name, callback, with_profile=True): | 260 def register(self, name, callback, with_profile=True): |
241 """Register a signal | 261 """Register a signal |
242 | 262 |
243 @param: name of the signal to register | 263 @param: name of the signal to register |