comparison sat/core/xmpp.py @ 3224:0404b306641d

core (xmpp): use async corouting for startConnection
author Goffi <goffi@goffi.org>
date Sun, 22 Mar 2020 14:31:48 +0100
parents b1c90eedbc12
children 2f406b762788
comparison
equal deleted inserted replaced
3223:163014f09bf4 3224:0404b306641d
148 may return a Deferred, to perform initialisation tasks 148 may return a Deferred, to perform initialisation tasks
149 """ 149 """
150 return 150 return
151 151
152 @classmethod 152 @classmethod
153 @defer.inlineCallbacks 153 async def startConnection(cls, host, profile, max_retries):
154 def startConnection(cls, host, profile, max_retries):
155 """instantiate the entity and start the connection""" 154 """instantiate the entity and start the connection"""
156 # FIXME: reconnection doesn't seems to be handled correclty 155 # FIXME: reconnection doesn't seems to be handled correclty
157 # (client is deleted then recreated from scratch) 156 # (client is deleted then recreated from scratch)
158 # most of methods called here should be called once on first connection 157 # most of methods called here should be called once on first connection
159 # (e.g. adding subprotocols) 158 # (e.g. adding subprotocols)
172 log.debug(_("Can't parse port value, using default value")) 171 log.debug(_("Can't parse port value, using default value"))
173 port = ( 172 port = (
174 None 173 None
175 ) # will use default value 5222 or be retrieved from a DNS SRV record 174 ) # will use default value 5222 or be retrieved from a DNS SRV record
176 175
177 password = yield host.memory.asyncGetParamA( 176 password = await host.memory.asyncGetParamA(
178 "Password", "Connection", profile_key=profile 177 "Password", "Connection", profile_key=profile
179 ) 178 )
180 179
181 entity_jid_s = yield host.memory.asyncGetParamA( 180 entity_jid_s = await host.memory.asyncGetParamA(
182 "JabberID", "Connection", profile_key=profile) 181 "JabberID", "Connection", profile_key=profile)
183 entity_jid = jid.JID(entity_jid_s) 182 entity_jid = jid.JID(entity_jid_s)
184 183
185 if not entity_jid.resource and not cls.is_component and entity_jid.user: 184 if not entity_jid.resource and not cls.is_component and entity_jid.user:
186 # if no resource is specified, we create our own instead of using 185 # if no resource is specified, we create our own instead of using
187 # server returned one, as it will then stay stable in case of reconnection. 186 # server returned one, as it will then stay stable in case of reconnection.
188 # we only do that for client and if there is a user part, to let server 187 # we only do that for client and if there is a user part, to let server
189 # decide for anonymous login 188 # decide for anonymous login
190 resource_dict = yield host.memory.storage.getPrivates( 189 resource_dict = await host.memory.storage.getPrivates(
191 "core:xmpp", ["resource"] , profile=profile) 190 "core:xmpp", ["resource"] , profile=profile)
192 try: 191 try:
193 resource = resource_dict["resource"] 192 resource = resource_dict["resource"]
194 except KeyError: 193 except KeyError:
195 resource = f"{C.APP_NAME_FILE}.{shortuuid.uuid()}" 194 resource = f"{C.APP_NAME_FILE}.{shortuuid.uuid()}"
196 yield host.memory.storage.setPrivateValue( 195 await host.memory.storage.setPrivateValue(
197 "core:xmpp", "resource", resource, profile=profile) 196 "core:xmpp", "resource", resource, profile=profile)
198 197
199 log.info(_("We'll use the stable resource {resource}").format( 198 log.info(_("We'll use the stable resource {resource}").format(
200 resource=resource)) 199 resource=resource))
201 entity_jid.resource = resource 200 entity_jid.resource = resource
221 entity.identityHandler = SatIdentityHandler() 220 entity.identityHandler = SatIdentityHandler()
222 entity.identityHandler.setHandlerParent(entity) 221 entity.identityHandler.setHandlerParent(entity)
223 222
224 log.debug(_("setting plugins parents")) 223 log.debug(_("setting plugins parents"))
225 224
226 plugin_conn_cb = yield entity._callConnectionTriggers() 225 plugin_conn_cb = await entity._callConnectionTriggers()
227 226
228 entity.startService() 227 entity.startService()
229 228
230 yield entity.conn_deferred 229 await entity.conn_deferred
231 230
232 yield defer.maybeDeferred(entity.entityConnected) 231 await defer.maybeDeferred(entity.entityConnected)
233 232
234 # Call profileConnected callback for all plugins, 233 # Call profileConnected callback for all plugins,
235 # and print error message if any of them fails 234 # and print error message if any of them fails
236 conn_cb_list = [] 235 conn_cb_list = []
237 for __, callback in plugin_conn_cb: 236 for __, callback in plugin_conn_cb:
250 "name": plugin_conn_cb[idx][0]._info["import_name"], 249 "name": plugin_conn_cb[idx][0]._info["import_name"],
251 "failure": result, 250 "failure": result,
252 } 251 }
253 ) 252 )
254 253
255 yield list_d.addCallback( 254 await list_d.addCallback(
256 logPluginResults 255 logPluginResults
257 ) # FIXME: we should have a timeout here, and a way to know if a plugin freeze 256 ) # FIXME: we should have a timeout here, and a way to know if a plugin freeze
258 # TODO: mesure launch time of each plugin 257 # TODO: mesure launch time of each plugin
259 258
260 cls.profiles_connecting.remove(profile) 259 cls.profiles_connecting.remove(profile)
809 808
810 self.presence = SatPresenceProtocol(self.host_app) 809 self.presence = SatPresenceProtocol(self.host_app)
811 self.presence.setHandlerParent(self) 810 self.presence.setHandlerParent(self)
812 811
813 @classmethod 812 @classmethod
814 @defer.inlineCallbacks 813 async def startConnection(cls, host, profile, max_retries):
815 def startConnection(cls, host, profile, max_retries):
816 try: 814 try:
817 yield super(SatXMPPClient, cls).startConnection(host, profile, max_retries) 815 await super(SatXMPPClient, cls).startConnection(host, profile, max_retries)
818 except exceptions.CancelError as e: 816 except exceptions.CancelError as e:
819 log.warning(f"startConnection cancelled: {e}") 817 log.warning(f"startConnection cancelled: {e}")
820 return 818 return
821 entity = host.profiles[profile] 819 entity = host.profiles[profile]
822 # we finally send our presence 820 # we finally send our presence