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

example simulation program for students

parent 708b7c02
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2008-2020, MOVES Institute, Naval Postgraduate School (NPS). All rights reserved.
* This work is provided under a BSD open-source license, see project license.html and license.txt
*/
package OpenDis7Examples;
import edu.nps.moves.dis7.enumerations.VariableRecordType;
import edu.nps.moves.dis7.pdus.CommentPdu;
import edu.nps.moves.dis7.pdus.EntityID;
import edu.nps.moves.dis7.pdus.EntityStatePdu;
import edu.nps.moves.dis7.pdus.FirePdu;
import edu.nps.moves.dis7.pdus.Pdu;
import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
import edu.nps.moves.dis7.utilities.PduFactory;
public class ExampleSimulation
{
// class variables
PduFactory pduFactory = new PduFactory();
DisThreadedNetworkInterface disNetworkInterface;
DisThreadedNetworkInterface.PduListener pduListener;
Pdu receivedPdu;
String networkAddress = "239.1.2.3";
int networkPort = 3000;
/**
* Initialize network interface, choosing best available network interface
*/
public void setUpNetworkInterface()
{
disNetworkInterface = new DisThreadedNetworkInterface(networkAddress, networkPort);
System.out.println("Network confirmation: address=" + disNetworkInterface.getMcastGroup() + " port=" + disNetworkInterface.getDisPort());
pduListener = new DisThreadedNetworkInterface.PduListener()
{
/** Callback handler for listener */
@Override
public void incomingPdu(Pdu newPdu)
{
receivedPdu = newPdu;
}
};
disNetworkInterface.addListener(pduListener);
}
/** All done, release network resources */
public void tearDownNetworkInterface()
{
disNetworkInterface.removeListener(pduListener);
disNetworkInterface.kill();
disNetworkInterface = null;
}
/**
* Send a PDU of any type
* @param pdu the pdu to send
*/
private void sendPdu(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);
}
}
/**
* PDU sending: Entity State, Fire, Comment
* @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 firePdu the FirePDU to send, if any
* @param commentType enumeration value describing the narrative comment
* @param comments String array of narrative comments
*/
public void createSendPdus(EntityStatePdu entityStatePdu,
FirePdu firePdu,
VariableRecordType commentType,
// vararg... variable length string
String... comments)
{
if (entityStatePdu != null)
sendPdu(entityStatePdu);
if (firePdu != null)
sendPdu(firePdu); // bang
if ((comments == null) || (comments.length > 0))
{
if (commentType == null)
commentType = VariableRecordType.OTHER;
CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, comments);
sendPdu(commentPdu);
}
}
public static void main(String[] args)
{
// initial execution: can handle args array of initialization arguments here
ExampleSimulation thisProgram = new ExampleSimulation(); // creates instance
thisProgram.setUpNetworkInterface();
thisProgram.runSimulation ();
thisProgram.tearDownNetworkInterface();
}
public void runSimulation ()
{
final int MAX_LOOP_COUNT = 10;
int loopCount = 0;
VariableRecordType narrativeType = VariableRecordType.OTHER;
boolean simulationComplete = false; // termination condition
// model setup
EntityID entityID_1 = new EntityID();
entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3);
EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu();
entityStatePdu.setEntityID(entityID_1);
FirePdu firePdu = pduFactory.makeFirePdu();
while (loopCount < MAX_LOOP_COUNT) // loop while allowed, can set additional conditions to break
{
String narrativeMessage1, narrativeMessage2, narrativeMessage3;
// initialize loop variables
loopCount++;
// your simulation code here!
// your narrative code for CommentPdu here
narrativeMessage1 = "MV3500 ExampleSimulation";
narrativeMessage2 = "runSimulation loop " + loopCount;
narrativeMessage3 = ""; // intentionally blank for testing
// your termination condition
if (loopCount > 4) // for example
{
simulationComplete = true;
System.out.println ("*** termination condition met, simulationComplete=" + simulationComplete);
}
// loop now finished so terminate if simulation complete, otherwise send latest PDUs and continue
if (simulationComplete)
break;
System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent");
createSendPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3);
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