MUC: Establish callbacks before sending the presence

This commit is contained in:
Florian Schmaus 2014-10-14 09:41:48 +02:00
parent ce203e76b5
commit b5328d38fa
1 changed files with 27 additions and 12 deletions

View File

@ -286,15 +286,8 @@ public class MultiUserChat {
// Wait for a presence packet back from the server. // Wait for a presence packet back from the server.
PacketFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(room + "/" PacketFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(room + "/"
+ nickname), new PacketTypeFilter(Presence.class)); + nickname), new PacketTypeFilter(Presence.class));
PacketCollector response = null;
response = connection.createPacketCollectorAndSend(responseFilter, joinPresence); // Setup the messageListeners and presenceListeners *before* the join presence is send.
// Wait up to a certain number of seconds for a reply.
Presence presence = response.nextResultOrThrow(timeout);
this.nickname = nickname;
joined = true;
// Setup the messageListeners and presenceListeners
connection.addPacketListener(messageListener, fromRoomGroupchatFilter); connection.addPacketListener(messageListener, fromRoomGroupchatFilter);
connection.addPacketListener(presenceListener, new AndFilter(fromRoomFilter, connection.addPacketListener(presenceListener, new AndFilter(fromRoomFilter,
PacketTypeFilter.PRESENCE)); PacketTypeFilter.PRESENCE));
@ -305,6 +298,20 @@ public class MultiUserChat {
connection.addPacketInterceptor(presenceInterceptor, new AndFilter(new ToFilter(room), connection.addPacketInterceptor(presenceInterceptor, new AndFilter(new ToFilter(room),
PacketTypeFilter.PRESENCE)); PacketTypeFilter.PRESENCE));
messageCollector = connection.createPacketCollector(fromRoomGroupchatFilter); messageCollector = connection.createPacketCollector(fromRoomGroupchatFilter);
Presence presence;
try {
presence = connection.createPacketCollectorAndSend(responseFilter, joinPresence).nextResultOrThrow(timeout);
}
catch (NoResponseException | XMPPErrorException e) {
// Ensure that all callbacks are removed if there is an exception
removeConnectionCallbacks();
throw e;
}
this.nickname = nickname;
joined = true;
// Update the list of joined rooms // Update the list of joined rooms
multiUserChatManager.addJoinedRoom(room); multiUserChatManager.addJoinedRoom(room);
return presence; return presence;
@ -1667,11 +1674,10 @@ public class MultiUserChat {
} }
/** /**
* Notification message that the user has left the room. * Remove the connection callbacks (PacketListener, PacketInterceptor, PacketCollector) used by this MUC from the
* connection.
*/ */
private synchronized void userHasLeft() { private void removeConnectionCallbacks() {
// Update the list of joined rooms
multiUserChatManager.removeJoinedRoom(room);
connection.removePacketListener(messageListener); connection.removePacketListener(messageListener);
connection.removePacketListener(presenceListener); connection.removePacketListener(presenceListener);
connection.removePacketListener(declinesListener); connection.removePacketListener(declinesListener);
@ -1682,6 +1688,15 @@ public class MultiUserChat {
} }
} }
/**
* Remove all callbacks and resources necessary when the user has left the room for some reason.
*/
private synchronized void userHasLeft() {
// Update the list of joined rooms
multiUserChatManager.removeJoinedRoom(room);
removeConnectionCallbacks();
}
/** /**
* Adds a listener that will be notified of changes in your status in the room * Adds a listener that will be notified of changes in your status in the room
* such as the user being kicked, banned, or granted admin permissions. * such as the user being kicked, banned, or granted admin permissions.