1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 04:22:05 +01:00

Cleanup of listener logic, now possible to get count of listeners.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2355 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2004-08-09 21:15:04 +00:00 committed by mtucker
parent 0553ef7e65
commit 785e319e7c

View file

@ -72,6 +72,7 @@ class PacketWriter {
private boolean done = false; private boolean done = false;
private List listeners = new ArrayList(); private List listeners = new ArrayList();
private boolean listenersUpdated = false;
private Thread listenerThread; private Thread listenerThread;
private LinkedList sentPackets = new LinkedList(); private LinkedList sentPackets = new LinkedList();
@ -122,8 +123,8 @@ class PacketWriter {
queue.addFirst(packet); queue.addFirst(packet);
queue.notifyAll(); queue.notifyAll();
} }
// Add the sent packet to the list of sent packets // Add the sent packet to the list of sent packets. The
// The PacketWriterListeners will be notified of the new packet // PacketWriterListeners will be notified of the new packet.
synchronized(sentPackets) { synchronized(sentPackets) {
sentPackets.addFirst(packet); sentPackets.addFirst(packet);
sentPackets.notifyAll(); sentPackets.notifyAll();
@ -156,11 +157,25 @@ class PacketWriter {
ListenerWrapper wrapper = (ListenerWrapper)listeners.get(i); ListenerWrapper wrapper = (ListenerWrapper)listeners.get(i);
if (wrapper != null && wrapper.packetListener.equals(packetListener)) { if (wrapper != null && wrapper.packetListener.equals(packetListener)) {
listeners.set(i, null); listeners.set(i, null);
// Set the flag to indicate that the listener list needs
// to be cleaned up.
listenersUpdated = true;
} }
} }
} }
} }
/**
* Returns the number of registered packet listeners.
*
* @return the count of packet listeners.
*/
public int getPacketListenerCount() {
synchronized (listeners) {
return listeners.size();
}
}
/** /**
* Starts the packet writer thread and opens a connection to the server. The * Starts the packet writer thread and opens a connection to the server. The
* packet writer will continue writing packets until {@link #shutdown} or an * packet writer will continue writing packets until {@link #shutdown} or an
@ -250,7 +265,7 @@ class PacketWriter {
while (!done) { while (!done) {
Packet sentPacket; Packet sentPacket;
// Wait until a new packet has been sent // Wait until a new packet has been sent
synchronized(sentPackets) { synchronized (sentPackets) {
while (!done && sentPackets.size() == 0) { while (!done && sentPackets.size() == 0) {
try { try {
sentPackets.wait(2000); sentPackets.wait(2000);
@ -265,14 +280,17 @@ class PacketWriter {
} }
} }
if (sentPacket != null) { if (sentPacket != null) {
// Clean up null entries in the listeners list // Clean up null entries in the listeners list if the flag is set. List
// removes are done seperately so that the main notification process doesn't
// need to synchronize on the list.
synchronized (listeners) { synchronized (listeners) {
if (listeners.size() > 0) { if (listenersUpdated) {
for (int i=listeners.size()-1; i>=0; i--) { for (int i=listeners.size()-1; i>=0; i--) {
if (listeners.get(i) == null) { if (listeners.get(i) == null) {
listeners.remove(i); listeners.remove(i);
} }
} }
listenersUpdated = false;
} }
} }
// Notify the listeners of the new sent packet // Notify the listeners of the new sent packet
@ -324,7 +342,7 @@ class PacketWriter {
/** /**
* A TimerTask that keeps connections to the server alive by sending a space * A TimerTask that keeps connections to the server alive by sending a space
* character. The * character on an interval.
*/ */
private class KeepAliveTask implements Runnable { private class KeepAliveTask implements Runnable {
@ -344,7 +362,7 @@ class PacketWriter {
catch (Exception e) { } catch (Exception e) { }
} }
try { try {
// Sleep 30 seconds. // Sleep until we should write the next keep-alive.
Thread.sleep(delay); Thread.sleep(delay);
} }
catch (InterruptedException ie) { } catch (InterruptedException ie) { }