From c2c7dbddfe9d9494efccfea8ce1ff89125a8e3fa Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 6 Apr 2019 19:05:57 +0200 Subject: [PATCH] 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. --- sat/core/sat_main.py | 2 +- sat/core/xmpp.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/sat/core/sat_main.py b/sat/core/sat_main.py index 64d0024d..ded0cc73 100644 --- a/sat/core/sat_main.py +++ b/sat/core/sat_main.py @@ -418,7 +418,7 @@ class SAT(service.Service): def connectProfile(__=None): if self.isConnected(profile): - log.info(_("already connected !")) + log.info(_(u"already connected !")) return True if self.memory.isComponent(profile): diff --git a/sat/core/xmpp.py b/sat/core/xmpp.py index 1df1b043..34a0319a 100644 --- a/sat/core/xmpp.py +++ b/sat/core/xmpp.py @@ -320,6 +320,43 @@ class SatXMPPEntity(object): return return cb(connector, reason) + def networkDisabled(self): + """Indicate that network has been completely disabled + + In other words, internet is not available anymore and transport must be stopped. + Retrying is disabled too, as it makes no sense to try without network, and it may + use resources (notably battery on mobiles). + """ + log.info(_(u"stopping connection because of network disabled")) + self.factory.continueTrying = 0 + self._network_disabled = True + if self.xmlstream is not None: + self.xmlstream.transport.abortConnection() + + def networkEnabled(self): + """Indicate that network has been (re)enabled + + This happens when e.g. user activate WIFI connection. + """ + try: + connector = self._saved_connector + network_disabled = self._network_disabled + except AttributeError: + # connection has not been stopped by networkDisabled + # we don't have to restart it + log.debug(u"no connection to restart") + return + else: + del self._network_disabled + if not network_disabled: + raise exceptions.InternalError(u"network_disabled should be True") + log.info(_(u"network is available, trying to connect")) + # we want to be sure to start fresh + self.factory.resetDelay() + # we have a saved connector, meaning the connection has been stopped previously + # we can now try to reconnect + connector.connect() + def _connected(self, xs): send_hooks = [] receive_hooks = [] @@ -359,6 +396,13 @@ class SatXMPPEntity(object): u"somebody is trying to spy on you.\n" u"Please contact your server administrator.")) self.factory.stopTrying() + try: + # with invalid certificate, we should not retry to connect + # so we delete saved connector to avoid reconnection if + # networkEnabled is called. + del self._saved_connector + except AttributeError: + pass except (IndexError, TypeError): pass self.conn_deferred.errback(err)