1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-15 20:12:04 +01:00

Implemented listeners.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1806 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2003-01-17 18:26:14 +00:00 committed by mtucker
parent 0614057cf2
commit 1c40c3c348

View file

@ -82,7 +82,7 @@ class PacketReader {
private XmlPullParser parser; private XmlPullParser parser;
private boolean done = false; private boolean done = false;
protected List collectors = new ArrayList(); protected List collectors = new ArrayList();
private List listeners = Collections.synchronizedList(new ArrayList()); private List listeners = new ArrayList();
private String connectionID = null; private String connectionID = null;
private Object connectionIDLock = new Object(); 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) { 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) { 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) { 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() { private void processListeners() {
boolean processedPacket = false; boolean processedPacket = false;
while (true) { while (!done) {
synchronized (listeners) { synchronized (listeners) {
// TODO: need to loop through lists and remove null elements. Should loop through
// TODO: list backwards so that index values stay correct.
}
processedPacket = false;
int size = listeners.size(); int size = listeners.size();
for (int i=0; i<size; i++) { for (int i=0; i<size; i++) {
ListenerWrapper wrapper = (ListenerWrapper)listeners.get(i);
if (wrapper != null) {
processedPacket = processedPacket || wrapper.notifyListener();
} }
} }
if (!processedPacket) { if (!processedPacket) {
@ -240,7 +274,8 @@ class PacketReader {
/** /**
* 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 letting them examine the packet to see if they are a match. * packet collectors and listeners and letting them examine the packet to see if
* they are a match with the filter.
* *
* @param packet the packet to process. * @param packet the packet to process.
*/ */
@ -248,9 +283,10 @@ class PacketReader {
if (packet == null) { if (packet == null) {
return; return;
} }
// TODO: loop through collectors and remove null values.
// Loop through all collectors and notify the appropriate ones. // Loop through all collectors and notify the appropriate ones.
synchronized (collectors) {
// Loop through packet collectors backwards.
int size = collectors.size(); int size = collectors.size();
for (int i=0; i<size; i++) { for (int i=0; i<size; i++) {
PacketCollector collector = (PacketCollector)collectors.get(i); PacketCollector collector = (PacketCollector)collectors.get(i);
@ -260,7 +296,6 @@ class PacketReader {
} }
} }
} }
}
/** /**
* Parses an IQ packet. * Parses an IQ packet.
@ -555,16 +590,50 @@ class PacketReader {
/** /**
* A wrapper class to associate a packet collector with a listener. * A wrapper class to associate a packet collector with a listener.
*/ */
private static class PacketListenerWrapper { private static class ListenerWrapper {
private PacketListener packetListener; private PacketListener packetListener;
private PacketCollector packetCollector; private PacketCollector packetCollector;
public PacketListenerWrapper(PacketReader packetReader, PacketListener packetListener, public ListenerWrapper(PacketReader packetReader, PacketListener packetListener,
PacketFilter packetFilter) PacketFilter packetFilter)
{ {
this.packetListener = packetListener; this.packetListener = packetListener;
this.packetCollector = new PacketCollector(packetReader, packetFilter); this.packetCollector = new PacketCollector(packetReader, packetFilter);
} }
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object instanceof ListenerWrapper) {
return ((ListenerWrapper)object).packetListener.equals(this.packetListener);
}
else if (object instanceof PacketListener) {
return object.equals(this.packetListener);
}
return false;
}
public void processPacket(Packet packet) {
packetCollector.processPacket(packet);
}
public boolean notifyListener() {
Packet packet = packetCollector.pollResult();
if (packet != null) {
packetListener.processPacket(packet);
return true;
}
else {
return false;
}
}
public void cancel() {
packetCollector.cancel();
packetCollector = null;
packetListener = null;
}
} }
} }