Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add the ability to register for roster events before logging in (SMAC…

…K-156)

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11826 b35dd754-fafc-0310-a699-88a17e54d16e
  • Loading branch information...
commit c0ad78da322251ab59fc6d1d49966268af26bcde 1 parent 2b9fcbf
henning authored René Treffer committed
View
6 documentation/roster.html
@@ -22,7 +22,7 @@
etc.<p>
A <tt>Roster</tt> instance is obtained using the <tt>Connection.getRoster()</tt>
-method, but only after successfully logging into a server.
+method.
<p class="subheader">Roster Entries</p>
@@ -70,7 +70,9 @@
<p>The presence information will likely
change often, and it's also possible for the roster entries to change or be deleted.
-To listen for changing roster and presence data, a RosterListener should be used.
+To listen for changing roster and presence data, a RosterListener should be used.
+To be informed about all changes to the roster the RosterListener should be registered
+before logging into the XMPP server.
The following code snippet registers a RosterListener with the Roster that prints
any presence changes in the roster to standard out. A normal client would use
similar code to update the roster UI with the changing information.
View
14 source/org/jivesoftware/smack/Connection.java
@@ -428,11 +428,15 @@ public synchronized ChatManager getChatManager() {
}
/**
- * Returns the roster for the user logged into the server. If the user has not yet
- * logged into the server (or if the user is logged in anonymously), this method will return
- * <tt>null</tt>.
- *
- * @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
+ * Returns the roster for the user.
+ * <p>
+ * This method will never return <code>null</code>, instead if the user has not yet logged into
+ * the server or is logged in anonymously all modifying methods of the returned roster object
+ * like {@link Roster#createEntry(String, String, String[])},
+ * {@link Roster#removeEntry(RosterEntry)} , etc. except adding or removing
+ * {@link RosterListener}s will throw an IllegalStateException.
+ *
+ * @return the user's roster.
*/
public abstract Roster getRoster();
View
68 source/org/jivesoftware/smack/Roster.java
@@ -63,7 +63,7 @@
private final List<RosterListener> rosterListeners;
private Map<String, Map<String, Presence>> presenceMap;
// The roster is marked as initialized when at least a single roster packet
- // has been recieved and processed.
+ // has been received and processed.
boolean rosterInitialized = false;
private PresencePacketListener presencePacketListener;
@@ -123,8 +123,10 @@ public static void setDefaultSubscriptionMode(SubscriptionMode subscriptionMode)
PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
presencePacketListener = new PresencePacketListener();
connection.addPacketListener(presencePacketListener, presenceFilter);
+
// Listen for connection events
- connection.addConnectionListener(new ConnectionListener() {
+ final ConnectionListener connectionListener = new AbstractConnectionListener() {
+
public void connectionClosed() {
// Changes the presence available contacts to unavailable
setOfflinePresences();
@@ -135,19 +137,22 @@ public void connectionClosedOnError(Exception e) {
setOfflinePresences();
}
- public void reconnectingIn(int seconds) {
- // Ignore
- }
-
- public void reconnectionFailed(Exception e) {
- // Ignore
- }
-
- public void reconnectionSuccessful() {
- // Ignore
- }
- });
+ };
+ // if not connected add listener after successful login
+ if(!this.connection.isConnected()) {
+ Connection.addConnectionCreationListener(new ConnectionCreationListener() {
+
+ public void connectionCreated(Connection connection) {
+ if(connection.equals(Roster.this.connection)) {
+ Roster.this.connection.addConnectionListener(connectionListener);
+ }
+
+ }
+ });
+ } else {
+ connection.addConnectionListener(connectionListener);
+ }
}
/**
@@ -184,8 +189,17 @@ public void setSubscriptionMode(SubscriptionMode subscriptionMode) {
* Reloads the entire roster from the server. This is an asynchronous operation,
* which means the method will return immediately, and the roster will be
* reloaded at a later point when the server responds to the reload request.
+ *
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void reload() {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
RosterPacket packet = new RosterPacket();
if(persistentStorage!=null){
packet.setVersion(persistentStorage.getRosterVersion());
@@ -226,11 +240,19 @@ public void removeRosterListener(RosterListener rosterListener) {
*
* @param name the name of the group.
* @return a new group.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public RosterGroup createGroup(String name) {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
if (groups.containsKey(name)) {
throw new IllegalArgumentException("Group with name " + name + " alread exists.");
}
+
RosterGroup group = new RosterGroup(name, connection);
groups.put(name, group);
return group;
@@ -245,8 +267,16 @@ public RosterGroup createGroup(String name) {
* @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
* the roster entry won't belong to a group.
* @throws XMPPException if an XMPP exception occurs.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void createEntry(String user, String name, String[] groups) throws XMPPException {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
// Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
@@ -408,8 +438,16 @@ private void insertRosterItem(RosterPacket.Item item, Collection<String> addedEn
*
* @param entry a roster entry.
* @throws XMPPException if an XMPP error occurs.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void removeEntry(RosterEntry entry) throws XMPPException {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
// Only remove the entry if it's in the entry list.
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
if (!entries.containsKey(entry.getUser())) {
@@ -530,7 +568,7 @@ public int getGroupCount() {
}
/**
- * Returns an unmodiable collections of all the roster groups.
+ * Returns an unmodifiable collections of all the roster groups.
*
* @return an iterator for all roster groups.
*/
View
35 source/org/jivesoftware/smack/XMPPConnection.java
@@ -119,7 +119,7 @@ public XMPPConnection(String serviceName, CallbackHandler callbackHandler) {
}
/**
- * Creates a new XMPP conection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
+ * Creates a new XMPP connection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@@ -135,7 +135,7 @@ public XMPPConnection(String serviceName) {
}
/**
- * Creates a new XMPP conection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
+ * Creates a new XMPP connection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@@ -210,7 +210,7 @@ public String getUser() {
* @param resource the resource.
* @throws XMPPException if an error occurs.
* @throws IllegalStateException if not connected to the server, or already logged in
- * to the serrver.
+ * to the server.
*/
public synchronized void login(String username, String password, String resource) throws XMPPException {
if (!isConnected()) {
@@ -257,7 +257,11 @@ public synchronized void login(String username, String password, String resource
useCompression();
}
- // Create the roster if it is not a reconnection.
+ // Indicate that we're now authenticated.
+ authenticated = true;
+ anonymous = false;
+
+ // Create the roster if it is not a reconnection or roster already created by getRoster()
if (this.roster == null) {
if(rosterStorage==null){
this.roster = new Roster(this);
@@ -275,11 +279,7 @@ public synchronized void login(String username, String password, String resource
packetWriter.sendPacket(new Presence(Presence.Type.available));
}
- // Indicate that we're now authenticated.
- authenticated = true;
- anonymous = false;
-
- // Stores the autentication for future reconnection
+ // Stores the authentication for future reconnection
config.setLoginInfo(username, password, resource);
// If debugging is enabled, change the the debug window title to include the
@@ -299,7 +299,7 @@ public synchronized void login(String username, String password, String resource
*
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
* @throws IllegalStateException if not connected to the server, or already logged in
- * to the serrver.
+ * to the server.
*/
public synchronized void loginAnonymously() throws XMPPException {
if (!isConnected()) {
@@ -329,9 +329,6 @@ public synchronized void loginAnonymously() throws XMPPException {
useCompression();
}
- // Anonymous users can't have a roster.
- roster = null;
-
// Set presence to online.
packetWriter.sendPacket(new Presence(Presence.Type.available));
@@ -349,8 +346,16 @@ public synchronized void loginAnonymously() throws XMPPException {
}
public Roster getRoster() {
- if (roster == null) {
- return null;
+ // synchronize against login()
+ synchronized(this) {
+ // if connection is authenticated the roster is already set by login()
+ // or a previous call to getRoster()
+ if (!isAuthenticated() || isAnonymous()) {
+ if (roster == null) {
+ roster = new Roster(this);
+ }
+ return roster;
+ }
}
// If this is the first time the user has asked for the roster after calling
// login, we want to wait for the server to send back the user's roster. This
View
92 test-unit/org/jivesoftware/smack/RosterOfflineTest.java
@@ -0,0 +1,92 @@
+package org.jivesoftware.smack;
+
+import static org.junit.Assert.*;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.jivesoftware.smack.Roster.SubscriptionMode;
+import org.jivesoftware.smack.packet.Presence;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the behavior of the roster if the connection is not authenticated yet.
+ *
+ * @author Henning Staib
+ */
+public class RosterOfflineTest {
+
+ Connection connection;
+
+ Roster roster;
+
+ @Before
+ public void setup() {
+ this.connection = new XMPPConnection("localhost");
+ assertFalse(connection.isConnected());
+
+ roster = connection.getRoster();
+ assertNotNull(roster);
+ }
+
+ @Test
+ public void shouldThrowNoExceptionOnGetterMethods() {
+ // all getter methods should work
+ assertFalse(roster.contains("test"));
+
+ Collection<RosterEntry> entries = roster.getEntries();
+ assertTrue(entries.size() == 0);
+
+ assertNull(roster.getEntry("test"));
+
+ assertEquals(0, roster.getEntryCount());
+
+ assertNull(roster.getGroup("test"));
+
+ assertEquals(0, roster.getGroupCount());
+
+ Collection<RosterGroup> groups = roster.getGroups();
+ assertEquals(0, groups.size());
+
+ Presence presence = roster.getPresence("test");
+ assertEquals(Presence.Type.unavailable, presence.getType());
+
+ Presence presenceResource = roster.getPresenceResource("test");
+ assertEquals(Presence.Type.unavailable, presenceResource.getType());
+
+ Iterator<Presence> iterator = roster.getPresences("test");
+ assertTrue(iterator.hasNext());
+ assertEquals(Presence.Type.unavailable, iterator.next().getType());
+ assertFalse(iterator.hasNext());
+
+ assertEquals(0, roster.getUnfiledEntries().size());
+
+ assertEquals(0, roster.getUnfiledEntryCount());
+
+ roster.setSubscriptionMode(SubscriptionMode.accept_all);
+ assertEquals(SubscriptionMode.accept_all, roster.getSubscriptionMode());
+
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnCreateEntry() throws Exception {
+ roster.createEntry("test", "test", null);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnCreateGroup() throws Exception {
+ roster.createGroup("test");
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnReload() throws Exception {
+ roster.reload();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionRemoveEntry() throws Exception {
+ roster.removeEntry(null);
+ }
+
+}
View
34 test/org/jivesoftware/smack/RosterInitializedBeforeConnectTest.java
@@ -0,0 +1,34 @@
+package org.jivesoftware.smack;
+
+/**
+ * Run all tests defined in RosterTest but initialize the roster before connection is logged in and
+ * authenticated.
+ *
+ * @author Henning Staib
+ */
+public class RosterInitializedBeforeConnectTest extends RosterSmackTest {
+
+ public RosterInitializedBeforeConnectTest(String name) {
+ super(name);
+ }
+
+ protected boolean createOfflineConnections() {
+ return true;
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // initialize all rosters before login
+ for (int i = 0; i < getMaxConnections(); i++) {
+ XMPPConnection connection = getConnection(i);
+ assertFalse(connection.isConnected());
+
+ Roster roster = connection.getRoster();
+ assertNotNull(roster);
+
+ connectAndLogin(i);
+ }
+ }
+
+}
View
193 test/org/jivesoftware/smack/RosterListenerTest.java
@@ -0,0 +1,193 @@
+package org.jivesoftware.smack;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.test.SmackTestCase;
+
+/**
+ * Test cases for adding the {@link RosterListener} in different connection states.
+ *
+ * @author Henning Staib
+ */
+public class RosterListenerTest extends SmackTestCase {
+
+ public RosterListenerTest(String arg0) {
+ super(arg0);
+ }
+
+ public void testAddingRosterListenerBeforeConnect() throws Exception {
+ int inviterIndex = 0;
+ int inviteeIndex = 1;
+ XMPPConnection inviterConnection = getConnection(inviterIndex);
+ connectAndLogin(inviterIndex);
+
+ assertTrue("Inviter is not online", inviterConnection.isConnected());
+
+ Roster inviterRoster = inviterConnection.getRoster();
+
+ // add user1 to roster to create roster events stored at XMPP server
+ inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
+
+ Thread.sleep(500); // wait for XMPP server
+
+ XMPPConnection inviteeConnection = getConnection(inviteeIndex);
+ assertFalse("Invitee is already online", inviteeConnection.isConnected());
+
+ // collector for added entries
+ final List<String> addedEntries = new ArrayList<String>();
+
+ // register roster listener before login
+ Roster inviteeRoster = inviteeConnection.getRoster();
+ inviteeRoster.addRosterListener(new RosterListener() {
+
+ public void presenceChanged(Presence presence) {
+ // ignore
+ }
+
+ public void entriesUpdated(Collection<String> addresses) {
+ // ignore
+ }
+
+ public void entriesDeleted(Collection<String> addresses) {
+ // ignore
+ }
+
+ public void entriesAdded(Collection<String> addresses) {
+ addedEntries.addAll(addresses);
+ }
+ });
+
+ // connect after adding the listener
+ connectAndLogin(inviteeIndex);
+
+ Thread.sleep(500); // wait for packets to be processed
+
+ assertNotNull("inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
+
+ assertTrue("got no event for adding inviter",
+ addedEntries.contains(getBareJID(inviterIndex)));
+
+ }
+
+ public void testAddingRosterListenerAfterConnect() throws Exception {
+ int inviterIndex = 0;
+ int inviteeIndex = 1;
+ XMPPConnection inviterConnection = getConnection(inviterIndex);
+ connectAndLogin(inviterIndex);
+ assertTrue("Inviter is not online", inviterConnection.isConnected());
+
+ Roster inviterRoster = inviterConnection.getRoster();
+
+ // add user1 to roster to create roster events stored at XMPP server
+ inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
+
+ Thread.sleep(500); // wait for XMPP server
+
+ XMPPConnection inviteeConnection = getConnection(inviteeIndex);
+ connectAndLogin(inviteeIndex);
+ assertTrue("Invitee is not online", inviteeConnection.isConnected());
+
+ // collector for added entries
+ final List<String> addedEntries = new ArrayList<String>();
+
+ // wait to simulate concurrency before adding listener
+ Thread.sleep(200);
+
+ // register roster listener after login
+ Roster inviteeRoster = inviteeConnection.getRoster();
+ inviteeRoster.addRosterListener(new RosterListener() {
+
+ public void presenceChanged(Presence presence) {
+ // ignore
+ }
+
+ public void entriesUpdated(Collection<String> addresses) {
+ // ignore
+ }
+
+ public void entriesDeleted(Collection<String> addresses) {
+ // ignore
+ }
+
+ public void entriesAdded(Collection<String> addresses) {
+ addedEntries.addAll(addresses);
+ }
+ });
+
+ Thread.sleep(500); // wait for packets to be processed
+
+ assertNotNull("Inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
+
+ assertFalse("got event for adding inviter", addedEntries.contains(getBareJID(inviterIndex)));
+
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ cleanUpRoster();
+ super.tearDown();
+ }
+
+ protected int getMaxConnections() {
+ return 2;
+ }
+
+ protected boolean createOfflineConnections() {
+ return true;
+ }
+
+ /**
+ * Clean up all the entries in the roster
+ */
+ private void cleanUpRoster() {
+ for (int i = 0; i < getMaxConnections(); i++) {
+ // Delete all the entries from the roster
+ Roster roster = getConnection(i).getRoster();
+ for (RosterEntry entry : roster.getEntries()) {
+ try {
+ roster.removeEntry(entry);
+ Thread.sleep(100);
+ }
+ catch (XMPPException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ catch (InterruptedException e) {
+ // ignore
+ }
+ }
+
+ try {
+ Thread.sleep(700);
+ }
+ catch (InterruptedException e) {
+ fail(e.getMessage());
+ }
+ }
+ // Wait up to 6 seconds to receive roster removal notifications
+ long initial = System.currentTimeMillis();
+ while (System.currentTimeMillis() - initial < 6000
+ && (getConnection(0).getRoster().getEntryCount() != 0 || getConnection(1).getRoster().getEntryCount() != 0)) {
+ try {
+ Thread.sleep(100);
+ }
+ catch (InterruptedException e) {
+ }
+ }
+
+ assertEquals("Wrong number of entries in connection 0", 0,
+ getConnection(0).getRoster().getEntryCount());
+ assertEquals("Wrong number of groups in connection 0", 0,
+ getConnection(0).getRoster().getGroupCount());
+
+ assertEquals("Wrong number of entries in connection 1", 0,
+ getConnection(1).getRoster().getEntryCount());
+ assertEquals("Wrong number of groups in connection 1", 0,
+ getConnection(1).getRoster().getGroupCount());
+
+ }
+
+}
View
14 test/org/jivesoftware/smack/RosterSmackTest.java
@@ -446,18 +446,12 @@ public void testRenameRosterGroup() {
}
assertNull("The group Amigos still exists", roster.getGroup("Amigos"));
- assertNotNull("The group with no name does not exist", roster.getGroup(""));
+ assertNull("The group with no name does exist", roster.getGroup(""));
+ assertEquals("There are still groups in the roster", 0, roster.getGroupCount());
assertEquals(
- "Wrong number of entries in the group with no name",
+ "Wrong number of unfiled entries",
2,
- roster.getGroup("").getEntryCount());
-
- /*assertEquals("There are still groups in the roster", 0, roster.getGroupCount());
- assertEquals(
- "Wrong number of entries in the group \"\" ",
- 2,
- roster.getUnfiledEntryCount());*/
-
+ roster.getUnfiledEntryCount());
Thread.sleep(200);
}
View
97 test/org/jivesoftware/smack/test/SmackTestCase.java
@@ -24,8 +24,6 @@
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
-import org.jivesoftware.smack.packet.XMPPError;
-import org.jivesoftware.smack.packet.XMPPError.Type;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
@@ -98,6 +96,23 @@ protected SocketFactory getSocketFactory() {
}
/**
+ * Returns <code>false</code> if the connections initialized by the test case will be
+ * automatically connected to the XMPP server.
+ * Returns <code>true</code> if the connections initialized by the test case will
+ * NOT be connected to the XMPP server. To connect the connections invoke
+ * {@link #connectAndLogin(int)}.
+ * <p>
+ * Connections are connected by default.
+ * Overwrite this method if the test case needs unconnected connections.
+ *
+ * @return <code>true</code> if connections should NOT be connected automatically,
+ * <code>false</code> if connections should be connected automatically.
+ */
+ protected boolean createOfflineConnections() {
+ return false;
+ }
+
+ /**
* Returns the XMPPConnection located at the requested position. Each test case holds a
* pool of connections which is initialized while setting up the test case. The maximum
* number of connections is controlled by the message {@link #getMaxConnections()} which
@@ -210,7 +225,8 @@ protected void setUp() throws Exception {
// Connect to the server
for (int i = 0; i < getMaxConnections(); i++) {
connections[i] = createConnection();
- connections[i].connect();
+ if (!createOfflineConnections())
+ connections[i].connect();
}
// Use the host name that the server reports. This is a good idea in most
// cases, but could fail if the user set a hostname in their XMPP server
@@ -218,41 +234,37 @@ protected void setUp() throws Exception {
host = connections[0].getHost();
serviceName = connections[0].getServiceName();
- for (int i = 0; i < getMaxConnections(); i++) {
- String password = usernamePrefix + (i+1);
- String currentUser = password;
-
- if (passwordPrefix != null)
- password = (samePassword ? passwordPrefix : passwordPrefix + (i+1));
-
- try
- {
- getConnection(i).login(currentUser, password, "Smack");
- }
- catch (XMPPException e)
- {
- e.printStackTrace();
-
- // Create the test accounts
- if (!getConnection(0).getAccountManager().supportsAccountCreation())
- fail("Server does not support account creation");
-
- // Create the account and try logging in again as the
- // same user.
- try
- {
- createAccount(i, currentUser, password);
- }
- catch (Exception e1)
- {
- e1.printStackTrace();
- fail("Could not create user: " + currentUser);
- }
- i--;
- }
+ if (!createOfflineConnections()) {
+ for (int i = 0; i < getMaxConnections(); i++) {
+ String password = usernamePrefix + (i+1);
+ String currentUser = password;
+
+ if (passwordPrefix != null)
+ password = (samePassword ? passwordPrefix : passwordPrefix + (i+1));
+
+ try {
+ getConnection(i).login(currentUser, password, "Smack");
+ } catch (XMPPException e) {
+ e.printStackTrace();
+
+ // Create the test accounts
+ if (!getConnection(0).getAccountManager().supportsAccountCreation())
+ fail("Server does not support account creation");
+
+ // Create the account and try logging in again as the
+ // same user.
+ try {
+ createAccount(i, currentUser, password);
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ fail("Could not create user: " + currentUser);
+ }
+ i--;
+ }
+ }
+ // Let the server process the available presences
+ Thread.sleep(150);
}
- // Let the server process the available presences
- Thread.sleep(150);
}
catch (Exception e) {
e.printStackTrace();
@@ -262,16 +274,21 @@ protected void setUp() throws Exception {
protected void connectAndLogin(int connectionIndex) throws XMPPException
{
- String password = usernamePrefix + connectionIndex;
+ String password = usernamePrefix + (connectionIndex + 1);
if (passwordPrefix != null)
- password = (samePassword ? passwordPrefix : passwordPrefix + connectionIndex);
+ password = (samePassword ? passwordPrefix : passwordPrefix + (connectionIndex + 1));
XMPPConnection con = getConnection(connectionIndex);
if (!con.isConnected())
con.connect();
- con.login(usernamePrefix + connectionIndex, password, "Smack");
+ try {
+ con.login(usernamePrefix + (connectionIndex + 1), password, "Smack");
+ } catch (XMPPException e) {
+ createAccount(connectionIndex, usernamePrefix + (connectionIndex + 1), password);
+ con.login(usernamePrefix + (connectionIndex + 1), password, "Smack");
+ }
}
protected void disconnect(int connectionIndex) throws XMPPException
Please sign in to comment.
Something went wrong with that request. Please try again.