1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 15:32:06 +01:00

Improve MUC Presence interceptors

Use PresenceListener as intercepting callback interface.

Remove the for-each loops in MultiUserChat by using the interception
facilities of the XMPPConnection.

Add ToFilter.
This commit is contained in:
Florian Schmaus 2014-10-12 19:34:14 +02:00
parent 55d7c57a4f
commit 8ce474b0df
2 changed files with 58 additions and 18 deletions

View file

@ -0,0 +1,41 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.filter;
import java.util.Locale;
import org.jivesoftware.smack.packet.Packet;
public class ToFilter implements PacketFilter {
private final String to;
public ToFilter(String to) {
this.to = to.toLowerCase(Locale.US);
}
@Override
public boolean accept(Packet packet) {
String packetTo = packet.getTo();
if (packetTo == null) {
return false;
}
packetTo = packetTo.toLowerCase(Locale.US);
return packetTo.equals(to);
}
}

View file

@ -55,6 +55,7 @@ import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter; import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.filter.ToFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
@ -107,9 +108,10 @@ public class MultiUserChat {
new ArrayList<ParticipantStatusListener>(); new ArrayList<ParticipantStatusListener>();
private final Set<MessageListener> messageListeners = new CopyOnWriteArraySet<MessageListener>(); private final Set<MessageListener> messageListeners = new CopyOnWriteArraySet<MessageListener>();
private final Set<PresenceListener> presenceListeners = new CopyOnWriteArraySet<PresenceListener>(); private final Set<PresenceListener> presenceListeners = new CopyOnWriteArraySet<PresenceListener>();
private final Set<PacketListener> presenceInterceptors = new CopyOnWriteArraySet<PacketListener>(); private final Set<PresenceListener> presenceInterceptors = new CopyOnWriteArraySet<PresenceListener>();
private final ConnectionDetachedPacketCollector<Message> messageCollector = new ConnectionDetachedPacketCollector<Message>(); private final ConnectionDetachedPacketCollector<Message> messageCollector = new ConnectionDetachedPacketCollector<Message>();
private final PacketListener presenceInterceptor;
private final PacketFilter fromRoomFilter; private final PacketFilter fromRoomFilter;
private final PacketListener messageListener; private final PacketListener messageListener;
private final PacketListener presenceListener; private final PacketListener presenceListener;
@ -295,6 +297,16 @@ public class MultiUserChat {
roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection); roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection);
roomListenerMultiplexor.addRoom(room, packetMultiplexor); roomListenerMultiplexor.addRoom(room, packetMultiplexor);
presenceInterceptor = new PacketListener() {
@Override
public void processPacket(Packet packet) {
Presence presence = (Presence) packet;
for (PresenceListener interceptor : presenceInterceptors) {
interceptor.processPresence(presence);
}
}
};
} }
/** /**
@ -451,10 +463,6 @@ public class MultiUserChat {
mucInitialPresence.setHistory(history.getMUCHistory()); mucInitialPresence.setHistory(history.getMUCHistory());
} }
joinPresence.addExtension(mucInitialPresence); joinPresence.addExtension(mucInitialPresence);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// 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 + "/"
@ -474,6 +482,8 @@ public class MultiUserChat {
MessageTypeFilter.GROUPCHAT)); MessageTypeFilter.GROUPCHAT));
connection.addPacketListener(presenceListener, new AndFilter(fromRoomFilter, connection.addPacketListener(presenceListener, new AndFilter(fromRoomFilter,
PacketTypeFilter.PRESENCE)); PacketTypeFilter.PRESENCE));
connection.addPacketInterceptor(presenceInterceptor, new AndFilter(new ToFilter(room),
PacketTypeFilter.PRESENCE));
// Update the list of joined rooms through this connection // Update the list of joined rooms through this connection
List<String> rooms = joinedRooms.get(connection); List<String> rooms = joinedRooms.get(connection);
if (rooms == null) { if (rooms == null) {
@ -658,10 +668,6 @@ public class MultiUserChat {
// field is in the form "roomName@service/nickname" // field is in the form "roomName@service/nickname"
Presence leavePresence = new Presence(Presence.Type.unavailable); Presence leavePresence = new Presence(Presence.Type.unavailable);
leavePresence.setTo(room + "/" + nickname); leavePresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(leavePresence);
}
connection.sendPacket(leavePresence); connection.sendPacket(leavePresence);
// Reset occupant information. // Reset occupant information.
occupantsMap.clear(); occupantsMap.clear();
@ -973,7 +979,7 @@ public class MultiUserChat {
* *
* @param presenceInterceptor the new packet interceptor that will intercept presence packets. * @param presenceInterceptor the new packet interceptor that will intercept presence packets.
*/ */
public void addPresenceInterceptor(PacketListener presenceInterceptor) { public void addPresenceInterceptor(PresenceListener presenceInterceptor) {
presenceInterceptors.add(presenceInterceptor); presenceInterceptors.add(presenceInterceptor);
} }
@ -1068,10 +1074,6 @@ public class MultiUserChat {
// We don't have to signal the MUC support again // We don't have to signal the MUC support again
Presence joinPresence = new Presence(Presence.Type.available); Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname); joinPresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// Wait for a presence packet back from the server. // Wait for a presence packet back from the server.
PacketFilter responseFilter = PacketFilter responseFilter =
@ -1113,10 +1115,6 @@ public class MultiUserChat {
joinPresence.setStatus(status); joinPresence.setStatus(status);
joinPresence.setMode(mode); joinPresence.setMode(mode);
joinPresence.setTo(room + "/" + nickname); joinPresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketListener packetInterceptor : presenceInterceptors) {
packetInterceptor.processPacket(joinPresence);
}
// Send join packet. // Send join packet.
connection.sendPacket(joinPresence); connection.sendPacket(joinPresence);
@ -1925,6 +1923,7 @@ public class MultiUserChat {
} }
connection.removePacketListener(messageListener); connection.removePacketListener(messageListener);
connection.removePacketListener(presenceListener); connection.removePacketListener(presenceListener);
connection.removePacketInterceptor(presenceInterceptor);
rooms.remove(room); rooms.remove(room);
cleanup(); cleanup();
} }