diff --git a/source/org/jivesoftware/smack/ConnectionConfiguration.java b/source/org/jivesoftware/smack/ConnectionConfiguration.java index cabe21576..703b6bd27 100644 --- a/source/org/jivesoftware/smack/ConnectionConfiguration.java +++ b/source/org/jivesoftware/smack/ConnectionConfiguration.java @@ -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; + } } diff --git a/source/org/jivesoftware/smack/XMPPConnection.java b/source/org/jivesoftware/smack/XMPPConnection.java index f3f191933..9d77d9cf1 100644 --- a/source/org/jivesoftware/smack/XMPPConnection.java +++ b/source/org/jivesoftware/smack/XMPPConnection.java @@ -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(); diff --git a/test/org/jivesoftware/smack/ReconnectionTest.java b/test/org/jivesoftware/smack/ReconnectionTest.java index b2341cfe8..62c394bf3 100644 --- a/test/org/jivesoftware/smack/ReconnectionTest.java +++ b/test/org/jivesoftware/smack/ReconnectionTest.java @@ -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.