comparison sat/core/xmpp.py @ 2886:b06cb71079fa

core (xmpp): new networkEnabled() and networkDisabled() methods: those methods can be called by platform specific plugins when network is known to be (un)available. This way, connection attempts can be cancelled when no network is available, saving resources (notably battery on mobile devices), or attempts can be restarted immediately when network is known to be available again.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2019 19:05:57 +0200
parents e9016bfd8cb2
children 6a0f42e9410a
comparison
equal deleted inserted replaced
2885:e9016bfd8cb2 2886:b06cb71079fa
317 term_type = term_type, 317 term_type = term_type,
318 reason=reason_str)) 318 reason=reason_str))
319 if not self.host_app.trigger.point(u"connection_" + term_type, connector, reason): 319 if not self.host_app.trigger.point(u"connection_" + term_type, connector, reason):
320 return 320 return
321 return cb(connector, reason) 321 return cb(connector, reason)
322
323 def networkDisabled(self):
324 """Indicate that network has been completely disabled
325
326 In other words, internet is not available anymore and transport must be stopped.
327 Retrying is disabled too, as it makes no sense to try without network, and it may
328 use resources (notably battery on mobiles).
329 """
330 log.info(_(u"stopping connection because of network disabled"))
331 self.factory.continueTrying = 0
332 self._network_disabled = True
333 if self.xmlstream is not None:
334 self.xmlstream.transport.abortConnection()
335
336 def networkEnabled(self):
337 """Indicate that network has been (re)enabled
338
339 This happens when e.g. user activate WIFI connection.
340 """
341 try:
342 connector = self._saved_connector
343 network_disabled = self._network_disabled
344 except AttributeError:
345 # connection has not been stopped by networkDisabled
346 # we don't have to restart it
347 log.debug(u"no connection to restart")
348 return
349 else:
350 del self._network_disabled
351 if not network_disabled:
352 raise exceptions.InternalError(u"network_disabled should be True")
353 log.info(_(u"network is available, trying to connect"))
354 # we want to be sure to start fresh
355 self.factory.resetDelay()
356 # we have a saved connector, meaning the connection has been stopped previously
357 # we can now try to reconnect
358 connector.connect()
322 359
323 def _connected(self, xs): 360 def _connected(self, xs):
324 send_hooks = [] 361 send_hooks = []
325 receive_hooks = [] 362 receive_hooks = []
326 self.host_app.trigger.point( 363 self.host_app.trigger.point(
357 u"(its identity can't be checked).\n\n" 394 u"(its identity can't be checked).\n\n"
358 u"This should never happen and may indicate that " 395 u"This should never happen and may indicate that "
359 u"somebody is trying to spy on you.\n" 396 u"somebody is trying to spy on you.\n"
360 u"Please contact your server administrator.")) 397 u"Please contact your server administrator."))
361 self.factory.stopTrying() 398 self.factory.stopTrying()
399 try:
400 # with invalid certificate, we should not retry to connect
401 # so we delete saved connector to avoid reconnection if
402 # networkEnabled is called.
403 del self._saved_connector
404 except AttributeError:
405 pass
362 except (IndexError, TypeError): 406 except (IndexError, TypeError):
363 pass 407 pass
364 self.conn_deferred.errback(err) 408 self.conn_deferred.errback(err)
365 409
366 def _disconnected(self, reason): 410 def _disconnected(self, reason):