1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-07-02 08:16:45 +02:00

Formatting, Javadoc and other minor fixes.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@10845 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2008-10-24 04:49:05 +00:00 committed by matt
parent 2c93fc9e70
commit 73a302aef1

View file

@ -4,26 +4,20 @@ import org.jivesoftware.smack.packet.StreamError;
/** /**
* Handles the automatic reconnection process. Every time a connection is dropped without * Handles the automatic reconnection process. Every time a connection is dropped without
* the application explicitly closing it, the manager automatically tries to reconnect to * the application explictly closing it, the manager automatically tries to reconnect to
* the server.<p> * the server.<p>
* <p/> *
* The reconnection mechanism will try to reconnect periodically: * The reconnection mechanism will try to reconnect periodically:
* <ol> * <ol>
* <li>First it will try 6 times every 10 seconds. * <li>For the first minute it will attempt to connect once every ten seconds.
* <li>Then it will try 10 times every 1 minute. * <li>For the next five minutes it will attempt to connect once a minute.
* <li>Finally it will try indefinitely every 5 minutes. * <li>If that fails it will indefinitely try to connect once every five minutes.
* </ol> * </ol>
* *
* @author Francisco Vives * @author Francisco Vives
*/ */
public class ReconnectionManager implements ConnectionListener { public class ReconnectionManager implements ConnectionListener {
// Holds the time elapsed between each reconnection attempt
private int secondBetweenReconnection = 5 * 60; // 5 minutes
// Holds the thread that produces a periodical reconnection.
private Thread reconnectionThread;
// Holds the connection to the server // Holds the connection to the server
private XMPPConnection connection; private XMPPConnection connection;
@ -57,30 +51,6 @@ public class ReconnectionManager implements ConnectionListener {
&& connection.packetReader != null; && connection.packetReader != null;
} }
/**
* Returns the time elapsed between each reconnection attempt.
* By default it will try to reconnect every 5 minutes.
* It is used when the client has lost the server connection and the XMPPConnection
* automatically tries to reconnect.
*
* @return Returns the number of seconds between reconnection.
*/
private int getSecondBetweenReconnection() {
return secondBetweenReconnection;
}
/**
* Sets the time elapsed between each reconnection attempt.
* It is used when the client has lost the server connection and the XMPPConnection
* automatically tries to reconnect.
*
* @param secondBetweenReconnection The number of seconds between reconnection.
*/
protected void setSecondBetweenReconnection(
int secondBetweenReconnection) {
this.secondBetweenReconnection = secondBetweenReconnection;
}
/** /**
* Starts a reconnection mechanism if it was configured to do that. * Starts a reconnection mechanism if it was configured to do that.
* The algorithm is been executed when the first connection error is detected. * The algorithm is been executed when the first connection error is detected.
@ -96,33 +66,26 @@ public class ReconnectionManager implements ConnectionListener {
if (this.isReconnectionAllowed()) { if (this.isReconnectionAllowed()) {
// Since there is no thread running, creates a new one to attempt // Since there is no thread running, creates a new one to attempt
// the reconnection. // the reconnection.
reconnectionThread = new Thread() { Thread reconnectionThread = new Thread() {
/**
* Holds the number of reconnection attempts
*/
private int attempts = 0;
private int firstReconnectionPeriod = 7; // 6 attempts
private int secondReconnectionPeriod = 10 + firstReconnectionPeriod; // 16 attempts
private int firstReconnectionTime = 10; // 10 seconds
private int secondReconnectionTime = 60; // 1 minute
private int lastReconnectionTime =
getSecondBetweenReconnection(); // user defined in seconds
private int remainingSeconds = 0; // The seconds remaining to a reconnection
private int notificationPeriod = 1000; // 1 second
/** /**
* Returns the amount of time until the next reconnection attempt. * Holds the current number of reconnection attempts
*/
private int attempts = 0;
/**
* Returns the number of seconds until the next reconnection attempt.
* *
* @return the amount of time until the next reconnection attempt. * @return the number of seconds until the next reconnection attempt.
*/ */
private int timeDelay() { private int timeDelay() {
if (attempts > secondReconnectionPeriod) { if (attempts > 13) {
return lastReconnectionTime; // 5 minutes return 60 * 5; // 5 minutes
} }
if (attempts > firstReconnectionPeriod) { if (attempts > 7) {
return secondReconnectionTime; // 1 minute return 60; // 1 minute
} }
return firstReconnectionTime; // 10 seconds return 10; // 10 seconds
} }
/** /**
@ -133,15 +96,17 @@ public class ReconnectionManager implements ConnectionListener {
// The process will try to reconnect until the connection is established or // The process will try to reconnect until the connection is established or
// the user cancel the reconnection process {@link XMPPConnection#disconnect()} // the user cancel the reconnection process {@link XMPPConnection#disconnect()}
while (ReconnectionManager.this.isReconnectionAllowed()) { while (ReconnectionManager.this.isReconnectionAllowed()) {
// Indicate how much time will wait until next reconnection // Find how much time we should wait until the next reconnection
remainingSeconds = timeDelay(); int remainingSeconds = timeDelay();
// Notifies the remaining time until the next reconnection attempt // Sleep until we're ready for the next reconnection attempt. Notify
// every 1 second. // listeners once per second about how much time remains before the next
// reconnection attempt.
while (ReconnectionManager.this.isReconnectionAllowed() && while (ReconnectionManager.this.isReconnectionAllowed() &&
remainingSeconds > 0) { remainingSeconds > 0)
{
try { try {
Thread.sleep(notificationPeriod); Thread.sleep(1000);
remainingSeconds = remainingSeconds - 1; remainingSeconds--;
ReconnectionManager.this ReconnectionManager.this
.notifyAttemptToReconnectIn(remainingSeconds); .notifyAttemptToReconnectIn(remainingSeconds);
} }
@ -151,12 +116,10 @@ public class ReconnectionManager implements ConnectionListener {
ReconnectionManager.this.notifyReconnectionFailed(e1); ReconnectionManager.this.notifyReconnectionFailed(e1);
} }
} }
// Waiting time have finished
// Makes the reconnection attempt // Makes a reconnection attempt
try { try {
if (ReconnectionManager.this.isReconnectionAllowed()) { if (ReconnectionManager.this.isReconnectionAllowed()) {
// Attempts to reconnect.
connection.connect(); connection.connect();
} }
} }
@ -239,4 +202,4 @@ public class ReconnectionManager implements ConnectionListener {
// ignore // ignore
} }
} }