From 7e435b703de9ef184ea92d3822d9f85e3beebf79 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 11:01:36 +0100 Subject: Propagate disconnect() reason into 'disconnected' event --- slixmpp/xmlstream/xmlstream.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'slixmpp/xmlstream') diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index db3d4684..9e407d7e 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -215,6 +215,9 @@ class XMLStream(asyncio.BaseProtocol): #: ``_xmpp-client._tcp`` service. self.dns_service = None + #: The reason why we are disconnecting from the server + self.disconnect_reason = None + #: An asyncio Future being done when the stream is disconnected. self.disconnected = asyncio.Future() @@ -268,6 +271,7 @@ class XMLStream(asyncio.BaseProtocol): localhost """ + self.disconnect_reason = None self.cancel_connection_attempt() if host and port: self.address = (host, int(port)) @@ -403,6 +407,7 @@ class XMLStream(asyncio.BaseProtocol): # The stream's root element has closed, # terminating the stream. log.debug("End of stream received") + self.disconnect_reason = "End of stream" self.abort() elif self.xml_depth == 1: # A stanza is an XML element that is a direct child of @@ -437,7 +442,7 @@ class XMLStream(asyncio.BaseProtocol): closure of the TCP connection """ log.info("connection_lost: %s", (exception,)) - self.event("disconnected") + self.event("disconnected", self.disconnect_reason or exception and exception.strerror) if self.end_session_on_disconnect: self.event('session_end') # All these objects are associated with one TCP connection. Since @@ -457,7 +462,7 @@ class XMLStream(asyncio.BaseProtocol): self._current_connection_attempt.cancel() self._current_connection_attempt = None - def disconnect(self, wait=2.0): + def disconnect(self, wait=2.0, reason=None): """Close the XML stream and wait for an acknowldgement from the server for at most `wait` seconds. After the given number of seconds has passed without a response from the serveur, or when the server @@ -470,6 +475,7 @@ class XMLStream(asyncio.BaseProtocol): :param wait: Time to wait for a response from the server. """ + self.disconnect_reason = reason self.cancel_connection_attempt() if self.transport: self.send_raw(self.stream_footer) @@ -488,12 +494,12 @@ class XMLStream(asyncio.BaseProtocol): self.disconnected.set_result(True) self.disconnected = asyncio.Future() - def reconnect(self, wait=2.0): + def reconnect(self, wait=2.0, reason="Reconnecting"): """Calls disconnect(), and once we are disconnected (after the timeout, or when the server acknowledgement is received), call connect() """ log.debug("reconnecting...") - self.disconnect(wait) + self.disconnect(wait, reason) self.add_event_handler('disconnected', self.connect, disposable=True) def configure_socket(self): -- cgit v1.2.3