mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-28 07:07:58 +01:00
Listeners are now removed from PacketReader. SMACK-165
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@4883 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
469ff4d487
commit
157a1e9659
2 changed files with 50 additions and 7 deletions
|
@ -50,7 +50,7 @@ class PacketReader {
|
|||
private boolean done = false;
|
||||
private final VolatileMemberCollection<PacketCollector> collectors =
|
||||
new VolatileMemberCollection<PacketCollector>(50);
|
||||
private final VolatileMemberCollection listeners = new VolatileMemberCollection(50);
|
||||
protected final VolatileMemberCollection listeners = new VolatileMemberCollection(50);
|
||||
protected final List<ConnectionListener> connectionListeners =
|
||||
new ArrayList<ConnectionListener>();
|
||||
|
||||
|
@ -814,10 +814,13 @@ class PacketReader {
|
|||
|
||||
public void remove(E member) {
|
||||
synchronized (mutex) {
|
||||
int index = collectors.lastIndexOf(member);
|
||||
if (index >= 0) {
|
||||
collectors.set(index, null);
|
||||
nullArray[++nullIndex] = index;
|
||||
for (int i = collectors.size()-1; i >= 0; i--) {
|
||||
E element = collectors.get(i);
|
||||
if (element != null && element.equals(member)) {
|
||||
collectors.set(i, null);
|
||||
nullArray[++nullIndex] = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -860,6 +863,21 @@ class PacketReader {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this collection.
|
||||
*
|
||||
* @return the number of elements in this collection
|
||||
*/
|
||||
public int size() {
|
||||
int size = 0;
|
||||
for (E element : collectors) {
|
||||
if (element != null) {
|
||||
size++;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,8 +52,10 @@
|
|||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
import org.jivesoftware.smack.packet.*;
|
||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.MockPacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.test.SmackTestCase;
|
||||
|
||||
|
||||
|
@ -96,6 +98,29 @@ public class PacketReaderTest extends SmackTestCase {
|
|||
collector.cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that PacketReader adds new listeners and also removes them correctly.
|
||||
*/
|
||||
public void testRemoveListener() {
|
||||
PacketListener listener = new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
//Ignore
|
||||
}
|
||||
};
|
||||
// Keep number of current listeners
|
||||
int listenersSize = getConnection(0).packetReader.listeners.size();
|
||||
// Add a new listener
|
||||
getConnection(0).addPacketListener(listener, new MockPacketFilter(true));
|
||||
// Check that the listener was added
|
||||
assertEquals("Listener was not added", listenersSize + 1,
|
||||
getConnection(0).packetReader.listeners.size());
|
||||
// Remove the listener
|
||||
getConnection(0).removePacketListener(listener);
|
||||
// Check that the number of listeners is correct (i.e. the listener was removed)
|
||||
assertEquals("Listener was not removed", listenersSize,
|
||||
getConnection(0).packetReader.listeners.size());
|
||||
}
|
||||
|
||||
protected int getMaxConnections() {
|
||||
return 2;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue