70 |
|
boolean run = true; |
71 |
|
// Tell the client our protocol, and ask it for it's name |
72 |
|
try { |
73 |
< |
_socketOut.println(PROTOVER); |
74 |
< |
_socketOut.flush(); |
73 |
> |
send(PROTOVER); |
74 |
|
_clientName = _socketIn.readLine(); |
75 |
|
if(_clientName==null) { |
76 |
|
throw new IOException("Fatal error reading from client"); |
77 |
|
} |
78 |
< |
_socketOut.println("OK"); |
80 |
< |
_socketOut.flush(); |
78 |
> |
sendOK(); |
79 |
|
} |
80 |
|
catch(IOException e) { |
81 |
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
82 |
|
run=false; |
83 |
|
} |
84 |
|
|
85 |
< |
_logger.write(toString(), Logger.SYSMSG, "Client has connected: "+_clientName); |
85 |
> |
_logger.write(toString(), Logger.DEBUG, "Client has connected: "+_clientName); |
86 |
|
|
87 |
|
// loop until we decide to shutdown (run=false) |
88 |
|
while(run) { |
95 |
|
if(cmd.equals("STARTCONFIG")) { |
96 |
|
// get the configuration for this client |
97 |
|
Configuration myConfig = _configManager.getConfiguration("Client."+_clientName); |
98 |
< |
_socketOut.println("OK"); |
101 |
< |
_socketOut.flush(); |
98 |
> |
sendOK(); |
99 |
|
// get properties |
100 |
|
cmd = _socketIn.readLine(); |
101 |
|
if(cmd==null) { |
107 |
|
if(cmd.startsWith("Client.") || cmd.startsWith("Host.")) { |
108 |
|
try { |
109 |
|
String returnedProperty = myConfig.getProperty(cmd); |
110 |
< |
_socketOut.println(returnedProperty); |
114 |
< |
_socketOut.flush(); |
110 |
> |
send(returnedProperty); |
111 |
|
} |
112 |
|
catch (org.omg.CORBA.MARSHAL e) { |
113 |
< |
_socketOut.println("ERROR"); |
118 |
< |
_socketOut.flush(); |
113 |
> |
sendERROR(); |
114 |
|
} |
115 |
|
} |
116 |
|
else { |
117 |
< |
_socketOut.println("ERROR"); |
123 |
< |
_socketOut.flush(); |
117 |
> |
sendERROR(); |
118 |
|
} |
119 |
|
cmd = _socketIn.readLine(); |
120 |
|
if(cmd==null) { |
121 |
|
throw new IOException("Fatal error reading from client"); |
122 |
|
} |
123 |
|
} |
124 |
< |
_socketOut.println("OK"); |
125 |
< |
_socketOut.flush(); |
132 |
< |
_logger.write(toString(), Logger.SYSMSG, "Client has been configured"); |
124 |
> |
sendOK(); |
125 |
> |
_logger.write(toString(), Logger.DEBUG, "Client has been configured"); |
126 |
|
|
127 |
|
} |
128 |
|
else if(cmd.equals("STARTDATA")) { |
131 |
|
// create a serversocket |
132 |
|
ServerSocket ss = new ServerSocket(0); |
133 |
|
// get the port |
134 |
< |
int port = ss.getLocalPort(); |
134 |
> |
String port = new Integer(ss.getLocalPort()).toString(); |
135 |
|
// tell the client the port |
136 |
< |
_socketOut.println(port); |
144 |
< |
_socketOut.flush(); |
136 |
> |
send(port); |
137 |
|
// wait for the client to connect |
138 |
|
Socket s = ss.accept(); |
139 |
|
// when we get the Socket back, give it to the data thread |
144 |
|
dh.start(); |
145 |
|
// Hold a reference to the DataHandler, so we can stop it later |
146 |
|
_dataHandler = dh; |
147 |
< |
_socketOut.println("OK"); |
148 |
< |
_socketOut.flush(); |
157 |
< |
_logger.write(toString(), Logger.SYSMSG, "Data stream started at Clients Request on port: "+port); |
147 |
> |
sendOK(); |
148 |
> |
_logger.write(toString(), Logger.DEBUG, "Data stream started at Clients Request on port: "+port); |
149 |
|
} |
150 |
|
else { |
151 |
< |
_socketOut.println("ERROR"); |
161 |
< |
_socketOut.flush(); |
151 |
> |
sendERROR(); |
152 |
|
} |
153 |
|
} |
154 |
|
else if(cmd.equals("STOPDATA")) { |
155 |
< |
// if we have a DataHandler, shut it down |
156 |
< |
if(_dataHandler != null) { |
157 |
< |
// Deregister the DataHandler, giving the host list |
158 |
< |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
169 |
< |
// Shut down the data handler |
170 |
< |
_dataHandler.shutdown(); |
171 |
< |
// Destroy our reference, leaving it for the GC |
172 |
< |
_dataHandler = null; |
173 |
< |
_socketOut.println("OK"); |
174 |
< |
_socketOut.flush(); |
175 |
< |
_logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request"); |
155 |
> |
// attempt to close the data channel |
156 |
> |
if(closeData()) { |
157 |
> |
sendOK(); |
158 |
> |
_logger.write(toString(), Logger.DEBUG, "Data stream stopped at Clients Request"); |
159 |
|
} |
160 |
|
else { |
161 |
< |
_socketOut.println("ERROR"); |
179 |
< |
_socketOut.flush(); |
161 |
> |
sendERROR(); |
162 |
|
} |
163 |
|
} |
164 |
|
else if(cmd.equals("SETHOSTLIST")) { |
165 |
|
// we can only set the host list when |
166 |
|
// the DataHandler is not connected |
167 |
|
if(_dataHandler == null) { |
168 |
< |
_socketOut.println("OK"); |
187 |
< |
_socketOut.flush(); |
168 |
> |
sendOK(); |
169 |
|
// read and set the host list |
170 |
|
cmd = _socketIn.readLine(); |
171 |
|
if(cmd==null) { |
172 |
|
throw new IOException("Fatal error reading from client"); |
173 |
|
} |
174 |
|
_hostList = cmd; |
175 |
< |
_socketOut.println("OK"); |
195 |
< |
_socketOut.flush(); |
175 |
> |
sendOK(); |
176 |
|
} |
177 |
|
else { |
178 |
< |
_socketOut.println("ERROR"); |
199 |
< |
_socketOut.flush(); |
178 |
> |
sendERROR(); |
179 |
|
} |
180 |
|
} |
181 |
|
else if(cmd.equals("DISCONNECT")) { |
182 |
< |
// we going to disconnect, so lets stop this loop |
182 |
> |
// we going to disconnect, so lets stop the main loop |
183 |
|
run=false; |
184 |
|
// if there is a DataHandler, we'd best shut it down |
185 |
< |
if(_dataHandler != null) { |
186 |
< |
// Deregister the DataHandler, giving the host list |
208 |
< |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
209 |
< |
// Shut down the data handler |
210 |
< |
_dataHandler.shutdown(); |
211 |
< |
// Destroy our reference, leaving it for the GC |
212 |
< |
_dataHandler = null; |
213 |
< |
_socketOut.println("OK"); |
214 |
< |
_socketOut.flush(); |
215 |
< |
_logger.write(toString(), Logger.SYSMSG, "Data stream stopped at Clients Request"); |
185 |
> |
if(closeData()) { |
186 |
> |
_logger.write(toString(), Logger.DEBUG, "Data stream stopped at Clients Request"); |
187 |
|
} |
188 |
< |
_socketOut.println("OK"); |
189 |
< |
_socketOut.flush(); |
188 |
> |
// say bye to the client |
189 |
> |
sendOK(); |
190 |
|
// close the reader, writer and Socket |
191 |
|
_socketIn.close(); |
192 |
|
_socketOut.close(); |
193 |
|
_socket.close(); |
194 |
< |
_logger.write(toString(), Logger.SYSMSG, "Closing at Clients Request"); |
194 |
> |
_logger.write(toString(), Logger.DEBUG, "Closing at Clients Request"); |
195 |
|
} |
196 |
|
else { |
197 |
< |
_socketOut.println("ERROR"); |
227 |
< |
_socketOut.flush(); |
197 |
> |
sendERROR(); |
198 |
|
} |
199 |
|
} |
200 |
|
catch(IOException e) { |
203 |
|
_logger.write(toString(), Logger.FATAL, "Fatal communication error, shutdown pending"); |
204 |
|
} |
205 |
|
} |
206 |
< |
_logger.write(toString(), Logger.SYSMSG, "Shutting down Control Handler, client has gone."); |
206 |
> |
_logger.write(toString(), Logger.DEBUG, "Shutting down Control Handler, client has gone."); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
223 |
|
} |
224 |
|
|
225 |
|
//---PRIVATE METHODS--- |
226 |
+ |
|
227 |
+ |
/** |
228 |
+ |
* Attempt to close down the DataHandler. This will return |
229 |
+ |
* true if it managed to close it down, or false if there |
230 |
+ |
* wasn't a DataHandler to close. |
231 |
+ |
* |
232 |
+ |
* @return whether the channel could be closed |
233 |
+ |
*/ |
234 |
+ |
private boolean closeData() { |
235 |
+ |
// if we have a DataHandler, shut it down |
236 |
+ |
if(_dataHandler != null) { |
237 |
+ |
// Deregister the DataHandler, giving the host list |
238 |
+ |
_packetSorter.deregister(_dataHandler.getQueue(), _hostList); |
239 |
+ |
// Shut down the data handler |
240 |
+ |
_dataHandler.shutdown(); |
241 |
+ |
// Destroy our reference, leaving it for the GC |
242 |
+ |
_dataHandler = null; |
243 |
+ |
return true; |
244 |
+ |
} |
245 |
+ |
else { |
246 |
+ |
return false; |
247 |
+ |
} |
248 |
+ |
} |
249 |
+ |
|
250 |
+ |
/** |
251 |
+ |
* Send an OK message to the outgoing Socket |
252 |
+ |
*/ |
253 |
+ |
private void sendOK() { |
254 |
+ |
send("OK"); |
255 |
+ |
} |
256 |
+ |
|
257 |
+ |
/** |
258 |
+ |
* Send an ERROR message to the outgoing Socket. |
259 |
+ |
*/ |
260 |
+ |
private void sendERROR() { |
261 |
+ |
send("ERROR"); |
262 |
+ |
} |
263 |
+ |
|
264 |
+ |
/** |
265 |
+ |
* Send any String message to the outgoing Socket. |
266 |
+ |
* |
267 |
+ |
* @param message The message/command to send. |
268 |
+ |
*/ |
269 |
+ |
private void send(String message) { |
270 |
+ |
_socketOut.println(message); |
271 |
+ |
_socketOut.flush(); |
272 |
+ |
} |
273 |
|
|
274 |
|
//---ACCESSOR/MUTATOR METHODS--- |
275 |
|
|