2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.packet.*;
|
|
|
|
import org.jivesoftware.smack.provider.IQProvider;
|
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2005-03-23 03:36:19 +01:00
|
|
|
import org.xmlpull.mxp1.MXParser;
|
2005-08-27 04:29:04 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2003-04-08 17:32:26 +02:00
|
|
|
|
2005-11-08 21:10:33 +01:00
|
|
|
import java.io.IOException;
|
2006-02-27 19:17:27 +01:00
|
|
|
import java.util.*;
|
2007-02-16 02:56:16 +01:00
|
|
|
import java.util.concurrent.*;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2003-05-01 04:06:21 +02:00
|
|
|
* Listens for XML traffic from the XMPP server and parses it into packet objects.
|
|
|
|
* The packet reader also manages all packet listeners and collectors.<p>
|
|
|
|
*
|
2003-01-13 17:58:47 +01:00
|
|
|
* @see PacketCollector
|
2003-01-17 08:11:33 +01:00
|
|
|
* @see PacketListener
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
class PacketReader {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
private Thread readerThread;
|
2007-02-21 01:57:31 +01:00
|
|
|
private ExecutorService listenerExecutor;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
private XMPPConnection connection;
|
|
|
|
private XmlPullParser parser;
|
2006-09-14 21:16:40 +02:00
|
|
|
private boolean done;
|
2007-02-16 02:56:16 +01:00
|
|
|
private Collection<PacketCollector> collectors = new ConcurrentLinkedQueue<PacketCollector>();
|
2007-02-19 06:48:57 +01:00
|
|
|
protected final Map<PacketListener, ListenerWrapper> listeners =
|
|
|
|
new ConcurrentHashMap<PacketListener, ListenerWrapper>();
|
2007-02-16 02:56:16 +01:00
|
|
|
protected final Collection<ConnectionListener> connectionListeners =
|
2007-02-16 01:12:17 +01:00
|
|
|
new CopyOnWriteArrayList<ConnectionListener>();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
private String connectionID = null;
|
2006-09-13 19:58:28 +02:00
|
|
|
private Semaphore connectionSemaphore;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
protected PacketReader(final XMPPConnection connection) {
|
2003-01-13 17:58:47 +01:00
|
|
|
this.connection = connection;
|
2006-09-14 21:16:40 +02:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the reader in order to be used. The reader is initialized during the
|
|
|
|
* first connection and when reconnecting due to an abruptly disconnection.
|
|
|
|
*/
|
|
|
|
protected void init() {
|
|
|
|
done = false;
|
|
|
|
connectionID = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
readerThread = new Thread() {
|
|
|
|
public void run() {
|
2006-09-14 21:16:40 +02:00
|
|
|
parsePackets(this);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
};
|
2007-02-04 21:52:45 +01:00
|
|
|
readerThread.setName("Smack Packet Reader (" + connection.connectionCounterValue + ")");
|
2003-01-13 17:58:47 +01:00
|
|
|
readerThread.setDaemon(true);
|
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
// 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 (" + connection.connectionCounterValue + ")");
|
|
|
|
thread.setDaemon(true);
|
|
|
|
return thread;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-02-21 01:57:31 +01:00
|
|
|
});
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2005-12-08 22:10:41 +01:00
|
|
|
resetParser();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 19:26:14 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public PacketCollector createPacketCollector(PacketFilter packetFilter) {
|
2006-07-10 18:51:56 +02:00
|
|
|
PacketCollector collector = new PacketCollector(this, packetFilter);
|
|
|
|
collectors.add(collector);
|
|
|
|
// Add the collector to the list of active collector.
|
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void cancelPacketCollector(PacketCollector packetCollector) {
|
|
|
|
collectors.remove(packetCollector);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 19:26:14 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
|
2007-02-21 01:57:31 +01:00
|
|
|
ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
|
2007-02-16 02:56:16 +01:00
|
|
|
listeners.put(packetListener, wrapper);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-17 19:26:14 +01:00
|
|
|
/**
|
2003-04-07 07:39:44 +02:00
|
|
|
* Removes a packet listener.
|
2003-01-17 19:26:14 +01:00
|
|
|
*
|
|
|
|
* @param packetListener the packet listener to remove.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public void removePacketListener(PacketListener packetListener) {
|
2007-02-21 01:57:31 +01:00
|
|
|
listeners.remove(packetListener);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the packet reader thread and returns once a connection to the server
|
|
|
|
* has been established. A connection will be attempted for a maximum of five
|
|
|
|
* seconds. An XMPPException will be thrown if the connection fails.
|
|
|
|
*
|
|
|
|
* @throws XMPPException if the server fails to send an opening stream back
|
|
|
|
* for more than five seconds.
|
|
|
|
*/
|
|
|
|
public void startup() throws XMPPException {
|
2006-09-13 19:58:28 +02:00
|
|
|
connectionSemaphore = new Semaphore(1);
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
readerThread.start();
|
2004-09-24 04:11:49 +02:00
|
|
|
// Wait for stream tag before returing. We'll wait a couple of seconds before
|
2003-01-13 17:58:47 +01:00
|
|
|
// giving up and throwing an error.
|
|
|
|
try {
|
2006-09-13 19:58:28 +02:00
|
|
|
connectionSemaphore.acquire();
|
|
|
|
|
|
|
|
// A waiting thread may be woken up before the wait time or a notify
|
|
|
|
// (although this is a rare thing). Therefore, we continue waiting
|
|
|
|
// until either a connectionID has been set (and hence a notify was
|
|
|
|
// made) or the total wait time has elapsed.
|
|
|
|
int waitTime = SmackConfiguration.getPacketReplyTimeout();
|
|
|
|
connectionSemaphore.tryAcquire(3 * waitTime, TimeUnit.MILLISECONDS);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2005-09-05 22:00:45 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
if (connectionID == null) {
|
|
|
|
throw new XMPPException("Connection failed. No response from server.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connection.connectionID = connectionID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuts the packet reader down.
|
|
|
|
*/
|
|
|
|
public void shutdown() {
|
2003-08-20 18:29:46 +02:00
|
|
|
// Notify connection listeners of the connection closing if done hasn't already been set.
|
|
|
|
if (!done) {
|
2007-02-16 01:12:17 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
|
|
|
try {
|
|
|
|
listener.connectionClosed();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Cath and print any exception so we can recover
|
|
|
|
// from a faulty listener and finish the shutdown process
|
|
|
|
e.printStackTrace();
|
2003-08-20 18:29:46 +02:00
|
|
|
}
|
2003-07-16 04:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-20 18:29:46 +02:00
|
|
|
done = true;
|
2006-01-26 19:31:35 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
// Shut down the listener executor.
|
|
|
|
listenerExecutor.shutdown();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2007-02-19 06:48:57 +01:00
|
|
|
/**
|
|
|
|
* Cleans up all resources used by the packet reader.
|
|
|
|
*/
|
|
|
|
void cleanup() {
|
|
|
|
connectionListeners.clear();
|
|
|
|
listeners.clear();
|
|
|
|
collectors.clear();
|
|
|
|
}
|
|
|
|
|
2003-09-19 00:35:42 +02:00
|
|
|
/**
|
|
|
|
* Sends out a notification that there was an error with the connection
|
|
|
|
* and closes the connection.
|
|
|
|
*
|
|
|
|
* @param e the exception that causes the connection close event.
|
|
|
|
*/
|
|
|
|
void notifyConnectionError(Exception e) {
|
|
|
|
done = true;
|
2006-09-14 21:16:40 +02:00
|
|
|
// Closes the connection temporary. A reconnection is possible
|
2007-02-13 03:23:28 +01:00
|
|
|
connection.shutdown(new Presence(Presence.Type.unavailable));
|
2005-07-23 17:56:06 +02:00
|
|
|
// Print the stack trace to help catch the problem
|
|
|
|
e.printStackTrace();
|
2003-09-19 00:35:42 +02:00
|
|
|
// Notify connection listeners of the error.
|
2007-02-16 01:12:17 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
|
|
|
try {
|
|
|
|
listener.connectionClosedOnError(e);
|
|
|
|
}
|
|
|
|
catch (Exception e2) {
|
|
|
|
// Cath and print any exception so we can recover
|
|
|
|
// from a faulty listener
|
|
|
|
e2.printStackTrace();
|
2003-09-19 00:35:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
/**
|
|
|
|
* Sends a notification indicating that the connection was reconnected successfully.
|
|
|
|
*/
|
|
|
|
protected void notifyReconnection() {
|
|
|
|
// Notify connection listeners of the reconnection.
|
2007-02-21 01:57:31 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
|
|
|
try {
|
|
|
|
listener.reconnectionSuccessful();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Cath and print any exception so we can recover
|
|
|
|
// from a faulty listener
|
|
|
|
e.printStackTrace();
|
2006-09-14 21:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
/**
|
|
|
|
* Resets the parser using the latest connection's reader. Reseting the parser is necessary
|
|
|
|
* when the plain connection has been secured or when a new opening stream element is going
|
|
|
|
* to be sent by the server.
|
|
|
|
*/
|
2005-12-08 22:10:41 +01:00
|
|
|
private void resetParser() {
|
|
|
|
try {
|
|
|
|
parser = new MXParser();
|
|
|
|
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
|
|
|
parser.setInput(connection.reader);
|
|
|
|
}
|
|
|
|
catch (XmlPullParserException xppe) {
|
|
|
|
xppe.printStackTrace();
|
|
|
|
}
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Parse top-level packets in order to process them further.
|
2006-09-14 21:16:40 +02:00
|
|
|
*
|
|
|
|
* @param thread the thread that is being used by the reader to parse incoming packets.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2006-09-14 21:16:40 +02:00
|
|
|
private void parsePackets(Thread thread) {
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
|
|
|
int eventType = parser.getEventType();
|
|
|
|
do {
|
2003-04-08 17:32:26 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (parser.getName().equals("message")) {
|
2005-02-23 04:32:10 +01:00
|
|
|
processPacket(PacketParserUtils.parseMessage(parser));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("iq")) {
|
|
|
|
processPacket(parseIQ(parser));
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("presence")) {
|
2005-02-23 04:32:10 +01:00
|
|
|
processPacket(PacketParserUtils.parsePresence(parser));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
// We found an opening stream. Record information about it, then notify
|
|
|
|
// the connectionID lock so that the packet reader startup can finish.
|
|
|
|
else if (parser.getName().equals("stream")) {
|
|
|
|
// Ensure the correct jabber:client namespace is being used.
|
|
|
|
if ("jabber:client".equals(parser.getNamespace(null))) {
|
|
|
|
// Get the connection id.
|
|
|
|
for (int i=0; i<parser.getAttributeCount(); i++) {
|
|
|
|
if (parser.getAttributeName(i).equals("id")) {
|
2005-09-06 00:03:47 +02:00
|
|
|
// Save the connectionID
|
|
|
|
connectionID = parser.getAttributeValue(i);
|
|
|
|
if (!"1.0".equals(parser.getAttributeValue("", "version"))) {
|
|
|
|
// Notify that a stream has been opened if the
|
|
|
|
// server is not XMPP 1.0 compliant otherwise make the
|
|
|
|
// notification after TLS has been negotiated or if TLS
|
|
|
|
// is not supported
|
|
|
|
releaseConnectionIDLock();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
2003-10-03 02:47:45 +02:00
|
|
|
else if (parser.getAttributeName(i).equals("from")) {
|
|
|
|
// Use the server name that the server says that it is.
|
2005-08-27 04:29:04 +02:00
|
|
|
connection.serviceName = parser.getAttributeValue(i);
|
2003-10-03 02:47:45 +02:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-08 21:10:33 +01:00
|
|
|
else if (parser.getName().equals("error")) {
|
|
|
|
throw new XMPPException(parseStreamError(parser));
|
|
|
|
}
|
2005-08-27 04:29:04 +02:00
|
|
|
else if (parser.getName().equals("features")) {
|
2006-09-13 19:58:28 +02:00
|
|
|
parseFeatures(parser);
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("proceed")) {
|
|
|
|
// Secure the connection by negotiating TLS
|
|
|
|
connection.proceedTLSReceived();
|
|
|
|
// Reset the state of the parser since a new stream element is going
|
|
|
|
// to be sent by the server
|
|
|
|
resetParser();
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("failure")) {
|
2006-01-16 18:34:56 +01:00
|
|
|
String namespace = parser.getNamespace(null);
|
|
|
|
if ("urn:ietf:params:xml:ns:xmpp-tls".equals(namespace)) {
|
2005-09-13 22:05:29 +02:00
|
|
|
// TLS negotiation has failed. The server will close the connection
|
|
|
|
throw new Exception("TLS negotiation has failed");
|
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
else if ("http://jabber.org/protocol/compress".equals(namespace)) {
|
|
|
|
// Stream compression has been denied. This is a recoverable
|
|
|
|
// situation. It is still possible to authenticate and
|
|
|
|
// use the connection but using an uncompressed connection
|
|
|
|
connection.streamCompressionDenied();
|
|
|
|
}
|
2005-09-13 22:05:29 +02:00
|
|
|
else {
|
|
|
|
// SASL authentication has failed. The server may close the connection
|
|
|
|
// depending on the number of retries
|
|
|
|
connection.getSASLAuthentication().authenticationFailed();
|
|
|
|
}
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("challenge")) {
|
|
|
|
// The server is challenging the SASL authentication made by the client
|
|
|
|
connection.getSASLAuthentication().challengeReceived(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("success")) {
|
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
else if (parser.getName().equals("compressed")) {
|
|
|
|
// Server confirmed that it's possible to use stream compression. Start
|
|
|
|
// stream compression
|
|
|
|
connection.startStreamCompression();
|
|
|
|
// Reset the state of the parser since a new stream element is going
|
|
|
|
// to be sent by the server
|
|
|
|
resetParser();
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-04-08 17:32:26 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (parser.getName().equals("stream")) {
|
2006-09-14 21:16:40 +02:00
|
|
|
// Disconnect the connection
|
|
|
|
connection.disconnect();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2008-10-31 04:51:12 +01:00
|
|
|
else if (parser.getName().equals("success")) {
|
|
|
|
// We now need to bind a resource for the connection
|
|
|
|
// Open a new stream and wait for the response
|
|
|
|
connection.packetWriter.openStream();
|
|
|
|
// Reset the state of the parser since a new stream element is going
|
|
|
|
// to be sent by the server
|
|
|
|
resetParser();
|
|
|
|
// The SASL authentication with the server was successful. The next step
|
|
|
|
// will be to bind the resource
|
|
|
|
connection.getSASLAuthentication().authenticated();
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
eventType = parser.next();
|
2006-09-14 21:16:40 +02:00
|
|
|
} while (!done && eventType != XmlPullParser.END_DOCUMENT && thread == readerThread);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
if (!done) {
|
2003-09-19 00:35:42 +02:00
|
|
|
// Close the connection and notify connection listeners of the
|
|
|
|
// error.
|
|
|
|
notifyConnectionError(e);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-06 00:03:47 +02:00
|
|
|
/**
|
|
|
|
* Releases the connection ID lock so that the thread that was waiting can resume. The
|
|
|
|
* lock will be released when one of the following three conditions is met:<p>
|
|
|
|
*
|
|
|
|
* 1) An opening stream was sent from a non XMPP 1.0 compliant server
|
|
|
|
* 2) Stream features were received from an XMPP 1.0 compliant server that does not support TLS
|
|
|
|
* 3) TLS negotiation was successful
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private void releaseConnectionIDLock() {
|
2006-09-13 19:58:28 +02:00
|
|
|
connectionSemaphore.release();
|
2005-09-06 00:03:47 +02:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Processes a packet after it's been fully parsed by looping through the installed
|
2003-01-17 19:26:14 +01:00
|
|
|
* packet collectors and listeners and letting them examine the packet to see if
|
|
|
|
* they are a match with the filter.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param packet the packet to process.
|
|
|
|
*/
|
|
|
|
private void processPacket(Packet packet) {
|
2003-01-15 16:01:30 +01:00
|
|
|
if (packet == null) {
|
|
|
|
return;
|
|
|
|
}
|
2003-01-17 19:26:14 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
// Loop through all collectors and notify the appropriate ones.
|
2007-02-16 01:12:17 +01:00
|
|
|
for (PacketCollector collector: collectors) {
|
2006-07-10 18:51:56 +02:00
|
|
|
collector.processPacket(packet);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
// Deliver the incoming packet to listeners.
|
|
|
|
listenerExecutor.submit(new ListenerNotification(packet));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2005-11-08 21:10:33 +01:00
|
|
|
private StreamError parseStreamError(XmlPullParser parser) throws IOException,
|
|
|
|
XmlPullParserException {
|
|
|
|
StreamError streamError = null;
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
streamError = new StreamError(parser.getName());
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("error")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return streamError;
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
private void parseFeatures(XmlPullParser parser) throws Exception {
|
2005-09-06 00:03:47 +02:00
|
|
|
boolean startTLSReceived = false;
|
2007-01-11 20:01:24 +01:00
|
|
|
boolean startTLSRequired = false;
|
2005-09-06 00:03:47 +02:00
|
|
|
boolean done = false;
|
2005-08-27 04:29:04 +02:00
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("starttls")) {
|
2005-09-06 00:03:47 +02:00
|
|
|
startTLSReceived = true;
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
2005-09-06 00:03:47 +02:00
|
|
|
else if (parser.getName().equals("mechanisms")) {
|
|
|
|
// The server is reporting available SASL mechanisms. Store this information
|
2005-08-27 04:29:04 +02:00
|
|
|
// which will be used later while logging (i.e. authenticating) into
|
|
|
|
// the server
|
|
|
|
connection.getSASLAuthentication()
|
|
|
|
.setAvailableSASLMethods(parseMechanisms(parser));
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("bind")) {
|
|
|
|
// The server requires the client to bind a resource to the stream
|
|
|
|
connection.getSASLAuthentication().bindingRequired();
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("session")) {
|
|
|
|
// The server supports sessions
|
|
|
|
connection.getSASLAuthentication().sessionsSupported();
|
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
else if (parser.getName().equals("compression")) {
|
|
|
|
// The server supports stream compression
|
|
|
|
connection.setAvailableCompressionMethods(parseCompressionMethods(parser));
|
|
|
|
}
|
2006-08-03 19:04:01 +02:00
|
|
|
else if (parser.getName().equals("register")) {
|
|
|
|
connection.getAccountManager().setSupportsAccountCreation(true);
|
|
|
|
}
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2007-01-11 20:01:24 +01:00
|
|
|
if (parser.getName().equals("starttls")) {
|
|
|
|
// Confirm the server that we want to use TLS
|
|
|
|
connection.startTLSReceived(startTLSRequired);
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("required") && startTLSReceived) {
|
|
|
|
startTLSRequired = true;
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("features")) {
|
2005-08-27 04:29:04 +02:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
|
|
|
|
// If TLS is required but the server doesn't offer it, disconnect
|
|
|
|
// from the server and throw an error. First check if we've already negotiated TLS
|
|
|
|
// and are secure, however (features get parsed a second time after TLS is established).
|
|
|
|
if (!connection.isSecureConnection()) {
|
|
|
|
if (!startTLSReceived && connection.getConfiguration().getSecurityMode() ==
|
|
|
|
ConnectionConfiguration.SecurityMode.required)
|
|
|
|
{
|
|
|
|
throw new XMPPException("Server does not support security (TLS), " +
|
|
|
|
"but security required by connection configuration.",
|
|
|
|
new XMPPError(XMPPError.Condition.forbidden));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
// Release the lock after TLS has been negotiated or we are not insterested in TLS
|
2007-01-11 20:01:24 +01:00
|
|
|
if (!startTLSReceived || connection.getConfiguration().getSecurityMode() ==
|
|
|
|
ConnectionConfiguration.SecurityMode.disabled)
|
|
|
|
{
|
2005-09-06 00:03:47 +02:00
|
|
|
releaseConnectionIDLock();
|
|
|
|
}
|
2005-08-27 04:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a collection of Stings with the mechanisms included in the mechanisms stanza.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the start of an IQ packet.
|
|
|
|
* @return a collection of Stings with the mechanisms included in the mechanisms stanza.
|
|
|
|
* @throws Exception if an exception occurs while parsing the stanza.
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
private Collection<String> parseMechanisms(XmlPullParser parser) throws Exception {
|
|
|
|
List<String> mechanisms = new ArrayList<String>();
|
2005-08-27 04:29:04 +02:00
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elementName = parser.getName();
|
|
|
|
if (elementName.equals("mechanism")) {
|
|
|
|
mechanisms.add(parser.nextText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("mechanisms")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mechanisms;
|
|
|
|
}
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
private Collection<String> parseCompressionMethods(XmlPullParser parser)
|
2006-01-16 18:34:56 +01:00
|
|
|
throws IOException, XmlPullParserException {
|
2006-07-18 07:14:33 +02:00
|
|
|
List<String> methods = new ArrayList<String>();
|
2006-01-16 18:34:56 +01:00
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elementName = parser.getName();
|
|
|
|
if (elementName.equals("method")) {
|
|
|
|
methods.add(parser.nextText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("compression")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return methods;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Parses an IQ packet.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the start of an IQ packet.
|
|
|
|
* @return an IQ object.
|
|
|
|
* @throws Exception if an exception occurs while parsing the packet.
|
|
|
|
*/
|
2003-10-21 19:39:04 +02:00
|
|
|
private IQ parseIQ(XmlPullParser parser) throws Exception {
|
2003-01-15 16:01:30 +01:00
|
|
|
IQ iqPacket = null;
|
|
|
|
|
|
|
|
String id = parser.getAttributeValue("", "id");
|
|
|
|
String to = parser.getAttributeValue("", "to");
|
|
|
|
String from = parser.getAttributeValue("", "from");
|
|
|
|
IQ.Type type = IQ.Type.fromString(parser.getAttributeValue("", "type"));
|
2003-03-07 20:47:49 +01:00
|
|
|
XMPPError error = null;
|
2003-01-15 16:01:30 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
2003-05-08 17:55:06 +02:00
|
|
|
|
2003-04-08 17:32:26 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2003-05-08 17:55:06 +02:00
|
|
|
String elementName = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
|
|
|
if (elementName.equals("error")) {
|
2005-02-23 04:32:10 +01:00
|
|
|
error = PacketParserUtils.parseError(parser);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-05-08 17:55:06 +02:00
|
|
|
else if (elementName.equals("query") && namespace.equals("jabber:iq:auth")) {
|
|
|
|
iqPacket = parseAuthentication(parser);
|
|
|
|
}
|
|
|
|
else if (elementName.equals("query") && namespace.equals("jabber:iq:roster")) {
|
|
|
|
iqPacket = parseRoster(parser);
|
|
|
|
}
|
|
|
|
else if (elementName.equals("query") && namespace.equals("jabber:iq:register")) {
|
|
|
|
iqPacket = parseRegistration(parser);
|
|
|
|
}
|
2006-02-27 19:17:27 +01:00
|
|
|
else if (elementName.equals("bind") &&
|
|
|
|
namespace.equals("urn:ietf:params:xml:ns:xmpp-bind")) {
|
|
|
|
iqPacket = parseResourceBinding(parser);
|
|
|
|
}
|
2003-05-08 17:55:06 +02:00
|
|
|
// Otherwise, see if there is a registered provider for
|
|
|
|
// this element name and namespace.
|
|
|
|
else {
|
2006-11-16 20:31:30 +01:00
|
|
|
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
|
2003-05-08 17:55:06 +02:00
|
|
|
if (provider != null) {
|
|
|
|
if (provider instanceof IQProvider) {
|
|
|
|
iqPacket = ((IQProvider)provider).parseIQ(parser);
|
|
|
|
}
|
|
|
|
else if (provider instanceof Class) {
|
2004-03-29 23:37:24 +02:00
|
|
|
iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
|
2003-05-08 18:17:48 +02:00
|
|
|
(Class)provider, parser);
|
2003-05-08 17:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
2003-01-16 02:00:55 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-04-08 17:32:26 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (parser.getName().equals("iq")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
// Decide what to do when an IQ packet was not understood
|
2003-01-15 16:01:30 +01:00
|
|
|
if (iqPacket == null) {
|
2004-01-29 23:02:50 +01:00
|
|
|
if (IQ.Type.GET == type || IQ.Type.SET == type ) {
|
2005-02-23 04:32:10 +01:00
|
|
|
// If the IQ stanza is of type "get" or "set" containing a child element
|
|
|
|
// qualified by a namespace it does not understand, then answer an IQ of
|
2004-01-29 23:02:50 +01:00
|
|
|
// type "error" with code 501 ("feature-not-implemented")
|
|
|
|
iqPacket = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
iqPacket.setPacketID(id);
|
|
|
|
iqPacket.setTo(from);
|
|
|
|
iqPacket.setFrom(to);
|
|
|
|
iqPacket.setType(IQ.Type.ERROR);
|
2006-07-19 21:24:00 +02:00
|
|
|
iqPacket.setError(new XMPPError(XMPPError.Condition.feature_not_implemented));
|
2004-01-29 23:02:50 +01:00
|
|
|
connection.sendPacket(iqPacket);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If an IQ packet wasn't created above, create an empty IQ packet.
|
|
|
|
iqPacket = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2003-01-15 16:01:30 +01:00
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
|
2004-01-29 23:02:50 +01:00
|
|
|
// Set basic values on the iq packet.
|
2003-01-15 16:01:30 +01:00
|
|
|
iqPacket.setPacketID(id);
|
|
|
|
iqPacket.setTo(to);
|
|
|
|
iqPacket.setFrom(from);
|
|
|
|
iqPacket.setType(type);
|
|
|
|
iqPacket.setError(error);
|
2003-08-04 23:58:31 +02:00
|
|
|
|
2003-01-15 16:01:30 +01:00
|
|
|
return iqPacket;
|
|
|
|
}
|
|
|
|
|
2003-05-01 04:06:21 +02:00
|
|
|
private Authentication parseAuthentication(XmlPullParser parser) throws Exception {
|
2003-01-15 16:01:30 +01:00
|
|
|
Authentication authentication = new Authentication();
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
2003-04-08 17:32:26 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2003-01-15 16:01:30 +01:00
|
|
|
if (parser.getName().equals("username")) {
|
|
|
|
authentication.setUsername(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("password")) {
|
|
|
|
authentication.setPassword(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("digest")) {
|
|
|
|
authentication.setDigest(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("resource")) {
|
|
|
|
authentication.setResource(parser.nextText());
|
|
|
|
}
|
|
|
|
}
|
2003-04-08 17:32:26 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2003-01-15 16:01:30 +01:00
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authentication;
|
|
|
|
}
|
|
|
|
|
2003-05-01 04:06:21 +02:00
|
|
|
private RosterPacket parseRoster(XmlPullParser parser) throws Exception {
|
2003-02-10 06:01:01 +01:00
|
|
|
RosterPacket roster = new RosterPacket();
|
|
|
|
boolean done = false;
|
|
|
|
RosterPacket.Item item = null;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
2003-04-08 17:32:26 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2003-02-10 06:01:01 +01:00
|
|
|
if (parser.getName().equals("item")) {
|
|
|
|
String jid = parser.getAttributeValue("", "jid");
|
|
|
|
String name = parser.getAttributeValue("", "name");
|
2003-06-17 22:22:43 +02:00
|
|
|
// Create packet.
|
2003-02-10 06:01:01 +01:00
|
|
|
item = new RosterPacket.Item(jid, name);
|
2003-06-17 22:22:43 +02:00
|
|
|
// Set status.
|
|
|
|
String ask = parser.getAttributeValue("", "ask");
|
|
|
|
RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask);
|
|
|
|
item.setItemStatus(status);
|
|
|
|
// Set type.
|
|
|
|
String subscription = parser.getAttributeValue("", "subscription");
|
2007-01-08 00:03:16 +01:00
|
|
|
RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription);
|
2003-06-17 22:22:43 +02:00
|
|
|
item.setItemType(type);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
2005-09-05 22:00:45 +02:00
|
|
|
if (parser.getName().equals("group") && item!= null) {
|
|
|
|
item.addGroupName(parser.nextText());
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
2003-04-08 17:32:26 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2003-02-10 06:01:01 +01:00
|
|
|
if (parser.getName().equals("item")) {
|
|
|
|
roster.addRosterItem(item);
|
|
|
|
}
|
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roster;
|
|
|
|
}
|
|
|
|
|
2003-05-01 04:06:21 +02:00
|
|
|
private Registration parseRegistration(XmlPullParser parser) throws Exception {
|
2003-03-09 00:09:48 +01:00
|
|
|
Registration registration = new Registration();
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, String> fields = null;
|
2003-03-09 00:09:48 +01:00
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
2003-04-08 17:32:26 +02:00
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2005-06-18 00:29:33 +02:00
|
|
|
// Any element that's in the jabber:iq:register namespace,
|
2003-09-27 19:27:40 +02:00
|
|
|
// attempt to parse it if it's in the form <name>value</name>.
|
2005-06-18 00:29:33 +02:00
|
|
|
if (parser.getNamespace().equals("jabber:iq:register")) {
|
2003-03-09 00:09:48 +01:00
|
|
|
String name = parser.getName();
|
2004-09-16 16:42:23 +02:00
|
|
|
String value = "";
|
|
|
|
if (fields == null) {
|
2006-07-18 07:14:33 +02:00
|
|
|
fields = new HashMap<String, String>();
|
2004-09-16 16:42:23 +02:00
|
|
|
}
|
|
|
|
|
2003-09-27 19:27:40 +02:00
|
|
|
if (parser.next() == XmlPullParser.TEXT) {
|
2004-09-16 16:42:23 +02:00
|
|
|
value = parser.getText();
|
2003-03-09 00:09:48 +01:00
|
|
|
}
|
2004-09-16 16:42:23 +02:00
|
|
|
// Ignore instructions, but anything else should be added to the map.
|
|
|
|
if (!name.equals("instructions")) {
|
|
|
|
fields.put(name, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
registration.setInstructions(value);
|
|
|
|
}
|
2007-02-21 01:57:31 +01:00
|
|
|
}
|
2004-03-29 23:37:24 +02:00
|
|
|
// Otherwise, it must be a packet extension.
|
|
|
|
else {
|
|
|
|
registration.addExtension(
|
|
|
|
PacketParserUtils.parsePacketExtension(
|
|
|
|
parser.getName(),
|
|
|
|
parser.getNamespace(),
|
|
|
|
parser));
|
|
|
|
}
|
2003-03-09 00:09:48 +01:00
|
|
|
}
|
2003-04-08 17:32:26 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
2003-03-09 00:09:48 +01:00
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
registration.setAttributes(fields);
|
|
|
|
return registration;
|
|
|
|
}
|
|
|
|
|
2006-02-27 19:17:27 +01:00
|
|
|
private Bind parseResourceBinding(XmlPullParser parser) throws IOException,
|
2007-02-21 01:57:31 +01:00
|
|
|
XmlPullParserException
|
|
|
|
{
|
2006-02-27 19:17:27 +01:00
|
|
|
Bind bind = new Bind();
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("resource")) {
|
|
|
|
bind.setResource(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (parser.getName().equals("jid")) {
|
|
|
|
bind.setJid(parser.nextText());
|
|
|
|
}
|
|
|
|
} else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("bind")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bind;
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:30 +01:00
|
|
|
/**
|
2007-02-21 01:57:31 +01:00
|
|
|
* A runnable to notify all listeners of a packet.
|
2003-01-15 16:01:30 +01:00
|
|
|
*/
|
2007-02-21 01:57:31 +01:00
|
|
|
private class ListenerNotification implements Runnable {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
private Packet packet;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
public ListenerNotification(Packet packet) {
|
|
|
|
this.packet = packet;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-01-17 19:26:14 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
public void run() {
|
|
|
|
for (ListenerWrapper listenerWrapper : listeners.values()) {
|
|
|
|
listenerWrapper.notifyListener(packet);
|
2003-01-17 19:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2007-02-21 01:57:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper class to associate a packet filter with a listener.
|
|
|
|
*/
|
|
|
|
private static class ListenerWrapper {
|
|
|
|
|
|
|
|
private PacketListener packetListener;
|
|
|
|
private PacketFilter packetFilter;
|
2003-01-17 19:26:14 +01:00
|
|
|
|
2007-02-21 01:57:31 +01:00
|
|
|
public ListenerWrapper(PacketListener packetListener, PacketFilter packetFilter) {
|
|
|
|
this.packetListener = packetListener;
|
|
|
|
this.packetFilter = packetFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyListener(Packet packet) {
|
|
|
|
if (packetFilter == null || packetFilter.accept(packet)) {
|
2003-01-17 19:26:14 +01:00
|
|
|
packetListener.processPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-10-21 19:39:04 +02:00
|
|
|
}
|