From ecf22cb4a4261b1fbbe893a0ec1bf185fcc02db8 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 30 Jul 2014 17:24:41 +0200 Subject: [PATCH] Fix all the connect() reconnect() stuff The /reconnect command should now properly work everytime. --- src/core/commands.py | 6 ++++-- src/core/core.py | 19 +++++++++++-------- src/core/handlers.py | 4 ++-- src/tabs/rostertab.py | 5 ++++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/core/commands.py b/src/core/commands.py index 1713c0f1..c7d900c3 100644 --- a/src/core/commands.py +++ b/src/core/commands.py @@ -807,6 +807,9 @@ def command_quit(self, arg=''): """ /quit """ + if not self.xmpp.is_connected(): + self.exit() + return if len(arg.strip()) != 0: msg = arg else: @@ -820,8 +823,7 @@ def command_quit(self, arg=''): self.save_config() self.plugin_manager.disable_plugins() self.disconnect(msg) - self.running = False - self.reset_curses() + self.xmpp.add_event_handler("disconnected", self.exit, disposable=True) def command_destroy_room(self, arg=''): """ diff --git a/src/core/core.py b/src/core/core.py index 42d8df31..feccd01c 100644 --- a/src/core/core.py +++ b/src/core/core.py @@ -9,6 +9,7 @@ import logging log = logging.getLogger(__name__) +import asyncio import collections import curses import os @@ -189,6 +190,7 @@ class Core(object): # Add handlers self.xmpp.add_event_handler('connected', self.on_connected) + self.xmpp.add_event_handler('connection_failed', self.on_failed_connection) self.xmpp.add_event_handler('disconnected', self.on_disconnected) self.xmpp.add_event_handler('failed_auth', self.on_failed_auth) self.xmpp.add_event_handler('no_auth', self.on_no_auth) @@ -427,13 +429,8 @@ class Core(object): if config.get('enable_user_gaming', True): self.xmpp.plugin['xep_0196'].stop(block=False) self.plugin_manager.disable_plugins() - self.disconnect('') - self.running = False - try: - self.reset_curses() - except: # too bad - pass - sys.exit() + self.disconnect('%s received' % signals.get(sig)) + self.xmpp.add_event_handler("disconnected", self.exit, disposable=True) def autoload_plugins(self): """ @@ -470,6 +467,10 @@ class Core(object): self.refresh_window() self.xmpp.plugin['xep_0012'].begin_idle(jid=self.xmpp.boundjid) + def exit(self, event=None): + log.debug("exit(%s)" % (event,)) + asyncio.get_event_loop().stop() + def on_exception(self, typ, value, trace): """ When an exception is raised, just reset curses and call @@ -786,7 +787,9 @@ class Core(object): tab.command_part(msg) self.xmpp.disconnect() if reconnect: - self.xmpp.start() + # Add a one-time event to reconnect as soon as we are + # effectively disconnected + self.xmpp.add_event_handler('disconnected', lambda event: self.xmpp.connect(), disposable=True) def send_message(self, msg): """ diff --git a/src/core/handlers.py b/src/core/handlers.py index 92533d63..94a0614b 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -812,11 +812,11 @@ def on_groupchat_presence(self, presence): ### Connection-related handlers ### -def on_failed_connection(self): +def on_failed_connection(self, error): """ We cannot contact the remote server """ - self.information(_("Connection to remote server failed"), _('Error')) + self.information(_("Connection to remote server failed: %s" % (error,)), _('Error')) def on_disconnected(self, event): """ diff --git a/src/tabs/rostertab.py b/src/tabs/rostertab.py index 3d01046b..cb80007a 100644 --- a/src/tabs/rostertab.py +++ b/src/tabs/rostertab.py @@ -241,7 +241,10 @@ class RosterInfoTab(Tab): """ /reconnect """ - self.core.disconnect(reconnect=True) + if self.core.xmpp.is_connected(): + self.core.disconnect(reconnect=True) + else: + self.core.xmpp.connect() def command_disconnect(self, args=None): """