diff --git a/CoTreceiver/src/edu/nps/moves/cot/main/CoTMsgReceiver.java b/CoTreceiver/src/edu/nps/moves/cot/main/CoTMsgReceiver.java index ebdf3ab9a131317b27589088c583fbc8df2f023c..6868271132ba147ea240ae80311a5757e46686b3 100644 --- a/CoTreceiver/src/edu/nps/moves/cot/main/CoTMsgReceiver.java +++ b/CoTreceiver/src/edu/nps/moves/cot/main/CoTMsgReceiver.java @@ -37,6 +37,7 @@ import edu.nps.moves.cot.dis.CoTMsgParserDisRunner; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.util.Arrays; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -64,11 +65,14 @@ public class CoTMsgReceiver { Logger.getLogger(CoTMsgReceiver.class.getName()).log(Level.SEVERE, null, ex); } }//sleep + + static final String[] ARGS_DEFAULT = {"--performanceTestSendCoT", "udp", "localhost", "9999", "1", "1"}; /** * @param args the command line arguments */ - public static void main(String[] args) { + public static void main(String[] args) + { System.out.print ("CoTMsgReceiver"); for (String arg:args) @@ -77,7 +81,15 @@ public class CoTMsgReceiver { } System.out.println(); - boolean isTcp = args[1].equalsIgnoreCase("tcp"); + boolean isTcp = false; + if (args.length > 0) + { + isTcp = args[1].equalsIgnoreCase("tcp"); + } + else + { + System.out.println ("CoTMsgReceiver invoked without parameter args, using default args " + Arrays.toString(ARGS_DEFAULT)); + } // - Create a custom CoTparser class. See CustomCoTparser.java. // - Override the coTeventHandler() to add your custom CoT data handling. @@ -95,28 +107,40 @@ public class CoTMsgReceiver { connectors.addCoTparser(disr); // - Common usage example: - System.out.println("\n\nStarting listener pool\n"); + System.out.println("\nStarting listener pool\n"); - if (isTcp) { + if (isTcp) + { connectors.addTcpListener(CoTtcpListener.DEFAULT_COT_TCP_PORT); - connectors.startListener(CoTtcpListener.DEFAULT_COT_TCP_PORT); - } else { + connectors.startListener (CoTtcpListener.DEFAULT_COT_TCP_PORT); + } + else + { connectors.addUdpListener(CoTudpListener.DEFAULT_COT_UDP_PORT); // default packet size 1024 - connectors.startListener(CoTudpListener.DEFAULT_COT_UDP_PORT); + connectors.startListener (CoTudpListener.DEFAULT_COT_UDP_PORT); } - PipedOutputStream out = null; + PipedOutputStream pipedOutputStream = null; // Redirect an output stream to be read by an input stream in order to // stop the TCP listener try { - out = new PipedOutputStream(); - System.setIn(new PipedInputStream(out)); - } catch (IOException ex) {} + pipedOutputStream = new PipedOutputStream(); + System.setIn(new PipedInputStream(pipedOutputStream)); + } + catch (IOException ioe) + { + System.out.println("*** pipedOutputStream ioe " + ioe.getMessage()); + } // Start the CoT message generator/server Runnable r = () -> { - com.aciedge.cotplitool.Main.main(args); + // odd invocation avoids resetting empty args array, which can provoke following obscure Java error + // error: local variables referenced from a lambda expression must be final or effectively final + // com.aciedge.cotplitool.Main.main(args); + if (args.length > 0) + com.aciedge.cotplitool.Main.main(args); + else com.aciedge.cotplitool.Main.main(ARGS_DEFAULT); }; Thread t = new Thread(r); t.setDaemon(true); @@ -125,7 +149,7 @@ public class CoTMsgReceiver { // TODO: can make this another input argument sleep(5L); // default to 5 seconds of operation - System.out.println("\n\nStopping listener pool\n"); + System.out.println("\nStopping listener pool\n"); if (isTcp) connectors.stopListener(CoTtcpListener.DEFAULT_COT_TCP_PORT); @@ -134,8 +158,8 @@ public class CoTMsgReceiver { // Issue the CoT message generator/server quit/exit command try { - if (out != null) - out.write("quit\n".getBytes("utf-8")); + if (pipedOutputStream != null) + pipedOutputStream.write("quit\n".getBytes("utf-8")); } catch (IOException ex) {} // Shutdown the DIS listerner