From 157a1e96594061a5673c1f6a5fae7bb567f7fb37 Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Thu, 10 Aug 2006 22:09:30 +0000 Subject: [PATCH] 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 --- .../org/jivesoftware/smack/PacketReader.java | 28 ++++++++++++++---- .../jivesoftware/smack/PacketReaderTest.java | 29 +++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/source/org/jivesoftware/smack/PacketReader.java b/source/org/jivesoftware/smack/PacketReader.java index 47616c106..5458a2fb7 100644 --- a/source/org/jivesoftware/smack/PacketReader.java +++ b/source/org/jivesoftware/smack/PacketReader.java @@ -50,7 +50,7 @@ class PacketReader { private boolean done = false; private final VolatileMemberCollection collectors = new VolatileMemberCollection(50); - private final VolatileMemberCollection listeners = new VolatileMemberCollection(50); + protected final VolatileMemberCollection listeners = new VolatileMemberCollection(50); protected final List connectionListeners = new ArrayList(); @@ -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; + } } /** diff --git a/test/org/jivesoftware/smack/PacketReaderTest.java b/test/org/jivesoftware/smack/PacketReaderTest.java index 77fb14f1a..63e7b488a 100644 --- a/test/org/jivesoftware/smack/PacketReaderTest.java +++ b/test/org/jivesoftware/smack/PacketReaderTest.java @@ -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; }