/*
*
* Hamsam - Instant Messaging API
*
* Copyright (C) 2003 Mike Miller <mikemil@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package hamsam.protocol.aim;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Vector;
import java.util.logging.Logger;
import hamsam.net.Connection;
import hamsam.util.log.LogManager;
import hamsam.protocol.aim.command.Command;
import hamsam.protocol.aim.util.ByteUtils;
/**
* A thread to send all outgoing packets to Yahoo.
*
* @author Raghu
*/
public class AIMWriterThread extends Thread {
Logger log = LogManager.getLogger();
//~ Instance fields ----------------------------------------------------------------------------
/**
* The connection used for writing.
*/
private Connection conn;
/**
* The buffer from which to read.
*/
private Vector buffer;
/**
* Flag to stop this thread.
*/
private boolean quit;
//~ Constructors -------------------------------------------------------------------------------
/**
* Construct a new writer thread which reads from a specified buffer and
* writes to a specified connection.
*
* @param protocol the parent YahooProtocol thread that created this writer thread.
* @param conn the connection for writing.
* @param buffer the buffer from which to get the packets.
*/
public AIMWriterThread(Connection conn, Vector buffer) {
this.conn = conn;
this.buffer = buffer;
this.quit = false;
setName("hamsam.protocol.aim.AIMWriterThread");
setDaemon(true);
}
//~ Methods ------------------------------------------------------------------------------------
/**
* Change the connection object and start using a new one. This
* will close the old connection if it is not already closed.
*
* @param conn the new connection object.
*/
public void changeConnection(Connection conn) {
synchronized (this.conn) {
try {
this.conn.close();
} catch (IOException e) {
/* we may get an exception because the connection is already closed */
}
this.conn = conn;
}
}
/**
* The thread starts here.
*/
public void run() {
while (!quit) {
Command cmd = null;
synchronized (buffer) {
while (buffer.isEmpty()) {
try {
buffer.wait();
} catch (InterruptedException e) {
log.severe("writerThread interrupted!");
break;
}
}
if (buffer.isEmpty()) {
break;
}
cmd = (Command)buffer.remove(0);
}
try {
if (cmd != null) {
byte[] cmdBytes = cmd.getBytes();
StringWriter sw = new StringWriter();
ByteUtils.dump(cmdBytes, sw);
//log.fine(sw.toString());
log.severe(sw.toString());
conn.write(cmdBytes);
}
} catch (IOException e) {
return;
}
}
}
/**
* Stop this thread safely.
*/
public void stopWriting() {
quit = true;
this.interrupt();
}
}