From e5791ee5c643708d3f513d257a465ef676d1a0b1 Mon Sep 17 00:00:00 2001 From: Daniel Henninger Date: Sun, 23 Dec 2007 23:25:02 +0000 Subject: [PATCH] Added ability to force an immediate reconnection. Not sure I'm fond of it, but it's doing the trick. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@9669 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smack/ReconnectionManager.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/source/org/jivesoftware/smack/ReconnectionManager.java b/source/org/jivesoftware/smack/ReconnectionManager.java index f4c0544e3..c1689482e 100644 --- a/source/org/jivesoftware/smack/ReconnectionManager.java +++ b/source/org/jivesoftware/smack/ReconnectionManager.java @@ -2,9 +2,6 @@ package org.jivesoftware.smack; import org.jivesoftware.smack.packet.StreamError; -import java.util.ArrayList; -import java.util.List; - /** * Handles the automatic reconnection process. Every time a connection is dropped without * the application explicitly closing it, the manager automatically tries to reconnect to @@ -25,7 +22,7 @@ public class ReconnectionManager implements ConnectionListener { private int secondBetweenReconnection = 5 * 60; // 5 minutes // Holds the thread that produces a periodical reconnection. - private Thread reconnectionThread; + static private Thread reconnectionThread; // Holds the connection to the server private XMPPConnection connection; @@ -84,6 +81,17 @@ public class ReconnectionManager implements ConnectionListener { this.secondBetweenReconnection = secondBetweenReconnection; } + /** + * Forces an immediate reconnection and attempts an immediate reconnection. + * + * Interrupts the existing reconnection thread so that it can try an immediate connection attempt. + */ + static public void forceReconnection() { + if (reconnectionThread != null) { + reconnectionThread.interrupt(); + } + } + /** * Starts a reconnection mechanism if it was configured to do that. * The algorithm is been executed when the first connection error is detected. @@ -149,9 +157,12 @@ public class ReconnectionManager implements ConnectionListener { .notifyAttemptToReconnectIn(remainingSeconds); } catch (InterruptedException e1) { - e1.printStackTrace(); + // We want to be able to legitimately interrupt this thread so we can + // force a reconnection. + remainingSeconds = 0; +// e1.printStackTrace(); // Notify the reconnection has failed - ReconnectionManager.this.notifyReconnectionFailed(e1); +// ReconnectionManager.this.notifyReconnectionFailed(e1); } } // Waiting time have finished @@ -182,7 +193,6 @@ public class ReconnectionManager implements ConnectionListener { * @param exception the exception that occured. */ protected void notifyReconnectionFailed(Exception exception) { - List listenersCopy; if (isReconnectionAllowed()) { for (ConnectionListener listener : connection.packetReader.connectionListeners) { listener.reconnectionFailed(exception);