From e7c37c4ec5a31402ba4d3fd95f2ee31d72183a83 Mon Sep 17 00:00:00 2001 From: Thom Nichols Date: Fri, 4 Jun 2010 17:00:51 -0400 Subject: connect uses the new function-on-state-transition so when the connect method returns you are guaranteed to be either in the 'connected' or 'disconnected' state. Could remove the 'connecting' state except uses it. --- sleekxmpp/xmlstream/xmlstream.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index cc5e1ec8..6dbe7b30 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -101,19 +101,16 @@ class XMLStream(object): def connect(self, host='', port=0, use_ssl=None, use_tls=None): "Establish a socket connection to the given XMPP server." - if not self.state.transition('disconnected','connecting'): - logging.warning("Can't connect now; Already in state %s", self.state.current_state()) + + if not self.state.transition('disconnected','connected', + func=self.connectTCP, args=[host, port, use_ssl, use_tls] ): + + if self.state['connected']: logging.debug('Already connected') + else: logging.warning("Connection failed" ) return False - try: - return self.connectTCP(host, port, use_ssl, use_tls) - finally: - # attempt to ensure once a connection attempt starts, we leave either in the - # 'connected' or 'disconnected' state. Otherwise the connect method is not reentrant - if self.state['connecting']: - if not self.state.transition('connecting','disconnected'): - logging.error("Couldn't return to the 'disconnected' state after connection failure!") - + logging.debug('Connection complete.') + return True # TODO currently a caller can't distinguish between "connection failed" and # "we're already trying to connect from another thread" @@ -148,9 +145,6 @@ class XMLStream(object): self.socket.connect(self.address) self.filesocket = self.socket.makefile('rb', 0) - if not self.state.transition('connecting','connected'): - logging.error( "State transition error!!!! Shouldn't have happened" ) - logging.debug('connect complete.') return True except socket.error as serr: -- cgit v1.2.3