2006-09-14 21:13:35 +02:00
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2006-11-15 23:29:25 +01:00
|
|
|
import org.jivesoftware.smack.packet.StreamError;
|
2011-03-31 08:35:59 +02:00
|
|
|
import java.util.Random;
|
2006-09-14 21:13:35 +02:00
|
|
|
/**
|
2006-09-15 23:51:08 +02:00
|
|
|
* Handles the automatic reconnection process. Every time a connection is dropped without
|
2008-10-24 06:49:05 +02:00
|
|
|
* the application explictly closing it, the manager automatically tries to reconnect to
|
2006-09-15 23:51:08 +02:00
|
|
|
* the server.<p>
|
2008-10-24 06:49:05 +02:00
|
|
|
*
|
2006-09-15 23:51:08 +02:00
|
|
|
* The reconnection mechanism will try to reconnect periodically:
|
2006-09-14 21:13:35 +02:00
|
|
|
* <ol>
|
2008-10-24 06:49:05 +02:00
|
|
|
* <li>For the first minute it will attempt to connect once every ten seconds.
|
|
|
|
* <li>For the next five minutes it will attempt to connect once a minute.
|
|
|
|
* <li>If that fails it will indefinitely try to connect once every five minutes.
|
2006-09-14 21:13:35 +02:00
|
|
|
* </ol>
|
|
|
|
*
|
|
|
|
* @author Francisco Vives
|
|
|
|
*/
|
|
|
|
public class ReconnectionManager implements ConnectionListener {
|
|
|
|
|
|
|
|
// Holds the connection to the server
|
2010-02-09 12:55:56 +01:00
|
|
|
private Connection connection;
|
2011-03-31 08:35:59 +02:00
|
|
|
private Thread reconnectionThread;
|
|
|
|
private int randomBase = new Random().nextInt(11) + 5; // between 5 and 15 seconds
|
|
|
|
|
2006-09-14 21:13:35 +02:00
|
|
|
// Holds the state of the reconnection
|
|
|
|
boolean done = false;
|
|
|
|
|
|
|
|
static {
|
|
|
|
// Create a new PrivacyListManager on every established connection. In the init()
|
|
|
|
// method of PrivacyListManager, we'll add a listener that will delete the
|
|
|
|
// instance when the connection is closed.
|
2010-02-09 12:55:56 +01:00
|
|
|
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
|
|
|
public void connectionCreated(Connection connection) {
|
2006-09-14 21:13:35 +02:00
|
|
|
connection.addConnectionListener(new ReconnectionManager(connection));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private ReconnectionManager(Connection connection) {
|
2006-09-14 21:13:35 +02:00
|
|
|
this.connection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-09-15 23:51:08 +02:00
|
|
|
* Returns true if the reconnection mechanism is enabled.
|
|
|
|
*
|
|
|
|
* @return true if automatic reconnections are allowed.
|
2006-09-14 21:13:35 +02:00
|
|
|
*/
|
|
|
|
private boolean isReconnectionAllowed() {
|
2010-02-09 12:55:56 +01:00
|
|
|
return !done && !connection.isConnected()
|
|
|
|
&& connection.isReconnectionAllowed();
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a reconnection mechanism if it was configured to do that.
|
|
|
|
* The algorithm is been executed when the first connection error is detected.
|
|
|
|
* <p/>
|
|
|
|
* The reconnection mechanism will try to reconnect periodically in this way:
|
|
|
|
* <ol>
|
|
|
|
* <li>First it will try 6 times every 10 seconds.
|
|
|
|
* <li>Then it will try 10 times every 1 minute.
|
|
|
|
* <li>Finally it will try indefinitely every 5 minutes.
|
|
|
|
* </ol>
|
|
|
|
*/
|
2011-03-31 08:35:59 +02:00
|
|
|
synchronized protected void reconnect() {
|
2006-11-20 19:03:53 +01:00
|
|
|
if (this.isReconnectionAllowed()) {
|
2006-09-14 21:13:35 +02:00
|
|
|
// Since there is no thread running, creates a new one to attempt
|
|
|
|
// the reconnection.
|
2011-03-31 08:35:59 +02:00
|
|
|
// avoid to run duplicated reconnectionThread -- fd: 16/09/2010
|
|
|
|
if (reconnectionThread!=null && reconnectionThread.isAlive()) return;
|
|
|
|
|
|
|
|
reconnectionThread = new Thread() {
|
|
|
|
|
2006-09-14 21:13:35 +02:00
|
|
|
/**
|
2008-10-24 06:49:05 +02:00
|
|
|
* Holds the current number of reconnection attempts
|
2006-09-14 21:13:35 +02:00
|
|
|
*/
|
|
|
|
private int attempts = 0;
|
|
|
|
|
|
|
|
/**
|
2008-10-24 06:49:05 +02:00
|
|
|
* Returns the number of seconds until the next reconnection attempt.
|
2006-09-15 23:51:08 +02:00
|
|
|
*
|
2008-10-24 06:49:05 +02:00
|
|
|
* @return the number of seconds until the next reconnection attempt.
|
2006-09-14 21:13:35 +02:00
|
|
|
*/
|
|
|
|
private int timeDelay() {
|
2011-03-31 08:35:59 +02:00
|
|
|
attempts++;
|
2008-10-24 06:49:05 +02:00
|
|
|
if (attempts > 13) {
|
2011-03-31 08:35:59 +02:00
|
|
|
return randomBase*6*5; // between 2.5 and 7.5 minutes (~5 minutes)
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
2008-10-24 06:49:05 +02:00
|
|
|
if (attempts > 7) {
|
2011-03-31 08:35:59 +02:00
|
|
|
return randomBase*6; // between 30 and 90 seconds (~1 minutes)
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
2011-03-31 08:35:59 +02:00
|
|
|
return randomBase; // 10 seconds
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The process will try the reconnection until the connection succeed or the user
|
|
|
|
* cancell it
|
|
|
|
*/
|
|
|
|
public void run() {
|
|
|
|
// The process will try to reconnect until the connection is established or
|
2010-02-09 12:55:56 +01:00
|
|
|
// the user cancel the reconnection process {@link Connection#disconnect()}
|
2006-09-14 21:13:35 +02:00
|
|
|
while (ReconnectionManager.this.isReconnectionAllowed()) {
|
2008-10-24 06:49:05 +02:00
|
|
|
// Find how much time we should wait until the next reconnection
|
|
|
|
int remainingSeconds = timeDelay();
|
|
|
|
// Sleep until we're ready for the next reconnection attempt. Notify
|
|
|
|
// listeners once per second about how much time remains before the next
|
|
|
|
// reconnection attempt.
|
2006-09-14 21:13:35 +02:00
|
|
|
while (ReconnectionManager.this.isReconnectionAllowed() &&
|
2008-10-24 06:49:05 +02:00
|
|
|
remainingSeconds > 0)
|
|
|
|
{
|
2006-09-14 21:13:35 +02:00
|
|
|
try {
|
2008-10-24 06:49:05 +02:00
|
|
|
Thread.sleep(1000);
|
|
|
|
remainingSeconds--;
|
2006-09-14 21:13:35 +02:00
|
|
|
ReconnectionManager.this
|
|
|
|
.notifyAttemptToReconnectIn(remainingSeconds);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e1) {
|
2008-10-24 04:12:39 +02:00
|
|
|
e1.printStackTrace();
|
2006-09-14 21:13:35 +02:00
|
|
|
// Notify the reconnection has failed
|
2008-10-24 04:12:39 +02:00
|
|
|
ReconnectionManager.this.notifyReconnectionFailed(e1);
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 06:49:05 +02:00
|
|
|
// Makes a reconnection attempt
|
2006-09-14 21:13:35 +02:00
|
|
|
try {
|
|
|
|
if (ReconnectionManager.this.isReconnectionAllowed()) {
|
|
|
|
connection.connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
// Fires the failed reconnection notification
|
|
|
|
ReconnectionManager.this.notifyReconnectionFailed(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
reconnectionThread.setName("Smack Reconnection Manager");
|
|
|
|
reconnectionThread.setDaemon(true);
|
|
|
|
reconnectionThread.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires listeners when a reconnection attempt has failed.
|
2006-09-15 23:51:08 +02:00
|
|
|
*
|
|
|
|
* @param exception the exception that occured.
|
2006-09-14 21:13:35 +02:00
|
|
|
*/
|
|
|
|
protected void notifyReconnectionFailed(Exception exception) {
|
2010-02-09 12:55:56 +01:00
|
|
|
if (isReconnectionAllowed()) {
|
|
|
|
for (ConnectionListener listener : connection.connectionListeners) {
|
2007-02-16 01:12:17 +01:00
|
|
|
listener.reconnectionFailed(exception);
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Fires listeners when The Connection will retry a reconnection. Expressed in seconds.
|
2006-09-15 23:51:08 +02:00
|
|
|
*
|
|
|
|
* @param seconds the number of seconds that a reconnection will be attempted in.
|
2006-09-14 21:13:35 +02:00
|
|
|
*/
|
|
|
|
protected void notifyAttemptToReconnectIn(int seconds) {
|
2010-02-09 12:55:56 +01:00
|
|
|
if (isReconnectionAllowed()) {
|
|
|
|
for (ConnectionListener listener : connection.connectionListeners) {
|
2007-02-16 01:12:17 +01:00
|
|
|
listener.reconnectingIn(seconds);
|
2006-09-14 21:13:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosed() {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
|
|
|
done = false;
|
2006-11-15 23:29:25 +01:00
|
|
|
if (e instanceof XMPPException) {
|
|
|
|
XMPPException xmppEx = (XMPPException) e;
|
|
|
|
StreamError error = xmppEx.getStreamError();
|
|
|
|
|
2006-11-15 23:33:48 +01:00
|
|
|
// Make sure the error is not null
|
|
|
|
if (error != null) {
|
|
|
|
String reason = error.getCode();
|
|
|
|
|
|
|
|
if ("conflict".equals(reason)) {
|
|
|
|
return;
|
|
|
|
}
|
2006-11-15 23:29:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:13:35 +02:00
|
|
|
if (this.isReconnectionAllowed()) {
|
|
|
|
this.reconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The connection has successfull gotten connected.
|
|
|
|
*/
|
2006-11-14 22:58:03 +01:00
|
|
|
public void reconnectionSuccessful() {
|
2006-09-14 21:13:35 +02:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2008-10-24 06:49:05 +02:00
|
|
|
}
|