-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendDpRtCommand.java
More file actions
499 lines (470 loc) · 14.1 KB
/
SendDpRtCommand.java
File metadata and controls
499 lines (470 loc) · 14.1 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Copyright 2006, Astrophysics Research Institute, Liverpool John Moores University.
This file is part of DpRt.
DpRt 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.
DpRt 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 DpRt; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// SendDpRtCommand.java
// $Header: /space/home/eng/cjm/cvs/dprt/test/SendDpRtCommand.java,v 1.5 2007-09-03 13:39:56 cjm Exp $
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
import ngat.message.INST_DP.*;
import ngat.util.*;
/**
* This class send a DpRt command to the DpRt.
* @author Chris Mottram
* @version $Revision: 1.5 $
*/
public class SendDpRtCommand
{
/**
* The default port number to send ISS commands to.
*/
static final int DEFAULT_DPRT_PORT_NUMBER = 6880;
/**
* Type (class) of command that can be sent to the DpRt.
* This value means no command has been selected.
* @see #commandType
*/
static final int COMMAND_TYPE_NONE = 0;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_CALIBRATE_REDUCE = 1;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_EXPOSE_REDUCE = 2;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_MAKE_MASTER_BIAS = 3;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_MAKE_MASTER_FLAT = 4;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_ABORT = 5;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_REBOOT = 6;
/**
* Type (class) of command that can be sent to the DpRt.
* @see #commandType
*/
static final int COMMAND_TYPE_STOP = 7;
/**
* The ip address to send the messages to, this should be the machine the DpRt is on.
*/
private InetAddress address = null;
/**
* The port number to send commands from the file to the DpRt.
*/
private int dprtPortNumber = DEFAULT_DPRT_PORT_NUMBER;
/**
* The stream to write error messages to - defaults to System.err.
*/
private PrintStream errorStream = System.err;
/**
* Which command to send to the DpRt.
* This is one of COMMAND_TYPE_CALIBRATE_REDUCE, COMMAND_TYPE_EXPOSE_REDUCE,
* COMMAND_TYPE_MAKE_MASTER_BIAS, COMMAND_TYPE_MAKE_MASTER_FLAT, COMMAND_TYPE_ABORT,
* COMMAND_TYPE_REBOOT, COMMAND_TYPE_STOP
* Defaults to COMMAND_TYPE_NONE, which should cause this program to return an error.
* @see #COMMAND_TYPE_CALIBRATE_REDUCE
* @see #COMMAND_TYPE_EXPOSE_REDUCE
* @see #COMMAND_TYPE_MAKE_MASTER_BIAS
* @see #COMMAND_TYPE_MAKE_MASTER_FLAT
* @see #COMMAND_TYPE_ABORT
* @see #COMMAND_TYPE_REBOOT
* @see #COMMAND_TYPE_STOP
* @see #COMMAND_TYPE_NONE
*/
private int commandType = 0;
/**
* Filename to send to the data pipeline.
*/
private String filename = null;
/**
* Directory name to send to the data pipeline.
*/
private String dirname = null;
/**
* If we are sending a REBOOT command, this defines the level of reboot.
*/
private int rebootLevel = 1;
/**
* If we are sending an EXPOSE_REDUCE, whether to set the wcsFit flag to true.
*/
private boolean wcsFit = false;
/**
* This is the initialisation routine.
*/
private void init()
{
}
/**
* This routine creates a DpRt command.
* @return An instance of a sub-class of DP_TO_INST.
* @exception Exception Thrown if commandType does not specify a command to create.
* @see #commandType
* @see #filename
* @see #rebootLevel
* @see #wcsFit
*/
private INST_TO_DP createCommand() throws Exception
{
INST_TO_DP command = null;
switch(commandType)
{
case COMMAND_TYPE_CALIBRATE_REDUCE:
CALIBRATE_REDUCE calibrateReduce = null;
calibrateReduce = new CALIBRATE_REDUCE("SendDpRtCommand");
calibrateReduce.setFilename(filename);
command = (INST_TO_DP)calibrateReduce;
break;
case COMMAND_TYPE_EXPOSE_REDUCE:
EXPOSE_REDUCE exposeReduce = null;
exposeReduce = new EXPOSE_REDUCE("SendDpRtCommand");
exposeReduce.setFilename(filename);
exposeReduce.setWcsFit(wcsFit);
command = (INST_TO_DP)exposeReduce;
break;
case COMMAND_TYPE_MAKE_MASTER_BIAS:
MAKE_MASTER_BIAS makeMasterBias = null;
makeMasterBias = new MAKE_MASTER_BIAS("SendDpRtCommand");
makeMasterBias.setDirname(dirname);
command = (INST_TO_DP)makeMasterBias;
break;
case COMMAND_TYPE_MAKE_MASTER_FLAT:
MAKE_MASTER_FLAT makeMasterFlat = null;
makeMasterFlat = new MAKE_MASTER_FLAT("SendDpRtCommand");
makeMasterFlat.setDirname(dirname);
command = (INST_TO_DP)makeMasterFlat;
break;
case COMMAND_TYPE_ABORT:
ABORT abortCommand = null;
abortCommand = new ABORT("SendDpRtCommand");
command = (INST_TO_DP)abortCommand;
break;
case COMMAND_TYPE_REBOOT:
REBOOT rebootCommand = null;
rebootCommand = new REBOOT("SendDpRtCommand");
rebootCommand.setLevel(rebootLevel);
command = (INST_TO_DP)rebootCommand;
break;
case COMMAND_TYPE_STOP:
STOP stopCommand = null;
stopCommand = new STOP("SendDpRtCommand");
command = (INST_TO_DP)stopCommand;
break;
case COMMAND_TYPE_NONE:
default:
throw new Exception("Illegal command type:"+commandType);
}
return command;
}
/**
* This is the run routine. It calls createCommand and sends the resulting command instance to the DpRt
* using a DpRtTestTCPClientConnectionThread, and awaiting the thread termination to signify message
* completion. getThreadResult is then called to print out the result.
* @return The routine returns true if the command succeeded, false if it failed.
* @exception Exception Thrown if an exception occurs.
* @see #createCommand
* @see DpRtTestTCPClientConnectionThread
* @see #getThreadResult
*/
private boolean run() throws Exception
{
INST_TO_DP dprtCommand = null;
DpRtTestTCPClientConnectionThread thread = null;
boolean retval;
dprtCommand = (INST_TO_DP)(createCommand());
thread = new DpRtTestTCPClientConnectionThread(address,dprtPortNumber,dprtCommand);
thread.start();
while(thread.isAlive())
{
try
{
thread.join();
}
catch(InterruptedException e)
{
System.err.println("run:join interrupted:"+e);
}
}// end while isAlive
retval = getThreadResult(thread);
return retval;
}
/**
* Find out the completion status of the thread and print out the final status of some variables.
* @param thread The Thread to print some information for.
* @return The routine returns true if the thread completed successfully,
* false if some error occured.
*/
private boolean getThreadResult(DpRtTestTCPClientConnectionThread thread)
{
boolean retval;
if(thread.getAcknowledge() == null)
System.err.println("Acknowledge was null");
else
System.err.println("Acknowledge with timeToComplete:"+
thread.getAcknowledge().getTimeToComplete());
if(thread.getDone() == null)
{
System.out.println("Done was null");
retval = false;
}
else
{
if(thread.getDone().getSuccessful())
{
System.out.println("Done was successful");
if(thread.getDone() instanceof REDUCE_DONE)
{
System.out.println("\tFilename:"+
((REDUCE_DONE)(thread.getDone())).getFilename());
}
if(thread.getDone() instanceof CALIBRATE_REDUCE_DONE)
{
CALIBRATE_REDUCE_DONE calibrateReduceDone = (CALIBRATE_REDUCE_DONE)
(thread.getDone());
System.out.println("\tMean Counts:"+calibrateReduceDone.getMeanCounts());
System.out.println("\tPeak Counts:"+calibrateReduceDone.getPeakCounts());
}
if(thread.getDone() instanceof EXPOSE_REDUCE_DONE)
{
EXPOSE_REDUCE_DONE exposeReduceDone = (EXPOSE_REDUCE_DONE)(thread.getDone());
System.out.println("\tCounts:"+exposeReduceDone.getCounts());
System.out.println("\tSeeing:"+exposeReduceDone.getSeeing());
System.out.println("\tX Pix:"+exposeReduceDone.getXpix());
System.out.println("\tY Pix:"+exposeReduceDone.getYpix());
System.out.println("\tPhotometricity:"+exposeReduceDone.getPhotometricity());
System.out.println("\tSky Brightness:"+exposeReduceDone.getSkyBrightness());
System.out.println("\tSaturated:"+exposeReduceDone.getSaturation());
}
// No test for MAKE_MASTER_BIAS_DONE and MAKE_MASTER_FLAT_DONE,
// they return no extra data.
retval = true;
}
else
{
System.out.println("Done returned error("+thread.getDone().getErrorNum()+
"): "+thread.getDone().getErrorString());
retval = false;
}
}
return retval;
}
/**
* This routine parses arguments passed into SendDpRtCommand.
* @see #commandType
* @see #dirname
* @see #filename
* @see #rebootLevel
* @see #dprtPortNumber
* @see #address
* @see #help
* @see #wcsFit
* @see #COMMAND_TYPE_CALIBRATE_REDUCE
* @see #COMMAND_TYPE_EXPOSE_REDUCE
* @see #COMMAND_TYPE_ABORT
* @see #COMMAND_TYPE_REBOOT
* @see #COMMAND_TYPE_STOP
*/
private void parseArgs(String[] args)
{
for(int i = 0; i < args.length;i++)
{
if(args[i].equals("-a")||args[i].equals("-abort"))
{
commandType = COMMAND_TYPE_ABORT;
}
else if(args[i].equals("-c")||args[i].equals("-calibrate"))
{
if((i+1)< args.length)
{
commandType = COMMAND_TYPE_CALIBRATE_REDUCE;
filename = args[i+1];
i++;
}
else
errorStream.println("-calibrate requires an argument.");
}
else if(args[i].equals("-d")||args[i].equals("-dprtport"))
{
if((i+1)< args.length)
{
dprtPortNumber = Integer.parseInt(args[i+1]);
i++;
}
else
errorStream.println("-dprtport requires a port number");
}
else if(args[i].equals("-e")||args[i].equals("-expose"))
{
if((i+1)< args.length)
{
commandType = COMMAND_TYPE_EXPOSE_REDUCE;
filename = args[i+1];
i++;
}
else
errorStream.println("-expose requires an argument.");
}
else if(args[i].equals("-h")||args[i].equals("-help"))
{
help();
System.exit(0);
}
else if(args[i].equals("-ip")||args[i].equals("-address"))
{
if((i+1)< args.length)
{
try
{
address = InetAddress.getByName(args[i+1]);
}
catch(UnknownHostException e)
{
System.err.println(this.getClass().getName()+":illegal address:"+
args[i+1]+":"+e);
}
i++;
}
else
errorStream.println("-address requires an address");
}
else if(args[i].equals("-mb")||args[i].equals("-make_master_bias"))
{
if((i+1)< args.length)
{
commandType = COMMAND_TYPE_MAKE_MASTER_BIAS;
dirname = args[i+1];
i++;
}
else
errorStream.println("-make_master_bias requires an argument.");
}
else if(args[i].equals("-mf")||args[i].equals("-make_master_flat"))
{
if((i+1)< args.length)
{
commandType = COMMAND_TYPE_MAKE_MASTER_FLAT;
dirname = args[i+1];
i++;
}
else
errorStream.println("-make_master_flat requires an argument.");
}
else if(args[i].equals("-r")||args[i].equals("-reboot"))
{
if((i+1)< args.length)
{
commandType = COMMAND_TYPE_REBOOT;
rebootLevel = Integer.parseInt(args[i+1]);
i++;
}
else
errorStream.println("-reboot requires an argument.");
}
else if(args[i].equals("-s")||args[i].equals("-stop"))
{
commandType = COMMAND_TYPE_STOP;
}
else if(args[i].equals("-w")||args[i].equals("-wcs_fit"))
{
wcsFit = true;
}
else
System.out.println(this.getClass().getName()+":Option not supported:"+args[i]);
}
}
/**
* Help message routine.
*/
private void help()
{
System.out.println(this.getClass().getName()+" Help:");
System.out.println("Options are:");
System.out.println("\t-a[bort] - Send abort command.");
System.out.println("\t-c[alibrate] <filename> - Send calibrate reduce command.");
System.out.println("\t-d[prtport] <port number> - Port to send commands to.");
System.out.println("\t-e[xpose] <filename> - Send expose reduce command.");
System.out.println("\t-[ip]|[address] <address> - Address to send commands to.");
System.out.println("\t-[make_master_bias]|[mb] <directory> - Send make master bias command.");
System.out.println("\t-[make_master_flat]|[mf] <directory> - Send make master flat command.");
System.out.println("\t-r[eboot] <level> - Send reboot command.");
System.out.println("\t-s[top] - Send stop command.");
System.out.println("\t-w[cs_fit] - (Expose Reduce command only) Set WCS Fit flag.");
System.out.println("The default DpRt port is "+DEFAULT_DPRT_PORT_NUMBER+".");
}
/**
* The main routine, called when SendDpRtCommand is executed. This initialises the object, parses
* it's arguments, opens the filename, runs the run routine, and then closes the file.
* @see #parseArgs
* @see #init
* @see #run
*/
public static void main(String[] args)
{
boolean retval;
SendDpRtCommand command = new SendDpRtCommand();
command.parseArgs(args);
command.init();
if(command.address == null)
{
System.err.println("No DpRt Address Specified.");
command.help();
System.exit(1);
}
try
{
retval = command.run();
}
catch (Exception e)
{
retval = false;
System.err.println("run failed:"+e);
}
if(retval)
System.exit(0);
else
System.exit(2);
}
}
//
// $Log: not supported by cvs2svn $
// Revision 1.4 2006/05/16 16:55:38 cjm
// gnuify: Added GNU General Public License.
//
// Revision 1.3 2003/06/06 12:59:31 cjm
// backup.
//
// Revision 1.2 2002/05/20 16:38:37 cjm
// Added extra parameters to EXPOSE_REDUCE_DONE prints.
//
// Revision 1.1 2001/08/10 13:06:58 cjm
// Initial revision
//
//