KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > pipeserver > Data


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

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 JavaDoc 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 JavaDoc 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 JavaDoc remoteHost) {
51         this.remoteHost = remoteHost;
52     }
53     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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                 //e.printStackTrace();
119
}
120         }//end of while
121
}
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"); //: "+new String(data));
127
try {
128             bout.write(data, 0, data.length);
129             bout.flush();
130         } catch(Exception JavaDoc 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 JavaDoc makeObject() {
157                 return new Data();
158             }
159             public void passivateObject(Object JavaDoc obj) {
160                 Data ed = (Data)obj;
161                 ed.clean();
162             }
163             public void destroyObject(Object JavaDoc obj) {
164                 if(obj==null) return;
165                 passivateObject(obj);
166                 obj = null;
167             }
168             public boolean validateObject(Object JavaDoc obj) {
169                 if(obj==null)
170                     return false;
171                 else
172                     return true;
173             }
174         };
175     }
176
177     //-- helper methods --
178
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; //Connection lost
188
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