From 1c40c3c3480cbddfcbb300b65408114f8b99ed09 Mon Sep 17 00:00:00 2001 From: Matt Tucker Date: Fri, 17 Jan 2003 18:26:14 +0000 Subject: [PATCH] Implemented listeners. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1806 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/PacketReader.java | 111 ++++++++++++++---- 1 file changed, 90 insertions(+), 21 deletions(-) diff --git a/source/org/jivesoftware/smack/PacketReader.java b/source/org/jivesoftware/smack/PacketReader.java index 0abad971f..c0a49664d 100644 --- a/source/org/jivesoftware/smack/PacketReader.java +++ b/source/org/jivesoftware/smack/PacketReader.java @@ -82,7 +82,7 @@ class PacketReader { private XmlPullParser parser; private boolean done = false; protected List collectors = new ArrayList(); - private List listeners = Collections.synchronizedList(new ArrayList()); + private List listeners = new ArrayList(); private String connectionID = null; private Object connectionIDLock = new Object(); @@ -119,16 +119,44 @@ class PacketReader { } } + /** + * Creates a new packet collector for this reader. A packet filter determines + * which packets will be accumulated by the collector. + * + * @param packetFilter the packet filter to use. + * @return a new packet collector. + */ public PacketCollector createPacketCollector(PacketFilter packetFilter) { - return new PacketCollector(this, packetFilter); + PacketCollector packetCollector = new PacketCollector(this, packetFilter); + + return packetCollector; } + /** + * Registers a packet listener with this reader. A packet filter determines + * which packets will be delivered to the listener. + * + * @param packetListener the packet listener to notify of new packets. + * @param packetFilter the packet filter to use. + */ public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) { - // TODO: implement + ListenerWrapper wrapper = new ListenerWrapper(this, packetListener, + packetFilter); + synchronized (listeners) { + listeners.add(wrapper); + } } + /** + * Removes a packet listener from this reader. + * + * @param packetListener the packet listener to remove. + */ public void removePacketListener(PacketListener packetListener) { - // TODO: implement + synchronized (listeners) { + int index = listeners.indexOf(packetListener); + listeners.set(index, null); + } } /** @@ -170,11 +198,17 @@ class PacketReader { */ private void processListeners() { boolean processedPacket = false; - while (true) { - synchronized(listeners) { - int size = listeners.size(); - for (int i=0; i