1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Use standard stanza listeners in MultiUserChat

Those, relatively new, listeners guarantee that the individual
listeners are not invoked in concurrently while preserving the
order. Exactly what MultiUserChat previously did with AsyncButOrdered,
which is now no longer needed and hence can be removed.
This commit is contained in:
Florian Schmaus 2019-11-06 18:44:07 +01:00
parent eb4c2c5572
commit a7a298c5d8

View file

@ -27,7 +27,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.AsyncButOrdered;
import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PresenceListener; import org.jivesoftware.smack.PresenceListener;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
@ -144,8 +143,6 @@ public class MultiUserChat {
private final StanzaListener presenceListener; private final StanzaListener presenceListener;
private final StanzaListener subjectListener; private final StanzaListener subjectListener;
private static final AsyncButOrdered<MultiUserChat> asyncButOrdered = new AsyncButOrdered<>();
private static final StanzaFilter DECLINE_FILTER = new AndFilter(MessageTypeFilter.NORMAL, private static final StanzaFilter DECLINE_FILTER = new AndFilter(MessageTypeFilter.NORMAL,
new StanzaExtensionFilter(MUCUser.ELEMENT, MUCUser.NAMESPACE)); new StanzaExtensionFilter(MUCUser.ELEMENT, MUCUser.NAMESPACE));
private final StanzaListener declinesListener; private final StanzaListener declinesListener;
@ -168,14 +165,9 @@ public class MultiUserChat {
public void processStanza(Stanza packet) throws NotConnectedException { public void processStanza(Stanza packet) throws NotConnectedException {
final Message message = (Message) packet; final Message message = (Message) packet;
asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() { for (MessageListener listener : messageListeners) {
@Override
public void run() {
for (MessageListener listener : messageListeners) {
listener.processMessage(message); listener.processMessage(message);
} }
}
});
} }
}; };
@ -188,15 +180,10 @@ public class MultiUserChat {
// Update the room subject // Update the room subject
subject = msg.getSubject(); subject = msg.getSubject();
asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() { // Fire event for subject updated listeners
@Override for (SubjectUpdatedListener listener : subjectUpdatedListeners) {
public void run() { listener.subjectUpdated(msg.getSubject(), from);
// Fire event for subject updated listeners }
for (SubjectUpdatedListener listener : subjectUpdatedListeners) {
listener.subjectUpdated(msg.getSubject(), from);
}
}
});
} }
}; };
@ -212,66 +199,61 @@ public class MultiUserChat {
final EntityFullJid myRoomJID = myRoomJid; final EntityFullJid myRoomJID = myRoomJid;
final boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); final boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
asyncButOrdered.performAsyncButOrdered(MultiUserChat.this, new Runnable() { switch (presence.getType()) {
@Override case available:
public void run() { Presence oldPresence = occupantsMap.put(from, presence);
switch (presence.getType()) { if (oldPresence != null) {
case available: // Get the previous occupant's affiliation & role
Presence oldPresence = occupantsMap.put(from, presence); MUCUser mucExtension = MUCUser.from(oldPresence);
if (oldPresence != null) { MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation();
// Get the previous occupant's affiliation & role MUCRole oldRole = mucExtension.getItem().getRole();
MUCUser mucExtension = MUCUser.from(oldPresence); // Get the new occupant's affiliation & role
MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation(); mucExtension = MUCUser.from(packet);
MUCRole oldRole = mucExtension.getItem().getRole(); MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation();
// Get the new occupant's affiliation & role MUCRole newRole = mucExtension.getItem().getRole();
mucExtension = MUCUser.from(packet); // Fire role modification events
MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation(); checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
MUCRole newRole = mucExtension.getItem().getRole(); // Fire affiliation modification events
// Fire role modification events checkAffiliationModifications(
checkRoleModifications(oldRole, newRole, isUserStatusModification, from); oldAffiliation,
// Fire affiliation modification events newAffiliation,
checkAffiliationModifications( isUserStatusModification,
oldAffiliation, from);
newAffiliation, }
isUserStatusModification, else {
from); // A new occupant has joined the room
if (!isUserStatusModification) {
for (ParticipantStatusListener listener : participantStatusListeners) {
listener.joined(from);
} }
else {
// A new occupant has joined the room
if (!isUserStatusModification) {
for (ParticipantStatusListener listener : participantStatusListeners) {
listener.joined(from);
}
}
}
break;
case unavailable:
occupantsMap.remove(from);
MUCUser mucUser = MUCUser.from(packet);
if (mucUser != null && mucUser.hasStatus()) {
// Fire events according to the received presence code
checkPresenceCode(
mucUser.getStatus(),
presence.getFrom().equals(myRoomJID),
mucUser,
from);
} else {
// An occupant has left the room
if (!isUserStatusModification) {
for (ParticipantStatusListener listener : participantStatusListeners) {
listener.left(from);
}
}
}
break;
default:
break;
}
for (PresenceListener listener : presenceListeners) {
listener.processPresence(presence);
} }
} }
}); break;
case unavailable:
occupantsMap.remove(from);
MUCUser mucUser = MUCUser.from(packet);
if (mucUser != null && mucUser.hasStatus()) {
// Fire events according to the received presence code
checkPresenceCode(
mucUser.getStatus(),
presence.getFrom().equals(myRoomJID),
mucUser,
from);
} else {
// An occupant has left the room
if (!isUserStatusModification) {
for (ParticipantStatusListener listener : participantStatusListeners) {
listener.left(from);
}
}
}
break;
default:
break;
}
for (PresenceListener listener : presenceListeners) {
listener.processPresence(presence);
}
} }
}; };
@ -341,13 +323,13 @@ public class MultiUserChat {
Presence joinPresence = conf.getJoinPresence(this); Presence joinPresence = conf.getJoinPresence(this);
// Setup the messageListeners and presenceListeners *before* the join presence is send. // Setup the messageListeners and presenceListeners *before* the join presence is send.
connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter); connection.addStanzaListener(messageListener, fromRoomGroupchatFilter);
StanzaFilter presenceFromRoomFilter = new AndFilter(fromRoomFilter, StanzaFilter presenceFromRoomFilter = new AndFilter(fromRoomFilter,
StanzaTypeFilter.PRESENCE, StanzaTypeFilter.PRESENCE,
PossibleFromTypeFilter.ENTITY_FULL_JID); PossibleFromTypeFilter.ENTITY_FULL_JID);
connection.addSyncStanzaListener(presenceListener, presenceFromRoomFilter); connection.addStanzaListener(presenceListener, presenceFromRoomFilter);
// @formatter:off // @formatter:off
connection.addSyncStanzaListener(subjectListener, connection.addStanzaListener(subjectListener,
new AndFilter(fromRoomFilter, new AndFilter(fromRoomFilter,
MessageWithSubjectFilter.INSTANCE, MessageWithSubjectFilter.INSTANCE,
new NotFilter(MessageTypeFilter.ERROR), new NotFilter(MessageTypeFilter.ERROR),
@ -357,7 +339,7 @@ public class MultiUserChat {
new NotFilter(MessageWithThreadFilter.INSTANCE)) new NotFilter(MessageWithThreadFilter.INSTANCE))
); );
// @formatter:on // @formatter:on
connection.addSyncStanzaListener(declinesListener, new AndFilter(fromRoomFilter, DECLINE_FILTER)); connection.addStanzaListener(declinesListener, new AndFilter(fromRoomFilter, DECLINE_FILTER));
connection.addStanzaSendingListener(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room), connection.addStanzaSendingListener(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room),
StanzaTypeFilter.PRESENCE)); StanzaTypeFilter.PRESENCE));
messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter); messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter);
@ -2118,10 +2100,10 @@ public class MultiUserChat {
* connection. * connection.
*/ */
private void removeConnectionCallbacks() { private void removeConnectionCallbacks() {
connection.removeSyncStanzaListener(messageListener); connection.removeStanzaListener(messageListener);
connection.removeSyncStanzaListener(presenceListener); connection.removeStanzaListener(presenceListener);
connection.removeSyncStanzaListener(subjectListener); connection.removeStanzaListener(subjectListener);
connection.removeSyncStanzaListener(declinesListener); connection.removeStanzaListener(declinesListener);
connection.removeStanzaSendingListener(presenceInterceptor); connection.removeStanzaSendingListener(presenceInterceptor);
if (messageCollector != null) { if (messageCollector != null) {
messageCollector.cancel(); messageCollector.cancel();