diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java index 7ac8d10c8b73ee98ca7fee129585e298bac81c85..d9c6d83f9d721e708039421ddb34e53b9edc4e41 100644 --- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java +++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java @@ -7,8 +7,8 @@ package OpenDis7Examples; import edu.nps.moves.dis7.entities.swe.platform.surface._001Poseidon; import edu.nps.moves.dis7.entities.swe.platform.surface._002Triton; -import edu.nps.moves.dis7.enumerations.*; // match any -import edu.nps.moves.dis7.pdus.*; // match any of the PDU classes, easier than listing individually +import edu.nps.moves.dis7.enumerations.*; +import edu.nps.moves.dis7.pdus.*; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import edu.nps.moves.dis7.utilities.PduFactory; import edu.nps.moves.dis7.utilities.stream.PduRecorder; @@ -80,11 +80,14 @@ public class ExampleSimulationProgram entityStatePdu_1.setEntityID(entityID_1); entityStatePdu_1.setForceId(ForceID.FRIENDLY); entityStatePdu_1.setEntityType(new _001Poseidon()); // note import statement above + entityStatePdu_1.setMarking("Entity #1"); + entityStatePdu_1.getMarkingString(); // check EntityStatePdu entityStatePdu_2 = pduFactory.makeEntityStatePdu(); entityStatePdu_2.setEntityID(entityID_2); entityStatePdu_2.setForceId(ForceID.OPPOSING); entityStatePdu_2.setEntityType(new _002Triton()); // note import statement above + entityStatePdu_2.setMarking("Entity #2"); FirePdu firePdu_1a = pduFactory.makeFirePdu(); // for entity 1 first weapon (if any) // FirePdu firePdu_1b = pduFactory.makeFirePdu(); // for entity 1 second weapon (if any) @@ -270,11 +273,11 @@ public class ExampleSimulationProgram /** All done, release network resources */ public void tearDownNetworkInterface() { - pduRecorder.stop(); + pduRecorder.stop(); // handles disNetworkInterface.close() - disNetworkInterface.removeListener(pduListener); - - disNetworkInterface.close(); +// disNetworkInterface.removeListener(pduListener); +// +// disNetworkInterface.close(); // disNetworkInterface.kill(); // renamed as close(), deprecated // disNetworkInterface = null; // making sure no possibility of zombie process remaining... } @@ -352,32 +355,59 @@ public class ExampleSimulationProgram } } } - + /** - * Main method is first executed when a program instance is loaded. - * @see <a href="https://docs.oracle.com/javase/tutorial/getStarted/application/index.html">Java Tutorials: A Closer Look at the "Hello World!" Application</a> - * @param args command-line arguments are an array of optional String parameters that are passed from execution environment during invocation + * @return whether verboseComments mode is enabled */ - public static void main(String[] args) + public boolean isVerboseComments() { + return verboseComments; + } + + /** + * @param newVerboseComments whether verboseComments mode is enabled + */ + public void setVerboseComments(boolean newVerboseComments) { + this.verboseComments = newVerboseComments; + } + + /** + * Initial execution via main() method: handle args array of initialization arguments here + * @param args command-line parameters: network address and port + */ + private static void handleArgs (String[] args) { - System.out.println(TRACE_PREFIX + "main() started..."); - - ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance within static main() method - // initial execution: handle args array of initialization arguments here - if (args.length == 2) + if (args.length == 2) { if ((args[0] != null) && !args[0].isEmpty()) thisProgram.setNetworkAddress(args[0]); - if ((args[1] != null) && !args[1].isEmpty()) thisProgram.setNetworkPort(Integer.parseInt(args[1])); } - else if (args.length != 0) + else if (args.length != 0) { System.err.println("Usage: " + thisProgram.getClass().getSimpleName() + " [address port]"); System.exit(-1); } + } + + /** Locally instantiable copy of program, can be subclassed. */ + protected static ExampleSimulationProgram thisProgram; + + /** + * Main method is first executed when a program instance is loaded. + * @see <a href="https://docs.oracle.com/javase/tutorial/getStarted/application/index.html">Java Tutorials: A Closer Look at the "Hello World!" Application</a> + * @param args command-line parameters: network address and port. + * Command-line arguments are an array of optional String parameters that are passed from execution environment during invocation + */ + public static void main(String[] args) + { + System.out.println(TRACE_PREFIX + "main() started..."); + + thisProgram = new ExampleSimulationProgram(); // creates instance of self within static main() method + + handleArgs (args); + // OK here we go... thisProgram.setUpNetworkInterface(); @@ -388,18 +418,4 @@ public class ExampleSimulationProgram System.out.println(TRACE_PREFIX + "complete."); // report successful completion } - - /** - * @return whether verboseComments mode is enabled - */ - public boolean isVerboseComments() { - return verboseComments; - } - - /** - * @param newVerboseComments whether verboseComments mode is enabled - */ - public void setVerboseComments(boolean newVerboseComments) { - this.verboseComments = newVerboseComments; - } }