1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Move processPacket() method to XMPPConnection

This removes code duplication in BOSHConnection and TCPConnection's
PacketReader, by moving the common code to the superclass
XMPPConnection.
This commit is contained in:
Florian Schmaus 2014-03-14 00:25:22 +01:00
parent 89ed4d7492
commit 4b6f09f962
3 changed files with 71 additions and 134 deletions

View file

@ -21,14 +21,10 @@ import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Writer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
@ -84,11 +80,6 @@ public class BOSHConnection extends XMPPConnection {
private boolean wasAuthenticated = false;
private boolean done = false;
/**
* The Thread environment for sending packet listeners.
*/
private ExecutorService listenerExecutor;
// The readerPipe and consumer thread are used for the debugger.
private PipedWriter readerPipe;
private Thread readerConsumer;
@ -166,18 +157,6 @@ public class BOSHConnection extends XMPPConnection {
}
client = BOSHClient.create(cfgBuilder.build());
// Create an executor to deliver incoming packets to listeners.
// We'll use a single thread with an unbounded queue.
listenerExecutor = Executors
.newSingleThreadExecutor(new ThreadFactory() {
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable,
"Smack Listener Processor ("
+ connectionCounterValue + ")");
thread.setDaemon(true);
return thread;
}
});
client.addBOSHClientConnListener(new BOSHConnectionListener(this));
client.addBOSHClientResponseListener(new BOSHPacketReader(this));
@ -517,10 +496,6 @@ public class BOSHConnection extends XMPPConnection {
writer = null;
}
// Shut down the listener executor.
if (listenerExecutor != null) {
listenerExecutor.shutdown();
}
readerConsumer = null;
}
@ -554,27 +529,6 @@ public class BOSHConnection extends XMPPConnection {
client.send(body);
}
/**
* Processes a packet after it's been fully parsed by looping through the
* installed 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.
*/
protected void processPacket(Packet packet) {
if (packet == null) {
return;
}
// Loop through all collectors and notify the appropriate ones.
for (PacketCollector collector : getPacketCollectors()) {
collector.processPacket(packet);
}
// Deliver the incoming packet to listeners.
listenerExecutor.submit(new ListenerNotification(packet));
}
/**
* Initialize the SmackDebugger which allows to log and debug XML traffic.
*/
@ -738,22 +692,4 @@ public class BOSHConnection extends XMPPConnection {
}
}
}
/**
* This class notifies all listeners that a packet was received.
*/
private class ListenerNotification implements Runnable {
private Packet packet;
public ListenerNotification(Packet packet) {
this.packet = packet;
}
public void run() {
for (ListenerWrapper listenerWrapper : recvListeners.values()) {
listenerWrapper.notifyListener(packet);
}
}
}
}