1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 16:22:06 +01:00

Adds notification for sent packets

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2167 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2003-11-15 19:24:00 +00:00 committed by gdombiak
parent da49a174c3
commit 24f965ead7
2 changed files with 140 additions and 8 deletions

View file

@ -52,7 +52,7 @@
package org.jivesoftware.smack;
import java.util.LinkedList;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.packet.Packet;
@ -69,7 +69,10 @@ class PacketWriter {
private XMPPConnection connection;
private LinkedList queue;
private boolean done = false;
private int packetsWritten = 0;
private List listeners = new ArrayList();
private Thread listenerThread;
private LinkedList sentPackets = new LinkedList();
/**
* Creates a new packet writer with the specified connection.
@ -88,6 +91,14 @@ class PacketWriter {
};
writerThread.setName("Smack Packet Writer");
writerThread.setDaemon(true);
listenerThread = new Thread() {
public void run() {
processListeners();
}
};
listenerThread.setName("Smack Writer Listener Processor");
listenerThread.setDaemon(true);
}
/**
@ -101,16 +112,36 @@ class PacketWriter {
queue.addFirst(packet);
queue.notify();
}
// Add the sent packet to the list of sent packets
// The PacketWriterListeners will be notified of the new packet
synchronized(sentPackets) {
sentPackets.addFirst(packet);
sentPackets.notify();
}
}
}
/**
* Returns the number of packets written through this packet writer.
* Registers a packet writer listener with this writer. The listener will be
* notified of every packet that this writer sends.
*
* @return the number of packets written.
* @param packetWriterListener the packet writer listener to notify of sent packets.
*/
public int getPacketsWritten() {
return packetsWritten;
public void addPacketListener(PacketWriterListener packetWriterListener) {
synchronized (listeners) {
listeners.add(packetWriterListener);
}
}
/**
* Removes a packet writer listener.
*
* @param packetWriterListener the packet writer listener to remove.
*/
public void removePacketListener(PacketWriterListener packetWriterListener) {
synchronized (listeners) {
listeners.remove(packetWriterListener);
}
}
/**
@ -120,6 +151,7 @@ class PacketWriter {
*/
public void startup() {
writerThread.start();
listenerThread.start();
}
/**
@ -162,8 +194,6 @@ class PacketWriter {
while (!done) {
Packet packet = nextPacket();
writer.write(packet.toXML());
// Increment the count of packets written.
packetsWritten++;
writer.flush();
}
// Close the stream.
@ -186,4 +216,31 @@ class PacketWriter {
}
}
}
/**
* Process listeners.
*/
private void processListeners() {
while (!done) {
Packet sentPacket;
// Wait until a new packet has been sent
synchronized(sentPackets) {
while (sentPackets.size() == 0) {
try {
sentPackets.wait();
}
catch (InterruptedException ie) { }
}
sentPacket = (Packet)sentPackets.removeLast();
}
// Notify the listeners of the new sent packet
int size = listeners.size();
for (int i=0; i<size; i++) {
PacketWriterListener packetWriterListener = (PacketWriterListener)listeners.get(i);
if (packetWriterListener != null) {
packetWriterListener.processPacket(sentPacket);
}
}
}
}
}

View file

@ -0,0 +1,75 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.Packet;
/**
* Provides a mechanism to listen for packets that are written to a XMPP server.
* This allows event-style programming -- every time a new packet is written,
* the {@link #processPacket(Packet)} method will be called.
*
* @see XMPPConnection#addPacketListener(PacketWriterListener)
*
* @author Gaston Dombiak
*/
public interface PacketWriterListener {
/**
* Process the next packet sent to this packet listener.<p>
*
* @param packet the packet to process.
*/
public void processPacket(Packet packet);
}