comparison src/core/sat_main.py @ 1023:8bae81e254a2

core: added a getReady method which can be called by frontends to ensure that backend is fully initialised before doing anything + this ckeck is automatically done in asyncConnect
author Goffi <goffi@goffi.org>
date Thu, 15 May 2014 16:02:16 +0200
parents 6a16ec17a458
children 71fdc327b318
comparison
equal deleted inserted replaced
1022:002ee8397208 1023:8bae81e254a2
91 try: 91 try:
92 self.bridge = DBusBridge() 92 self.bridge = DBusBridge()
93 except exceptions.BridgeInitError: 93 except exceptions.BridgeInitError:
94 log.error(u"Bridge can't be initialised, can't start SàT core") 94 log.error(u"Bridge can't be initialised, can't start SàT core")
95 sys.exit(1) 95 sys.exit(1)
96 self.bridge.register("getReady", lambda: self.memory.initialized)
96 self.bridge.register("getVersion", lambda: C.APP_VERSION) 97 self.bridge.register("getVersion", lambda: C.APP_VERSION)
97 self.bridge.register("getProfileName", self.memory.getProfileName) 98 self.bridge.register("getProfileName", self.memory.getProfileName)
98 self.bridge.register("getProfilesList", self.memory.getProfilesList) 99 self.bridge.register("getProfilesList", self.memory.getProfilesList)
99 self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile)) 100 self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile))
100 self.bridge.register("asyncCreateProfile", self.memory.asyncCreateProfile) 101 self.bridge.register("asyncCreateProfile", self.memory.asyncCreateProfile)
201 202
202 def asyncConnect(self, profile_key=C.PROF_KEY_NONE): 203 def asyncConnect(self, profile_key=C.PROF_KEY_NONE):
203 """Connect to jabber server with asynchronous reply 204 """Connect to jabber server with asynchronous reply
204 @param profile_key: %(doc_profile)s 205 @param profile_key: %(doc_profile)s
205 """ 206 """
206 profile = self.memory.getProfileName(profile_key) 207 def backend_initialized(ignore):
207 if not profile: 208 profile = self.memory.getProfileName(profile_key)
208 log.error(_('Trying to connect a non-existant profile')) 209 if not profile:
209 raise exceptions.ProfileUnknownError(profile_key) 210 log.error(_('Trying to connect a non-existant profile'))
210 211 raise exceptions.ProfileUnknownError(profile_key)
211 if self.isConnected(profile): 212
212 log.info(_("already connected !")) 213 if self.isConnected(profile):
213 return defer.succeed("None") 214 log.info(_("already connected !"))
214 215 return defer.succeed("None")
215 def afterMemoryInit(ignore): 216
216 """This part must be called when we have loaded individual parameters from memory""" 217 def afterMemoryInit(ignore):
217 try: 218 """This part must be called when we have loaded individual parameters from memory"""
218 port = int(self.memory.getParamA("Port", "Connection", profile_key=profile)) 219 try:
219 except ValueError: 220 port = int(self.memory.getParamA("Port", "Connection", profile_key=profile))
220 log.error(_("Can't parse port value, using default value")) 221 except ValueError:
221 port = 5222 222 log.error(_("Can't parse port value, using default value"))
222 223 port = 5222
223 current = self.profiles[profile] = xmpp.SatXMPPClient( 224
224 self, profile, 225 current = self.profiles[profile] = xmpp.SatXMPPClient(
225 jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key=profile), profile), 226 self, profile,
226 self.memory.getParamA("Password", "Connection", profile_key=profile), 227 jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key=profile), profile),
227 self.memory.getParamA("Server", "Connection", profile_key=profile), 228 self.memory.getParamA("Password", "Connection", profile_key=profile),
228 port) 229 self.memory.getParamA("Server", "Connection", profile_key=profile),
229 230 port)
230 current.messageProt = xmpp.SatMessageProtocol(self) 231
231 current.messageProt.setHandlerParent(current) 232 current.messageProt = xmpp.SatMessageProtocol(self)
232 233 current.messageProt.setHandlerParent(current)
233 current.roster = xmpp.SatRosterProtocol(self) 234
234 current.roster.setHandlerParent(current) 235 current.roster = xmpp.SatRosterProtocol(self)
235 236 current.roster.setHandlerParent(current)
236 current.presence = xmpp.SatPresenceProtocol(self) 237
237 current.presence.setHandlerParent(current) 238 current.presence = xmpp.SatPresenceProtocol(self)
238 239 current.presence.setHandlerParent(current)
239 current.fallBack = xmpp.SatFallbackHandler(self) 240
240 current.fallBack.setHandlerParent(current) 241 current.fallBack = xmpp.SatFallbackHandler(self)
241 242 current.fallBack.setHandlerParent(current)
242 current.versionHandler = xmpp.SatVersionHandler(C.APP_NAME_FULL, 243
243 C.APP_VERSION) 244 current.versionHandler = xmpp.SatVersionHandler(C.APP_NAME_FULL,
244 current.versionHandler.setHandlerParent(current) 245 C.APP_VERSION)
245 246 current.versionHandler.setHandlerParent(current)
246 current.identityHandler = xmpp.SatIdentityHandler() 247
247 current.identityHandler.setHandlerParent(current) 248 current.identityHandler = xmpp.SatIdentityHandler()
248 249 current.identityHandler.setHandlerParent(current)
249 log.debug(_("setting plugins parents")) 250
250 251 log.debug(_("setting plugins parents"))
251 plugin_conn_cb = [] 252
252 for plugin in self.plugins.iteritems(): 253 plugin_conn_cb = []
253 if plugin[1].is_handler: 254 for plugin in self.plugins.iteritems():
254 plugin[1].getHandler(profile).setHandlerParent(current) 255 if plugin[1].is_handler:
255 connected_cb = getattr(plugin[1], "profileConnected", None) 256 plugin[1].getHandler(profile).setHandlerParent(current)
256 if connected_cb: 257 connected_cb = getattr(plugin[1], "profileConnected", None)
257 plugin_conn_cb.append((plugin[0], connected_cb)) 258 if connected_cb:
258 259 plugin_conn_cb.append((plugin[0], connected_cb))
259 current.startService() 260
260 261 current.startService()
261 d = current.getConnectionDeferred() 262
262 d.addCallback(lambda dummy: current.roster.got_roster) # we want to be sure that we got the roster 263 d = current.getConnectionDeferred()
263 264 d.addCallback(lambda dummy: current.roster.got_roster) # we want to be sure that we got the roster
264 def pluginsConnection(dummy): 265
265 """Call profileConnected callback for all plugins, and print error message if any of them fails""" 266 def pluginsConnection(dummy):
266 conn_cb_list = [] 267 """Call profileConnected callback for all plugins, and print error message if any of them fails"""
267 for dummy, callback in plugin_conn_cb: 268 conn_cb_list = []
268 conn_cb_list.append(defer.maybeDeferred(callback, profile)) 269 for dummy, callback in plugin_conn_cb:
269 list_d = defer.DeferredList(conn_cb_list) 270 conn_cb_list.append(defer.maybeDeferred(callback, profile))
270 271 list_d = defer.DeferredList(conn_cb_list)
271 def logPluginResults(results): 272
272 all_succeed = all([success for success, result in results]) 273 def logPluginResults(results):
273 if not all_succeed: 274 all_succeed = all([success for success, result in results])
274 log.error(_("Plugins initialisation error")) 275 if not all_succeed:
275 for idx, (success, result) in enumerate(results): 276 log.error(_("Plugins initialisation error"))
276 if not success: 277 for idx, (success, result) in enumerate(results):
277 log.error("error (plugin %(name)s): %(failure)s" % {'name': plugin_conn_cb[idx][0], 278 if not success:
278 'failure': result}) 279 log.error("error (plugin %(name)s): %(failure)s" % {'name': plugin_conn_cb[idx][0],
279 280 'failure': result})
280 list_d.addCallback(logPluginResults) 281
281 return list_d 282 list_d.addCallback(logPluginResults)
282 283 return list_d
283 d.addCallback(pluginsConnection) 284
284 return d 285 d.addCallback(pluginsConnection)
285 286 return d
286 self.memory.startProfileSession(profile) 287
287 return self.memory.loadIndividualParams(profile).addCallback(afterMemoryInit) 288 self.memory.startProfileSession(profile)
289 return self.memory.loadIndividualParams(profile).addCallback(afterMemoryInit)
290 return self.memory.initialized.addCallback(backend_initialized)
288 291
289 def disconnect(self, profile_key): 292 def disconnect(self, profile_key):
290 """disconnect from jabber server""" 293 """disconnect from jabber server"""
291 if not self.isConnected(profile_key): 294 if not self.isConnected(profile_key):
292 log.info(_("not connected !")) 295 log.info(_("not connected !"))