From f5ec53849dfd52b3d797a501edc0246fe187e98f Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@nps.edu>
Date: Sat, 21 May 2022 14:57:44 -0700
Subject: [PATCH] Extract common superclass OpenDis7NetworkCapabilities from
 ExampleSimulationProgram for simple extension of other programs

---
 .../ExampleSimulationProgram.java             | 145 +----------------
 .../ExampleSimulationProgramLog.txt           |  12 +-
 .../OpenDis7NetworkCapabilities.java          | 150 ++++++++++++++++++
 3 files changed, 162 insertions(+), 145 deletions(-)
 create mode 100644 examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java

diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
index 3b3ee40842..339cdabddc 100644
--- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
+++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java
@@ -25,31 +25,16 @@ import java.util.logging.Logger;
  *  Default program initialization includes PDU recording turned on by default.
  *  @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt">ExampleSimulationProgramLog.txt</a>
  */
-public class ExampleSimulationProgram
+public class ExampleSimulationProgram extends OpenDis7NetworkCapabilities
 {
-    private      boolean verboseComments         = true;
-    static final String  NETWORK_ADDRESS_DEFAULT  = "239.1.2.3";
-    static final int     NETWORK_PORT_DEFAULT     = 3000;
-                 String  networkAddress           = NETWORK_ADDRESS_DEFAULT;
-                 int     networkPort              = NETWORK_PORT_DEFAULT;
-                 String  thisHostName             = "localhost";
-                 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 help with logging by identifying this class (can be overridden in subclass).
-     */
-    protected static String TRACE_PREFIX;
     
-    /* Declare DIS Protocol Data Unit (PDU) classes for simulation entities */
-    
-    DisTime.TimestampStyle       timestampStyle      = DisTime.TimestampStyle.IEEE_ABSOLUTE;
+    /** Creates DIS Protocol Data Unit (PDU) classes for simulation entities */
     PduFactory                   pduFactory          = new PduFactory(timestampStyle);
     
     /** EntityID settings for entity 1 */
@@ -227,29 +212,14 @@ public class ExampleSimulationProgram
     VariableRecordType currentTimeStepComment = VariableRecordType.APPLICATION_TIMESTEP;
     VariableRecordType           otherComment = VariableRecordType.OTHER;
     
-    // class variables
-    DisThreadedNetworkInterface             disNetworkInterface;
-    DisThreadedNetworkInterface.PduListener pduListener;
-    Pdu                                     receivedPdu;
-    PduRecorder                             pduRecorder;
-    
     /**
-     * Constructor design goal: additional built-in initialization conveniences can go here
-     * to keep student efforts focused on the runSimulation() method.
+     * Constructor to create an instance of this class.
+     * Design goal: additional built-in initialization conveniences can go here
+     * to keep your efforts focused on the runSimulation() method.
      */
     public ExampleSimulationProgram()
     {
-        DisTime.setTimestampStyle(timestampStyle);
-        
-        try
-        {
-            thisHostName = InetAddress.getLocalHost().getHostName();
-            System.out.println(TRACE_PREFIX + "thisHostName=" + thisHostName);
-        }
-        catch (UnknownHostException uhe)
-        {
-            System.out.println(TRACE_PREFIX + thisHostName + "not connected to network: " + uhe.getMessage());
-        }
+        // additional constructor initialization goes here
     }
     
     /**
@@ -266,94 +236,6 @@ public class ExampleSimulationProgram
         setNetworkPort(port);
     }
 
-    /**
-     * get current networkAddress as a string
-     * @return the networkAddress
-     */
-    public String getNetworkAddress()
-    {
-        return networkAddress;
-    }
-    /**
-     * set current networkAddress using a string
-     * @param newNetworkAddress the networkAddress to set
-     */
-    public final void setNetworkAddress(String newNetworkAddress)
-    {
-        this.networkAddress = newNetworkAddress;
-    }
-
-    /**
-     * get current networkPort
-     * @return the networkPort
-     */
-    public int getNetworkPort()
-    {
-        return networkPort;
-    }
-    /**
-     * set current networkPort
-     * @param newNetworkPort the networkPort to set
-     */
-    public final void setNetworkPort(int newNetworkPort)
-    {
-        this.networkPort = newNetworkPort;
-    }
-    /**
-     * Get timestampStyle used by PduFactory
-     * @return current timestampStyle
-     */
-    public DisTime.TimestampStyle getTimestampStyle()
-    {
-        return timestampStyle;
-    }
-    /**
-     * Set timestampStyle used by PduFactory
-     * @param newTimestampStyle the timestampStyle to set
-     * @return same object to permit progressive setters 
-     */
-    public ExampleSimulationProgram setTimestampStyle(DisTime.TimestampStyle newTimestampStyle) 
-    {
-        timestampStyle = newTimestampStyle;
-        DisTime.setTimestampStyle(newTimestampStyle);
-        return this;
-    }
-
-    /**
-     * Initialize network interface, choosing best available network interface
-     */
-    public void setUpNetworkInterface()
-    {
-        disNetworkInterface = new DisThreadedNetworkInterface(getNetworkAddress(), getNetworkPort());
-        disNetworkInterface.setDescriptor ("ExampleSimulationProgram pdu looping");
-        
-        System.out.println("Network confirmation:" +
-               " address=" + disNetworkInterface.getAddress()+ //  disNetworkInterface.getMulticastGroup() + 
-                  " port=" + disNetworkInterface.getPort());   // + disNetworkInterface.getDisPort());
-        pduListener = new DisThreadedNetworkInterface.PduListener()
-        {
-            /** Callback handler for listener */
-            @Override
-            public void incomingPdu(Pdu newPdu)
-            {
-                receivedPdu = newPdu;
-            }
-        };
-        disNetworkInterface.addListener(pduListener);
-        
-        String outputDirectory = DEFAULT_OUTPUT_DIRECTORY;
-        System.out.println("Beginning pdu save to directory " + outputDirectory);
-        pduRecorder = new PduRecorder(outputDirectory, getNetworkAddress(), getNetworkPort()); // assumes save
-        pduRecorder.setEncodingPduLog(PduRecorder.ENCODING_PLAINTEXT);
-        pduRecorder.setVerbose(true); // either sending, receiving or both
-        pduRecorder.start(); // begin running
-    }
-
-    /** All done, release network resources */
-    public void tearDownNetworkInterface()
-    {
-        pduRecorder.stop(); // handles disNetworkInterface.close(), tears down threads and sockets
-    }
 
     /** 
      * Send a single Protocol Data Unit (PDU) of any type
@@ -432,21 +314,6 @@ public class ExampleSimulationProgram
         }
     }
 
-    /**
-     * test for verboseComments mode
-     * @return whether verboseComments mode is enabled
-     */
-    public boolean isVerboseComments() {
-        return verboseComments;
-    }
-
-    /**
-     * set verboseComments mode
-     * @param newVerboseComments whether verboseComments mode is enabled
-     */
-    public void setVerboseComments(boolean newVerboseComments) {
-        this.verboseComments = newVerboseComments;
-    }
     
     /**
      * Initial execution via main() method: handle args array of command-line initialization (CLI) arguments here
diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt b/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
index ca7a8fc18c..3c41b8971c 100644
--- a/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
+++ b/examples/src/OpenDis7Examples/ExampleSimulationProgramLog.txt
@@ -30,8 +30,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 PduRecorder] [receipt  2] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending  3] DisPduType 22 COMMENT, size 104 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  3] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt  3] DisPduType 22 COMMENT, size 104 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [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   Entity #2, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  4] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
@@ -51,8 +51,8 @@ sending PDUs for simulation step 2, monitor loopback to confirm sent
 [DisThreadedNetworkInterface 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   Entity #2, size 144 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  8] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt  8] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt  8] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
 ... [PDUs successfully sent for this loop]
 ... My simulation just did something, no really...
 ... [Pausing for 1.0 seconds]
@@ -78,8 +78,8 @@ sending PDUs for simulation step 4, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 13] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 13] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 14] DisPduType 02 FIRE, size 96 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 14] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 14] DisPduType 02 FIRE, size 96 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 14] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 15] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 15] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 15] DisPduType 22 COMMENT, size 104 bytes)
@@ -92,23 +92,23 @@ sending PDUs for simulation step 4, monitor loopback to confirm sent
 ... [Pausing for 1.0 seconds]
 sending PDUs for simulation step 5, monitor loopback to confirm sent
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 17] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 17] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 17] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 17] DisPduType 01 ENTITY_STATE   Entity #1, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 18] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 18] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 18] DisPduType 02 FIRE, size 96 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 19] DisPduType 22 COMMENT, size 104 bytes)
-[DisThreadedNetworkInterface PduRecorder] [receipt 19] DisPduType 22 COMMENT, size 104 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 19] DisPduType 22 COMMENT, size 104 bytes)
+[DisThreadedNetworkInterface PduRecorder] [receipt 19] DisPduType 22 COMMENT, size 104 bytes)
 *** [Narrative comment sent: APPLICATION_TIMESTEP] [MV3500 ExampleSimulationProgram, runSimulation() loop 5]
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 20] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 20] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 20] DisPduType 01 ENTITY_STATE   Entity #2, size 144 bytes)
 ... [PDUs successfully sent for this loop]
 ... [loop termination condition met, simulationComplete=true]
-[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 21] DisPduType 22 COMMENT, size 120 bytes)
 [DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [receipt 21] DisPduType 22 COMMENT, size 120 bytes)
 [DisThreadedNetworkInterface PduRecorder] [receipt 21] DisPduType 22 COMMENT, size 120 bytes)
+[DisThreadedNetworkInterface ExampleSimulationProgram pdu looping] [sending 21] DisPduType 22 COMMENT, size 120 bytes)
 *** [Narrative comment sent: COMPLETE_EVENT_REPORT] [MV3500 ExampleSimulationProgram, runSimulation() completed successfully]
 ... [final CommentPdu successfully sent for simulation]
 *** setKillSentinelAndInterrupts() killed=true sendingThread.isInterrupted()=true receiveThread.isInterrupted()=true
diff --git a/examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java b/examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java
new file mode 100644
index 0000000000..114ead964c
--- /dev/null
+++ b/examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java
@@ -0,0 +1,150 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package OpenDis7Examples;
+
+import edu.nps.moves.dis7.pdus.Pdu;
+import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
+import edu.nps.moves.dis7.utilities.DisTime;
+import edu.nps.moves.dis7.utilities.stream.PduRecorder;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+// import jdk.internal.vm.annotation.IntrinsicCandidate;
+
+/**
+ *
+ * @author brutzman
+ */
+public class OpenDis7NetworkCapabilities {
+    
+    static final String NETWORK_ADDRESS_DEFAULT = "239.1.2.3";
+    static final int       NETWORK_PORT_DEFAULT = 3000;
+    
+    String  thisHostName             = "localhost";
+    String  DEFAULT_PDULOG_OUTPUT_DIRECTORY = "./pduLog";
+    /**
+     * Output prefix to help with logging by identifying this class (can be overridden in subclass).
+     */
+    protected static String TRACE_PREFIX;
+    protected boolean verboseComments = true;
+    String networkAddress = NETWORK_ADDRESS_DEFAULT;
+    int       networkPort = NETWORK_PORT_DEFAULT;
+    DisTime.TimestampStyle timestampStyle = DisTime.TimestampStyle.IEEE_ABSOLUTE;
+    
+    // class variables
+    DisThreadedNetworkInterface             disNetworkInterface;
+    DisThreadedNetworkInterface.PduListener pduListener;
+    Pdu                                     receivedPdu;
+    PduRecorder                             pduRecorder;
+
+    public OpenDis7NetworkCapabilities()
+    {
+        DisTime.setTimestampStyle(timestampStyle); // DISTime is a singleton shared class
+        
+        try
+        {
+            thisHostName = InetAddress.getLocalHost().getHostName();
+            System.out.println(TRACE_PREFIX + "thisHostName=" + thisHostName);
+        }
+        catch (UnknownHostException uhe)
+        {
+            System.out.println(TRACE_PREFIX + thisHostName + "not connected to network: " + uhe.getMessage());
+        }
+    }
+    
+    /**
+     * get current networkAddress as a string
+     * @return the networkAddress
+     */
+    public String getNetworkAddress() {
+        return networkAddress;
+    }
+
+    /**
+     * set current networkAddress using a string
+     * @param newNetworkAddress the networkAddress to set
+     */
+    public final void setNetworkAddress(String newNetworkAddress) {
+        this.networkAddress = newNetworkAddress;
+    }
+
+    /**
+     * get current networkPort
+     * @return the networkPort
+     */
+    public int getNetworkPort() {
+        return networkPort;
+    }
+
+    /**
+     * set current networkPort
+     * @param newNetworkPort the networkPort to set
+     */
+    public final void setNetworkPort(int newNetworkPort) {
+        this.networkPort = newNetworkPort;
+    }
+
+    /**
+     * Get timestampStyle used by PduFactory
+     * @return current timestampStyle
+     */
+    public DisTime.TimestampStyle getTimestampStyle() {
+        return timestampStyle;
+    }
+
+    /**
+     * Set timestampStyle used by PduFactory
+     * @param newTimestampStyle the timestampStyle to set
+     */
+    public void setTimestampStyle(DisTime.TimestampStyle newTimestampStyle) {
+        timestampStyle = newTimestampStyle;
+        DisTime.setTimestampStyle(newTimestampStyle);
+    }
+
+    /**
+     * Initialize network interface, choosing best available network interface
+     */
+    public void setUpNetworkInterface() {
+        disNetworkInterface = new DisThreadedNetworkInterface(getNetworkAddress(), getNetworkPort());
+        disNetworkInterface.setDescriptor("ExampleSimulationProgram pdu looping");
+        System.out.println("Network confirmation:" + " address=" + disNetworkInterface.getAddress() + //  disNetworkInterface.getMulticastGroup() +
+        " port=" + disNetworkInterface.getPort()); // + disNetworkInterface.getDisPort());
+        pduListener = new DisThreadedNetworkInterface.PduListener() {
+            /** Callback handler for listener */
+            @Override
+            public void incomingPdu(Pdu newPdu) {
+                receivedPdu = newPdu;
+            }
+        };
+        disNetworkInterface.addListener(pduListener);
+        String pduLogOutputDirectory = DEFAULT_PDULOG_OUTPUT_DIRECTORY;
+        System.out.println("Beginning pdu save to directory " + pduLogOutputDirectory);
+        pduRecorder = new PduRecorder(pduLogOutputDirectory, getNetworkAddress(), getNetworkPort()); // assumes save
+        pduRecorder.setEncodingPduLog(PduRecorder.ENCODING_PLAINTEXT);
+        pduRecorder.setVerbose(true); // either sending, receiving or both
+        pduRecorder.start(); // begin running
+    }
+
+    /** All done, release network resources */
+    public void tearDownNetworkInterface() {
+        pduRecorder.stop(); // handles disNetworkInterface.close(), tears down threads and sockets
+    }
+
+    /**
+     * test for verboseComments mode
+     * @return whether verboseComments mode is enabled
+     */
+    public boolean isVerboseComments() {
+        return verboseComments;
+    }
+
+    /**
+     * set verboseComments mode
+     * @param newVerboseComments whether verboseComments mode is enabled
+     */
+    public void setVerboseComments(boolean newVerboseComments) {
+        this.verboseComments = newVerboseComments;
+    }
+    
+}
-- 
GitLab