From c7892d5cf9f49641195ca649da91ed51f76e6483 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@DESKTOP-2S09UKA> Date: Wed, 2 Sep 2020 10:59:07 -0700 Subject: [PATCH] further refinements --- .../ExampleSimulationProgram.java | 67 +++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java index bdc353af46..e3bb082c56 100644 --- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java +++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java @@ -12,6 +12,7 @@ import edu.nps.moves.dis7.pdus.FirePdu; import edu.nps.moves.dis7.pdus.Pdu; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import edu.nps.moves.dis7.utilities.PduFactory; +import java.util.ArrayList; public class ExampleSimulationProgram { @@ -106,10 +107,10 @@ public class ExampleSimulationProgram } /** - * Send a PDU of any type + * Send a single Protocol Data Unit (PDU) of any type * @param pdu the pdu to send */ - private void sendPdu(Pdu pdu) + private void sendSinglePdu(Pdu pdu) { try { @@ -124,51 +125,78 @@ public class ExampleSimulationProgram } /** - * PDU sending: Entity State, Fire, Comment + * Send EntityState, Fire, Comment PDUs * @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html">Passing Information to a Method or a Constructor</a> Arbitrary Number of Arguments * @param entityStatePdu the ESPDU to send, if any * @param firePdu the FirePDU to send, if any * @param commentType enumeration value describing the narrative comment * @param comments String array of narrative comments */ - public void createSendPdus(EntityStatePdu entityStatePdu, + public void sendAllPdus(EntityStatePdu entityStatePdu, FirePdu firePdu, VariableRecordType commentType, // vararg... variable length string String... comments) { if (entityStatePdu != null) - sendPdu(entityStatePdu); + sendSinglePdu(entityStatePdu); if (firePdu != null) - sendPdu(firePdu); // bang + sendSinglePdu(firePdu); // bang - if ((comments == null) || (comments.length > 0)) + if ((comments != null) && (comments.length > 0)) { - if (commentType == null) - commentType = VariableRecordType.OTHER; - CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, comments); - sendPdu(commentPdu); + ArrayList<String> newCommentsList = new ArrayList<>(); + for (int i = 0; i < comments.length; i++) + { + if (!comments[i].isEmpty()) + newCommentsList.add(comments[i]); // OK found something to send + } + if (!newCommentsList.isEmpty()) + { + if (commentType == null) + commentType = VariableRecordType.OTHER; + CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, newCommentsList.toArray(new String[0])); // comments); + sendSinglePdu(commentPdu); + } } } + /** + * 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 + */ public static void main(String[] args) { + ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance + // initial execution: can handle args array of initialization arguments here 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])); } - - ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance + else if (args.length != 0) + { + System.err.println("Usage: " + thisProgram.getClass().getName() + " [address port]"); + System.exit(-1); + } + // OK here we go... thisProgram.setUpNetworkInterface(); - thisProgram.runSimulation (); + thisProgram.runSimulation (); // customization code goes in there thisProgram.tearDownNetworkInterface(); } + /** + * User-modifiable method for defining and running a simulation. + */ public void runSimulation () { final int MAX_LOOP_COUNT = 10; @@ -191,28 +219,29 @@ public class ExampleSimulationProgram // initialize loop variables loopCount++; - // your simulation code here! + // =============================== + // your own simulation code here! - // your narrative code for CommentPdu here + // your narrative code for CommentPdu here, set all to empty strings to avoid sending narrativeMessage1 = "MV3500 ExampleSimulation"; narrativeMessage2 = "runSimulation loop " + loopCount; narrativeMessage3 = ""; // intentionally blank for testing - // your termination condition + // =============================== + // your loop termination condition if (loopCount > 4) // for example { simulationComplete = true; System.out.println ("*** termination condition met, simulationComplete=" + simulationComplete); } - // loop now finished so terminate if simulation complete, otherwise send latest PDUs and continue if (simulationComplete) break; System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); - createSendPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); + sendAllPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); System.out.println ("... PDUs successfully sent"); } } -- GitLab