From f5eb765cf6cd1c57a4d59ed22c440f1ed5d5b00d Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@nps.edu>
Date: Thu, 23 Dec 2021 16:28:37 -0800
Subject: [PATCH] multiple refinements to allow satisfactory subclassing

---
 .../ExampleSimulationProgram.java             | 58 +++++++++++--------
 .../ExampleSimulationProgramLog.txt           | 17 +++---
 2 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
index 610b346ede..7ac8d10c8b 100644
--- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
+++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
@@ -22,16 +22,29 @@ import java.util.logging.Logger;
  */
 public class ExampleSimulationProgram
 {
-    private      boolean verboseComments         = true;
+    protected    boolean verboseComments         = true;
     static final String  NETWORK_ADDRESS_DEFAULT  = "239.1.2.3";
     static final int     NETWORK_PORT_DEFAULT     = 3000;
-    static       String  networkAddress           = NETWORK_ADDRESS_DEFAULT;
-    static       int     networkPort              = NETWORK_PORT_DEFAULT;
+                 String  networkAddress           = NETWORK_ADDRESS_DEFAULT;
+                 int     networkPort              = NETWORK_PORT_DEFAULT;
                  String  DEFAULT_OUTPUT_DIRECTORY = "./pduLog";
+    
+    /** seconds for real-time execution (not simulation time, which may or may not be the same) */
+    double  currentTimeStep  =  1.0; // seconds
+    /** initial simulation time */
+    double  initialTime = 0.0;
+    /** current simulation time */
+    double  simulationTime;
+
+    /**
+     * Output prefix to identify this class (override in subclass), helps with logging
+     */
+    private final static String TRACE_PREFIX = "[" + ExampleSimulationProgram.class.getName() + "] ";
                  
     /**
-     * This runSimulationLoops() method is for you, a
-     * programmer-modifiable method for defining and running a new simulation of interest.
+     * This runSimulationLoops() method is for you, a programmer-modifiable code block
+     * for defining and running a new simulation of interest.
+     * 
      * Welcome! Other parts of this program handle bookkeeping and plumbing tasks so that
      * you can focus on your model entities and activities.
      * Expandable support includes DIS EntityStatePdu, FirePdu and CommentPdu all available for 
@@ -45,12 +58,10 @@ public class ExampleSimulationProgram
     public void runSimulationLoops ()
     {
       try
-      {
-        /** seconds for real-time execution (not simulation time, which may or may not be the same) */
-        final double  SIMULATION_LOOP_DURATION_SECONDS  =  1.0; 
-        final int     SIMULATION_MAX_LOOP_COUNT = 10; // be deliberate out out there!  also avoid infinite loops.
+      {              
+        final int     SIMULATION_MAX_LOOP_COUNT = 10; // be deliberate out there!  also avoid infinite loops.
               int     simulationLoopCount = 0;        // variable, initialized at 0
-              boolean simulationComplete = false;     // sentinel variable as termination condition,, are we done yet?
+              boolean simulationComplete = false;     // sentinel variable as termination condition, are we done yet?
         
         // TODO reset clock to zero each time for consistent outputs
         
@@ -121,8 +132,8 @@ public class ExampleSimulationProgram
             
             // staying synchronized with timestep: wait duration for elapsed time in this loop
             // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes
-            Thread.sleep((long)(SIMULATION_LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds
-            System.out.println ("... [Pausing for " + SIMULATION_LOOP_DURATION_SECONDS + " seconds]");
+            Thread.sleep((long)(currentTimeStep * 1000)); // seconds * (1000 msec/sec) = milliseconds
+            System.out.println ("... [Pausing for " + currentTimeStep + " seconds]");
             
             // OK now send the status PDUs for this loop, and then continue
             System.out.println ("sending PDUs for simulation step " + simulationLoopCount + ", monitor loopback to confirm sent");
@@ -162,11 +173,6 @@ public class ExampleSimulationProgram
     VariableRecordType      statusComment = VariableRecordType.APPLICATION_STATUS;
     VariableRecordType    timeStepComment = VariableRecordType.APPLICATION_TIMESTEP;
     VariableRecordType       otherComment = VariableRecordType.OTHER;
-        
-	/**
-	 * Output prefix to identify this class, helps with logging
-	 */
-    private final static String TRACE_PREFIX = "[" + ExampleSimulationProgram.class.getName() + "] ";
     
     // class variables
     PduFactory                              pduFactory = new PduFactory();
@@ -191,6 +197,8 @@ public class ExampleSimulationProgram
      */
     public ExampleSimulationProgram(String address, int port)
     {
+        super(); // if code block is provided
+        
         setNetworkAddress(address);
         
         setNetworkPort(port);
@@ -209,7 +217,7 @@ public class ExampleSimulationProgram
      */
     public final void setNetworkAddress(String newNetworkAddress)
     {
-        ExampleSimulationProgram.networkAddress = newNetworkAddress;
+        this.networkAddress = newNetworkAddress;
     }
 
     /**
@@ -225,7 +233,7 @@ public class ExampleSimulationProgram
      */
     public final void setNetworkPort(int newNetworkPort)
     {
-        ExampleSimulationProgram.networkPort = newNetworkPort;
+        this.networkPort = newNetworkPort;
     }
 
     /**
@@ -255,7 +263,7 @@ public class ExampleSimulationProgram
         pduRecorder = new PduRecorder(outputDirectory, getNetworkAddress(), getNetworkPort()); // assumes save
         pduRecorder.setDescriptor ("ExampleSimulationProgram pduRecorder");
         pduRecorder.setEncodingPduLog(PduRecorder.ENCODING_PLAINTEXT);
-        pduRecorder.setVerbose(true);
+        pduRecorder.setVerbose(true); // either sending, receiving or both
         pduRecorder.start(); // begin running
     }
 
@@ -275,7 +283,7 @@ public class ExampleSimulationProgram
      * Send a single Protocol Data Unit (PDU) of any type
      * @param pdu the pdu to send
      */
-    private void sendSinglePdu(Pdu pdu)
+    protected void sendSinglePdu(Pdu pdu)
     {
         try
         {
@@ -352,11 +360,11 @@ public class ExampleSimulationProgram
      */
     public static void main(String[] args)
     {
-        System.out.println(TRACE_PREFIX + "started...");
+        System.out.println(TRACE_PREFIX + "main() started...");
         
-        ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance
+        ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance within static main() method
         
-        // initial execution: can handle args array of initialization arguments here
+        // initial execution: handle args array of initialization arguments here
         if (args.length == 2)
         {
             if ((args[0] != null) && !args[0].isEmpty())
@@ -367,7 +375,7 @@ public class ExampleSimulationProgram
         }
         else if (args.length != 0)
         {
-            System.err.println("Usage: " + thisProgram.getClass().getName() + " [address port]");
+            System.err.println("Usage: " + thisProgram.getClass().getSimpleName() + " [address port]");
             System.exit(-1);
         }
         // OK here we go...
diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt b/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
index 67e209c354..0af3c3c2bb 100644
--- a/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
+++ b/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
@@ -6,12 +6,12 @@ Updating property file: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\build\b
 Compiling 1 source file to C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\build\classes
 compile-single:
 run-single:
-[OpenDis7Examples.ExampleSimulationProgram] started...
+[OpenDis7Examples.ExampleSimulationProgram] main() started...
 [DisThreadedNetworkInterface] using network interface Intel(R) Wi-Fi 6E AX210 160MHz
 [DisThreadedNetworkInterface] datagramSocket.joinGroup  address=239.1.2.3 port=3000 start() complete
 Network confirmation: address=239.1.2.3 port=3000
 Beginning pdu save to directory ./pduLog
-Recorder log file open: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\pduLog\PduCaptureLog.dislog
+Recorder log file open: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\pduLog\PduCaptureLog29.dislog
 [DisThreadedNetworkInterface] using network interface Intel(R) Wi-Fi 6E AX210 160MHz
 [DisThreadedNetworkInterface] datagramSocket.joinGroup  address=239.1.2.3 port=3000 start() complete
 [PduRecorder ExampleSimulationProgram pduRecorder] listening to IP address 239.1.2.3 on port 3000
@@ -25,8 +25,8 @@ sending PDUs for simulation step 1, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  2] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  2] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending  3] DisPduType 22 COMMENT, size 104 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  3] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  3] DisPduType 22 COMMENT, size 104 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  3] DisPduType 22 COMMENT, size 104 bytes)
 *** [Narrative comment sent: APPLICATION_TIMESTEP] [MV3500 ExampleSimulationProgram, runSimulation() loop 1]
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending  4] DisPduType 01 ENTITY_STATE, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  4] DisPduType 01 ENTITY_STATE, size 144 bytes)
@@ -46,15 +46,15 @@ sending PDUs for simulation step 2, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  7] DisPduType 22 COMMENT, size 104 bytes)
 *** [Narrative comment sent: APPLICATION_TIMESTEP] [MV3500 ExampleSimulationProgram, runSimulation() loop 2]
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending  8] DisPduType 01 ENTITY_STATE, size 144 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  8] DisPduType 01 ENTITY_STATE, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  8] DisPduType 01 ENTITY_STATE, size 144 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  8] DisPduType 01 ENTITY_STATE, size 144 bytes)
 ... [PDUs successfully sent for this loop]
 ... My simulation just did something, no really...
 ... [Pausing for 1.0 seconds]
 sending PDUs for simulation step 3, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending  9] DisPduType 01 ENTITY_STATE, size 144 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  9] DisPduType 01 ENTITY_STATE, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  9] DisPduType 01 ENTITY_STATE, size 144 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt  9] DisPduType 01 ENTITY_STATE, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 10] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 10] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt 10] DisPduType 02 FIRE, size 96 bytes)
@@ -76,8 +76,8 @@ sending PDUs for simulation step 4, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 14] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt 14] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 15] DisPduType 22 COMMENT, size 104 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt 15] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 15] DisPduType 22 COMMENT, size 104 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pduRecorder] [receipt 15] DisPduType 22 COMMENT, size 104 bytes)
 *** [Narrative comment sent: APPLICATION_TIMESTEP] [MV3500 ExampleSimulationProgram, runSimulation() loop 4]
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 16] DisPduType 01 ENTITY_STATE, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 16] DisPduType 01 ENTITY_STATE, size 144 bytes)
@@ -107,7 +107,6 @@ sending PDUs for simulation step 5, monitor loopback to confirm sent
 *** [Narrative comment sent: COMPLETE_EVENT_REPORT] [MV3500 ExampleSimulationProgram, runSimulation() completed successfully]
 ... [final CommentPdu successfully sent for simulation]
 
-Closing recorder log file: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\pduLog\PduCaptureLog.dislog
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] datagramSocket.leaveGroup address=239.1.2.3 port=3000  stop() complete
+Closing recorder log file: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\pduLog\PduCaptureLog29.dislog
 [OpenDis7Examples.ExampleSimulationProgram] complete.
-BUILD SUCCESSFUL (total time: 17 seconds)
+BUILD SUCCESSFUL (total time: 9 seconds)
-- 
GitLab