1 |
ajm |
1.6 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.24 |
package uk.org.iscream.conient; |
3 |
ajm |
1.6 |
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.24 |
import uk.org.iscream.util.*; |
6 |
ajm |
1.2 |
import java.io.*; |
7 |
ajm |
1.1 |
import java.net.*; |
8 |
ajm |
1.2 |
import javax.swing.JOptionPane; |
9 |
ajm |
1.1 |
|
10 |
ajm |
1.6 |
/** |
11 |
|
|
* This is the main thread for the client. |
12 |
|
|
* Once started i continually checks it actionQueue |
13 |
|
|
* for actions that other areas of the system have placed |
14 |
|
|
* there, it then performs those actions. |
15 |
|
|
* |
16 |
|
|
* Currently this is the main thread where all non-GUI events |
17 |
|
|
* are dispatched to. |
18 |
|
|
* |
19 |
ajm |
1.30 |
* @author $Author: ajm4 $ |
20 |
|
|
* @version $Id: ConnectionHandler.java,v 1.29 2001/03/22 01:19:04 ajm4 Exp $ |
21 |
ajm |
1.6 |
*/ |
22 |
ajm |
1.1 |
public class ConnectionHandler extends Thread { |
23 |
ajm |
1.2 |
|
24 |
ajm |
1.6 |
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
ajm |
1.30 |
public final String REVISION = "$Revision: 1.29 $"; |
30 |
ajm |
1.6 |
|
31 |
tdb |
1.7 |
|
32 |
|
|
/** |
33 |
|
|
* The hardcoded protocol version that we are using. |
34 |
|
|
* Used when handshaking with the server |
35 |
|
|
*/ |
36 |
ajm |
1.20 |
public final double PROTOCOL_VERSION = 1.1; |
37 |
ajm |
1.2 |
|
38 |
tdb |
1.7 |
/** |
39 |
|
|
* Thread action DONOTHING. |
40 |
|
|
* This is an invalid action, but here for completeness |
41 |
|
|
* if anything sends this action it will warn appropriately |
42 |
|
|
* but do what it says, ie, nothing ;) |
43 |
|
|
*/ |
44 |
ajm |
1.1 |
public static final int DONOTHING = 0; |
45 |
tdb |
1.7 |
|
46 |
|
|
/** |
47 |
|
|
* Thread action CONNECT. |
48 |
|
|
* Opens the control link to the i-scream server. |
49 |
|
|
*/ |
50 |
ajm |
1.1 |
public static final int CONNECT = 1; |
51 |
tdb |
1.7 |
|
52 |
|
|
/** |
53 |
|
|
* Thread action STARTDATA. |
54 |
|
|
* Opens the data link to the i-scream server |
55 |
|
|
* and prepares all gui data components for data |
56 |
|
|
* it then starts all relavant threads going. |
57 |
|
|
*/ |
58 |
ajm |
1.2 |
public static final int STARTDATA = 2; |
59 |
tdb |
1.7 |
|
60 |
|
|
/** |
61 |
|
|
* Thread action STOPDATA. |
62 |
|
|
* Closes the data link and shuts down all components |
63 |
|
|
* that update gui for various things. |
64 |
|
|
*/ |
65 |
ajm |
1.2 |
public static final int STOPDATA = 3; |
66 |
tdb |
1.7 |
|
67 |
|
|
/** |
68 |
|
|
* Thread action DISCONNECT. |
69 |
|
|
* Checks to see if STOPDATA has been called if not, |
70 |
|
|
* it will add STOPDATA and then DISCONNECT to |
71 |
|
|
* the action queue and return. If STOPDATA has been |
72 |
|
|
* called it closes down the Control Link and tidies up |
73 |
|
|
* all relevant threads |
74 |
|
|
*/ |
75 |
ajm |
1.2 |
public static final int DISCONNECT = 4; |
76 |
tdb |
1.7 |
|
77 |
|
|
/** |
78 |
|
|
* Thread action QUIT. |
79 |
|
|
* Checks the status of the two links, if either |
80 |
|
|
* are still up, it queues the appropriate commands |
81 |
|
|
* to close them down. It then System.exit(0)'s! |
82 |
|
|
*/ |
83 |
ajm |
1.4 |
public static final int QUIT = 5; |
84 |
ajm |
1.6 |
|
85 |
ajm |
1.12 |
/** |
86 |
ajm |
1.14 |
* Thread action GETCONFIGURATION |
87 |
|
|
* Starts the command to obtain the configuration |
88 |
|
|
* from the server. |
89 |
|
|
* It then passes the IO to the Configuration object |
90 |
|
|
* so it can obtain any specific configuration |
91 |
|
|
*/ |
92 |
|
|
public static final int GETCONFIGURATION = 6; |
93 |
|
|
|
94 |
|
|
/** |
95 |
ajm |
1.12 |
* This is the time in seconds that this class |
96 |
|
|
* should wait on the DataReader class to fully |
97 |
|
|
* shutdown after calling shutdown() before |
98 |
|
|
* forcing a shutdown |
99 |
|
|
*/ |
100 |
|
|
public static final int DATAREADER_SHUTDOWN_TIMEOUT = 5; |
101 |
|
|
|
102 |
ajm |
1.13 |
/** |
103 |
|
|
* The default local server to connect to when a |
104 |
|
|
* firewall is in use if one is not specified in the server |
105 |
|
|
*/ |
106 |
|
|
public static final String DEFAULT_FIREWALL_SERVER = "localhost"; |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* The default time in seconds to wait for the |
110 |
|
|
* firewall setup command to execute |
111 |
|
|
*/ |
112 |
|
|
public static final int DEFAULT_FIREWALL_COMMANDWAIT = 5; |
113 |
ajm |
1.29 |
|
114 |
|
|
/** |
115 |
|
|
* The default maximum queue size in use for the data queue |
116 |
|
|
*/ |
117 |
|
|
public static final int DEFAULT_QUEUE_LIMIT = 500; |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* The algorithm we use to remove items from the queue |
121 |
|
|
* if it reaches its maximum size. |
122 |
|
|
* |
123 |
|
|
* We use a FIFO algorithm, see the uk.org.iscream.util.Queue |
124 |
|
|
* class for other options. |
125 |
|
|
*/ |
126 |
|
|
public static final int QUEUE_ALGORITHM = Queue.FIRST; |
127 |
ajm |
1.13 |
|
128 |
ajm |
1.6 |
//---STATIC METHODS--- |
129 |
|
|
|
130 |
|
|
//---CONSTRUCTORS--- |
131 |
|
|
|
132 |
tdb |
1.7 |
/** |
133 |
|
|
* Constructs new data handler. |
134 |
|
|
* Needs a reference to the data panel so that |
135 |
|
|
* it can set it processing inBound data |
136 |
|
|
* Also gets a reference to the queue that will be |
137 |
|
|
* used by other areas of the system to send it actions |
138 |
|
|
* |
139 |
|
|
* @param data the DataPanel in use |
140 |
|
|
* @param actionQueue the actionQueue for this class |
141 |
|
|
*/ |
142 |
ajm |
1.1 |
public ConnectionHandler(DataPanel data, Queue actionQueue) { |
143 |
|
|
_data = data; |
144 |
|
|
_actionQueue = actionQueue; |
145 |
|
|
_myQueue = _actionQueue.getQueue(); |
146 |
ajm |
1.26 |
_configuration.setConnectionHandler(this); |
147 |
ajm |
1.1 |
} |
148 |
|
|
|
149 |
ajm |
1.6 |
//---PUBLIC METHODS--- |
150 |
|
|
|
151 |
tdb |
1.7 |
|
152 |
|
|
/** |
153 |
|
|
* Starts this ConnectionHandler running. |
154 |
|
|
* This basically runs until told to stop, it gets "actions" |
155 |
|
|
* from its actionQueue and switch's on them do determine |
156 |
|
|
* what it should do it then carries out the action |
157 |
|
|
* |
158 |
|
|
* For details on what each action does see the action |
159 |
|
|
* types. |
160 |
|
|
*/ |
161 |
ajm |
1.1 |
public void run() { |
162 |
ajm |
1.20 |
if(_configuration.getProperty("control.onstartconnect").equals("1")) { |
163 |
ajm |
1.13 |
_actionQueue.add(new Integer(CONNECT)); |
164 |
|
|
} |
165 |
ajm |
1.20 |
if(_configuration.getProperty("data.onstartconnect").equals("1")) { |
166 |
ajm |
1.13 |
_actionQueue.add(new Integer(STARTDATA)); |
167 |
|
|
} |
168 |
tdb |
1.7 |
while(_running) { |
169 |
ajm |
1.1 |
// we wait for a call... |
170 |
|
|
int action = 0; |
171 |
|
|
try { |
172 |
|
|
action = ((Integer) _actionQueue.get(_myQueue)).intValue(); |
173 |
|
|
} catch (InvalidQueueException e) { |
174 |
ajm |
1.12 |
// we 're never going to get this |
175 |
|
|
// but if we do we should do something nasty |
176 |
|
|
throw new RuntimeException("unable to retrieve events from actionQueue!"); |
177 |
ajm |
1.1 |
} |
178 |
|
|
|
179 |
|
|
// examine our action... |
180 |
|
|
// if it was to connect...then we connect... |
181 |
ajm |
1.2 |
switch(action) { |
182 |
|
|
case CONNECT: |
183 |
ajm |
1.12 |
if (_controlLink == null) { |
184 |
ajm |
1.13 |
try { |
185 |
|
|
// get the server name from the config |
186 |
|
|
_configuredServer = _configuration.getProperty("control.server"); |
187 |
|
|
|
188 |
|
|
if (_configuredServer == null) { |
189 |
|
|
throw new IOException("no i-scream server in current configuration"); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
// open the socket to the server and bind the IO |
193 |
|
|
// get the port from the config |
194 |
|
|
String portString = _configuration.getProperty("control.port"); |
195 |
|
|
if (portString == null) { |
196 |
|
|
throw new IOException("no i-scream server port in current configuration"); |
197 |
|
|
} |
198 |
|
|
int port = 0; |
199 |
ajm |
1.12 |
try { |
200 |
ajm |
1.13 |
port = Integer.parseInt(portString); |
201 |
|
|
} catch (NumberFormatException e) { |
202 |
|
|
throw new IOException("no valid i-scream server port in current configuration"); |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
// start firewall if needed |
206 |
|
|
_server = handleFirewall(_configuredServer, port, _controlFirewallProcess); |
207 |
|
|
|
208 |
|
|
Conient.setControlStatus("Connecting to - " + _server); |
209 |
|
|
_controlLink = new Socket(_server, port); |
210 |
|
|
_inBound = new BufferedReader(new InputStreamReader(_controlLink.getInputStream())); |
211 |
|
|
_outBound = new PrintWriter(_controlLink.getOutputStream()); |
212 |
|
|
Conient.setControlStatus("Connection Established - " + _server); |
213 |
|
|
|
214 |
|
|
String response; |
215 |
|
|
response = _inBound.readLine(); |
216 |
|
|
|
217 |
|
|
// check the servers Protocol Identity against our own |
218 |
|
|
// we SHOULD be backwards compatible, so we can continue if |
219 |
|
|
// they are using a newer protocol, anything else then we die |
220 |
ajm |
1.23 |
double serverProtocolVersion = Double.parseDouble(response.substring(9, response.length())); |
221 |
|
|
Conient.addMessage("Protocol Versions: server [" + serverProtocolVersion + "] client [" + PROTOCOL_VERSION + "]"); |
222 |
|
|
if (serverProtocolVersion > PROTOCOL_VERSION) { |
223 |
ajm |
1.13 |
Conient.addMessage("WARNING{control link}: server is using a newer protocol (" + response + "), please update your client, continuing with old protocol (PROTOCOL " + PROTOCOL_VERSION + ")" ); |
224 |
ajm |
1.23 |
} else if (serverProtocolVersion < PROTOCOL_VERSION) { |
225 |
ajm |
1.13 |
// tidy up |
226 |
|
|
throw new IOException("incompatible protocol version"); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
// send the name of the client |
230 |
|
|
_outBound.println(_configuration.getProperty("clientname")); |
231 |
|
|
_outBound.flush(); |
232 |
|
|
response = _inBound.readLine(); |
233 |
|
|
if (!response.equals("OK")) { |
234 |
|
|
// tidy up |
235 |
ajm |
1.12 |
|
236 |
ajm |
1.13 |
throw new IOException("client name rejected - " + _configuration.getProperty("clientname")); |
237 |
|
|
} |
238 |
ajm |
1.22 |
|
239 |
ajm |
1.13 |
} catch (IOException e) { |
240 |
|
|
// print the error and tidy up what's left |
241 |
|
|
Conient.addMessage("ERROR{control link}: " + e); |
242 |
|
|
_controlLink = null; |
243 |
|
|
// and the firewall handler if there is one |
244 |
ajm |
1.16 |
closeFirewall(_controlFirewallProcess); |
245 |
ajm |
1.13 |
_actionQueue.clearQueue(_myQueue); |
246 |
|
|
Conient.setControlStatus("Disconnected"); |
247 |
ajm |
1.2 |
} |
248 |
ajm |
1.12 |
} else { |
249 |
|
|
Conient.addMessage("WARNING{control link}: already established"); |
250 |
ajm |
1.2 |
} |
251 |
|
|
break; |
252 |
|
|
case STARTDATA: |
253 |
ajm |
1.12 |
// as long as the data link hasn't been established |
254 |
|
|
// we want to establish it |
255 |
|
|
if (_dataLink == null) { |
256 |
|
|
// check that the control link is open, if it isn't we |
257 |
|
|
// might want to sort that problemo out |
258 |
|
|
// we do this by simply queueing the event to occour, then |
259 |
|
|
// this event to run again ;-) |
260 |
tdb |
1.7 |
if(_controlLink == null) { |
261 |
ajm |
1.12 |
Conient.addMessage("WARNING{data link}: control link not established - queueing start events"); |
262 |
tdb |
1.7 |
_actionQueue.add(new Integer(CONNECT)); |
263 |
|
|
_actionQueue.add(new Integer(STARTDATA)); |
264 |
ajm |
1.12 |
} else { |
265 |
|
|
try { |
266 |
ajm |
1.22 |
// set our host list if we know we have one we need to set |
267 |
|
|
String hostList = _configuration.getProperty("hostList"); |
268 |
|
|
boolean hostListSet = false; |
269 |
|
|
// send our hostList if we have |
270 |
|
|
if (_configuration.getProperty("useHostList").equals("1")) { |
271 |
|
|
if (hostList.equals("")) { |
272 |
|
|
Conient.addMessage("WARNING{control link}: your host list is empty, the server will send ALL hosts"); |
273 |
|
|
} |
274 |
|
|
hostListSet = setHostList(hostList); |
275 |
|
|
// if not, indicate we want the lot |
276 |
|
|
} else { |
277 |
|
|
hostListSet = setHostList(""); |
278 |
|
|
} |
279 |
|
|
// warn if there was a problem, it will have already error'd |
280 |
|
|
if (!hostListSet) { |
281 |
|
|
Conient.addMessage("WARNING{control link}: unable to set host list"); |
282 |
|
|
} |
283 |
ajm |
1.12 |
// ask the server to start the data link |
284 |
|
|
String response; |
285 |
|
|
_outBound.println("STARTDATA"); |
286 |
|
|
_outBound.flush(); |
287 |
|
|
response = _inBound.readLine(); |
288 |
|
|
|
289 |
|
|
// see if the server suggested a good port |
290 |
|
|
if (response.equals("ERROR")) { |
291 |
|
|
throw new IOException("server unable to start data link at this time"); |
292 |
|
|
} |
293 |
ajm |
1.13 |
int port = 0; |
294 |
ajm |
1.12 |
try { |
295 |
ajm |
1.13 |
port = Integer.parseInt(response); |
296 |
ajm |
1.12 |
} catch (NumberFormatException e) { |
297 |
|
|
throw new IOException("invalid data port suggested by server - " + response); |
298 |
|
|
} |
299 |
|
|
|
300 |
ajm |
1.13 |
// start firewall if needed |
301 |
|
|
_server = handleFirewall(_configuredServer, port, _dataFirewallProcess); |
302 |
|
|
Conient.setDataStatus("Connecting to - " + _server + ":" + response); |
303 |
|
|
|
304 |
|
|
_dataLink = new Socket(_server, port); |
305 |
|
|
|
306 |
ajm |
1.12 |
response = _inBound.readLine(); |
307 |
|
|
if (!response.equals("OK")) { |
308 |
|
|
throw new IOException("server reported error establishing data channel"); |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
// if the socket was ok, then we attack our IO hooks |
312 |
|
|
_dataInBound = new BufferedReader(new InputStreamReader(_dataLink.getInputStream())); |
313 |
|
|
_dataOutBound = new PrintWriter(_dataLink.getOutputStream()); |
314 |
|
|
Conient.setDataStatus("Connection Established - " + _server); |
315 |
ajm |
1.15 |
// now we want to start reading the data in |
316 |
|
|
// so we start the appropriate components on their way |
317 |
|
|
// we create a queue to give to both the reader and the |
318 |
|
|
// displayer |
319 |
ajm |
1.29 |
|
320 |
|
|
String configQueueSize = _configuration.getProperty("dataQueueSize"); |
321 |
|
|
int queueLimit = DEFAULT_QUEUE_LIMIT; |
322 |
|
|
if (configQueueSize != null) { |
323 |
|
|
try { |
324 |
|
|
queueLimit = Integer.parseInt(configQueueSize); |
325 |
|
|
} catch (NumberFormatException e) { |
326 |
|
|
// if it fails it will be default |
327 |
|
|
Conient.addMessage("WARNING{data link}: invalid queue size in configuration, using default of - " + queueLimit); |
328 |
|
|
} |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
Queue theQueue = new Queue(queueLimit, QUEUE_ALGORITHM); |
332 |
ajm |
1.30 |
_dataReader = new DataReader(_dataInBound, theQueue, this); |
333 |
ajm |
1.15 |
_data.setQueue(theQueue); |
334 |
|
|
_data.cleanUpTabs(); |
335 |
|
|
|
336 |
|
|
// start the data rocking |
337 |
|
|
new Thread(_data).start(); |
338 |
|
|
_dataReader.start(); |
339 |
|
|
// finished for us.... |
340 |
ajm |
1.12 |
} catch (IOException e) { |
341 |
|
|
// print the error and tidy up what's left |
342 |
|
|
Conient.addMessage("ERROR{data link}: " + e); |
343 |
|
|
_dataLink = null; |
344 |
ajm |
1.16 |
// and the firewall handler if there is one |
345 |
|
|
closeFirewall(_dataFirewallProcess); |
346 |
ajm |
1.13 |
_actionQueue.clearQueue(_myQueue); |
347 |
ajm |
1.12 |
Conient.setDataStatus("Disconnected"); |
348 |
|
|
} |
349 |
ajm |
1.2 |
} |
350 |
ajm |
1.12 |
} else { |
351 |
|
|
Conient.addMessage("WARNING{data link}: already established"); |
352 |
ajm |
1.2 |
} |
353 |
|
|
break; |
354 |
|
|
case STOPDATA: |
355 |
ajm |
1.12 |
if(_dataLink != null) { |
356 |
|
|
try { |
357 |
|
|
String response; |
358 |
|
|
// shut down the data link |
359 |
|
|
Conient.setDataStatus("Disconnecting - " + _server); |
360 |
|
|
|
361 |
|
|
// close the reader |
362 |
|
|
_dataReader.shutdown(); |
363 |
|
|
// wait for it to close |
364 |
|
|
boolean dirtyShutdown = true; |
365 |
|
|
long startTime = System.currentTimeMillis(); |
366 |
|
|
while((System.currentTimeMillis() - startTime) < (DATAREADER_SHUTDOWN_TIMEOUT * 1000)) { |
367 |
|
|
if (!_dataReader.isAlive()) { |
368 |
|
|
dirtyShutdown = false; |
369 |
|
|
break; |
370 |
|
|
} |
371 |
|
|
} |
372 |
|
|
// warn if it didn't shutdown in time |
373 |
|
|
if (dirtyShutdown) { |
374 |
|
|
Conient.addMessage("WARNING{data link}: data reader thread did not close within timeout, killing its IO anyway!"); |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
// tell the server |
378 |
|
|
_outBound.println("STOPDATA"); |
379 |
|
|
_outBound.flush(); |
380 |
|
|
response = _inBound.readLine(); |
381 |
|
|
|
382 |
|
|
// check the server was ok with our request... |
383 |
|
|
// even if it wasn't we will go anyway! |
384 |
|
|
if (!response.equals("OK")) { |
385 |
|
|
throw new IOException("server didn't OK request to stop data channel - stopping anyway"); |
386 |
|
|
} |
387 |
|
|
|
388 |
|
|
|
389 |
|
|
// close the lot down |
390 |
|
|
_dataInBound.close(); |
391 |
|
|
_dataOutBound.close(); |
392 |
|
|
_dataLink.close(); |
393 |
|
|
// get rid of the socket |
394 |
|
|
_dataLink = null; |
395 |
ajm |
1.13 |
|
396 |
|
|
// and the firewall handler if there is one |
397 |
ajm |
1.16 |
closeFirewall(_dataFirewallProcess); |
398 |
ajm |
1.13 |
|
399 |
ajm |
1.12 |
Conient.setDataStatus("Disconnected"); |
400 |
|
|
} catch (IOException e) { |
401 |
|
|
// print the error and tidy up what's left |
402 |
|
|
Conient.addMessage("ERROR{data link}: " + e); |
403 |
|
|
try { |
404 |
|
|
_dataOutBound.close(); |
405 |
|
|
_dataInBound.close(); |
406 |
|
|
_dataLink.close(); |
407 |
ajm |
1.13 |
// and the firewall handler if there is one |
408 |
ajm |
1.16 |
closeFirewall(_dataFirewallProcess); |
409 |
ajm |
1.12 |
} catch (IOException e2) { |
410 |
|
|
Conient.addMessage("CRITICAL{control link}: unable to close socket - " + e2); |
411 |
|
|
} |
412 |
|
|
_dataLink = null; |
413 |
ajm |
1.13 |
_actionQueue.clearQueue(_myQueue); |
414 |
ajm |
1.12 |
Conient.setDataStatus("Disconnected"); |
415 |
ajm |
1.2 |
} |
416 |
ajm |
1.12 |
} else { |
417 |
|
|
Conient.addMessage("WARNING{data link}: already disconnected"); |
418 |
ajm |
1.2 |
} |
419 |
|
|
break; |
420 |
|
|
case DISCONNECT: |
421 |
ajm |
1.12 |
if (_controlLink != null) { |
422 |
ajm |
1.2 |
if (_dataLink != null) { |
423 |
ajm |
1.12 |
// we want to tell ourselves to stop it |
424 |
|
|
Conient.addMessage("WARNING{control link}: data link not disconnected - queueing stop events"); |
425 |
|
|
_actionQueue.add(new Integer(STOPDATA)); |
426 |
|
|
_actionQueue.add(new Integer(DISCONNECT)); |
427 |
|
|
} else { |
428 |
|
|
try { |
429 |
|
|
// request the server to disconnect |
430 |
|
|
String response; |
431 |
|
|
_outBound.println("DISCONNECT"); |
432 |
|
|
_outBound.flush(); |
433 |
|
|
response = _inBound.readLine(); |
434 |
|
|
|
435 |
|
|
// check the server was ok with our request... |
436 |
|
|
// even if it wasn't we will go anyway! |
437 |
|
|
if (!response.equals("OK")) { |
438 |
|
|
throw new IOException("server didn't OK request to stop control channel - stopping anyway"); |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
// then lets shutdown the link |
442 |
|
|
Conient.setControlStatus("Disconnecting - " + _server); |
443 |
|
|
_inBound.close(); |
444 |
|
|
_outBound.close(); |
445 |
|
|
_controlLink.close(); |
446 |
|
|
// for good measure |
447 |
|
|
_controlLink = null; |
448 |
ajm |
1.13 |
|
449 |
|
|
// and the firewall handler if there is one |
450 |
ajm |
1.16 |
closeFirewall(_controlFirewallProcess); |
451 |
ajm |
1.13 |
|
452 |
ajm |
1.12 |
Conient.setControlStatus("Disconnected"); |
453 |
|
|
} catch (IOException e) { |
454 |
ajm |
1.14 |
Conient.addMessage("ERROR{control link}: " + e); |
455 |
ajm |
1.12 |
try { |
456 |
|
|
_inBound.close(); |
457 |
|
|
_outBound.close(); |
458 |
|
|
_controlLink.close(); |
459 |
ajm |
1.13 |
// and the firewall handler if there is one |
460 |
ajm |
1.16 |
closeFirewall(_controlFirewallProcess); |
461 |
ajm |
1.12 |
} catch (IOException e2) { |
462 |
|
|
Conient.addMessage("CRITICAL{control link}: unable to close socket - " + e2); |
463 |
|
|
} |
464 |
|
|
_controlLink = null; |
465 |
ajm |
1.13 |
_actionQueue.clearQueue(_myQueue); |
466 |
ajm |
1.12 |
Conient.setControlStatus("Disconnected"); |
467 |
|
|
} |
468 |
ajm |
1.2 |
} |
469 |
ajm |
1.12 |
} else { |
470 |
|
|
Conient.addMessage("WARNING{control link}: already disconnected"); |
471 |
ajm |
1.1 |
} |
472 |
ajm |
1.2 |
break; |
473 |
ajm |
1.4 |
case QUIT: |
474 |
ajm |
1.12 |
Conient.addMessage("Exiting."); |
475 |
ajm |
1.5 |
try { |
476 |
|
|
// stop data and control if data up |
477 |
|
|
if (_dataLink != null) { |
478 |
|
|
_actionQueue.add(new Integer(STOPDATA)); |
479 |
|
|
_actionQueue.add(new Integer(DISCONNECT)); |
480 |
|
|
_actionQueue.add(new Integer(QUIT)); |
481 |
|
|
throw new IOException(); |
482 |
|
|
} |
483 |
|
|
// stop control |
484 |
|
|
if (_controlLink != null) { |
485 |
|
|
_actionQueue.add(new Integer(DISCONNECT)); |
486 |
|
|
_actionQueue.add(new Integer(QUIT)); |
487 |
|
|
throw new IOException(); |
488 |
|
|
} |
489 |
ajm |
1.11 |
Conient.addMessage("Finished."); |
490 |
ajm |
1.5 |
// go! |
491 |
|
|
System.exit(0); |
492 |
|
|
} catch (IOException e) { |
493 |
ajm |
1.12 |
Conient.addMessage("WARNING: open connections detected - queueing stop events"); |
494 |
ajm |
1.4 |
} |
495 |
|
|
break; |
496 |
ajm |
1.1 |
} |
497 |
|
|
} |
498 |
|
|
} |
499 |
ajm |
1.26 |
|
500 |
|
|
/** |
501 |
|
|
* This method is called by the Configuration object |
502 |
|
|
* when a request is made for a server property. |
503 |
|
|
* The Configuration object is informed of the Connection |
504 |
|
|
* Handler's whereabouts on construction of the ConnectionHandler. |
505 |
|
|
* |
506 |
|
|
* This implements the STARTCONFIG command in the 1.0 protocol. |
507 |
|
|
*/ |
508 |
|
|
public String getConfigFromServer(String configName, String propertyName) { |
509 |
|
|
String property = ""; |
510 |
|
|
if(_controlLink != null) { |
511 |
|
|
try { |
512 |
|
|
Conient.addMessage("Getting configuration from server"); |
513 |
|
|
String response = null; |
514 |
|
|
_outBound.println("STARTCONFIG"); |
515 |
|
|
_outBound.flush(); |
516 |
|
|
response = _inBound.readLine(); |
517 |
|
|
if (!response.equals("OK")) { |
518 |
|
|
throw new IOException("server refused (" + response + ")"); |
519 |
|
|
} |
520 |
|
|
|
521 |
tdb |
1.28 |
_outBound.println(configName + ";" + propertyName); |
522 |
ajm |
1.26 |
_outBound.flush(); |
523 |
|
|
response = _inBound.readLine(); |
524 |
|
|
if (response.equals("ERROR")) { |
525 |
|
|
throw new IOException("server did not returned ERROR"); |
526 |
|
|
} |
527 |
|
|
property = response; |
528 |
|
|
_outBound.println("ENDCONFIG"); |
529 |
|
|
_outBound.flush(); |
530 |
|
|
response = _inBound.readLine(); |
531 |
|
|
if (!response.equals("OK")) { |
532 |
|
|
throw new IOException("server reported error when finishing configuration"); |
533 |
|
|
} |
534 |
|
|
} catch (IOException e) { |
535 |
|
|
Conient.addMessage("ERROR{control link}: when getting configuration - " + e); |
536 |
|
|
} |
537 |
|
|
} |
538 |
|
|
return property; |
539 |
|
|
} |
540 |
|
|
|
541 |
ajm |
1.12 |
/** |
542 |
|
|
* This method allows other classes |
543 |
|
|
* to shutdown this connection handler. |
544 |
|
|
*/ |
545 |
|
|
public void shutdown() { |
546 |
|
|
_running = false; |
547 |
ajm |
1.30 |
} |
548 |
|
|
|
549 |
|
|
/** |
550 |
|
|
* Called by the DataReader if the link |
551 |
|
|
* is unexpectedly lost |
552 |
|
|
*/ |
553 |
|
|
public void shutdownLinks() { |
554 |
|
|
_actionQueue.add(new Integer(DISCONNECT)); |
555 |
ajm |
1.12 |
} |
556 |
|
|
|
557 |
ajm |
1.6 |
//---PRIVATE METHODS--- |
558 |
ajm |
1.18 |
|
559 |
|
|
/** |
560 |
ajm |
1.20 |
* This method performs the SETHOSTLIST command. |
561 |
|
|
* This is run by the CONNECT command. |
562 |
|
|
* It tells the server which hosts we are interested |
563 |
|
|
* in, if "" is sent, this indicates we want ALL hosts. |
564 |
|
|
* |
565 |
|
|
* @param hostList the list of hosts as gained from the config |
566 |
|
|
* @return if we succeeded in setting the host list |
567 |
|
|
*/ |
568 |
|
|
private boolean setHostList(String hostList) { |
569 |
|
|
boolean success = false; |
570 |
|
|
// must have a control link open |
571 |
|
|
if (_controlLink != null) { |
572 |
|
|
// data link must be closed (according to 1.1 PROTOCOL) |
573 |
|
|
if(_dataLink == null) { |
574 |
|
|
try { |
575 |
ajm |
1.21 |
Conient.addMessage("Setting host list to:" + hostList); |
576 |
ajm |
1.20 |
String response = null; |
577 |
|
|
_outBound.println("SETHOSTLIST"); |
578 |
|
|
_outBound.flush(); |
579 |
|
|
response = _inBound.readLine(); |
580 |
|
|
if (!response.equals("OK")) { |
581 |
|
|
throw new IOException("server refused - data link possibly still open?"); |
582 |
|
|
} |
583 |
|
|
_outBound.println(hostList); |
584 |
|
|
_outBound.flush(); |
585 |
|
|
response = _inBound.readLine(); |
586 |
|
|
if (!response.equals("OK")) { |
587 |
|
|
throw new IOException("server had trouble with our request"); |
588 |
|
|
} |
589 |
|
|
success = true; |
590 |
|
|
} catch (IOException e) { |
591 |
|
|
Conient.addMessage("ERROR{control link}: when setting hostlist - " + e); |
592 |
|
|
} |
593 |
|
|
} |
594 |
|
|
} |
595 |
|
|
return success; |
596 |
ajm |
1.18 |
} |
597 |
ajm |
1.6 |
|
598 |
ajm |
1.13 |
/** |
599 |
|
|
* Handles opening pipes through firewalls. |
600 |
|
|
* It checks the configuration for various entries |
601 |
|
|
* to set up the link or not. |
602 |
|
|
* |
603 |
|
|
* If a link is setup it will return the name of the |
604 |
|
|
* new server to connect to, if a link is NOT setup, |
605 |
|
|
* it will simply return the original server name. |
606 |
|
|
* |
607 |
|
|
* The name of the machine that the pipe is setup on |
608 |
|
|
* defaults to "localhost", unless the configuration |
609 |
|
|
* specifies otherwise. |
610 |
|
|
* |
611 |
|
|
* Once it has run the firewall command it then waits |
612 |
|
|
* a set period according to the config for the firewall |
613 |
|
|
* pipe to be set up. |
614 |
|
|
* |
615 |
|
|
* The firewall process should be destroyed when the |
616 |
|
|
* link is finished with. |
617 |
|
|
* |
618 |
|
|
* @param server the name of the server to open up the pipe to |
619 |
|
|
* @param port the port number to open up the pipe to |
620 |
|
|
* @param firewallProcess the holder for the new firewall process |
621 |
|
|
* @return the server to connect to, as determined by this routine |
622 |
|
|
*/ |
623 |
|
|
private String handleFirewall(String server, int port, Process firewallProcess) { |
624 |
|
|
String firewallCommand = _configuration.getProperty("firewall.command"); |
625 |
ajm |
1.19 |
String useFirewall = _configuration.getProperty("useFirewall"); |
626 |
ajm |
1.13 |
String firewallCommandWait = _configuration.getProperty("firewall.commandwait"); |
627 |
|
|
String firewallServer = _configuration.getProperty("firewall.server"); |
628 |
|
|
// if we are running firewall support...then lets start it |
629 |
ajm |
1.20 |
if (useFirewall.equals("1")) { |
630 |
ajm |
1.13 |
// clean up the command with what we want |
631 |
ajm |
1.25 |
firewallCommand = StringUtils.replaceText(firewallCommand, "%PORT%", new Integer(port).toString()); |
632 |
|
|
firewallCommand = StringUtils.replaceText(firewallCommand, "%SERVER%", server); |
633 |
ajm |
1.13 |
Conient.addMessage("WARNING{firewall}: firewall pipes requested, running pipe setup command \"" + firewallCommand + "\""); |
634 |
|
|
try { |
635 |
|
|
// run the command |
636 |
|
|
firewallProcess = Runtime.getRuntime().exec(firewallCommand); |
637 |
|
|
|
638 |
|
|
// work out how long we should wait before carrying on |
639 |
|
|
int time = 0; |
640 |
|
|
try { |
641 |
|
|
time = Integer.parseInt(firewallCommandWait); |
642 |
|
|
} catch (NumberFormatException e) { |
643 |
|
|
time = 0; |
644 |
|
|
} |
645 |
|
|
if (time == 0) { |
646 |
|
|
time = DEFAULT_FIREWALL_COMMANDWAIT; |
647 |
|
|
} |
648 |
|
|
Conient.addMessage("WARNING{firewall}: waiting " + time + " seconds for command to complete!"); |
649 |
|
|
// wait for the command to finish |
650 |
|
|
try { |
651 |
|
|
Thread.sleep(time * 1000); |
652 |
|
|
} catch (InterruptedException e) { |
653 |
|
|
} |
654 |
|
|
|
655 |
|
|
// set the server we want to return |
656 |
|
|
server = firewallServer; |
657 |
ajm |
1.19 |
if (server.equals("")) { |
658 |
ajm |
1.13 |
server = DEFAULT_FIREWALL_SERVER; |
659 |
|
|
} |
660 |
|
|
} catch (IOException e) { |
661 |
|
|
Conient.addMessage("ERROR{firewall}: unable to start pipe to i-scream server"); |
662 |
|
|
} |
663 |
|
|
} |
664 |
|
|
return server; |
665 |
ajm |
1.16 |
} |
666 |
|
|
|
667 |
|
|
/** |
668 |
|
|
* Checks to see if the given firewall process |
669 |
|
|
* has been created. If it has it calls destroy() |
670 |
|
|
* on it. |
671 |
|
|
* |
672 |
|
|
* @param firewallProcess the process to check |
673 |
|
|
*/ |
674 |
|
|
private void closeFirewall(Process firewallProcess) { |
675 |
|
|
if (firewallProcess != null) { |
676 |
ajm |
1.17 |
firewallProcess.destroy(); |
677 |
|
|
firewallProcess = null; |
678 |
ajm |
1.16 |
Conient.addMessage("WARNING{firewall}: firewall process destroyed"); |
679 |
|
|
} |
680 |
ajm |
1.13 |
} |
681 |
|
|
|
682 |
ajm |
1.6 |
//---ACCESSOR/MUTATOR METHODS--- |
683 |
|
|
|
684 |
|
|
//---ATTRIBUTES--- |
685 |
|
|
|
686 |
ajm |
1.12 |
/** |
687 |
|
|
* The state if this thread |
688 |
|
|
*/ |
689 |
ajm |
1.13 |
private boolean _running = true; |
690 |
ajm |
1.12 |
|
691 |
|
|
/** |
692 |
|
|
* A reference to the displaying class DataPanel |
693 |
|
|
*/ |
694 |
ajm |
1.13 |
private DataPanel _data; |
695 |
ajm |
1.12 |
|
696 |
|
|
/** |
697 |
|
|
* The queue that actions are added to by other parts of the system |
698 |
|
|
*/ |
699 |
ajm |
1.13 |
private Queue _actionQueue; |
700 |
ajm |
1.12 |
|
701 |
|
|
/** |
702 |
|
|
* The queue number that we are reading from on the action queue |
703 |
|
|
*/ |
704 |
ajm |
1.13 |
private int _myQueue; |
705 |
ajm |
1.12 |
|
706 |
|
|
/** |
707 |
|
|
* The control link socket |
708 |
|
|
*/ |
709 |
ajm |
1.13 |
private Socket _controlLink; |
710 |
ajm |
1.12 |
|
711 |
|
|
/** |
712 |
|
|
* The data link socket |
713 |
|
|
*/ |
714 |
ajm |
1.13 |
private Socket _dataLink; |
715 |
ajm |
1.12 |
|
716 |
|
|
/** |
717 |
|
|
* The input for the control link |
718 |
|
|
*/ |
719 |
ajm |
1.13 |
private BufferedReader _inBound; |
720 |
ajm |
1.12 |
|
721 |
|
|
/** |
722 |
|
|
* The output for the control link |
723 |
|
|
*/ |
724 |
ajm |
1.13 |
private PrintWriter _outBound; |
725 |
ajm |
1.12 |
|
726 |
|
|
/** |
727 |
|
|
* The input for the data link |
728 |
|
|
*/ |
729 |
ajm |
1.13 |
private BufferedReader _dataInBound; |
730 |
ajm |
1.12 |
|
731 |
|
|
/** |
732 |
|
|
* The output for the data link |
733 |
|
|
*/ |
734 |
ajm |
1.13 |
private PrintWriter _dataOutBound; |
735 |
ajm |
1.12 |
|
736 |
|
|
/** |
737 |
|
|
* A reference to the DataReader in use |
738 |
|
|
*/ |
739 |
ajm |
1.13 |
private DataReader _dataReader; |
740 |
|
|
|
741 |
|
|
/** |
742 |
|
|
* A reference to the system configuration component |
743 |
|
|
*/ |
744 |
|
|
private Configuration _configuration = Configuration.getInstance(); |
745 |
|
|
|
746 |
|
|
/** |
747 |
|
|
* The server we will be connecting to |
748 |
|
|
*/ |
749 |
|
|
private String _server = null; |
750 |
ajm |
1.12 |
|
751 |
|
|
/** |
752 |
ajm |
1.13 |
* The server that the config says we should connect to |
753 |
ajm |
1.12 |
*/ |
754 |
ajm |
1.13 |
private String _configuredServer = null; |
755 |
ajm |
1.6 |
|
756 |
ajm |
1.13 |
/** |
757 |
|
|
* The process used to start the firewall pipe |
758 |
|
|
* for the control link. |
759 |
|
|
*/ |
760 |
|
|
private Process _controlFirewallProcess = null; |
761 |
|
|
|
762 |
|
|
/** |
763 |
|
|
* The process used to start the firewall pipe |
764 |
|
|
* for the data link. |
765 |
|
|
*/ |
766 |
|
|
private Process _dataFirewallProcess = null; |
767 |
|
|
|
768 |
ajm |
1.6 |
//---STATIC ATTRIBUTES--- |
769 |
|
|
|
770 |
ajm |
1.27 |
} |