/* * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail/im me at jeti@jabber.org * * created 2001 */ package nu.fw.jeti.backend; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.Socket; import java.util.LinkedList; import nu.fw.jeti.jabber.elements.Packet; import nu.fw.jeti.plugins.PluginsInfo; import nu.fw.jeti.util.Preferences; /** * @author E.S. de Boer */ public class Output extends Thread { private Writer out; private LinkedList queue = new LinkedList(); private volatile boolean isRunning=true; private volatile boolean closeSocketOnExit=true; private Socket socket; public static final int KEEP_ALIVE = 60000; private KeepAlivePacket keepAlivePacket = new KeepAlivePacket(); private ConnectionPacketReceiver backend; private volatile boolean authenticated = false; private String host; public Output(Socket socket,String host,ConnectionPacketReceiver backend,OutputStream stream) throws IOException { this.socket = socket; this.backend = backend; this.host = host; try{ out = new BufferedWriter(new OutputStreamWriter(stream,"UTF8")); }catch (UnsupportedEncodingException e){e.printStackTrace();} writeHeader(); start(); } public void writeHeader() throws IOException { //send stream init tag out.write(""); out.write(""); out.flush(); } public void send(Packet p) { if(isRunning) { synchronized(queue) { queue.addLast(p); queue.notifyAll(); } } } public void disconnect(boolean closeSocket) { this.closeSocketOnExit = closeSocket; isRunning = false; synchronized(queue){queue.notifyAll();} } public void setAuthenticated() { authenticated = true; } public final void run() { Packet packet; while ((isRunning) || (!queue.isEmpty())) { synchronized(queue) { if (queue.isEmpty()) { if(isRunning) { try { queue.wait(KEEP_ALIVE); } catch(InterruptedException e) { e.printStackTrace(); return; } if(authenticated && queue.isEmpty()) queue.add(keepAlivePacket); } continue; } else packet =(Packet) queue.removeFirst(); } //send packet String xml = packet.toString(); try { nu.fw.jeti.util.Log.sendXML(xml); out.write(xml); out.flush(); } catch(IOException e) { if(!socket.isClosed()) { nu.fw.jeti.util.Log.notSend(xml); if(isRunning) backend.outputDeath();//only reconnect if output thread in running status try{ System.out.println("closing socket"); socket.close(); } catch (IOException ex){ } } return; } } if(closeSocketOnExit && !socket.isClosed()) { try {//close stream out.write(""); out.flush(); System.out.println("closing socket"); socket.close(); } catch(Exception e) {e.printStackTrace();} } } } class KeepAlivePacket extends Packet { public void appendToXML(StringBuffer xml) { xml.append(' '); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */