changeset 341:9eebdc655b8b

code: added asyncConnect
author Goffi <goffi@goffi.org>
date Thu, 26 May 2011 16:49:47 +0200
parents 2572351d875a
children c413be4893b5
files src/core/sat_main.py src/core/xmpp.py
diffstat 2 files changed, 22 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Thu May 26 16:49:07 2011 +0200
+++ b/src/core/sat_main.py	Thu May 26 16:49:47 2011 +0200
@@ -112,6 +112,7 @@
         self.bridge.register("deleteProfile", self.memory.deleteProfile)
         self.bridge.register("registerNewAccount", self.registerNewAccount)
         self.bridge.register("connect", self.connect)
+        self.bridge.register("asyncConnect", self.asyncConnect)
         self.bridge.register("disconnect", self.disconnect)
         self.bridge.register("getContacts", self.memory.getContacts)
         self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
@@ -181,8 +182,16 @@
             self.plugins[import_name].is_handler = False
         #TODO: test xmppclient presence and register handler parent
 
+    
     def connect(self, profile_key = '@DEFAULT@'):
         """Connect to jabber server"""
+        self.asyncConnect(profile_key)
+    
+    def asyncConnect(self, profile_key = '@DEFAULT@', callback=None, errback=None):
+        """Connect to jabber server with asynchronous reply
+        @param profile_key: %(doc_profile)s
+        @param callback: called when the profile is connected
+        @param errback: called is the connection fail"""
 
         profile = self.memory.getProfileName(profile_key)
         if not profile:
@@ -191,12 +200,17 @@
         
         if (self.isConnected(profile)):
             info(_("already connected !"))
+            if callback:
+                callback()
             return
         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), 5222)
 
+        if callback and errback:
+            current.getConnectionDeferred().addCallbacks(lambda x:callback(), errback)
+
         current.messageProt = xmpp.SatMessageProtocol(self)
         current.messageProt.setHandlerParent(current)
         
@@ -220,7 +234,7 @@
                 plugin[1].getHandler(profile).setHandlerParent(current)
 
         current.startService()
-    
+
     def disconnect(self, profile_key='@DEFAULT@'):
         """disconnect from jabber server"""
         if (not self.isConnected(profile_key)):
--- a/src/core/xmpp.py	Thu May 26 16:49:07 2011 +0200
+++ b/src/core/xmpp.py	Thu May 26 16:49:47 2011 +0200
@@ -34,6 +34,11 @@
         self.profile = profile
         self.host_app = host_app
         self.client_initialized = defer.Deferred()
+        self.conn_deferred = defer.Deferred()
+
+    def getConnectionDeferred(self):
+        """Return a deferred which fire when the client is connected"""
+        return self.conn_deferred
 
     def _authd(self, xmlstream):
         if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
@@ -65,6 +70,7 @@
        
         self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco, self.profile)  #FIXME: use these informations
         self.disco.requestItems(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDiscoItems, self.disco, self.profile, self.client_initialized)
+        self.conn_deferred.callback(None)
 
     def initializationFailed(self, reason):
         print ("initializationFailed: %s" % reason)
@@ -74,6 +80,7 @@
         except:
             #we already send an error signal, no need to raise an exception
             pass
+        self.conn_deferred.errback()
 
     def isConnected(self):
         return self.__connected
@@ -262,7 +269,6 @@
         if self.user_email:
             _email = query.addElement('email')
             _email.addContent(self.user_email)
-        print iq.toXml()
         reg = iq.send(self.jabber_host).addCallbacks(self.registrationAnswer, self.registrationFailure)
 
     def registrationAnswer(self, answer):