1 14 15 package pipeserver; 16 17 import org.quickserver.net.server.ClientData; 18 import org.quickserver.util.pool.PoolableObject; 19 import org.apache.commons.pool.BasePoolableObjectFactory; 20 import org.apache.commons.pool.PoolableObjectFactory; 21 22 import java.net.*; 23 import java.io.*; 24 import java.util.logging.*; 25 26 import org.quickserver.net.server.ClientHandler; 27 28 public class Data extends Thread implements ClientData, PoolableObject { 29 private static Logger logger = Logger.getLogger(Data.class.getName()); 30 31 private Socket socket; 32 private ClientHandler handler; 33 private BufferedInputStream bin; 34 private BufferedOutputStream bout; 35 36 37 private String remoteHost = "127.0.0.1"; 38 private int remotePort = 8080; 39 40 private boolean init = false; 41 private boolean closed = false; 42 43 44 public Data(){ 45 super("DataThread"); 46 setDaemon(true); 47 start(); 48 } 49 50 public void setRemoteHost(String remoteHost) { 51 this.remoteHost = remoteHost; 52 } 53 public String getRemoteHost() { 54 return remoteHost; 55 } 56 57 public void setRemotePort(int remotePort) { 58 this.remotePort = remotePort; 59 } 60 public int getRemotePort() { 61 return remotePort; 62 } 63 64 public void setClosed(boolean flag) { 65 closed = flag; 66 } 67 68 public void init(Socket socket, ClientHandler handler) { 69 this.socket = socket; 70 this.handler = handler; 71 closed = false; 72 try { 73 bin = new BufferedInputStream(socket.getInputStream()); 74 bout = new BufferedOutputStream(socket.getOutputStream()); 75 init = true; 76 } catch(Exception e) { 77 logger.warning("Error in init: "+e); 78 handler.closeConnection(); 79 init = false; 80 closed = true; 81 } 82 } 83 84 public void preclean() { 85 try { 86 if(bin!=null) bin.close(); 87 if(bout!=null) bout.close(); 88 if(socket!=null) socket.close(); 89 } catch(Exception e) { 90 logger.fine("Error in preclean: "+e); 91 } 92 } 93 94 public void run() { 95 byte data[] = null; 96 while(true) { 97 try { 98 if(init==false) { 99 sleep(50); 100 continue; 101 } 102 103 data = readInputStream(bin); 104 if(data==null) { 105 init = false; 106 logger.fine("Connection lost from remote pipe"); 107 handler.closeConnection(); 108 } else { 109 handler.sendClientBinary(data); 110 } 111 } catch(Exception e) { 112 init = false; 113 if(closed==false) { 114 logger.warning("Error in data thread : "+e); 115 } else { 116 logger.fine("Error after connection was closed in data thread : "+e); 117 } 118 } 120 } } 122 123 public void sendData(byte data[]) throws IOException { 124 if(init==false) 125 throw new IOException("Data is not yet init!"); 126 logger.fine("Sending data to pipe"); try { 128 bout.write(data, 0, data.length); 129 bout.flush(); 130 } catch(Exception e) { 131 if(closed==false) { 132 logger.warning("Error sending data : "+e); 133 throw new IOException(e.getMessage()); 134 } else { 135 logger.fine("Error after connection was closed : sending data : "+e); 136 } 137 } 138 } 139 140 public void clean() { 141 socket = null; 142 init = false; 143 handler = null; 144 bin = null; 145 bout = null; 146 remoteHost = "127.0.0.1"; 147 remotePort = 8080; 148 } 149 150 public boolean isPoolable() { 151 return true; 152 } 153 154 public PoolableObjectFactory getPoolableObjectFactory() { 155 return new BasePoolableObjectFactory() { 156 public Object makeObject() { 157 return new Data(); 158 } 159 public void passivateObject(Object obj) { 160 Data ed = (Data)obj; 161 ed.clean(); 162 } 163 public void destroyObject(Object obj) { 164 if(obj==null) return; 165 passivateObject(obj); 166 obj = null; 167 } 168 public boolean validateObject(Object obj) { 169 if(obj==null) 170 return false; 171 else 172 return true; 173 } 174 }; 175 } 176 177 private static byte[] readInputStream(BufferedInputStream bin) 179 throws IOException { 180 if(bin==null) { 181 logger.warning("BufferedInputStream was null ! "); 182 return null; 183 } 184 byte data[] = null; 185 int s = bin.read(); 186 if(s==-1) 187 return null; int alength = bin.available(); 189 if(alength > 0) { 190 data = new byte[alength+1]; 191 data[0] = (byte)s; 192 bin.read(data, 1, alength); 193 } else { 194 data = new byte[1]; 195 data[0] = (byte)s; 196 } 197 return data; 198 } 199 } 200 | Popular Tags |