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.

This commit is contained in:
Thom Nichols 2010-06-04 17:00:51 -04:00
parent 919c8c5633
commit e7c37c4ec5

View file

@ -101,19 +101,16 @@ class XMLStream(object):
def connect(self, host='', port=0, use_ssl=None, use_tls=None): def connect(self, host='', port=0, use_ssl=None, use_tls=None):
"Establish a socket connection to the given XMPP server." "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 return False
try: logging.debug('Connection complete.')
return self.connectTCP(host, port, use_ssl, use_tls) return True
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!")
# TODO currently a caller can't distinguish between "connection failed" and # TODO currently a caller can't distinguish between "connection failed" and
# "we're already trying to connect from another thread" # "we're already trying to connect from another thread"
@ -148,9 +145,6 @@ class XMLStream(object):
self.socket.connect(self.address) self.socket.connect(self.address)
self.filesocket = self.socket.makefile('rb', 0) 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 return True
except socket.error as serr: except socket.error as serr: