From 4ccdc1d0920a93b313d81b4014b6f45efb49b1fe Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 10 Mar 2018 19:46:08 +0100 Subject: Small connection fixes --- libdino/src/application.vala | 2 +- libdino/src/service/connection_manager.vala | 76 +++++++++++++---------------- libdino/src/service/module_manager.vala | 2 +- 3 files changed, 37 insertions(+), 43 deletions(-) (limited to 'libdino') diff --git a/libdino/src/application.vala b/libdino/src/application.vala index 4ea33fb7..370618b2 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -41,7 +41,7 @@ public interface Dino.Application : GLib.Application { create_actions(); - activate.connect(() => { + startup.connect(() => { stream_interactor.connection_manager.log_options = print_xmpp; Idle.add(() => { restore(); diff --git a/libdino/src/service/connection_manager.vala b/libdino/src/service/connection_manager.vala index 943dcbad..4413dfd7 100644 --- a/libdino/src/service/connection_manager.vala +++ b/libdino/src/service/connection_manager.vala @@ -17,7 +17,7 @@ public class ConnectionManager { DISCONNECTED } - private ArrayList connection_todo = new ArrayList(Account.equals_func); + private HashSet connection_todo = new HashSet(Account.hash_func, Account.equals_func); private HashMap connections = new HashMap(Account.hash_func, Account.equals_func); private HashMap connection_errors = new HashMap(Account.hash_func, Account.equals_func); @@ -106,7 +106,7 @@ public class ConnectionManager { return null; } - public ArrayList get_managed_accounts() { + public Collection get_managed_accounts() { return connection_todo; } @@ -154,6 +154,7 @@ public class ConnectionManager { stream.add_module(module); } stream.log = new XmppLog(account.bare_jid.to_string(), log_options); + print("[%s] New connection with resource %s: %p\n".printf(account.bare_jid.to_string(), resource, stream)); Connection connection = new Connection(stream, new DateTime.now_utc()); connections[account] = connection; @@ -178,10 +179,9 @@ public class ConnectionManager { try { yield stream.connect(account.domainpart); } catch (Error e) { - stderr.printf("Stream Error: %s\n", e.message); + print(@"[$(account.bare_jid)] Error: $(e.message)\n"); change_connection_state(account, ConnectionState.DISCONNECTED); if (!connection_todo.contains(account)) { - connections.unset(account); return; } if (e is IOStreamError.TLS) { @@ -192,39 +192,34 @@ public class ConnectionManager { if (flag != null) { set_connection_error(account, new ConnectionError(ConnectionError.Source.STREAM_ERROR, flag.error_type) { resource_rejected=flag.resource_rejected }); } - interpret_connection_error(account); - } - } - private void interpret_connection_error(Account account) { - ConnectionError? error = connection_errors[account]; - int wait_sec = 5; - if (error == null) { - wait_sec = 3; - } else if (error.source == ConnectionError.Source.STREAM_ERROR) { - if (error.resource_rejected) { - connect_(account, account.resourcepart + "-" + random_uuid()); - return; - } - switch (error.reconnect_recomendation) { - case ConnectionError.Reconnect.NOW: - wait_sec = 5; break; - case ConnectionError.Reconnect.LATER: - wait_sec = 60; break; - case ConnectionError.Reconnect.NEVER: + ConnectionError? error = connection_errors[account]; + int wait_sec = 5; + if (error == null) { + wait_sec = 3; + } else if (error.source == ConnectionError.Source.STREAM_ERROR) { + print(@"[$(account.bare_jid)] Stream Error: $(error.identifier)\n"); + if (error.resource_rejected) { + connect_(account, account.resourcepart + "-" + random_uuid()); return; + } + switch (error.reconnect_recomendation) { + case ConnectionError.Reconnect.NOW: + wait_sec = 5; break; + case ConnectionError.Reconnect.LATER: + wait_sec = 60; break; + case ConnectionError.Reconnect.NEVER: + return; + } + } else if (error.source == ConnectionError.Source.SASL) { + return; } - } else if (error.source == ConnectionError.Source.SASL) { - return; - } - if (network_is_online()) { - wait_sec = 30; + print(@"[$(account.bare_jid)] Check reconnect in $wait_sec sec\n"); + Timeout.add_seconds(wait_sec, () => { + check_reconnect(account); + return false; + }); } - print(@"recovering in $wait_sec\n"); - Timeout.add_seconds(wait_sec, () => { - check_reconnect(account); - return false; - }); } private void check_reconnects() { @@ -237,7 +232,8 @@ public class ConnectionManager { bool acked = false; XmppStream stream = connections[account].stream; - stream.get_module(Xep.Ping.Module.IDENTITY).send_ping(stream, account.bare_jid.domain_jid, (stream) => { + stream.get_module(Xep.Ping.Module.IDENTITY).send_ping(stream, account.bare_jid.domain_jid, () => { + if (connections[account].stream != stream) return; acked = true; change_connection_state(account, ConnectionState.CONNECTED); }); @@ -267,10 +263,10 @@ public class ConnectionManager { private void on_network_changed() { if (network_is_online()) { - print("network online\n"); + print("Network reported online\n"); check_reconnects(); } else { - print("network offline\n"); + print("Network reported offline\n"); foreach (Account account in connection_todo) { change_connection_state(account, ConnectionState.DISCONNECTED); } @@ -282,17 +278,15 @@ public class ConnectionManager { change_connection_state(account, ConnectionState.DISCONNECTED); } if (suspend) { - print("suspend\n"); + print("Device suspended\n"); foreach (Account account in connection_todo) { - Xmpp.Presence.Stanza presence = new Xmpp.Presence.Stanza(); - presence.type_ = Xmpp.Presence.Stanza.TYPE_UNAVAILABLE; try { - connections[account].stream.get_module(Presence.Module.IDENTITY).send_presence(connections[account].stream, presence); + make_offline(account); connections[account].stream.disconnect(); } catch (Error e) { print(@"on_prepare_for_sleep error $(e.message)\n"); } } } else { - print("un-suspend\n"); + print("Device un-suspend\n"); check_reconnects(); } } diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index f5f3b63e..78819bb3 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -40,7 +40,7 @@ public class ModuleManager { foreach (XmppStreamModule module in module_map[account]) { if (module.get_id() == Bind.Module.IDENTITY.id) { - (module as Bind.Module).requested_resource == null ? account.resourcepart : resource; + (module as Bind.Module).requested_resource = resource ?? account.resourcepart; } else if (module.get_id() == PlainSasl.Module.IDENTITY.id) { (module as PlainSasl.Module).password = account.password; } -- cgit v1.2.3-70-g09d2