Skip to content
Snippets Groups Projects
Commit 5ea80305 authored by ctsfi's avatar ctsfi
Browse files

Merge origin/master

parents 11ffe8fa 9105d2ee
No related branches found
No related tags found
No related merge requests found
...@@ -218,6 +218,7 @@ public class BrittSimulation ...@@ -218,6 +218,7 @@ public class BrittSimulation
boolean simulationComplete = false; // sentinel variable as termination condition boolean simulationComplete = false; // sentinel variable as termination condition
boolean fireBool = false; boolean fireBool = false;
boolean destBool = false; boolean destBool = false;
boolean resupBool = false;
// TODO reset clock to zero each time for consistent outputs. // TODO reset clock to zero each time for consistent outputs.
// your model setup: who's who in this zoo? // your model setup: who's who in this zoo?
...@@ -234,19 +235,19 @@ public class BrittSimulation ...@@ -234,19 +235,19 @@ public class BrittSimulation
EntityID M49ID = new EntityID(); EntityID MTVRID = new EntityID();
M49ID.setSiteID(1); MTVRID.setSiteID(1);
M49ID.setApplicationID(13); MTVRID.setApplicationID(13);
M49ID.setEntityID(26); MTVRID.setEntityID(26);
entityStatePdu.setEntityID(M49ID); entityStatePdu.setEntityID(MTVRID);
EntityType M49Type = new EntityType(); //1.1.225.7.1.11 Platform,Ground,USA,M49 EntityType MTVRType = new EntityType(); //1.1.225.7.39.3 MTVR
M49Type.setEntityKind(EntityKind.PLATFORM); MTVRType.setEntityKind(EntityKind.PLATFORM);
M49Type.setDomain(Domain.inst(PlatformDomain.LAND)); MTVRType.setDomain(Domain.inst(PlatformDomain.LAND));
M49Type.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); MTVRType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA);
M49Type.setCategory(7); MTVRType.setCategory(7);
M49Type.setSubCategory(1); MTVRType.setSubCategory(39);
M49Type.setSpecific(11); MTVRType.setSpecific(3);
entityStatePdu.setEntityType(M49Type); entityStatePdu.setEntityType(MTVRType);
Vector3Double eloc2 = new Vector3Double(); Vector3Double eloc2 = new Vector3Double();
double[] loc2 = CoordinateConversions.getXYZfromLatLonDegrees(36.599831, -121.878842, 0); //sloat delmonte intersection double[] loc2 = CoordinateConversions.getXYZfromLatLonDegrees(36.599831, -121.878842, 0); //sloat delmonte intersection
eloc2.setX(loc2[0]); eloc2.setX(loc2[0]);
...@@ -262,21 +263,20 @@ public class BrittSimulation ...@@ -262,21 +263,20 @@ public class BrittSimulation
EntityStatePdu entityStatePdu2 = pduFactory.makeEntityStatePdu(); EntityStatePdu entityStatePdu2 = pduFactory.makeEntityStatePdu();
EntityID lavID = new EntityID();
EntityID NPSID = new EntityID(); lavID.setSiteID(1);
NPSID.setSiteID(1); lavID.setApplicationID(13);
NPSID.setApplicationID(13); lavID.setEntityID(25);
NPSID.setEntityID(27); entityStatePdu2.setEntityID(lavID);
entityStatePdu2.setEntityID(NPSID); EntityType lavType = new EntityType(); //1.1.225.2.41.3 Platform,Ground,USA,ArmoredFightingVehicle,LAV,LAV25A2
EntityType NPSType = new EntityType(); //5.1.0.14.1.3 NPS Building not sure if this is the right info but its the enumeration of a building in the siso doc lavType.setEntityKind(EntityKind.PLATFORM);
NPSType.setEntityKind(EntityKind.CULTURAL_FEATURE); lavType.setDomain(Domain.inst(PlatformDomain.LAND));
NPSType.setDomain(Domain.inst(PlatformDomain.LAND)); lavType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA);
NPSType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); lavType.setCategory(2);
NPSType.setCategory(14); lavType.setSubCategory(41);
NPSType.setSubCategory(1); lavType.setSpecific(3);
NPSType.setSpecific(3); entityStatePdu2.setEntityType(lavType);
entityStatePdu2.setEntityType(NPSType);
Vector3Double eloc1 = new Vector3Double(); Vector3Double eloc1 = new Vector3Double();
double[] loc1 = CoordinateConversions.getXYZfromLatLonDegrees(36.594116, -121.877463, 0); //NPS Main Gate double[] loc1 = CoordinateConversions.getXYZfromLatLonDegrees(36.594116, -121.877463, 0); //NPS Main Gate
eloc1.setX(loc1[0]); eloc1.setX(loc1[0]);
...@@ -327,10 +327,21 @@ public class BrittSimulation ...@@ -327,10 +327,21 @@ public class BrittSimulation
HEID.setEntityID(1); HEID.setEntityID(1);
firePdu.setMunitionExpendibleID(HEID); firePdu.setMunitionExpendibleID(HEID);
ResupplyOfferPdu resupplyOfferPdu = pduFactory.makeResupplyOfferPdu();
ArrayList<SupplyQuantity> pSupplies = new ArrayList<SupplyQuantity>();
SupplyQuantity ammoSupplyQ = new SupplyQuantity();
ammoSupplyQ.setSupplyType(HEType);
ammoSupplyQ.setQuantity(500);
pSupplies.add(ammoSupplyQ);
resupplyOfferPdu.setSupplies(pSupplies);
resupplyOfferPdu.setReceivingEntityID(lavID);
resupplyOfferPdu.setSupplyingEntityID(MTVRID);
CommentReliablePdu bmpDestroyedComment = pduFactory.makeCommentReliablePdu("BMP2 DESTROYED BY LAV25-A2 AFTER TWO BURSTS OF 25mm HEI-T ON TARGET"); CommentReliablePdu bmpDestroyedComment = pduFactory.makeCommentReliablePdu("BMP2 DESTROYED BY LAV25-A2 AFTER TWO BURSTS OF 25mm HEI-T ON TARGET");
CommentReliablePdu bmpSightedComment = pduFactory.makeCommentReliablePdu("LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE"); CommentReliablePdu bmpSightedComment = pduFactory.makeCommentReliablePdu("LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE");
//if(eloc1.getX()) CommentReliablePdu lavSightedComment = pduFactory.makeCommentReliablePdu("LAV25-A2 IS WITHIN DISTANCE OF RESUPPLY");
//if(eloc1.getX())
// should we customize this munition? what is it for your simulation? // should we customize this munition? what is it for your simulation?
...@@ -358,13 +369,16 @@ public class BrittSimulation ...@@ -358,13 +369,16 @@ public class BrittSimulation
// etc. etc. your code goes here // etc. etc. your code goes here
if(range < 100){ if(range < 100){
if (!fireBool) if (!resupBool)
sendPDU(bmpSightedComment); sendPDU(lavSightedComment);
fireBool = true; resupBool = true;
System.out.println("Entity#" + firePdu.getFiringEntityID().getEntityID() + " is firing " + firePdu.getDescriptor().getMunitionType().getDomain() + "."+firePdu.getDescriptor().getMunitionType().getCountry() + "." + firePdu.getDescriptor().getMunitionType().getCategory() + "."+ firePdu.getDescriptor().getMunitionType().getSubCategory() + "." + firePdu.getDescriptor().getMunitionType().getSpecific() + "."+ " at Entity#"+ firePdu.getTargetEntityID().getEntityID()); System.out.println("Entity#" + entityStatePdu.getEntityID().getEntityID() + " is requesting to resupply " + " Entity#"+ entityStatePdu2.getEntityID().getEntityID());
if(firePdu.getTargetEntityID().getEntityID() == 2){
if(firePdu.getTargetEntityID().getEntityID() == 2){
BMPHitsReceived += 1; BMPHitsReceived += 1;
if (BMPHitsReceived > 1) { if (BMPHitsReceived > 1) {
//DESTROY THE BMP! //DESTROY THE BMP!
...@@ -403,8 +417,8 @@ public class BrittSimulation ...@@ -403,8 +417,8 @@ public class BrittSimulation
//sendAllPdus(entityStatePdu2, null, null, narrativeMessage1, narrativeMessage2, narrativeMessage4); //sendAllPdus(entityStatePdu2, null, null, narrativeMessage1, narrativeMessage2, narrativeMessage4);
sendPDU(entityStatePdu); sendPDU(entityStatePdu);
sendPDU(entityStatePdu2); sendPDU(entityStatePdu2);
if (fireBool) if (resupBool)
sendPDU(firePdu); sendPDU(resupplyOfferPdu);
if (destBool) if (destBool)
sendPDU(bmpDestroyedComment); sendPDU(bmpDestroyedComment);
...@@ -414,7 +428,7 @@ public class BrittSimulation ...@@ -414,7 +428,7 @@ public class BrittSimulation
// loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue
if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good
{ {
CommentReliablePdu completionPdu = pduFactory.makeCommentReliablePdu("White Simulation Completed"); CommentReliablePdu completionPdu = pduFactory.makeCommentReliablePdu("Britt Simulation Completed");
sendPDU(completionPdu); sendPDU(completionPdu);
System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete);
break; break;
......
/**
* 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 MV3500Cohort2020JulySeptember.homework4.White;
import MV3500Cohort2020JulySeptember.homework4.White.working.*;
import MV3500Cohort2020JulySeptember.homework4.White.*;
import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.utilities.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WhiteSimulation
{
// class variables
PduFactory pduFactory = new PduFactory();
DisThreadedNetworkInterface disNetworkInterface;
DisThreadedNetworkInterface.PduListener pduListener;
Pdu receivedPdu;
private String networkAddress = "10.1.105.7";
private int networkPort = 2317;
/**
* Constructor design goal: additional built-in initialization conveniences can go here
* to keep student efforts focused on the runSimulation() method.
*/
public WhiteSimulation()
{
// Under consideration. Constructor is not currently needed.
}
/**
* Utility Constructor
* @param address network address to use
* @param port corresponding network port to use
*/
public WhiteSimulation(String address, int port)
{
setNetworkAddress(address);
setNetworkPort(port);
}
/**
* @return the networkAddress
*/
public String getNetworkAddress()
{
return networkAddress;
}
/**
* @param networkAddress the networkAddress to set
*/
public final void setNetworkAddress(String networkAddress)
{
this.networkAddress = networkAddress;
}
/**
* @return the networkPort
*/
public int getNetworkPort()
{
return networkPort;
}
/**
* @param networkPort the networkPort to set
*/
public final void setNetworkPort(int networkPort)
{
this.networkPort = networkPort;
}
/**
* Initialize network interface, choosing best available network interface
*/
public void setUpNetworkInterface()
{
disNetworkInterface = new DisThreadedNetworkInterface(getNetworkAddress(), getNetworkPort());
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 single Protocol Data Unit (PDU) of any type
* @param pdu the pdu to send
*/
private 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 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
* @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 sendAllPdus(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))
{
ArrayList<String> newCommentsList = new ArrayList<>();
for (int i = 0; i < comments.length; i++)
{
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);
sendPDU(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)
{
WhiteSimulation thisProgram = new WhiteSimulation(); // creates instance
// initial execution: can handle args array of initialization arguments here
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)
{
System.err.println("Usage: " + thisProgram.getClass().getName() + " [address port]");
System.exit(-1);
}
// OK here we go...
//thisProgram.setUpNetworkInterface();
thisProgram.runSimulation (); // customization code goes in there
//thisProgram.tearDownNetworkInterface();
}
/**
* Programmer-modifiable method for defining and running a new simulation of interest.
* Support include DIS EntityStatePdu, FirePdu and CommentPdu all available for
* modification and sending in a simulation loop.
*/
@SuppressWarnings("SleepWhileInLoop")
public void runSimulation ()
{
try
{
final double LOOP_DURATION_SECONDS = 1.0; // seconds
final int MAX_LOOP_COUNT = 10;
int loopCount = 0;
VariableRecordType narrativeType = VariableRecordType.OTHER; // of potential use
boolean simulationComplete = false; // sentinel variable as termination condition
boolean fireBool = false;
boolean destBool = false;
// TODO reset clock to zero each time for consistent outputs.
// your model setup: who's who in this zoo?
// create PDU objects and set their values
Vector3Double eloc3 = new Vector3Double();
double[] loc3 = CoordinateConversions.getXYZfromLatLonDegrees(36.600757, -121.869309, 0 );//NPS Corner by lake
EntityID entityID_1 = new EntityID();
entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID
EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu();
entityStatePdu.setEntityID(entityID_1);
EntityID lavID = new EntityID();
lavID.setSiteID(1);
lavID.setApplicationID(13);
lavID.setEntityID(25);
entityStatePdu.setEntityID(lavID);
EntityType lavType = new EntityType(); //1.1.225.2.41.3 Platform,Ground,USA,ArmoredFightingVehicle,LAV,LAV25A2
lavType.setEntityKind(EntityKind.PLATFORM);
lavType.setDomain(Domain.inst(PlatformDomain.LAND));
lavType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA);
lavType.setCategory(2);
lavType.setSubCategory(41);
lavType.setSpecific(3);
entityStatePdu.setEntityType(lavType);
Vector3Double eloc2 = new Vector3Double();
double[] loc2 = CoordinateConversions.getXYZfromLatLonDegrees(36.599831, -121.878842, 0); //sloat delmonte intersection
eloc2.setX(loc2[0]);
eloc2.setY(loc2[1]);
eloc2.setZ(loc2[2]);
entityStatePdu.setEntityLocation(eloc2);
EulerAngles orient2 = new EulerAngles();
orient2.setPhi((float) 0.0);
orient2.setPsi((float) 0.0);
orient2.setTheta((float) 0.0);
entityStatePdu.setEntityOrientation(orient2);
EntityStatePdu entityStatePdu2 = pduFactory.makeEntityStatePdu();
EntityID bmpID = new EntityID();
bmpID.setSiteID(1);
bmpID.setApplicationID(13);
bmpID.setEntityID(2);
entityStatePdu2.setEntityID(bmpID);
EntityType bmpType = new EntityType(); //1.1.222.2.2.1 Platform,Ground,Russia,ArmoredFightingVehicle,BMP2,BMP2
bmpType.setEntityKind(EntityKind.PLATFORM);
bmpType.setDomain(Domain.inst(PlatformDomain.LAND));
bmpType.setCountry(Country.RUSSIA);
bmpType.setCategory(2);
bmpType.setSubCategory(41);
bmpType.setSpecific(3);
entityStatePdu2.setEntityType(bmpType);
Vector3Double eloc1 = new Vector3Double();
double[] loc1 = CoordinateConversions.getXYZfromLatLonDegrees(36.594116, -121.877463, 0); //NPS Main Gate
eloc1.setX(loc1[0]);
eloc1.setY(loc1[1]);
eloc1.setZ(loc1[2]);
entityStatePdu2.setEntityLocation(eloc1);
EulerAngles orient1 = new EulerAngles();
orient1.setPhi((float) 3.1415);
orient1.setPsi((float) 0.0);
orient1.setTheta((float) 0.0);
entityStatePdu2.setEntityOrientation(orient1);
int BMPHitsReceived = 0;
System.out.println(eloc2.toString());
System.out.println(eloc1.toString());
//FirePdu firePduNull = new FirePdu();
FirePdu firePdu = pduFactory.makeFirePdu();
EntityID fireID = new EntityID();
fireID.setSiteID(1);
fireID.setApplicationID(13);
fireID.setEntityID(25);
EntityID targetID = new EntityID();
targetID.setSiteID(1);
targetID.setApplicationID(13);
targetID.setEntityID(2);
firePdu.setFiringEntityID(fireID);
firePdu.setTargetEntityID(targetID);
EntityType HEType = new EntityType(); //2.9.225.2.2.1
HEType.setEntityKind(EntityKind.MUNITION);
HEType.setDomain(Domain.inst(PlatformDomain.AIR));
HEType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA);
HEType.setCategory(2);
HEType.setSubCategory(2);
HEType.setSpecific(1);
MunitionDescriptor HEIT = new MunitionDescriptor();
HEIT.setMunitionType(HEType);
HEIT.setQuantity(3);
HEIT.setFuse(MunitionDescriptorFuse.CONTACT_GRAZE);
HEIT.setRate(200);
firePdu.setDescriptor(HEIT);
EntityID HEID = new EntityID();
HEID.setEntityID(1);
firePdu.setMunitionExpendibleID(HEID);
CommentReliablePdu bmpDestroyedComment = pduFactory.makeCommentReliablePdu("BMP2 DESTROYED BY LAV25-A2 AFTER TWO BURSTS OF 25mm HEI-T ON TARGET");
CommentReliablePdu bmpSightedComment = pduFactory.makeCommentReliablePdu("LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE");
//if(eloc1.getX())
// should we customize this munition? what is it for your simulation?
while (loopCount < MAX_LOOP_COUNT) // loop the simulation while allowed, can set additional conditions to break
{
String narrativeMessage1, narrativeMessage2, narrativeMessage3, narrativeMessage4;
narrativeMessage4 = "";
// initialize loop variables
loopCount++;
// =============================================================================================
// your own simulation code starts here!
// compute a track, update an ESPDU, whatever it is that your model is doing...
// Where is my entity?
entityStatePdu.getEntityLocation().setX(entityStatePdu.getEntityLocation().getX() - 20); // 1m per timestep
entityStatePdu.getEntityLocation().setY(entityStatePdu.getEntityLocation().getY() - 75);
// decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoort at!
Double dx = eloc2.getX() - eloc1.getX();
Double dy = eloc2.getY() - eloc1.getY();
Double dz = eloc2.getZ() - eloc1.getZ();
Double range = Math.sqrt(dx*dx + dy*dy);
System.out.println("range" + range + " dx:" +dx + " dy:"+ dy);
// etc. etc. your code goes here
if(range < 100){
if (!fireBool)
sendPDU(bmpSightedComment);
fireBool = true;
System.out.println("Entity#" + firePdu.getFiringEntityID().getEntityID() + " is firing " + firePdu.getDescriptor().getMunitionType().getDomain() + "."+firePdu.getDescriptor().getMunitionType().getCountry() + "." + firePdu.getDescriptor().getMunitionType().getCategory() + "."+ firePdu.getDescriptor().getMunitionType().getSubCategory() + "." + firePdu.getDescriptor().getMunitionType().getSpecific() + "."+ " at Entity#"+ firePdu.getTargetEntityID().getEntityID());
if(firePdu.getTargetEntityID().getEntityID() == 2){
BMPHitsReceived += 1;
if (BMPHitsReceived > 1) {
//DESTROY THE BMP!
System.out.println("BMP Destroyed after "+ BMPHitsReceived + " hits from 25mm HEI-T");
narrativeMessage4 = "Destroyed BMP2";
destBool = true;
}
}
}
// make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending)
narrativeMessage1 = "MV3500 ExampleSimulationProgram";
narrativeMessage2 = "runSimulation() loop " + loopCount;
narrativeMessage3 = "LAV-25A2"; // intentionally blank for testing
if (narrativeMessage4.isEmpty()){
narrativeMessage4 = "BMP2";
}
// your loop termination condition goes here
if (loopCount > 4) // for example
{
simulationComplete = true;
}
// your own simulation code is finished here!
// =============================================================================================
// keep track of timestep: wait duration for elapsed time in this loop
// Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes
Thread.sleep((long)(LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds
System.out.println ("... Pausing for " + LOOP_DURATION_SECONDS + " seconds");
// send the status PDUs for this loop and continue
System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent");
//sendAllPdus(entityStatePdu, firePduNull, null, narrativeMessage1, narrativeMessage2, narrativeMessage3);
//sendAllPdus(entityStatePdu2, null, null, narrativeMessage1, narrativeMessage2, narrativeMessage4);
sendPDU(entityStatePdu);
sendPDU(entityStatePdu2);
if (fireBool)
sendPDU(firePdu);
if (destBool)
sendPDU(bmpDestroyedComment);
System.out.println ("... PDUs successfully sent");
// ===============================
// loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue
if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good
{
CommentReliablePdu completionPdu = pduFactory.makeCommentReliablePdu("White Simulation Completed");
sendPDU(completionPdu);
System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete);
break;
}
} // end of while loop
}
catch (Exception ex) // handle any exception that your code might choose to provoke!
{
Logger.getLogger(WhiteSimulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendPDU(Pdu pdu) {
System.out.println("Sending a PDU");
MulticastSocket socket = null; // must be initialized, even if null
InetAddress destinationIp = null; // must be initialized, even if null
try {
destinationIp = InetAddress.getByName(networkAddress);
} catch (UnknownHostException e) {
System.out.println(e + " Cannot create address");
System.exit(0);
}
try {
// Set up a socket to send information
socket = new MulticastSocket(2317);
} catch (IOException ex) {
Logger.getLogger(WhiteSimulation.class.getName()).log(Level.SEVERE, null, ex);
}
Set<InetAddress> broadcastAddresses;
// Loop through sending one ESPDUs
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
pdu.marshal(dos);
byte[] data = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(data, data.length, destinationIp, 2317);
socket.send(packet);
socket.close();
} catch (Exception ex) {
Logger.getLogger(WhiteSimulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
\ No newline at end of file
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