From f7b91d3d91892a7bac1efc05ae120787e52a84e9 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 22 Mar 2020 14:31:48 +0100 Subject: [PATCH] core (xmpp): use async corouting for startConnection --- sat/core/xmpp.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sat/core/xmpp.py b/sat/core/xmpp.py index 4f2b447c..0f58ee1c 100644 --- a/sat/core/xmpp.py +++ b/sat/core/xmpp.py @@ -150,8 +150,7 @@ class SatXMPPEntity: return @classmethod - @defer.inlineCallbacks - def startConnection(cls, host, profile, max_retries): + async def startConnection(cls, host, profile, max_retries): """instantiate the entity and start the connection""" # FIXME: reconnection doesn't seems to be handled correclty # (client is deleted then recreated from scratch) @@ -174,11 +173,11 @@ class SatXMPPEntity: None ) # will use default value 5222 or be retrieved from a DNS SRV record - password = yield host.memory.asyncGetParamA( + password = await host.memory.asyncGetParamA( "Password", "Connection", profile_key=profile ) - entity_jid_s = yield host.memory.asyncGetParamA( + entity_jid_s = await host.memory.asyncGetParamA( "JabberID", "Connection", profile_key=profile) entity_jid = jid.JID(entity_jid_s) @@ -187,13 +186,13 @@ class SatXMPPEntity: # server returned one, as it will then stay stable in case of reconnection. # we only do that for client and if there is a user part, to let server # decide for anonymous login - resource_dict = yield host.memory.storage.getPrivates( + resource_dict = await host.memory.storage.getPrivates( "core:xmpp", ["resource"] , profile=profile) try: resource = resource_dict["resource"] except KeyError: resource = f"{C.APP_NAME_FILE}.{shortuuid.uuid()}" - yield host.memory.storage.setPrivateValue( + await host.memory.storage.setPrivateValue( "core:xmpp", "resource", resource, profile=profile) log.info(_("We'll use the stable resource {resource}").format( @@ -223,13 +222,13 @@ class SatXMPPEntity: log.debug(_("setting plugins parents")) - plugin_conn_cb = yield entity._callConnectionTriggers() + plugin_conn_cb = await entity._callConnectionTriggers() entity.startService() - yield entity.conn_deferred + await entity.conn_deferred - yield defer.maybeDeferred(entity.entityConnected) + await defer.maybeDeferred(entity.entityConnected) # Call profileConnected callback for all plugins, # and print error message if any of them fails @@ -252,7 +251,7 @@ class SatXMPPEntity: } ) - yield list_d.addCallback( + await list_d.addCallback( logPluginResults ) # FIXME: we should have a timeout here, and a way to know if a plugin freeze # TODO: mesure launch time of each plugin @@ -811,10 +810,9 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient): self.presence.setHandlerParent(self) @classmethod - @defer.inlineCallbacks - def startConnection(cls, host, profile, max_retries): + async def startConnection(cls, host, profile, max_retries): try: - yield super(SatXMPPClient, cls).startConnection(host, profile, max_retries) + await super(SatXMPPClient, cls).startConnection(host, profile, max_retries) except exceptions.CancelError as e: log.warning(f"startConnection cancelled: {e}") return