001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package pipeserver;
016:
017: import org.quickserver.net.server.ClientData;
018: import org.quickserver.util.pool.PoolableObject;
019: import org.apache.commons.pool.BasePoolableObjectFactory;
020: import org.apache.commons.pool.PoolableObjectFactory;
021:
022: import java.net.*;
023: import java.io.*;
024: import java.util.logging.*;
025:
026: import org.quickserver.net.server.ClientHandler;
027:
028: public class Data extends Thread implements ClientData, PoolableObject {
029: private static Logger logger = Logger.getLogger(Data.class
030: .getName());
031:
032: private Socket socket;
033: private ClientHandler handler;
034: private BufferedInputStream bin;
035: private BufferedOutputStream bout;
036:
037: private String remoteHost = "127.0.0.1";
038: private int remotePort = 8080;
039:
040: private boolean init = false;
041: private boolean closed = false;
042:
043: public Data() {
044: super ("DataThread");
045: setDaemon(true);
046: start();
047: }
048:
049: public void setRemoteHost(String remoteHost) {
050: this .remoteHost = remoteHost;
051: }
052:
053: public String getRemoteHost() {
054: return remoteHost;
055: }
056:
057: public void setRemotePort(int remotePort) {
058: this .remotePort = remotePort;
059: }
060:
061: public int getRemotePort() {
062: return remotePort;
063: }
064:
065: public void setClosed(boolean flag) {
066: closed = flag;
067: }
068:
069: public void init(Socket socket, ClientHandler handler) {
070: this .socket = socket;
071: this .handler = handler;
072: closed = false;
073: try {
074: bin = new BufferedInputStream(socket.getInputStream());
075: bout = new BufferedOutputStream(socket.getOutputStream());
076: init = true;
077: } catch (Exception e) {
078: logger.warning("Error in init: " + e);
079: handler.closeConnection();
080: init = false;
081: closed = true;
082: }
083: }
084:
085: public void preclean() {
086: try {
087: if (bin != null)
088: bin.close();
089: if (bout != null)
090: bout.close();
091: if (socket != null)
092: socket.close();
093: } catch (Exception e) {
094: logger.fine("Error in preclean: " + e);
095: }
096: }
097:
098: public void run() {
099: byte data[] = null;
100: while (true) {
101: try {
102: if (init == false) {
103: sleep(50);
104: continue;
105: }
106:
107: data = readInputStream(bin);
108: if (data == null) {
109: init = false;
110: logger.fine("Connection lost from remote pipe");
111: handler.closeConnection();
112: } else {
113: handler.sendClientBinary(data);
114: }
115: } catch (Exception e) {
116: init = false;
117: if (closed == false) {
118: logger.warning("Error in data thread : " + e);
119: } else {
120: logger
121: .fine("Error after connection was closed in data thread : "
122: + e);
123: }
124: //e.printStackTrace();
125: }
126: }//end of while
127: }
128:
129: public void sendData(byte data[]) throws IOException {
130: if (init == false)
131: throw new IOException("Data is not yet init!");
132: logger.fine("Sending data to pipe"); //: "+new String(data));
133: try {
134: bout.write(data, 0, data.length);
135: bout.flush();
136: } catch (Exception e) {
137: if (closed == false) {
138: logger.warning("Error sending data : " + e);
139: throw new IOException(e.getMessage());
140: } else {
141: logger
142: .fine("Error after connection was closed : sending data : "
143: + e);
144: }
145: }
146: }
147:
148: public void clean() {
149: socket = null;
150: init = false;
151: handler = null;
152: bin = null;
153: bout = null;
154: remoteHost = "127.0.0.1";
155: remotePort = 8080;
156: }
157:
158: public boolean isPoolable() {
159: return true;
160: }
161:
162: public PoolableObjectFactory getPoolableObjectFactory() {
163: return new BasePoolableObjectFactory() {
164: public Object makeObject() {
165: return new Data();
166: }
167:
168: public void passivateObject(Object obj) {
169: Data ed = (Data) obj;
170: ed.clean();
171: }
172:
173: public void destroyObject(Object obj) {
174: if (obj == null)
175: return;
176: passivateObject(obj);
177: obj = null;
178: }
179:
180: public boolean validateObject(Object obj) {
181: if (obj == null)
182: return false;
183: else
184: return true;
185: }
186: };
187: }
188:
189: //-- helper methods --
190: private static byte[] readInputStream(BufferedInputStream bin)
191: throws IOException {
192: if (bin == null) {
193: logger.warning("BufferedInputStream was null ! ");
194: return null;
195: }
196: byte data[] = null;
197: int s = bin.read();
198: if (s == -1)
199: return null; //Connection lost
200: int alength = bin.available();
201: if (alength > 0) {
202: data = new byte[alength + 1];
203: data[0] = (byte) s;
204: bin.read(data, 1, alength);
205: } else {
206: data = new byte[1];
207: data[0] = (byte) s;
208: }
209: return data;
210: }
211: }
|