1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 22:32:06 +01:00

Add invokePacketCollectors, remove duplicate code

Add javadoc about why we use CocurrentLinkedQueue for collectors.
This commit is contained in:
Florian Schmaus 2014-10-10 02:41:37 +02:00
parent 15d59299a2
commit c3528d082e
2 changed files with 23 additions and 18 deletions

View file

@ -102,8 +102,15 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
/** /**
* A collection of PacketCollectors which collects packets for a specified filter * A collection of PacketCollectors which collects packets for a specified filter
* and perform blocking and polling operations on the result queue. * and perform blocking and polling operations on the result queue.
* <p>
* We use a ConcurrentLinkedQueue here, because its Iterator is weakly
* consistent and we want {@link #invokePacketCollectors(Packet)} for-each
* loop to be lock free. As drawback, removing a PacketCollector is O(n).
* The alternative would be a synchronized HashSet, but this would mean a
* synchronized block around every usage of <code>collectors</code>.
* </p>
*/ */
protected final Collection<PacketCollector> collectors = new ConcurrentLinkedQueue<PacketCollector>(); private final Collection<PacketCollector> collectors = new ConcurrentLinkedQueue<PacketCollector>();
/** /**
* List of PacketListeners that will be notified when a new packet was received. * List of PacketListeners that will be notified when a new packet was received.
@ -658,15 +665,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
collectors.remove(collector); collectors.remove(collector);
} }
/**
* Get the collection of all packet collectors for this connection.
*
* @return a collection of packet collectors for this connection.
*/
protected Collection<PacketCollector> getPacketCollectors() {
return collectors;
}
@Override @Override
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) { public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
if (packetListener == null) { if (packetListener == null) {
@ -804,6 +802,19 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
packetReplyTimeout = timeout; packetReplyTimeout = timeout;
} }
/**
* Invoke {@link PacketCollector#processPacket(Packet)} for every
* PacketCollector with the given packet.
*
* @param packet the packet to notify the PacketCollectors about.
*/
protected void invokePacketCollectors(Packet packet) {
// Loop through all collectors and notify the appropriate ones.
for (PacketCollector collector: collectors) {
collector.processPacket(packet);
}
}
/** /**
* Processes a packet after it's been fully parsed by looping through the installed * Processes a packet after it's been fully parsed by looping through the installed
* packet collectors and listeners and letting them examine the packet to see if * packet collectors and listeners and letting them examine the packet to see if
@ -816,10 +827,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return; return;
} }
// Loop through all collectors and notify the appropriate ones. invokePacketCollectors(packet);
for (PacketCollector collector: getPacketCollectors()) {
collector.processPacket(packet);
}
// Deliver the incoming packet to listeners. // Deliver the incoming packet to listeners.
executorService.submit(new ListenerNotification(packet)); executorService.submit(new ListenerNotification(packet));

View file

@ -240,10 +240,7 @@ public class DummyConnection extends AbstractXMPPConnection {
return; return;
} }
// Loop through all collectors and notify the appropriate ones. invokePacketCollectors(packet);
for (PacketCollector collector: getPacketCollectors()) {
collector.processPacket(packet);
}
if (SmackConfiguration.DEBUG_ENABLED) { if (SmackConfiguration.DEBUG_ENABLED) {
System.out.println("[RECV]: " + packet.toXML()); System.out.println("[RECV]: " + packet.toXML());