[3952] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail/im me at jeti@jabber.org |
---|
| 21 | * |
---|
| 22 | * created 2001 |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | package nu.fw.jeti.backend; |
---|
| 26 | |
---|
| 27 | import java.io.BufferedWriter; |
---|
| 28 | import java.io.IOException; |
---|
| 29 | import java.io.OutputStream; |
---|
| 30 | import java.io.OutputStreamWriter; |
---|
| 31 | import java.io.UnsupportedEncodingException; |
---|
| 32 | import java.io.Writer; |
---|
| 33 | import java.net.Socket; |
---|
| 34 | import java.util.LinkedList; |
---|
| 35 | |
---|
| 36 | import nu.fw.jeti.jabber.elements.Packet; |
---|
| 37 | import nu.fw.jeti.plugins.PluginsInfo; |
---|
| 38 | import nu.fw.jeti.util.Preferences; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * @author E.S. de Boer |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | public class Output extends Thread |
---|
| 46 | { |
---|
| 47 | private Writer out; |
---|
| 48 | private LinkedList queue = new LinkedList(); |
---|
| 49 | private volatile boolean isRunning=true; |
---|
| 50 | private volatile boolean closeSocketOnExit=true; |
---|
| 51 | private Socket socket; |
---|
| 52 | public static final int KEEP_ALIVE = 60000; |
---|
| 53 | private KeepAlivePacket keepAlivePacket = new KeepAlivePacket(); |
---|
| 54 | private ConnectionPacketReceiver backend; |
---|
| 55 | private volatile boolean authenticated = false; |
---|
| 56 | private String host; |
---|
| 57 | |
---|
| 58 | public Output(Socket socket,String host,ConnectionPacketReceiver backend,OutputStream stream) throws IOException |
---|
| 59 | { |
---|
| 60 | this.socket = socket; |
---|
| 61 | this.backend = backend; |
---|
| 62 | this.host = host; |
---|
| 63 | try{ |
---|
| 64 | out = new BufferedWriter(new OutputStreamWriter(stream,"UTF8")); |
---|
| 65 | }catch (UnsupportedEncodingException e){e.printStackTrace();} |
---|
| 66 | writeHeader(); |
---|
| 67 | start(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public void writeHeader() throws IOException |
---|
| 71 | { |
---|
| 72 | //send stream init tag |
---|
| 73 | out.write("<?xml version='1.0' encoding='UTF-8'?>"); |
---|
| 74 | out.write("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' "); |
---|
| 75 | out.write("xmlns='jabber:client' "); |
---|
| 76 | if(PluginsInfo.isPluginLoaded("xmpp"))out.write("version='1.0' "); |
---|
| 77 | out.write("to='"); |
---|
| 78 | out.write(host); |
---|
| 79 | String lang = Preferences.getString("jeti","language",null); |
---|
| 80 | if(lang!=null) |
---|
| 81 | { |
---|
| 82 | String country = Preferences.getString("jeti","country",null); |
---|
| 83 | if(country!=null)lang+="-"+country; |
---|
| 84 | out.write("' xml:lang='"); |
---|
| 85 | out.write(lang); |
---|
| 86 | } |
---|
| 87 | out.write("'>"); |
---|
| 88 | out.flush(); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void send(Packet p) |
---|
| 92 | { |
---|
| 93 | if(isRunning) |
---|
| 94 | { |
---|
| 95 | synchronized(queue) |
---|
| 96 | { |
---|
| 97 | queue.addLast(p); |
---|
| 98 | queue.notifyAll(); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | public void disconnect(boolean closeSocket) |
---|
| 104 | { |
---|
| 105 | this.closeSocketOnExit = closeSocket; |
---|
| 106 | isRunning = false; |
---|
| 107 | synchronized(queue){queue.notifyAll();} |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | public void setAuthenticated() |
---|
| 111 | { |
---|
| 112 | authenticated = true; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public final void run() |
---|
| 116 | { |
---|
| 117 | Packet packet; |
---|
| 118 | while ((isRunning) || (!queue.isEmpty())) |
---|
| 119 | { |
---|
| 120 | synchronized(queue) |
---|
| 121 | { |
---|
| 122 | if (queue.isEmpty()) |
---|
| 123 | { |
---|
| 124 | if(isRunning) |
---|
| 125 | { |
---|
| 126 | try |
---|
| 127 | { |
---|
| 128 | queue.wait(KEEP_ALIVE); |
---|
| 129 | } |
---|
| 130 | catch(InterruptedException e) |
---|
| 131 | { |
---|
| 132 | e.printStackTrace(); |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | if(authenticated && queue.isEmpty()) queue.add(keepAlivePacket); |
---|
| 136 | } |
---|
| 137 | continue; |
---|
| 138 | } |
---|
| 139 | else packet =(Packet) queue.removeFirst(); |
---|
| 140 | } |
---|
| 141 | //send packet |
---|
| 142 | String xml = packet.toString(); |
---|
| 143 | try |
---|
| 144 | { |
---|
| 145 | nu.fw.jeti.util.Log.sendXML(xml); |
---|
| 146 | out.write(xml); |
---|
| 147 | out.flush(); |
---|
| 148 | } |
---|
| 149 | catch(IOException e) |
---|
| 150 | { |
---|
| 151 | if(!socket.isClosed()) |
---|
| 152 | { |
---|
| 153 | nu.fw.jeti.util.Log.notSend(xml); |
---|
| 154 | if(isRunning) backend.outputDeath();//only reconnect if output thread in running status |
---|
| 155 | try{ |
---|
| 156 | System.out.println("closing socket"); |
---|
| 157 | socket.close(); |
---|
| 158 | } catch (IOException ex){ } |
---|
| 159 | } |
---|
| 160 | return; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | if(closeSocketOnExit && !socket.isClosed()) |
---|
| 164 | { |
---|
| 165 | try |
---|
| 166 | {//close stream |
---|
| 167 | out.write("</stream:stream>"); |
---|
| 168 | out.flush(); |
---|
| 169 | System.out.println("closing socket"); |
---|
| 170 | socket.close(); |
---|
| 171 | |
---|
| 172 | } |
---|
| 173 | catch(Exception e) |
---|
| 174 | {e.printStackTrace();} |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | class KeepAlivePacket extends Packet |
---|
| 180 | { |
---|
| 181 | public void appendToXML(StringBuffer xml) |
---|
| 182 | { |
---|
| 183 | xml.append(' '); |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /* |
---|
| 188 | * Overrides for emacs |
---|
| 189 | * Local variables: |
---|
| 190 | * tab-width: 4 |
---|
| 191 | * End: |
---|
| 192 | */ |
---|