Skip to content
Snippets Groups Projects
Commit 28efa36d authored by chris's avatar chris
Browse files

CANNON PROJECT2

parent 1b44ea97
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2020JulySeptember.homework4.Cannon;
import MV3500Cohort2020JulySeptember.homework4.White.*;
import java.net.*;
import java.io.*;
import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.utilities.PduFactory;
import java.util.ArrayList;
public class AllPduReceiver
{
public static final String DEFAULT_MULTICAST_ADDRESS = AllPduSender.DEFAULT_MULTICAST_ADDRESS;
public static final int DEFAULT_MULTICAST_PORT = AllPduSender.DEFAULT_MULTICAST_PORT;
public static final boolean USE_FAST_ESPDU = false;
public static void main(String args[])
{
PduFactory factory;
MulticastSocket socket;
InetAddress address;
DatagramPacket packet;
try {
System.out.println("OpenDis7Examples.AllPduReceiver started...");
if (args.length == 2) {
address = InetAddress.getByName(args[0]);
socket = new MulticastSocket(Integer.parseInt(args[1]));
}
else {
System.out.println("Usage: AllPduReceiver <multicast group> <port>");
System.out.println("Default: AllPduReceiver " + DEFAULT_MULTICAST_ADDRESS + " " + DEFAULT_MULTICAST_PORT);
socket = new MulticastSocket(DEFAULT_MULTICAST_PORT);
address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS);
}
socket.joinGroup(address);
factory = new PduFactory();
while (true) // Loop infinitely, receiving datagrams
{
byte buffer[] = new byte[1500]; // typical MTU size
packet = new DatagramPacket(buffer, buffer.length); // reset
socket.receive(packet);
Pdu pdu = factory.createPdu(packet.getData()); // packet.getData() returns byte[] array data buffer
if (pdu != null)
{
DISPDUType currentPduType = pdu.getPduType(); //short currentPduType = pdu.getPduType();
String currentPduTypeName = pdu.getClass().getName();
DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short currentProtocolFamilyID = pdu.getProtocolFamily();
String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName();
StringBuilder message = new StringBuilder();
message.append(DisTime.timeStampToString(pdu.getTimestamp()) + " ");
message.append("received DIS PDU ");
String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
message.append(" " ).append(currentPduTypePadded);
if (currentPduType.getValue() < 10)
message.append(" "); // column spacing
// String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
// message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
message.append(" (").append(currentProtocolFamilyID);
// message.append(" ").append(currentPduFamilyName); // class name is also available
message.append(")");
System.out.println(message.toString());
if (currentPduType == DISPDUType.FIRE){
FirePdu firePdu = new FirePdu();
firePdu = (FirePdu) pdu;
if(firePdu.getFiringEntityID().getEntityID()!= 0){
System.out.println("Entity#" + firePdu.getFiringEntityID().getEntityID() + " is firing Muition [" + firePdu.getDescriptor().getMunitionType().getDomain() + "."+firePdu.getDescriptor().getMunitionType().getCountry() + "." + firePdu.getDescriptor().getMunitionType().getCategory() + "."+ firePdu.getDescriptor().getMunitionType().getSubCategory() + "." + firePdu.getDescriptor().getMunitionType().getSpecific() + "." + firePdu.getDescriptor().getMunitionType().getExtra()+ "]"+ " at Entity#"+ firePdu.getTargetEntityID().getEntityID());
}
}
switch (currentPduType) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType
{
case COMMENT:
CommentPdu commentPdu = (CommentPdu)pdu; // cast to precise type
ArrayList<VariableDatum> payloadList = (ArrayList)commentPdu.getVariableDatums();
if (!payloadList.isEmpty())
System.out.print (" messages: ");
for (VariableDatum variableDatum : payloadList)
{
String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String
System.out.print (" \"" + nextComment + "\"");
}
System.out.println();
}
}
else
System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error...");
}
}
catch (IOException e) {
System.out.println("Problem with OpenDis7Examples.AllPduReceiver, see exception trace:");
System.out.println(e);
}
finally {
System.out.println("OpenDis7Examples.AllPduReceiver complete.");
}
}
}
......@@ -8,6 +8,13 @@ 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.HashSet;
import java.util.Set;
......@@ -22,8 +29,8 @@ public class ExampleArtillerySimulation {
DisThreadedNetworkInterface.PduListener pduListener;
Pdu receivedPdu;
private String networkAddress = "239.1.2.3";
private int networkPort = 3000;
private String networkAddress = "localhost";//"10.1.105.8";
private int networkPort = 2317;
/**
* Constructor design goal: additional built-in initialization conveniences
......@@ -185,11 +192,10 @@ public class ExampleArtillerySimulation {
}
// OK here we go...
thisProgram.setUpNetworkInterface();
//thisProgram.setUpNetworkInterface();
thisProgram.runSimulation(); // customization code goes in there
thisProgram.tearDownNetworkInterface();
//thisProgram.tearDownNetworkInterface();
}
/**
......@@ -205,6 +211,8 @@ public class ExampleArtillerySimulation {
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?
......@@ -278,7 +286,7 @@ public class ExampleArtillerySimulation {
System.out.println("BMP Location");
System.out.println(eloc1.toString());
FirePdu firePduNull = new FirePdu();
//FirePdu firePduNull = new FirePdu();
FirePdu firePdu = pduFactory.makeFirePdu();
EntityID fireID = new EntityID();
fireID.setSiteID(1);
......@@ -314,6 +322,8 @@ public class ExampleArtillerySimulation {
Double dy = eloc2.getY() - eloc1.getY();
Double range = Math.sqrt(dx * dx + dy * dy);
System.out.println("THE M109A6 PALADIN WILL FIRE 6 VOLLEYS OF HE/PD USING CHARGE 1L FOR DISTANCE OF " + range + " METERS AT HIGH ANGLE");
CommentReliablePdu bmpDestroyedComment = pduFactory.makeCommentReliablePdu("BMP2 DESTROYED BY M109A2 AFTER SIX VOLLEYS OF HE/PD ON TARGET");
while (loopCount < MAX_LOOP_COUNT) // loop the simulation while allowed, can set additional conditions to break
{
String narrativeMessage1, narrativeMessage2, narrativeMessage3, narrativeMessage4;
......@@ -325,19 +335,20 @@ public class ExampleArtillerySimulation {
// your own simulation code starts here!
// compute a track, update an ESPDU, whatever it is that your model is doing...
// Where is my entity?
firePduNull = firePdu;
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() + "." + firePdu.getDescriptor().getMunitionType().getExtra() + "."+ "at Entity#" + firePdu.getTargetEntityID().getEntityID());
if (firePduNull.getTargetEntityID().getEntityID() == 2) {
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() + "." + firePdu.getDescriptor().getMunitionType().getExtra() + "." + "at Entity#" + firePdu.getTargetEntityID().getEntityID());
fireBool = true;
if (firePdu.getTargetEntityID().getEntityID() == 2) {
BMPHitsReceived += 1;
if (BMPHitsReceived > 5) {
//DESTROY THE BMP!
System.out.println("BMP Destroyed after " + BMPHitsReceived + " hits from 155mm Indirect Fire");
narrativeMessage4 = "Destroyed BMP2";
destBool = true;
}
}
......@@ -363,14 +374,24 @@ public class ExampleArtillerySimulation {
// 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);
//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("Cannon Simulation Completed");
sendPDU(completionPdu);
System.out.println("... Termination condition met, simulationComplete=" + simulationComplete);
break;
}
......@@ -379,5 +400,41 @@ public class ExampleArtillerySimulation {
{
Logger.getLogger(ExampleArtillerySimulation.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(MV3500Cohort2020JulySeptember.homework4.White.working.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(MV3500Cohort2020JulySeptember.homework4.White.working.WhiteSimulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package MV3500Cohort2020JulySeptember.homework4.Cannon;
import MV3500Cohort2020JulySeptember.homework4.White.working.*;
import java.io.*;
import java.net.*;
import java.util.*;
import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.utilities.*;
/**
* Receives PDUs from GermanyEspduReceiverEspduVPNSender in IEEE DIS format.
*
* @date 09/05/2020
* @author Bernd/Stefan
* @version 0.1
*/
public class PDUReciever {
/**
* Max size of a PDU in binary format that we can receive. This is actually
* somewhat outdated--PDUs can be larger--but this is a reasonable starting
* point.
*/
public static final int MAX_PDU_SIZE = 8192;
/**
* Default port used, matches Wireshark DIS capture default
*/
public static final int DEFAULT_PORT = 2317;
public static final int SECOND_PORT = 3000;
public static final int THIRD_PORT = 2318;
/**
* Output prefix to identify this class
*/
private final static String TRACE_PREFIX = "[" + PDUReciever.class.getName() + "] ";
public static void main(String args[]) {
System.out.println(TRACE_PREFIX + "started...");
MulticastSocket socket1;
MulticastSocket socket2;
MulticastSocket socket3;
DatagramPacket packet;
DatagramPacket packet2;
DatagramPacket packet3;
PduFactory pduFactory = new PduFactory();
ArrayList<EntityID> knownEntities = new ArrayList<EntityID>();
int pduCount = 0;
try {
// Specify the socket to receive data
socket1 = new MulticastSocket(DEFAULT_PORT);
socket2 = new MulticastSocket(SECOND_PORT);
socket3 = new MulticastSocket(THIRD_PORT);
System.out.println(TRACE_PREFIX + "listening for PDU packets on port " + DEFAULT_PORT );//+ " " + SECOND_PORT + " " + THIRD_PORT);
System.out.println("====================================================");
while (true) // Loop infinitely, receiving datagrams
{
byte buffer[] = new byte[MAX_PDU_SIZE];
packet = new DatagramPacket(buffer, buffer.length);
socket1.receive(packet);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(), packet.getLength());
if (pduBundle.size() > 1) { // should be 1 for this project
System.out.println("Bundle size is " + pduBundle.size());
}
// end iterator loop through PDU bundle
for (Pdu aPdu : pduBundle) {
pduCount++;
String receiptMessage = String.format("%3s", pduCount) // right justify, 3 characters
+ ". received PDU type " + aPdu.getPduType().getValue() + "=" + aPdu.getPduType().name() + " " + aPdu.getClass().getName() + " from " + packet.getAddress();
if (aPdu instanceof EntityStatePdu) {
System.out.println(receiptMessage);
EntityID entityID = ((EntityStatePdu) aPdu).getEntityID();
Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation();
System.out.println(" entityID triplet: [" + entityID.getSiteID() + ", " + entityID.getApplicationID() + ", " + entityID.getEntityID() + "] ");
if (!knownEntities.contains(entityID)){
knownEntities.add(entityID);
EntityType entityType = ((EntityStatePdu) aPdu).getEntityType();
System.out.println(" New Entity: " +entityType.getEntityKind() + " "+ entityType.getDomain() + " "+ entityType.getCountry() + " "+ entityType.getCategory() + " "+ entityType.getSubCategory() + " "+ entityType.getSpecific() );
}
System.out.println(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
}
else if (aPdu instanceof FirePdu){
System.out.println(receiptMessage);
EntityID firingEntityID = ((FirePdu) aPdu).getFiringEntityID();
EntityID targetEntityID = ((FirePdu) aPdu).getTargetEntityID();
MunitionDescriptor munitionDescriptor = ((FirePdu) aPdu).getDescriptor();
System.out.println(" firingEntityID triplet: [" + firingEntityID.getSiteID() + ", " + firingEntityID.getApplicationID() + ", " + firingEntityID.getEntityID() + "] ");
System.out.println(" targetEntityID triplet: [" + targetEntityID.getSiteID() + ", " + targetEntityID.getApplicationID() + ", " + targetEntityID.getEntityID() + "] ");
System.out.println(" Munition Information: [" + munitionDescriptor.getMunitionType().getDomain() + "."+munitionDescriptor.getMunitionType().getCountry() + "." + munitionDescriptor.getMunitionType().getCategory() + "."+ munitionDescriptor.getMunitionType().getSubCategory() + "." + munitionDescriptor.getMunitionType().getSpecific() + "." + munitionDescriptor.getMunitionType().getExtra() + "]");
}
else if (aPdu instanceof CommentReliablePdu){
System.out.println(receiptMessage);
ArrayList<VariableDatum> payloadList = (ArrayList)((CommentReliablePdu) aPdu).getVariableDatumRecords();
if (!payloadList.isEmpty())
System.out.print (" messages: ");
for (VariableDatum variableDatum : payloadList)
{
String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String
System.out.print (" \"" + nextComment + "\"");
System.out.println();
}
} //OTHER PDU TYPES
else {
System.out.println(receiptMessage);
}
} // end of bundle loop
} // end of while loop
} // end try block // end try block // end try block // end try block
catch (IOException ioe) {
System.out.println(TRACE_PREFIX + "Problem with input/output, see exception trace:");
System.out.println(ioe);
}
System.out.println(TRACE_PREFIX + "complete.");
} // end main
} // end class
[MV3500Cohort2020JulySeptember.homework4.Cannon.PDUReciever] started...
[MV3500Cohort2020JulySeptember.homework4.Cannon.PDUReciever] listening for PDU packets on port 2317
====================================================
1. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
New Entity: EntityKind 1 PLATFORM Land Country 225 UNITED_STATES_OF_AMERICA_USA 4 3 7
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
2. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
New Entity: EntityKind 1 PLATFORM Land Country 260 RUSSIA 2 41 3
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
3. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
4. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
5. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
6. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
7. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
8. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
9. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
10. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
11. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
12. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
13. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
14. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
15. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
16. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 25]
Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564]
17. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /127.0.0.1
entityID triplet: [1, 13, 2]
Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432]
18. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /127.0.0.1
firingEntityID triplet: [1, 13, 25]
targetEntityID triplet: [1, 13, 2]
Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1.2]
19. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /127.0.0.1
messages: "BMP2 DESTROYED BY M109A2 AFTER SIX VOLLEYS OF HE/PD ON TARGET"
20. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /127.0.0.1
messages: "Cannon Simulation Completed"
\ 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