diff 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
line wrap: on
line diff
--- a/src/core/sat_main.py	Wed May 14 13:26:23 2014 +0200
+++ b/src/core/sat_main.py	Thu May 15 16:02:16 2014 +0200
@@ -93,6 +93,7 @@
         except exceptions.BridgeInitError:
             log.error(u"Bridge can't be initialised, can't start SàT core")
             sys.exit(1)
+        self.bridge.register("getReady", lambda: self.memory.initialized)
         self.bridge.register("getVersion", lambda: C.APP_VERSION)
         self.bridge.register("getProfileName", self.memory.getProfileName)
         self.bridge.register("getProfilesList", self.memory.getProfilesList)
@@ -203,88 +204,90 @@
         """Connect to jabber server with asynchronous reply
         @param profile_key: %(doc_profile)s
         """
-        profile = self.memory.getProfileName(profile_key)
-        if not profile:
-            log.error(_('Trying to connect a non-existant profile'))
-            raise exceptions.ProfileUnknownError(profile_key)
+        def backend_initialized(ignore):
+            profile = self.memory.getProfileName(profile_key)
+            if not profile:
+                log.error(_('Trying to connect a non-existant profile'))
+                raise exceptions.ProfileUnknownError(profile_key)
 
-        if self.isConnected(profile):
-            log.info(_("already connected !"))
-            return defer.succeed("None")
+            if self.isConnected(profile):
+                log.info(_("already connected !"))
+                return defer.succeed("None")
 
-        def afterMemoryInit(ignore):
-            """This part must be called when we have loaded individual parameters from memory"""
-            try:
-                port = int(self.memory.getParamA("Port", "Connection", profile_key=profile))
-            except ValueError:
-                log.error(_("Can't parse port value, using default value"))
-                port = 5222
+            def afterMemoryInit(ignore):
+                """This part must be called when we have loaded individual parameters from memory"""
+                try:
+                    port = int(self.memory.getParamA("Port", "Connection", profile_key=profile))
+                except ValueError:
+                    log.error(_("Can't parse port value, using default value"))
+                    port = 5222
 
-            current = self.profiles[profile] = xmpp.SatXMPPClient(
-                self, profile,
-                jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key=profile), profile),
-                self.memory.getParamA("Password", "Connection", profile_key=profile),
-                self.memory.getParamA("Server", "Connection", profile_key=profile),
-                port)
+                current = self.profiles[profile] = xmpp.SatXMPPClient(
+                    self, profile,
+                    jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key=profile), profile),
+                    self.memory.getParamA("Password", "Connection", profile_key=profile),
+                    self.memory.getParamA("Server", "Connection", profile_key=profile),
+                    port)
 
-            current.messageProt = xmpp.SatMessageProtocol(self)
-            current.messageProt.setHandlerParent(current)
+                current.messageProt = xmpp.SatMessageProtocol(self)
+                current.messageProt.setHandlerParent(current)
 
-            current.roster = xmpp.SatRosterProtocol(self)
-            current.roster.setHandlerParent(current)
+                current.roster = xmpp.SatRosterProtocol(self)
+                current.roster.setHandlerParent(current)
 
-            current.presence = xmpp.SatPresenceProtocol(self)
-            current.presence.setHandlerParent(current)
+                current.presence = xmpp.SatPresenceProtocol(self)
+                current.presence.setHandlerParent(current)
 
-            current.fallBack = xmpp.SatFallbackHandler(self)
-            current.fallBack.setHandlerParent(current)
+                current.fallBack = xmpp.SatFallbackHandler(self)
+                current.fallBack.setHandlerParent(current)
 
-            current.versionHandler = xmpp.SatVersionHandler(C.APP_NAME_FULL,
-                                                            C.APP_VERSION)
-            current.versionHandler.setHandlerParent(current)
+                current.versionHandler = xmpp.SatVersionHandler(C.APP_NAME_FULL,
+                                                                C.APP_VERSION)
+                current.versionHandler.setHandlerParent(current)
 
-            current.identityHandler = xmpp.SatIdentityHandler()
-            current.identityHandler.setHandlerParent(current)
+                current.identityHandler = xmpp.SatIdentityHandler()
+                current.identityHandler.setHandlerParent(current)
 
-            log.debug(_("setting plugins parents"))
+                log.debug(_("setting plugins parents"))
 
-            plugin_conn_cb = []
-            for plugin in self.plugins.iteritems():
-                if plugin[1].is_handler:
-                    plugin[1].getHandler(profile).setHandlerParent(current)
-                connected_cb = getattr(plugin[1], "profileConnected", None)
-                if connected_cb:
-                    plugin_conn_cb.append((plugin[0], connected_cb))
+                plugin_conn_cb = []
+                for plugin in self.plugins.iteritems():
+                    if plugin[1].is_handler:
+                        plugin[1].getHandler(profile).setHandlerParent(current)
+                    connected_cb = getattr(plugin[1], "profileConnected", None)
+                    if connected_cb:
+                        plugin_conn_cb.append((plugin[0], connected_cb))
 
-            current.startService()
+                current.startService()
 
-            d = current.getConnectionDeferred()
-            d.addCallback(lambda dummy: current.roster.got_roster)  # we want to be sure that we got the roster
+                d = current.getConnectionDeferred()
+                d.addCallback(lambda dummy: current.roster.got_roster)  # we want to be sure that we got the roster
 
-            def pluginsConnection(dummy):
-                """Call profileConnected callback for all plugins, and print error message if any of them fails"""
-                conn_cb_list = []
-                for dummy, callback in plugin_conn_cb:
-                    conn_cb_list.append(defer.maybeDeferred(callback, profile))
-                list_d = defer.DeferredList(conn_cb_list)
+                def pluginsConnection(dummy):
+                    """Call profileConnected callback for all plugins, and print error message if any of them fails"""
+                    conn_cb_list = []
+                    for dummy, callback in plugin_conn_cb:
+                        conn_cb_list.append(defer.maybeDeferred(callback, profile))
+                    list_d = defer.DeferredList(conn_cb_list)
 
-                def logPluginResults(results):
-                    all_succeed = all([success for success, result in results])
-                    if not all_succeed:
-                        log.error(_("Plugins initialisation error"))
-                        for idx, (success, result) in enumerate(results):
-                            if not success:
-                                log.error("error (plugin %(name)s): %(failure)s" % {'name': plugin_conn_cb[idx][0],
-                                                                                'failure': result})
+                    def logPluginResults(results):
+                        all_succeed = all([success for success, result in results])
+                        if not all_succeed:
+                            log.error(_("Plugins initialisation error"))
+                            for idx, (success, result) in enumerate(results):
+                                if not success:
+                                    log.error("error (plugin %(name)s): %(failure)s" % {'name': plugin_conn_cb[idx][0],
+                                                                                    'failure': result})
 
-                list_d.addCallback(logPluginResults)
-                return list_d
+                    list_d.addCallback(logPluginResults)
+                    return list_d
 
-            d.addCallback(pluginsConnection)
-            return d
+                d.addCallback(pluginsConnection)
+                return d
 
-        self.memory.startProfileSession(profile)
-        return self.memory.loadIndividualParams(profile).addCallback(afterMemoryInit)
+            self.memory.startProfileSession(profile)
+            return self.memory.loadIndividualParams(profile).addCallback(afterMemoryInit)
+        return self.memory.initialized.addCallback(backend_initialized)
 
     def disconnect(self, profile_key):
         """disconnect from jabber server"""