Skip to content
Snippets Groups Projects
Commit c7892d5c authored by brutzman's avatar brutzman
Browse files

further refinements

parent 459caadc
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ import edu.nps.moves.dis7.pdus.FirePdu; ...@@ -12,6 +12,7 @@ import edu.nps.moves.dis7.pdus.FirePdu;
import edu.nps.moves.dis7.pdus.Pdu; import edu.nps.moves.dis7.pdus.Pdu;
import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
import edu.nps.moves.dis7.utilities.PduFactory; import edu.nps.moves.dis7.utilities.PduFactory;
import java.util.ArrayList;
public class ExampleSimulationProgram public class ExampleSimulationProgram
{ {
...@@ -106,10 +107,10 @@ 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 * @param pdu the pdu to send
*/ */
private void sendPdu(Pdu pdu) private void sendSinglePdu(Pdu pdu)
{ {
try try
{ {
...@@ -124,51 +125,78 @@ public class ExampleSimulationProgram ...@@ -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 * @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 entityStatePdu the ESPDU to send, if any
* @param firePdu the FirePDU to send, if any * @param firePdu the FirePDU to send, if any
* @param commentType enumeration value describing the narrative comment * @param commentType enumeration value describing the narrative comment
* @param comments String array of narrative comments * @param comments String array of narrative comments
*/ */
public void createSendPdus(EntityStatePdu entityStatePdu, public void sendAllPdus(EntityStatePdu entityStatePdu,
FirePdu firePdu, FirePdu firePdu,
VariableRecordType commentType, VariableRecordType commentType,
// vararg... variable length string // vararg... variable length string
String... comments) String... comments)
{ {
if (entityStatePdu != null) if (entityStatePdu != null)
sendPdu(entityStatePdu); sendSinglePdu(entityStatePdu);
if (firePdu != null) if (firePdu != null)
sendPdu(firePdu); // bang sendSinglePdu(firePdu); // bang
if ((comments == null) || (comments.length > 0)) if ((comments != null) && (comments.length > 0))
{ {
if (commentType == null) ArrayList<String> newCommentsList = new ArrayList<>();
commentType = VariableRecordType.OTHER; for (int i = 0; i < comments.length; i++)
CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, comments); {
sendPdu(commentPdu); 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) public static void main(String[] args)
{ {
ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance
// initial execution: can handle args array of initialization arguments here // initial execution: can handle args array of initialization arguments here
if (args.length == 2) 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]));
} }
else if (args.length != 0)
ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance {
System.err.println("Usage: " + thisProgram.getClass().getName() + " [address port]");
System.exit(-1);
}
// OK here we go...
thisProgram.setUpNetworkInterface(); thisProgram.setUpNetworkInterface();
thisProgram.runSimulation (); thisProgram.runSimulation (); // customization code goes in there
thisProgram.tearDownNetworkInterface(); thisProgram.tearDownNetworkInterface();
} }
/**
* User-modifiable method for defining and running a simulation.
*/
public void runSimulation () public void runSimulation ()
{ {
final int MAX_LOOP_COUNT = 10; final int MAX_LOOP_COUNT = 10;
...@@ -191,28 +219,29 @@ public class ExampleSimulationProgram ...@@ -191,28 +219,29 @@ public class ExampleSimulationProgram
// initialize loop variables // initialize loop variables
loopCount++; 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"; narrativeMessage1 = "MV3500 ExampleSimulation";
narrativeMessage2 = "runSimulation loop " + loopCount; narrativeMessage2 = "runSimulation loop " + loopCount;
narrativeMessage3 = ""; // intentionally blank for testing narrativeMessage3 = ""; // intentionally blank for testing
// your termination condition // ===============================
// your loop termination condition
if (loopCount > 4) // for example if (loopCount > 4) // for example
{ {
simulationComplete = true; simulationComplete = true;
System.out.println ("*** termination condition met, simulationComplete=" + simulationComplete); System.out.println ("*** termination condition met, simulationComplete=" + simulationComplete);
} }
// loop now finished so terminate if simulation complete, otherwise send latest PDUs and continue // loop now finished so terminate if simulation complete, otherwise send latest PDUs and continue
if (simulationComplete) if (simulationComplete)
break; break;
System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); 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"); System.out.println ("... PDUs successfully sent");
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment