fixed disconnect

This commit is contained in:
Nathan Fritz 2010-10-20 16:32:50 -07:00
parent e18354ae0e
commit 6e34b2cfdd

View file

@ -175,6 +175,8 @@ class XMLStream(object):
self.stream_footer = "</stream>" self.stream_footer = "</stream>"
self.stop = threading.Event() self.stop = threading.Event()
self.stream_end_event = threading.Event()
self.stream_end_event.set()
self.event_queue = queue.Queue() self.event_queue = queue.Queue()
self.send_queue = queue.Queue() self.send_queue = queue.Queue()
self.scheduler = Scheduler(self.event_queue, self.stop) self.scheduler = Scheduler(self.event_queue, self.stop)
@ -295,8 +297,9 @@ class XMLStream(object):
# closed in the other direction. # closed in the other direction.
if not reconnect: if not reconnect:
self.auto_reconnect = False self.auto_reconnect = False
self.stream_end_event.wait(4)
if not self.auto_reconnect:
self.stop.set() self.stop.set()
time.sleep(1)
try: try:
self.socket.close() self.socket.close()
self.filesocket.close() self.filesocket.close()
@ -699,6 +702,7 @@ class XMLStream(object):
root = xml root = xml
# Perform any stream initialization actions, such # Perform any stream initialization actions, such
# as handshakes. # as handshakes.
self.stream_end_event.clear()
self.start_stream_handler(root) self.start_stream_handler(root)
depth += 1 depth += 1
if event == b'end': if event == b'end':
@ -707,6 +711,7 @@ class XMLStream(object):
# The stream's root element has closed, # The stream's root element has closed,
# terminating the stream. # terminating the stream.
logging.debug("Ending read XML loop") logging.debug("Ending read XML loop")
self.stream_end_event.set()
return False return False
elif depth == 1: elif depth == 1:
# We only raise events for stanzas that are direct # We only raise events for stanzas that are direct
@ -831,7 +836,10 @@ class XMLStream(object):
""" """
try: try:
while not self.stop.isSet(): while not self.stop.isSet():
data = self.send_queue.get(True) try:
data = self.send_queue.get(True, 1)
except queue.Empty:
continue
logging.debug("SEND: %s" % data) logging.debug("SEND: %s" % data)
try: try:
self.socket.send(data.encode('utf-8')) self.socket.send(data.encode('utf-8'))