code: added asyncConnect

This commit is contained in:
Goffi 2011-05-26 16:49:47 +02:00
commit 05987404cc
2 changed files with 22 additions and 2 deletions

View file

@ -112,6 +112,7 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
plugin[1].getHandler(profile).setHandlerParent(current)
current.startService()
def disconnect(self, profile_key='@DEFAULT@'):
"""disconnect from jabber server"""
if (not self.isConnected(profile_key)):

View file

@ -34,6 +34,11 @@ class SatXMPPClient(client.XMPPClient):
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 @@ class SatXMPPClient(client.XMPPClient):
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 @@ class SatXMPPClient(client.XMPPClient):
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 @@ class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
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):