-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOCAClient.java
More file actions
447 lines (422 loc) · 11.9 KB
/
TOCAClient.java
File metadata and controls
447 lines (422 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
Copyright 2006, Astrophysics Research Institute, Liverpool John Moores University.
This file is part of org.estar.toop.
org.estar.toop is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
org.estar.toop is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with org.estar.toop; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// TOCAClient.java
// $Header: /space/home/eng/cjm/cvs/org_estar_toop/TOCAClient.java,v 1.5 2007-01-30 18:35:30 cjm Exp $
package org.estar.toop;
import java.io.*;
import java.util.*;
import ngat.net.*;
import ngat.util.*;
import ngat.util.logging.*;
/**
* Handles responses to commands sent via "Target of Opportunity Control Protocol" (TOCP).
* @author Steve Fraser, Chris Mottram
* @version $Revision$
*/
class TOCAClient implements Logging, Runnable
{
/**
* Revision control system version id.
*/
public final static String RCSID = "$Id$";
/**
* Classname for logging.
*/
public static final String CLASS = "TOCAClient";
/**
* True if the command generated an error.
*/
private volatile boolean error = false;
/**
* TOCS Server IP Address.
*/
private String host;
/**
* TOCS Server port.
*/
private int port;
/**
* Command to send.
*/
private String command;
/**
* Error message from TOCS server.
*/
private String errorMessage = null;
/**
* Error code string from TOCS server.
*/
private String errorCode = null;
/**
* Response from TOCS server.
*/
private String replyString;
/**
* The TelnetConnection to use to connect to the TOCS server.
*/
private TelnetConnection tc;
/**
* Class logger.
*/
private Logger logger = null;
/**
* Properties filled with keyword-values parsed from a reply from the command.
*/
private NGATProperties replyProperties = null;
/**
* Default constructor.
* The logger instance is also created. The replyProperties instance is initialised.
* @see #logger
* @see #replyProperties
*/
public TOCAClient()
{
super();
replyProperties = new NGATProperties();
logger = LogManager.getLogger(this);
}
/**
* Create a TOCAClient using the supplied parameters.
* The logger instance is also created. The replyProperties instance is initialised.
* @param command The command string to send.
* @param host The TOCS Server IP Address.
* @param port The TOCS Server port.
* @see #TOCAClient()
* @see #logger
* @see #replyProperties
*/
public TOCAClient(String command, String host, int port)
{
this();
this.command = command;
this.host = host;
this.port = port;
}
/**
* Set the command the client sends to the RCS TOCA.
* @param s The command.
* @see #command
*/
public void setCommand(String s)
{
command = s;
}
/**
* Set the RCS TOCA host the client connects to.
* @param s The host name.
* @see #host
*/
public void setHost(String s)
{
host = s;
}
/**
* Set the RCS TOCA port the client connects to.
* @param i The port number.
* @see #port
*/
public void setPort(int i)
{
port = i;
}
/**
* Called to send the command. This method which delegates to the TelnetConnection
* will block until the reply is received from the server or connection fails for some reason.
* A single line command is sent and a single line reply is expected. The connection will be
* closed by this client after receiving this line, any extra lines are lost.
* @see #host
* @see #port
* @see #tc
* @see #logger
* @see #setError
* @see #replyString
* @see #parseError
* @see #parseReply
*/
public void run()
{
try
{
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Connecting to "+host+":"+port);
tc = new TelnetConnection(host, port);
try
{
tc.open();
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Opened connection");
}
catch (Exception e)
{
setError(true, "Failed to open connection to TOCS: "+e);
logger.log(INFO, 1, CLASS, RCSID,"run","Failed to open connection to TOCS: "+e);
logger.dumpStack(1,e);
return;
}
tc.sendLine(command);
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Sent ["+command+"]");
try
{
replyString = tc.readLine();
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Reply ["+replyString+"]");
if (replyString == null || replyString.equals(""))
{
setError(true, "Null reply from TOCS");
return;
}
replyString = replyString.trim();
if (replyString.startsWith("ERROR"))
{
parseError();
return;
}
parseReply();
}
catch (Exception e)
{
setError(true, "Failed to read TOCS response: "+e);
logger.log(INFO, 1, CLASS, RCSID,"run","Failed to read TOCS response: "+e);
logger.dumpStack(1,e);
return;
}
setError(false, "Command accepted by TOCS");
}
catch (Exception e)
{
setError(true, "Failed to read TOCS response: "+e);
logger.log(INFO, 1, CLASS, RCSID,"run","Failed to read TOCS response: "+e);
logger.dumpStack(1,e);
return;
}
finally
{
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Closing connection");
try
{
tc.close();
}
catch (Exception e)
{
// We dont really care..
logger.log(INFO, 1, CLASS, RCSID,"run","TOCAClient::Error closing connection:"+e);
logger.dumpStack(1,e);
}
}
}
/**
* Sets the current error state and message.
* @param error Whether an error has occured.
* @param errorMessage The text of any error message.
* @see #error
* @see #errorMessage
* @see #errorCode
*/
private void setError(boolean error, String errorMessage)
{
this.error = error;
this.errorMessage = errorMessage;
this.errorCode = "INTERNAL_ERROR";
}
/**
* Sets the current error state and message, from the contents of the reply string.
* The reply string should contain something like:
* <pre>
* ERROR NOT_OPERATIONAL
* </pre>
* or
* <pre>
* ERROR ABORTED Code=607001, message=Overridden by higher priority service
* </pre>
* The first space-separated word is put into the error code, the rest into the error string.
* @exception IllegalArgumentException Thrown if replyString is not formatted correctly.
* @see #error
* @see #errorMessage
* @see #errorCode
* @see #replyString
*/
private void parseError() throws IllegalArgumentException
{
String s = null;
int sindex;
if(replyString.startsWith("ERROR") == false)
{
throw new IllegalArgumentException(this.getClass().getName()+
":parseError:reply string not an error:"+replyString);
}
this.error = true;
// remove leading ERROR plus space
s = replyString.substring(6);
// find space after error code.
sindex = s.indexOf(' ');
if(sindex > -1)
this.errorCode = s.substring(0,sindex);
this.errorMessage = s.substring(sindex+1);
}
/**
* Returns True if there is an error.
* @return A boolean, true if an error occured, false if all was OK.
* @see #error
*/
public boolean isError()
{
return error;
}
/**
* Returns the current error code or null.
* @return A string.
* @see #errorCode
*/
public String getErrorCode()
{
return errorCode;
}
/**
* Returns the current error message or null.
* @return A string.
* @see #errorMessage
*/
public String getErrorMessage()
{
return errorMessage;
}
/**
* Sets the current replyProperties from the contents of the reply string.
* The string should be something like:
* <pre>
* OK sessionID=004951, sessionLimit=3600, timeRemaining=4612, priority=2
* </pre>
* The results should be parsed into keyword value pairs in replyProperties of the form:
* <ul>
* <li>sessionID, 004951
* <li>sessionLimit, 3600
* <li>timeRemaining, 4612
* <li>priority, 2
* </ul>
* The string can also be "blank" i.e. "OK ", or just contain an informational string
* i.e. "OK Service terminated."
* @exception IllegalArgumentException Thrown if the replyString is not formatted correctly.
* @see #logger
* @see #replyString
* @see #replyProperties
*/
private void parseReply() throws IllegalArgumentException
{
String s = null;
StringTokenizer st = null;
String keywordValueString = null;
int equalsIndex;
String keyword = null;
String valueString = null;
this.error = false;
if(replyString.startsWith("OK") == false)
{
throw new IllegalArgumentException(this.getClass().getName()+
":parseReply:reply string not OK:"+replyString);
}
// check for blank OK string, otherwise substring causes StringIndexOutOfBoundsException.
// Note Init returns "OK ", but replyString.trim() is called before calling parseReply.
if(replyString.equals("OK ")||replyString.equals("OK"))
{
logger.log(INFO, 1, CLASS, RCSID,"parseReply",
"TOCAClient::Reply string was empty.");
return;
}
// reset reply properties to empty.
replyProperties = new NGATProperties();
// remove OK from replyString and put into s.
s = replyString.substring(3);
// tokenize s by ',' separated values.
st = new StringTokenizer(s,",");
while (st.hasMoreTokens())
{
keywordValueString = st.nextToken();
// get rid of whitespace.
keywordValueString = keywordValueString.trim();
equalsIndex = keywordValueString.indexOf("=");
if(equalsIndex > -1)
{
keyword = keywordValueString.substring(0,equalsIndex);
valueString = keywordValueString.substring(equalsIndex+1);
logger.log(INFO, 1, CLASS, RCSID,"parseReply","TOCAClient::Reply Keyword ["+keyword+
"] has value ["+valueString+"]");
replyProperties.setProperty(keyword,valueString);
}
else
{
logger.log(INFO, 1, CLASS, RCSID,"parseReply",
"TOCAClient::Reply Keyword/Value string ["+keywordValueString+
"] has no comma, must not be keyword/value?");
}
}
}
/**
* Returns the command reply.
* @return The reply string from the server.
* @see #replyString
*/
public String getReply()
{
return replyString;
}
/**
* Returns the string value for the specified keyword, if it exists in the list of keyword values.
* @param keyword The keyword.
* @return The keyword's string value. If the keyword does not exist, null is returned.
* @see #replyProperties
*/
public String getReplyValue(String keyword)
{
return replyProperties.getProperty(keyword);
}
/**
* Returns the int value for the specified keyword, if it exists in the list of keyword values.
* @param keyword The keyword.
* @return The keyword's int value.
* @exception NGATPropertyException Thrown if the value is not a valid integer.
* @see #replyProperties
*/
public int getReplyValueInt(String keyword) throws NGATPropertyException
{
return replyProperties.getInt(keyword);
}
/**
* Returns the double value for the specified keyword, if it exists in the list of keyword values.
* @param keyword The keyword.
* @return The keyword's double value.
* @exception NGATPropertyException Thrown if the value is not a valid double.
* @see #replyProperties
*/
public double getReplyValueDouble(String keyword) throws NGATPropertyException
{
return replyProperties.getDouble(keyword);
}
}
/*
** $Log: not supported by cvs2svn $
** Revision 1.4 2005/06/08 09:59:50 cjm
** Lots more logging.
** Second fix for parseReply. Blank "OK " replies from init are trimmed
** to "OK" before passing to parseReply, now allow for this.
**
** Revision 1.3 2005/06/07 16:08:40 cjm
** First fix for parseReply. Allows blank OK replies, and
** replies containing just a string (no keyword/value pairs).
**
** Revision 1.2 2005/06/07 13:27:51 cjm
** Comment fix.
**
** Revision 1.1 2005/06/06 14:45:00 cjm
** Initial revision
**
*/