diff --git a/examples/src/OpenDis7Examples/ChannelOpenDis7.java b/examples/src/OpenDis7Examples/ChannelOpenDis7.java index be783b7541eb98b2631a0ace15c67ce97ff25b76..55275f9a7f861f0e2126eac259b685571fc1c05f 100644 --- a/examples/src/OpenDis7Examples/ChannelOpenDis7.java +++ b/examples/src/OpenDis7Examples/ChannelOpenDis7.java @@ -17,15 +17,16 @@ import java.util.ArrayList; // import jdk.internal.vm.annotation.IntrinsicCandidate; /** - * + * Handle most networking chores, provide clean interface for programs connecting to OpenDIS7 communications * @author brutzman */ -public class ChannelOpenDis7 { +public class ChannelOpenDis7 +{ /** * Output prefix to help with logging by identifying this class. */ - private static String TRACE_PREFIX = "[OpenDis7]"; - static String thisHostName = "localhost"; + private String TRACE_PREFIX = "[OpenDis7]"; // might have different ChannelOpenDis7 objects created on different channels, so non-static + private 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"; @@ -36,13 +37,13 @@ public class ChannelOpenDis7 { static DisTime.TimestampStyle timestampStyle = DisTime.TimestampStyle.IEEE_ABSOLUTE; /** Creates DIS Protocol Data Unit (PDU) classes for simulation entities */ - static PduFactory pduFactory; + private static PduFactory pduFactory; // class variables - DisThreadedNetworkInterface disNetworkInterface; - DisThreadedNetworkInterface.PduListener pduListener; - Pdu receivedPdu; - PduRecorder pduRecorder; + private DisThreadedNetworkInterface disNetworkInterface; + DisThreadedNetworkInterface.PduListener pduListener; + Pdu receivedPdu; + private PduRecorder pduRecorder; public ChannelOpenDis7() { @@ -56,7 +57,7 @@ public class ChannelOpenDis7 { } catch (UnknownHostException uhe) { - System.out.println(TRACE_PREFIX + thisHostName + "not connected to network: " + uhe.getMessage()); + System.out.println(TRACE_PREFIX + thisHostName + " id not connected to network: " + uhe.getMessage()); } } @@ -114,9 +115,9 @@ public class ChannelOpenDis7 { */ 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()); + getDisNetworkInterface().setDescriptor("ExampleSimulationProgram pdu looping"); + System.out.println("Network confirmation:" + " address=" + getDisNetworkInterface().getAddress() + // disNetworkInterface.getMulticastGroup() + + " port=" + getDisNetworkInterface().getPort()); // + disNetworkInterface.getDisPort()); pduListener = new DisThreadedNetworkInterface.PduListener() { /** Callback handler for listener */ @Override @@ -124,18 +125,18 @@ public class ChannelOpenDis7 { receivedPdu = newPdu; } }; - disNetworkInterface.addListener(pduListener); + getDisNetworkInterface().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 + getPduRecorder().setEncodingPduLog(PduRecorder.ENCODING_PLAINTEXT); + getPduRecorder().setVerbose(true); // either sending, receiving or both + getPduRecorder().start(); // begin running } /** All done, release network resources */ public void tearDownNetworkInterface() { - pduRecorder.stop(); // handles disNetworkInterface.close(), tears down threads and sockets + getPduRecorder().stop(); // handles disNetworkInterface.close(), tears down threads and sockets } /** @@ -144,11 +145,11 @@ public class ChannelOpenDis7 { */ protected void sendSinglePdu(Pdu pdu) { - if (disNetworkInterface == null) + if (getDisNetworkInterface() == null) setUpNetworkInterface(); // ensure connected try { - disNetworkInterface.send(pdu); + getDisNetworkInterface().send(pdu); Thread.sleep(100); // TODO consider refactoring the wait logic and moving externally } catch (InterruptedException ex) @@ -179,14 +180,14 @@ public class ChannelOpenDis7 { } if (!newCommentsList.isEmpty()) { - if (disNetworkInterface == null) + if (getDisNetworkInterface() == null) setUpNetworkInterface(); // ensure connected if (commentType == null) commentType = VariableRecordType.OTHER; // fallback value; TODO consider pushing into pduFactory // 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); + CommentPdu commentPdu = getPduFactory().makeCommentPdu(commentType, newCommentsList.toArray(new String[0])); // comments); sendSinglePdu(commentPdu); if (isVerboseComments()) { @@ -216,14 +217,14 @@ public class ChannelOpenDis7 { /** * @return the TRACE_PREFIX */ - public static String getTRACE_PREFIX() { + public String getTRACE_PREFIX() { return TRACE_PREFIX; } /** * @param aTRACE_PREFIX the TRACE_PREFIX to set */ - public static void setTRACE_PREFIX(String aTRACE_PREFIX) { + public void setTRACE_PREFIX(String aTRACE_PREFIX) { TRACE_PREFIX = aTRACE_PREFIX; } @@ -241,4 +242,41 @@ public class ChannelOpenDis7 { public void printlnTRACE(String message) { System.out.println(TRACE_PREFIX + message); } + + /** + * @return the pduFactory, simplifying program imports and configuration + */ + public PduFactory getPduFactory() { + if (pduFactory == null) + pduFactory = new PduFactory(timestampStyle); + return pduFactory; + } + + /** + * @return the disNetworkInterface + */ + public DisThreadedNetworkInterface getDisNetworkInterface() { + return disNetworkInterface; + } + + /** + * @return the thisHostName + */ + public static String getThisHostName() { + return thisHostName; + } + + /** + * @param aThisHostName the thisHostName to set + */ + public static void setThisHostName(String aThisHostName) { + thisHostName = aThisHostName; + } + + /** + * @return the pduRecorder + */ + public PduRecorder getPduRecorder() { + return pduRecorder; + } }