1) Fixed reconnection when connection was using compression.

2) Fixed conflict error while reconnecting (reconnection was not keeping track of resource and sendPresence.
3) Improved javadoc (and checking) that reconnectionlisteners should be added once connected.
4) Added new reconnection test case for reconnection and multiple resources.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6050 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2006-11-10 19:06:33 +00:00 committed by gato
parent 97824ebc1c
commit 53c97378d3
3 changed files with 82 additions and 12 deletions

View File

@ -66,7 +66,8 @@ public class ConnectionConfiguration implements Cloneable {
// Holds the authentication information for future reconnections
private String username;
private String password;
private String resource;
private boolean sendPresence;
/**
* Creates a new ConnectionConfiguration for a connection that will connect
* to the desired service name. A DNS SRV lookup will be performed to find out the
@ -441,21 +442,49 @@ public class ConnectionConfiguration implements Cloneable {
public SocketFactory getSocketFactory() {
return this.socketFactory;
}
protected void setUsernameAndPassword(String username, String password) {
protected void setLoginInfo(String username, String password, String resource,
boolean sendPresence) {
this.username = username;
this.password = password;
this.resource = resource;
this.sendPresence = sendPresence;
}
/**
* Returns the username used to login
/**
* Returns the username to use when trying to reconnect to the server.
*
* @return the username to use when trying to reconnect to the server.
*/
protected String getUsername() {
return this.username;
}
/**
* Returns the password used to login
/**
* Returns the password to use when trying to reconnect to the server.
*
* @return the password to use when trying to reconnect to the server.
*/
protected String getPassword() {
return this.password;
}
/**
* Returns the resource to use when trying to reconnect to the server.
*
* @return the resource to use when trying to reconnect to the server.
*/
protected String getResource() {
return resource;
}
/**
* Returns true if an available presence should be sent when logging in while reconnecting.
*
* @return true if an available presence should be sent when logging in while reconnecting
*/
protected boolean isSendPresence() {
return sendPresence;
}
}

View File

@ -442,7 +442,7 @@ public class XMPPConnection {
anonymous = false;
// Stores the autentication for future reconnection
this.getConfiguration().setUsernameAndPassword(username, password);
this.getConfiguration().setLoginInfo(username, password, resource, sendPresence);
// If debugging is enabled, change the the debug window title to include the
// name we are now logged-in as.
@ -785,11 +785,15 @@ public class XMPPConnection {
/**
* Adds a connection listener to this connection that will be notified when
* the connection closes or fails.
* the connection closes or fails. The connection needs to already be connected
* or otherwise an IllegalStateException will be thrown.
*
* @param connectionListener a connection listener.
*/
public void addConnectionListener(ConnectionListener connectionListener) {
if (!isConnected()) {
throw new IllegalStateException("Not connected to server.");
}
if (connectionListener == null) {
return;
}
@ -885,12 +889,15 @@ public class XMPPConnection {
* @throws XMPPException if establishing a connection to the server fails.
*/
private void initConnection() throws XMPPException {
boolean isFirstInitialization = packetReader == null || packetWriter == null;
if (!isFirstInitialization) {
usingCompression = false;
}
// Set the reader and writer instance variables
initReaderAndWriter();
try {
boolean isFirstInitialization = packetReader == null || packetWriter == null;
if (isFirstInitialization) {
packetWriter = new PacketWriter(this);
packetReader = new PacketReader(this);
@ -1342,7 +1349,8 @@ public class XMPPConnection {
// Make the anonymous login
loginAnonymously();
} else {
login(getConfiguration().getUsername(), getConfiguration().getPassword());
login(getConfiguration().getUsername(), getConfiguration().getPassword(),
getConfiguration().getResource(), getConfiguration().isSendPresence());
}
} catch (XMPPException e) {
e.printStackTrace();

View File

@ -39,6 +39,39 @@ public class ReconnectionTest extends SmackTestCase {
executeSomeServerInteraction(connection);
}
public void testAutomaticReconnectionWithCompression() throws Exception {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setTLSEnabled(true);
config.setCompressionEnabled(true);
config.setSASLAuthenticationEnabled(true);
XMPPConnection connection = new XMPPConnection(config);
// Connect to the server
connection.connect();
// Log into the server
connection.login(getUsername(0), getUsername(0), "MyOtherResource");
assertTrue("Failed to use compression", connection.isUsingCompression());
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
connection.addConnectionListener(listener);
// Simulates an error in the connection
connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
Thread.sleep(12000);
// After 10 seconds, the reconnection manager must reestablishes the connection
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
true, listener.reconnected);
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
listener.attemptsNotifications);
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
listener.remainingSeconds);
// Executes some server interaction testing the connection
executeSomeServerInteraction(connection);
}
/**
* Tests a manual reconnection.
* Simulates a connection error, disables the reconnection mechanism and then reconnects.