1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +01:00

Code cleanup.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1781 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2003-01-15 15:01:01 +00:00 committed by mtucker
parent 4e8a84834f
commit 6a1b7c6c2f

View file

@ -58,32 +58,68 @@ import java.io.*;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
/** /**
* Writes packets to a Jabber server. * Writes packets to an XMPP server.
* *
* @see XMPPConnection#getPacketWriter() * @see XMPPConnection#getPacketWriter()
* @author Matt Tucker * @author Matt Tucker
*/ */
public class PacketWriter extends Thread { public class PacketWriter {
private Thread writerThread;
private Writer writer; private Writer writer;
private XMPPConnection connection; private XMPPConnection connection;
private LinkedList queue; private LinkedList queue;
private boolean done = false; private boolean done = false;
/**
* Creates a new packet writer with the specified connection.
*
* @param connection the connection.
*/
protected PacketWriter(XMPPConnection connection) { protected PacketWriter(XMPPConnection connection) {
this.connection = connection; this.connection = connection;
this.writer = connection.writer; this.writer = connection.writer;
this.queue = new LinkedList(); 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) { public void sendPacket(Packet packet) {
if (!done) {
synchronized(queue) { synchronized(queue) {
queue.addFirst(packet); queue.addFirst(packet);
queue.notify(); 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. * 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 { try {
// Open the stream. // Open the stream.
StringBuffer stream = new StringBuffer(); StringBuffer stream = new StringBuffer();
stream.append("<stream:stream"); stream.append("<stream:stream");
stream.append(" to=\"" + connection.getHost() + "\"");
stream.append(" xmlns=\"jabber:client\""); stream.append(" xmlns=\"jabber:client\"");
stream.append("xmlns:stream=\"http://etherx.jabber.org/streams\" "); stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\">");
stream.append("xmlns:sasl=\"http://www.iana.org/assignments/sasl-mechanisms\" "); // stream.append("xmlns:sasl=\"http://www.iana.org/assignments/sasl-mechanisms\" ");
stream.append("to=\"" + connection.getHost() + "\">");
writer.write(stream.toString()); writer.write(stream.toString());
writer.flush(); writer.flush();
// Write out packets from the queue. // Write out packets from the queue.
@ -134,12 +170,9 @@ public class PacketWriter extends Thread {
} }
catch (IOException ioe){ catch (IOException ioe){
ioe.printStackTrace(); ioe.printStackTrace();
connection.close();
} }
} }
public void shutdown() {
done = true;
}
} }