# HG changeset patch # User Goffi # Date 1554570357 -7200 # Node ID b06cb71079faef87221f763a6771f67e68d75764 # Parent e9016bfd8cb2767bdc92edabfbac73c72355c3b9 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. diff -r e9016bfd8cb2 -r b06cb71079fa sat/core/sat_main.py --- a/sat/core/sat_main.py Sat Apr 06 18:51:20 2019 +0200 +++ b/sat/core/sat_main.py Sat Apr 06 19:05:57 2019 +0200 @@ -418,7 +418,7 @@ def connectProfile(__=None): if self.isConnected(profile): - log.info(_("already connected !")) + log.info(_(u"already connected !")) return True if self.memory.isComponent(profile): diff -r e9016bfd8cb2 -r b06cb71079fa sat/core/xmpp.py --- a/sat/core/xmpp.py Sat Apr 06 18:51:20 2019 +0200 +++ b/sat/core/xmpp.py Sat Apr 06 19:05:57 2019 +0200 @@ -320,6 +320,43 @@ 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 @@ 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)