2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Copyright 2003-2004 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;
|
|
|
|
|
2003-11-18 18:06:47 +01:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
2003-01-13 17:58:47 +01:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
|
2006-06-10 01:24:54 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2007-01-11 20:01:24 +01:00
|
|
|
import java.util.Queue;
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2006-06-10 01:24:54 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2006-02-09 00:11:21 +01:00
|
|
|
* Writes packets to a XMPP server. Packets are sent using a dedicated thread. Packet
|
|
|
|
* interceptors can be registered to dynamically modify packets before they're actually
|
|
|
|
* sent. Packet listeners can be registered to listen for all outgoing packets.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
class PacketWriter {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
private Thread writerThread;
|
2006-09-14 21:16:40 +02:00
|
|
|
private Thread keepAliveThread;
|
2003-01-13 17:58:47 +01:00
|
|
|
private Writer writer;
|
|
|
|
private XMPPConnection connection;
|
2007-01-11 20:01:24 +01:00
|
|
|
final private Queue<Packet> queue;
|
2006-09-14 21:16:40 +02:00
|
|
|
private boolean done;
|
2003-11-15 20:24:00 +01:00
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
final protected List<ListenerWrapper> listeners = new ArrayList<ListenerWrapper>();
|
|
|
|
private boolean listenersDeleted;
|
2006-02-09 00:11:21 +01:00
|
|
|
|
2006-06-12 18:32:18 +02:00
|
|
|
/**
|
|
|
|
* Timestamp when the last stanza was sent to the server. This information is used
|
|
|
|
* by the keep alive process to only send heartbeats when the connection has been idle.
|
|
|
|
*/
|
|
|
|
private long lastActive = System.currentTimeMillis();
|
|
|
|
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* List of PacketInterceptor that will be notified when a new packet is about to be
|
|
|
|
* sent to the server. These interceptors may modify the packet before it is being
|
|
|
|
* actually sent to the server.
|
|
|
|
*/
|
2006-06-12 18:32:18 +02:00
|
|
|
final private List interceptors = new ArrayList();
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* Flag that indicates if an interceptor was deleted. This is an optimization flag.
|
|
|
|
*/
|
|
|
|
private boolean interceptorDeleted = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Creates a new packet writer with the specified connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
protected PacketWriter(XMPPConnection connection) {
|
2007-01-11 20:01:24 +01:00
|
|
|
this.queue = new ConcurrentLinkedQueue<Packet>();
|
2003-01-13 17:58:47 +01:00
|
|
|
this.connection = connection;
|
2006-09-14 21:16:40 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the writer in order to be used. It is called at the first connection and also
|
|
|
|
* is invoked if the connection is disconnected by an error.
|
|
|
|
*/
|
|
|
|
protected void init() {
|
2003-01-13 17:58:47 +01:00
|
|
|
this.writer = connection.writer;
|
2006-09-14 21:16:40 +02:00
|
|
|
listenersDeleted = false;
|
|
|
|
interceptorDeleted = false;
|
|
|
|
done = false;
|
2003-01-15 16:01:01 +01:00
|
|
|
|
|
|
|
writerThread = new Thread() {
|
|
|
|
public void run() {
|
2006-09-14 21:16:40 +02:00
|
|
|
writePackets(this);
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
|
|
|
};
|
2007-02-04 21:52:45 +01:00
|
|
|
writerThread.setName("Smack Packet Writer (" + connection.connectionCounterValue + ")");
|
2003-01-15 16:01:01 +01:00
|
|
|
writerThread.setDaemon(true);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Sends the specified packet to the server.
|
|
|
|
*
|
|
|
|
* @param packet the packet to send.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public void sendPacket(Packet packet) {
|
2003-01-15 16:01:01 +01:00
|
|
|
if (!done) {
|
2005-11-02 00:49:07 +01:00
|
|
|
// Invoke interceptors for the new packet that is about to be sent. Interceptors
|
|
|
|
// may modify the content of the packet.
|
|
|
|
processInterceptors(packet);
|
|
|
|
|
2007-01-11 20:01:24 +01:00
|
|
|
queue.add(packet);
|
|
|
|
synchronized (queue) {
|
2004-04-08 01:49:00 +02:00
|
|
|
queue.notifyAll();
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
2006-02-09 00:11:21 +01:00
|
|
|
|
|
|
|
// Process packet writer listeners. Note that we're using the sending
|
|
|
|
// thread so it's expected that listeners are fast.
|
|
|
|
processListeners(packet);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-21 19:39:18 +02:00
|
|
|
/**
|
2003-11-18 18:06:47 +01:00
|
|
|
* Registers a packet listener with this writer. The listener will be
|
2006-02-09 00:11:21 +01:00
|
|
|
* notified immediately after every packet this writer sends. A packet filter
|
|
|
|
* determines which packets will be delivered to the listener. Note that the thread
|
|
|
|
* that writes packets will be used to invoke the listeners. Therefore, each
|
|
|
|
* packet listener should complete all operations quickly or use a different
|
|
|
|
* thread for processing.
|
2003-10-21 19:39:18 +02:00
|
|
|
*
|
2003-11-18 18:06:47 +01:00
|
|
|
* @param packetListener the packet listener to notify of sent packets.
|
|
|
|
* @param packetFilter the packet filter to use.
|
2003-10-21 19:39:18 +02:00
|
|
|
*/
|
2003-11-18 18:06:47 +01:00
|
|
|
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
|
2003-11-15 20:24:00 +01:00
|
|
|
synchronized (listeners) {
|
2003-11-18 18:06:47 +01:00
|
|
|
listeners.add(new ListenerWrapper(packetListener, packetFilter));
|
2003-11-15 20:24:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-11-18 18:06:47 +01:00
|
|
|
* Removes a packet listener.
|
2003-11-15 20:24:00 +01:00
|
|
|
*
|
2003-11-18 18:06:47 +01:00
|
|
|
* @param packetListener the packet listener to remove.
|
2003-11-15 20:24:00 +01:00
|
|
|
*/
|
2003-11-18 18:06:47 +01:00
|
|
|
public void removePacketListener(PacketListener packetListener) {
|
2003-11-15 20:24:00 +01:00
|
|
|
synchronized (listeners) {
|
2004-01-15 14:05:44 +01:00
|
|
|
for (int i=0; i<listeners.size(); i++) {
|
2006-07-18 07:14:33 +02:00
|
|
|
ListenerWrapper wrapper = listeners.get(i);
|
2004-01-15 14:05:44 +01:00
|
|
|
if (wrapper != null && wrapper.packetListener.equals(packetListener)) {
|
|
|
|
listeners.set(i, null);
|
2004-08-09 23:15:04 +02:00
|
|
|
// Set the flag to indicate that the listener list needs
|
|
|
|
// to be cleaned up.
|
2004-08-09 23:47:00 +02:00
|
|
|
listenersDeleted = true;
|
2004-01-15 14:05:44 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-15 20:24:00 +01:00
|
|
|
}
|
2003-10-21 19:39:18 +02:00
|
|
|
}
|
|
|
|
|
2004-08-09 23:15:04 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of registered packet listeners.
|
|
|
|
*
|
|
|
|
* @return the count of packet listeners.
|
|
|
|
*/
|
|
|
|
public int getPacketListenerCount() {
|
|
|
|
synchronized (listeners) {
|
|
|
|
return listeners.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* Registers a packet interceptor with this writer. The interceptor will be
|
|
|
|
* notified of every packet that this writer is about to send. Interceptors
|
|
|
|
* may modify the packet to be sent. A packet filter determines which packets
|
|
|
|
* will be delivered to the interceptor.
|
|
|
|
*
|
|
|
|
* @param packetInterceptor the packet interceptor to notify of packets about to be sent.
|
|
|
|
* @param packetFilter the packet filter to use.
|
|
|
|
*/
|
|
|
|
public void addPacketInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter) {
|
|
|
|
synchronized (interceptors) {
|
|
|
|
interceptors.add(new InterceptorWrapper(packetInterceptor, packetFilter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a packet interceptor.
|
|
|
|
*
|
|
|
|
* @param packetInterceptor the packet interceptor to remove.
|
|
|
|
*/
|
|
|
|
public void removePacketInterceptor(PacketInterceptor packetInterceptor) {
|
|
|
|
synchronized (interceptors) {
|
|
|
|
for (int i=0; i<interceptors.size(); i++) {
|
|
|
|
InterceptorWrapper wrapper = (InterceptorWrapper)interceptors.get(i);
|
|
|
|
if (wrapper != null && wrapper.packetInterceptor.equals(packetInterceptor)) {
|
|
|
|
interceptors.set(i, null);
|
|
|
|
// Set the flag to indicate that the interceptor list needs
|
|
|
|
// to be cleaned up.
|
|
|
|
interceptorDeleted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Starts the packet writer thread and opens a connection to the server. The
|
|
|
|
* packet writer will continue writing packets until {@link #shutdown} or an
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
public void startup() {
|
|
|
|
writerThread.start();
|
|
|
|
}
|
|
|
|
|
2006-06-12 18:32:18 +02:00
|
|
|
/**
|
|
|
|
* Starts the keep alive process. A white space (aka heartbeat) is going to be
|
|
|
|
* sent to the server every 30 seconds (by default) since the last stanza was sent
|
|
|
|
* to the server.
|
|
|
|
*/
|
|
|
|
void startKeepAliveProcess() {
|
|
|
|
// Schedule a keep-alive task to run if the feature is enabled. will write
|
|
|
|
// out a space character each time it runs to keep the TCP/IP connection open.
|
|
|
|
int keepAliveInterval = SmackConfiguration.getKeepAliveInterval();
|
|
|
|
if (keepAliveInterval > 0) {
|
2006-09-14 21:16:40 +02:00
|
|
|
KeepAliveTask target = new KeepAliveTask(keepAliveInterval);
|
|
|
|
keepAliveThread = new Thread(target);
|
|
|
|
target.setThread(keepAliveThread);
|
2006-06-12 18:32:18 +02:00
|
|
|
keepAliveThread.setDaemon(true);
|
2007-02-04 21:52:45 +01:00
|
|
|
keepAliveThread.setName("Smack Keep Alive (" + connection.connectionCounterValue + ")");
|
2006-06-12 18:32:18 +02:00
|
|
|
keepAliveThread.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
void setWriter(Writer writer) {
|
|
|
|
this.writer = writer;
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Shuts down the packet writer. Once this method has been called, no further
|
|
|
|
* packets will be written to the server.
|
|
|
|
*/
|
|
|
|
public void shutdown() {
|
|
|
|
done = true;
|
2007-01-11 20:01:24 +01:00
|
|
|
synchronized (queue) {
|
|
|
|
queue.notifyAll();
|
|
|
|
}
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the next available packet from the queue for writing.
|
|
|
|
*
|
|
|
|
* @return the next packet for writing.
|
|
|
|
*/
|
|
|
|
private Packet nextPacket() {
|
2007-01-11 20:01:24 +01:00
|
|
|
Packet packet = null;
|
|
|
|
// Wait until there's a packet or we're done.
|
|
|
|
while (!done && (packet = queue.poll()) == null) {
|
|
|
|
try {
|
|
|
|
synchronized (queue) {
|
|
|
|
queue.wait();
|
2006-06-12 18:32:18 +02:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
2004-01-13 19:13:23 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
return packet;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
private void writePackets(Thread thisThread) {
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
|
|
|
// Open the stream.
|
2005-08-27 04:29:04 +02:00
|
|
|
openStream();
|
2003-01-13 17:58:47 +01:00
|
|
|
// Write out packets from the queue.
|
2006-09-14 21:16:40 +02:00
|
|
|
while (!done && (writerThread == thisThread)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
Packet packet = nextPacket();
|
2004-01-13 19:13:23 +01:00
|
|
|
if (packet != null) {
|
2004-01-28 22:30:37 +01:00
|
|
|
synchronized (writer) {
|
|
|
|
writer.write(packet.toXML());
|
|
|
|
writer.flush();
|
2006-06-12 18:32:18 +02:00
|
|
|
// Keep track of the last time a stanza was sent to the server
|
|
|
|
lastActive = System.currentTimeMillis();
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
2004-01-13 19:13:23 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
// Flush out the rest of the queue.
|
|
|
|
try {
|
|
|
|
synchronized (writer) {
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
Packet packet = queue.remove();
|
|
|
|
writer.write(packet.toXML());
|
|
|
|
}
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
// Close the stream.
|
|
|
|
try {
|
2003-05-16 17:42:41 +02:00
|
|
|
writer.write("</stream:stream>");
|
2003-01-13 17:58:47 +01:00
|
|
|
writer.flush();
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
finally {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe){
|
2003-01-27 16:56:21 +01:00
|
|
|
if (!done) {
|
2003-09-19 00:35:42 +02:00
|
|
|
done = true;
|
|
|
|
connection.packetReader.notifyConnectionError(ioe);
|
2003-01-27 16:56:21 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-15 20:24:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process listeners.
|
|
|
|
*/
|
2006-02-09 00:11:21 +01:00
|
|
|
private void processListeners(Packet packet) {
|
|
|
|
// 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) {
|
|
|
|
if (listenersDeleted) {
|
|
|
|
for (int i=listeners.size()-1; i>=0; i--) {
|
|
|
|
if (listeners.get(i) == null) {
|
|
|
|
listeners.remove(i);
|
2003-11-15 20:24:00 +01:00
|
|
|
}
|
2004-01-13 19:13:23 +01:00
|
|
|
}
|
2006-02-09 00:11:21 +01:00
|
|
|
listenersDeleted = false;
|
2003-11-15 20:24:00 +01:00
|
|
|
}
|
2006-02-09 00:11:21 +01:00
|
|
|
}
|
|
|
|
// Notify the listeners of the new sent packet
|
|
|
|
int size = listeners.size();
|
|
|
|
for (int i=0; i<size; i++) {
|
2006-07-18 07:14:33 +02:00
|
|
|
ListenerWrapper listenerWrapper = listeners.get(i);
|
2006-02-09 00:11:21 +01:00
|
|
|
if (listenerWrapper != null) {
|
|
|
|
listenerWrapper.notifyListener(packet);
|
2003-11-15 20:24:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-18 18:06:47 +01:00
|
|
|
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* Process interceptors. Interceptors may modify the packet that is about to be sent.
|
|
|
|
* Since the thread that requested to send the packet will invoke all interceptors, it
|
|
|
|
* is important that interceptors perform their work as soon as possible so that the
|
|
|
|
* thread does not remain blocked for a long period.
|
|
|
|
*
|
|
|
|
* @param packet the packet that is going to be sent to the server
|
|
|
|
*/
|
|
|
|
private void processInterceptors(Packet packet) {
|
|
|
|
if (packet != null) {
|
|
|
|
// Clean up null entries in the interceptors 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 (interceptors) {
|
|
|
|
if (interceptorDeleted) {
|
|
|
|
for (int i=interceptors.size()-1; i>=0; i--) {
|
|
|
|
if (interceptors.get(i) == null) {
|
|
|
|
interceptors.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
interceptorDeleted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Notify the interceptors of the new packet to be sent
|
|
|
|
int size = interceptors.size();
|
|
|
|
for (int i=0; i<size; i++) {
|
|
|
|
InterceptorWrapper interceptorWrapper = (InterceptorWrapper)interceptors.get(i);
|
|
|
|
if (interceptorWrapper != null) {
|
|
|
|
interceptorWrapper.notifyListener(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
/**
|
|
|
|
* Sends to the server a new stream element. This operation may be requested several times
|
|
|
|
* so we need to encapsulate the logic in one place. This message will be sent while doing
|
|
|
|
* TLS, SASL and resource binding.
|
|
|
|
*
|
|
|
|
* @throws IOException If an error occurs while sending the stanza to the server.
|
|
|
|
*/
|
|
|
|
void openStream() throws IOException {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder stream = new StringBuilder();
|
2005-08-27 04:29:04 +02:00
|
|
|
stream.append("<stream:stream");
|
|
|
|
stream.append(" to=\"").append(connection.serviceName).append("\"");
|
|
|
|
stream.append(" xmlns=\"jabber:client\"");
|
|
|
|
stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
|
2006-07-18 08:47:38 +02:00
|
|
|
stream.append(" version=\"1.0\">");
|
2005-08-27 04:29:04 +02:00
|
|
|
writer.write(stream.toString());
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
|
2003-11-18 18:06:47 +01:00
|
|
|
/**
|
|
|
|
* A wrapper class to associate a packet filter with a listener.
|
|
|
|
*/
|
2006-09-14 21:16:40 +02:00
|
|
|
protected static class ListenerWrapper {
|
2003-11-18 18:06:47 +01:00
|
|
|
|
|
|
|
private PacketListener packetListener;
|
|
|
|
private PacketFilter packetFilter;
|
|
|
|
|
|
|
|
public ListenerWrapper(PacketListener packetListener,
|
2005-08-27 04:29:04 +02:00
|
|
|
PacketFilter packetFilter)
|
2003-11-18 18:06:47 +01:00
|
|
|
{
|
|
|
|
this.packetListener = packetListener;
|
|
|
|
this.packetFilter = 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 notifyListener(Packet packet) {
|
|
|
|
if (packetFilter == null || packetFilter.accept(packet)) {
|
|
|
|
packetListener.processPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-01-28 22:30:37 +01:00
|
|
|
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* A wrapper class to associate a packet filter with an interceptor.
|
|
|
|
*/
|
|
|
|
private static class InterceptorWrapper {
|
|
|
|
|
|
|
|
private PacketInterceptor packetInterceptor;
|
|
|
|
private PacketFilter packetFilter;
|
|
|
|
|
|
|
|
public InterceptorWrapper(PacketInterceptor packetInterceptor, PacketFilter packetFilter)
|
|
|
|
{
|
|
|
|
this.packetInterceptor = packetInterceptor;
|
|
|
|
this.packetFilter = packetFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (object == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (object instanceof InterceptorWrapper) {
|
|
|
|
return ((InterceptorWrapper) object).packetInterceptor
|
|
|
|
.equals(this.packetInterceptor);
|
|
|
|
}
|
|
|
|
else if (object instanceof PacketInterceptor) {
|
|
|
|
return object.equals(this.packetInterceptor);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyListener(Packet packet) {
|
|
|
|
if (packetFilter == null || packetFilter.accept(packet)) {
|
|
|
|
packetInterceptor.interceptPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-28 22:30:37 +01:00
|
|
|
/**
|
|
|
|
* A TimerTask that keeps connections to the server alive by sending a space
|
2004-08-09 23:15:04 +02:00
|
|
|
* character on an interval.
|
2004-01-28 22:30:37 +01:00
|
|
|
*/
|
2004-03-15 19:59:41 +01:00
|
|
|
private class KeepAliveTask implements Runnable {
|
|
|
|
|
|
|
|
private int delay;
|
2006-09-14 21:16:40 +02:00
|
|
|
private Thread thread;
|
2004-03-15 19:59:41 +01:00
|
|
|
|
|
|
|
public KeepAliveTask(int delay) {
|
|
|
|
this.delay = delay;
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
protected void setThread(Thread thread) {
|
|
|
|
this.thread = thread;
|
|
|
|
}
|
|
|
|
|
2004-01-28 22:30:37 +01:00
|
|
|
public void run() {
|
2006-06-10 01:24:54 +02:00
|
|
|
try {
|
|
|
|
// Sleep 15 seconds before sending first heartbeat. This will give time to
|
|
|
|
// properly finish TLS negotiation and then start sending heartbeats.
|
|
|
|
Thread.sleep(15000);
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2006-09-14 21:16:40 +02:00
|
|
|
while (!done && keepAliveThread == thread) {
|
2004-01-28 22:30:37 +01:00
|
|
|
synchronized (writer) {
|
2006-06-12 18:32:18 +02:00
|
|
|
// Send heartbeat if no packet has been sent to the server for a given time
|
|
|
|
if (System.currentTimeMillis() - lastActive >= delay) {
|
|
|
|
try {
|
|
|
|
writer.write(" ");
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
2004-03-15 23:58:48 +01:00
|
|
|
try {
|
2004-08-09 23:15:04 +02:00
|
|
|
// Sleep until we should write the next keep-alive.
|
2004-03-15 23:58:48 +01:00
|
|
|
Thread.sleep(delay);
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-05-16 17:42:41 +02:00
|
|
|
}
|