diff --git a/source/org/jivesoftware/smack/PacketWriter.java b/source/org/jivesoftware/smack/PacketWriter.java index 897431d16..148f0d667 100644 --- a/source/org/jivesoftware/smack/PacketWriter.java +++ b/source/org/jivesoftware/smack/PacketWriter.java @@ -58,33 +58,69 @@ import java.io.*; import org.jivesoftware.smack.packet.Packet; /** - * Writes packets to a Jabber server. + * Writes packets to an XMPP server. * * @see XMPPConnection#getPacketWriter() * @author Matt Tucker */ -public class PacketWriter extends Thread { +public class PacketWriter { + private Thread writerThread; private Writer writer; private XMPPConnection connection; private LinkedList queue; private boolean done = false; + /** + * Creates a new packet writer with the specified connection. + * + * @param connection the connection. + */ protected PacketWriter(XMPPConnection connection) { this.connection = connection; this.writer = connection.writer; this.queue = new LinkedList(); - // This should be a daemon thread. - setDaemon(true); + + writerThread = new Thread() { + public void run() { + writePackets(); + } + }; + writerThread.setName("Smack Packet Writer"); + writerThread.setDaemon(true); } + /** + * Sends the specified packet to the server. + * + * @param packet the packet to send. + */ public void sendPacket(Packet packet) { - synchronized(queue) { - queue.addFirst(packet); - queue.notify(); + if (!done) { + synchronized(queue) { + queue.addFirst(packet); + queue.notify(); + } } } + /** + * 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(); + } + + /** + * 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; + } + /** * Returns the next available packet from the queue for writing. * @@ -102,15 +138,15 @@ public class PacketWriter extends Thread { } } - public void run() { + private void writePackets() { try { // Open the stream. StringBuffer stream = new StringBuffer(); - stream.append(""); + stream.append(""); +// stream.append("xmlns:sasl=\"http://www.iana.org/assignments/sasl-mechanisms\" "); writer.write(stream.toString()); writer.flush(); // Write out packets from the queue. @@ -134,12 +170,9 @@ public class PacketWriter extends Thread { } catch (IOException ioe){ ioe.printStackTrace(); + connection.close(); } } - - public void shutdown() { - done = true; - } }