From b56c7ae7aea9b616f17c473222be4579e7c5b696 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Sat, 21 May 2022 21:01:09 -0700 Subject: [PATCH] refactor rename for clairty --- ...Capabilities.java => ChannelOpenDis7.java} | 67 ++++++++++++++++--- .../ExampleSimulationProgram.java | 32 ++------- .../ExampleTrackInterpolation.java | 20 +++--- .../ArrivalProcessOpenDis7.java | 16 +++-- 4 files changed, 83 insertions(+), 52 deletions(-) rename examples/src/OpenDis7Examples/{OpenDis7NetworkCapabilities.java => ChannelOpenDis7.java} (74%) diff --git a/examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java b/examples/src/OpenDis7Examples/ChannelOpenDis7.java similarity index 74% rename from examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java rename to examples/src/OpenDis7Examples/ChannelOpenDis7.java index 114ead964c..45102a73ae 100644 --- a/examples/src/OpenDis7Examples/OpenDis7NetworkCapabilities.java +++ b/examples/src/OpenDis7Examples/ChannelOpenDis7.java @@ -16,17 +16,16 @@ import java.net.UnknownHostException; * * @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"; +public class ChannelOpenDis7 { /** - * Output prefix to help with logging by identifying this class (can be overridden in subclass). + * Output prefix to help with logging by identifying this class. */ - protected static String TRACE_PREFIX; + private static String TRACE_PREFIX = "[OpenDis7]"; + static String thisHostName = "localhost"; + private static final String NETWORK_ADDRESS_DEFAULT = "239.1.2.3"; + private static final int NETWORK_PORT_DEFAULT = 3000; + private static final String DEFAULT_PDULOG_OUTPUT_DIRECTORY = "./pduLog"; + protected boolean verboseComments = true; String networkAddress = NETWORK_ADDRESS_DEFAULT; int networkPort = NETWORK_PORT_DEFAULT; @@ -38,7 +37,7 @@ public class OpenDis7NetworkCapabilities { Pdu receivedPdu; PduRecorder pduRecorder; - public OpenDis7NetworkCapabilities() + public ChannelOpenDis7() { DisTime.setTimestampStyle(timestampStyle); // DISTime is a singleton shared class @@ -131,6 +130,24 @@ public class OpenDis7NetworkCapabilities { pduRecorder.stop(); // handles disNetworkInterface.close(), tears down threads and sockets } + /** + * Send a single Protocol Data Unit (PDU) of any type + * @param pdu the pdu to send + */ + protected void sendSinglePdu(Pdu pdu) + { + try + { + disNetworkInterface.send(pdu); + Thread.sleep(100); // TODO consider refactoring the wait logic and moving externally + } + catch (InterruptedException ex) + { + System.err.println(this.getClass().getName() + " Error sending PDU: " + ex.getLocalizedMessage()); + System.exit(1); + } + } + /** * test for verboseComments mode * @return whether verboseComments mode is enabled @@ -146,5 +163,33 @@ public class OpenDis7NetworkCapabilities { public void setVerboseComments(boolean newVerboseComments) { this.verboseComments = newVerboseComments; } - + + /** + * @return the TRACE_PREFIX + */ + public static String getTRACE_PREFIX() { + return TRACE_PREFIX; + } + + /** + * @param aTRACE_PREFIX the TRACE_PREFIX to set + */ + public static void setTRACE_PREFIX(String aTRACE_PREFIX) { + TRACE_PREFIX = aTRACE_PREFIX; + } + + /** + * Print message with TRACE_PREFIX prepended + * @param message String to print + */ + public void printTRACE(String message) { + System.out.print(TRACE_PREFIX + message); + } + /** + * Print message with TRACE_PREFIX prepended + * @param message String to print + */ + public void printlnTRACE(String message) { + System.out.println(TRACE_PREFIX + message); + } } diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java index 339cdabddc..3fc3a404f9 100644 --- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java +++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java @@ -9,12 +9,7 @@ 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.*; import edu.nps.moves.dis7.pdus.*; -import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; -import edu.nps.moves.dis7.utilities.DisTime; import edu.nps.moves.dis7.utilities.PduFactory; -import edu.nps.moves.dis7.utilities.stream.PduRecorder; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,7 +20,7 @@ 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 extends OpenDis7NetworkCapabilities +public class ExampleSimulationProgram extends ChannelOpenDis7 { /** seconds for real-time execution (not simulation time, which may or may not be the same) */ double currentTimeStep = 1.0; // seconds @@ -237,24 +232,6 @@ public class ExampleSimulationProgram extends OpenDis7NetworkCapabilities } - /** - * Send a single Protocol Data Unit (PDU) of any type - * @param pdu the pdu to send - */ - protected void sendSinglePdu(Pdu pdu) - { - try - { - disNetworkInterface.send(pdu); - Thread.sleep(100); // TODO consider refactoring the wait logic and moving externally - } - catch (InterruptedException ex) - { - System.err.println(this.getClass().getName() + " Error sending PDU: " + ex.getLocalizedMessage()); - System.exit(1); - } - } - /** * Send Comment PDU * @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 @@ -303,6 +280,7 @@ public class ExampleSimulationProgram extends OpenDis7NetworkCapabilities if (commentType == null) commentType = otherComment; // fallback value otherComment // now build the commentPdu from these string inputs, thus constructing a narrative entry + @SuppressWarnings("CollectionsToArray") CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, newCommentsList.toArray(new String[0])); // comments); sendSinglePdu(commentPdu); if (isVerboseComments()) @@ -347,9 +325,9 @@ public class ExampleSimulationProgram extends OpenDis7NetworkCapabilities */ public static void main(String[] args) { - TRACE_PREFIX = "[" + ExampleSimulationProgram.class.getName() + "] "; + setTRACE_PREFIX("[" + ExampleSimulationProgram.class.getName() + "] "); - System.out.println(TRACE_PREFIX + "main() started..."); + System.out.println(getTRACE_PREFIX() + "main() started..."); thisProgram = new ExampleSimulationProgram(); // creates instance of self within static main() method @@ -363,7 +341,7 @@ public class ExampleSimulationProgram extends OpenDis7NetworkCapabilities thisProgram.tearDownNetworkInterface(); // make sure no processes are left lingering - System.out.println(TRACE_PREFIX + "complete."); // report successful completion + System.out.println(getTRACE_PREFIX() + "complete."); // report successful completion System.exit(0); // ensure all threads and sockets released } diff --git a/examples/src/OpenDis7Examples/ExampleTrackInterpolation.java b/examples/src/OpenDis7Examples/ExampleTrackInterpolation.java index 0b4f17453c..59075d6348 100644 --- a/examples/src/OpenDis7Examples/ExampleTrackInterpolation.java +++ b/examples/src/OpenDis7Examples/ExampleTrackInterpolation.java @@ -152,7 +152,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram if (false) // real-time operation or simulation speedup { Thread.sleep((long) (currentTimeStep * 1000)); // seconds * (1000 msec/sec) = milliseconds - System.out.println(TRACE_PREFIX + "Pausing for " + currentTimeStep + " seconds"); + System.out.println(getTRACE_PREFIX() + "Pausing for " + currentTimeStep + " seconds"); } // OK now send the status PDUs for this loop, and then continue @@ -161,7 +161,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram sendSinglePdu(espdu_1); sendCommentPdu(currentTimeStepComment, narrativeMessage1, narrativeMessage2, narrativeMessage3); if (pduRecorder.hasVerboseOutput()) - System.out.println(TRACE_PREFIX + "PDUs successfully sent for this loop"); + System.out.println(getTRACE_PREFIX() + "PDUs successfully sent for this loop"); pduSentList.add(espdu_1); pduTrack_1.addPdu(espdu_1); Vector3Double location = espdu_1.getEntityLocation(); @@ -171,15 +171,15 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram // current loop now finished, check whether to terminate if simulation complete, otherwise continue if (simulationComplete || (simulationLoopCount > 10000)) // for example; including fail-safe condition is good { - System.out.println(TRACE_PREFIX + "loop termination condition met, simulationComplete=" + simulationComplete); // ", final loopCount=" + loopCount + + System.out.println(getTRACE_PREFIX() + "loop termination condition met, simulationComplete=" + simulationComplete); // ", final loopCount=" + loopCount + break; } } // end of simulation loop // =================================================================================================== - System.out.println(TRACE_PREFIX + "all PDUs successfully sent for this loop (pduSentList.size()=" + pduSentList.size() + " total)"); + System.out.println(getTRACE_PREFIX() + "all PDUs successfully sent for this loop (pduSentList.size()=" + pduSentList.size() + " total)"); // track analysis - System.out.println(TRACE_PREFIX + "pduTrack_1 initialLocation=" + pduTrack_1.getInitialLocation() + ", latestLocation=" + pduTrack_1.getLatestLocation()); + System.out.println(getTRACE_PREFIX() + "pduTrack_1 initialLocation=" + pduTrack_1.getInitialLocation() + ", latestLocation=" + pduTrack_1.getLatestLocation()); pduTrack_1.sortPdus() .createRawWaypoints(); @@ -193,7 +193,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram narrativeMessage2 = "runSimulation() completed successfully"; // all done sendCommentPdu(narrativeComment, narrativeMessage1, narrativeMessage2, narrativeMessage3); if (pduRecorder.hasVerboseOutput()) - System.out.println(TRACE_PREFIX + "final CommentPdu successfully sent for simulation"); + System.out.println(getTRACE_PREFIX() + "final CommentPdu successfully sent for simulation"); // TODO simulation management PDUs } catch (InterruptedException iex) // handle any exception that your code might choose to provoke! @@ -233,9 +233,9 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram */ public static void main(String[] args) { - TRACE_PREFIX = "[" + ExampleTrackInterpolation.class.getName() + "] "; + setTRACE_PREFIX("[" + ExampleTrackInterpolation.class.getName() + "] "); - System.out.println(TRACE_PREFIX + "main() started..."); + System.out.println(getTRACE_PREFIX() + "main() started..."); thisProgram = new ExampleTrackInterpolation(); // creates instance of self within static main() method @@ -243,7 +243,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram thisProgram.setUpNetworkInterface(); - thisProgram.pduRecorder.setDescriptor (TRACE_PREFIX.replace("[","").replace("]","") + " pduRecorder"); + thisProgram.pduRecorder.setDescriptor (getTRACE_PREFIX().replace("[","").replace("]","") + " pduRecorder"); thisProgram.pduRecorder.setVerbose(false); thisProgram.setVerboseComments(false); @@ -253,7 +253,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram thisProgram.tearDownNetworkInterface(); // make sure no processes are left lingering - System.out.println(TRACE_PREFIX + "complete."); // report successful completion + System.out.println(getTRACE_PREFIX() + "complete."); // report successful completion System.exit(0); // ensure all threads and sockets released } diff --git a/examples/src/SimkitOpenDis7Examples/ArrivalProcessOpenDis7.java b/examples/src/SimkitOpenDis7Examples/ArrivalProcessOpenDis7.java index 7ee840b921..9a47b18cd4 100644 --- a/examples/src/SimkitOpenDis7Examples/ArrivalProcessOpenDis7.java +++ b/examples/src/SimkitOpenDis7Examples/ArrivalProcessOpenDis7.java @@ -1,6 +1,6 @@ package SimkitOpenDis7Examples; -import OpenDis7Examples.OpenDis7NetworkCapabilities; +import OpenDis7Examples.ChannelOpenDis7; import simkit.SimEntityBase; import simkit.random.RandomVariate; @@ -13,7 +13,7 @@ import simkit.random.RandomVariate; */ public class ArrivalProcessOpenDis7 extends SimEntityBase { - private final OpenDis7NetworkCapabilities opendis7 = new OpenDis7NetworkCapabilities(); + private final ChannelOpenDis7 channelOpenDis7 = new ChannelOpenDis7(); /** * Generates interarrival times @@ -24,6 +24,13 @@ public class ArrivalProcessOpenDis7 extends SimEntityBase { * State variable that counts the number of Arrival events */ protected int numberArrivals; + + /** initialization */ + private void initialize() + { + channelOpenDis7.printTRACE ("opendis7.getNetworkAddress()=" + channelOpenDis7.getNetworkAddress()); + channelOpenDis7.printlnTRACE (", getNetworkPort()=" + channelOpenDis7.getNetworkPort()); + } /** * Instantiate an ArrivalProcess with the given interarrivalTimeGenerator @@ -32,7 +39,8 @@ public class ArrivalProcessOpenDis7 extends SimEntityBase { * times */ public ArrivalProcessOpenDis7(RandomVariate interarrivalTimeGenerator) { - this.setInterarrivalTimeGenerator(interarrivalTimeGenerator); + this.interarrivalTimeGenerator = interarrivalTimeGenerator; + initialize(); } /** @@ -41,7 +49,7 @@ public class ArrivalProcessOpenDis7 extends SimEntityBase { * explicit call to its setter method. */ public ArrivalProcessOpenDis7() { - System.out.println("opendis7.getNetworkAddress()=" + opendis7.getNetworkAddress()); + initialize(); } /** -- GitLab