diff --git a/source/org/jivesoftware/smack/PacketWriter.java b/source/org/jivesoftware/smack/PacketWriter.java index 66662cb8b..6aeceed8f 100644 --- a/source/org/jivesoftware/smack/PacketWriter.java +++ b/source/org/jivesoftware/smack/PacketWriter.java @@ -43,6 +43,16 @@ class PacketWriter { private boolean listenersDeleted = false; private Thread listenerThread; private LinkedList sentPackets = new LinkedList(); + /** + * 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. + */ + private List interceptors = new ArrayList(); + /** + * Flag that indicates if an interceptor was deleted. This is an optimization flag. + */ + private boolean interceptorDeleted = false; /** * Creates a new packet writer with the specified connection. @@ -87,6 +97,10 @@ class PacketWriter { */ public void sendPacket(Packet packet) { if (!done) { + // Invoke interceptors for the new packet that is about to be sent. Interceptors + // may modify the content of the packet. + processInterceptors(packet); + synchronized(queue) { queue.addFirst(packet); queue.notifyAll(); @@ -144,6 +158,40 @@ class PacketWriter { } } + /** + * 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=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