diff --git a/assignments/nbproject/genfiles.properties b/assignments/nbproject/genfiles.properties index 81515a0c3dec1f6ab1d66241d0881f2f5426139c..1e5930a6e750414589c4a0e3c59358da0a8ace52 100644 --- a/assignments/nbproject/genfiles.properties +++ b/assignments/nbproject/genfiles.properties @@ -1,5 +1,5 @@ -# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. -# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=5eaf4250 -nbproject/build-impl.xml.script.CRC32=013963da -nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=ad8aeeb2 +nbproject/build-impl.xml.script.CRC32=bd12e823 +nbproject/build-impl.xml.stylesheet.CRC32=f89f7d21@1.95.0.48 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/BrittSimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/BrittSimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..d5a2909abd3d68c07c88c2745ffd765d5cb40152 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/BrittSimulation.java @@ -0,0 +1,477 @@ +/** + * 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.Britt; + +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 BrittSimulation +{ + // 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 BrittSimulation() + { + // Under consideration. Constructor is not currently needed. + } + + /** + * Utility Constructor + * @param address network address to use + * @param port corresponding network port to use + */ + public BrittSimulation(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) + { + BrittSimulation thisProgram = new BrittSimulation(); // 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; + boolean resupBool = 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 MTVRID = new EntityID(); + MTVRID.setSiteID(1); + MTVRID.setApplicationID(13); + MTVRID.setEntityID(26); + entityStatePdu.setEntityID(MTVRID); + EntityType MTVRType = new EntityType(); //1.1.225.7.39.3 MTVR + MTVRType.setEntityKind(EntityKind.PLATFORM); + MTVRType.setDomain(Domain.inst(PlatformDomain.LAND)); + MTVRType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); + MTVRType.setCategory(7); + MTVRType.setSubCategory(39); + MTVRType.setSpecific(3); + entityStatePdu.setEntityType(MTVRType); + 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 lavID = new EntityID(); + lavID.setSiteID(1); + lavID.setApplicationID(13); + lavID.setEntityID(25); + entityStatePdu2.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); + entityStatePdu2.setEntityType(lavType); + 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) 0.0); + 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(26); + EntityID targetID = new EntityID(); + targetID.setSiteID(1); + targetID.setApplicationID(13); + targetID.setEntityID(27); + + 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); + + 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 bmpSightedComment = pduFactory.makeCommentReliablePdu("LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE"); + 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? + + 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 (!resupBool) + sendPDU(lavSightedComment); + resupBool = true; + System.out.println("Entity#" + entityStatePdu.getEntityID().getEntityID() + " is requesting to resupply " + " Entity#"+ entityStatePdu2.getEntityID().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 (resupBool) + sendPDU(resupplyOfferPdu); + 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("Britt 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(BrittSimulation.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(BrittSimulation.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(BrittSimulation.class.getName()).log(Level.SEVERE, null, ex); + } +} +} \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/EspduSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/EspduSender.java new file mode 100644 index 0000000000000000000000000000000000000000..7a4225844410726cd3fe39440e50c02bc31ccf98 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/EspduSender.java @@ -0,0 +1,413 @@ +package OpenDis7Examples; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis7.pdus.*; +import edu.nps.moves.dis7.enumerations.Country; +import edu.nps.moves.dis7.enumerations.EntityKind; +import edu.nps.moves.dis7.enumerations.PlatformDomain; +import edu.nps.moves.dis7.utilities.*; +import edu.nps.moves.dis7.entities.usa.platform.land.M1A2; +import edu.nps.moves.dis7.entities.usa.platform.land.MTVRMK25Cargo; + +/** + * Creates and sends ESPDUs in IEEE binary format. Adapted from OpenDIS library + * example package edu.nps.moves.examples + * + * @author Don McGregor + * @author Don Brutzman + */ +public class EspduSender +{ + /** + * Putting any upper limit on # packets sent avoids possibility of non-terminating infinite loops that continue sending packets. + */ + public static final int NUMBER_OF_LOOPS = 2; // 5 + + /** + * Default multicast group address we send on. + */ + public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; + + /** + * Default multicast port used, matches Wireshark DIS capture default + */ + public static final int DEFAULT_MULTICAST_PORT = 3000; + + public enum NetworkMode { + UNICAST, MULTICAST, BROADCAST + }; + + /** + * Output prefix to identify this class, helps with logging + */ + private final static String TRACE_PREFIX = "[" + EspduSender.class.getName() + "] "; + + /** + * Possible system properties, passed in via -Dattr=val networkMode: + * unicast, broadcast, multicast destinationIp: where to send the packet. If + * in multicast mode, this can be multicast. To determine broadcast + * destination IP, use an online broadcast address calculator, for example + * http://www.remotemonitoringsystems.ca/broadcast.php If in multicast mode, + * a join() will be done on the multicast address. port: port used for both + * source and destination. + * + * @param args + */ + @SuppressWarnings("SleepWhileInLoop") // allows Thread.sleep(value) without warning in code + public static void main(String args[]) + { + System.out.println(TRACE_PREFIX + " started..."); + + // Default settings. These are used if no system properties are set. + // If system properties are passed in, these are overridden later. + NetworkMode networkMode = NetworkMode.MULTICAST; + InetAddress address = null; // must be initialized, even if null + int port = DEFAULT_MULTICAST_PORT; + MulticastSocket socket = null; // must be initialized to avoid later error, even if null; + EntityStatePdu espdu = new EntityStatePdu(); + DisTime disTime = new DisTime(); + + // ICBM coordinates for my office + double latitude = 36.639222; + double longitude = -121.803415; + try + { + address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); + } + catch (UnknownHostException e) + { + System.out.println(TRACE_PREFIX + e + " Cannot create multicast address"); + System.exit(0); + } + + // All system properties, passed in on the command line via -Dattribute=value + Properties systemProperties = System.getProperties(); + + // IP address we send to + String destinationIpString = systemProperties.getProperty("destinationIp"); + + // Port we send to, and local port we open the socket on + String portString = systemProperties.getProperty("port"); + + // Network mode: unicast, multicast, broadcast + String networkModeString = systemProperties.getProperty("networkMode"); // unicast or multicast or broadcast + + // Set up socket to send information + try + { + if (portString != null) // Update port we send to, if provided + { + port = Integer.parseInt(portString); + } + socket = new MulticastSocket(port); + + // Where we send packets to, the destination IP address + if (destinationIpString != null) + { + address = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + if (networkModeString != null) + { + if (networkModeString.equalsIgnoreCase("unicast")) + { + networkMode = NetworkMode.UNICAST; + } + else if (networkModeString.equalsIgnoreCase("broadcast")) + { + networkMode = NetworkMode.BROADCAST; + } + else if (networkModeString.equalsIgnoreCase("multicast")) + { + networkMode = NetworkMode.MULTICAST; + if (!address.isMulticastAddress()) + { + throw new RuntimeException("*** Error: sending to multicast address, but destination address " + address.toString() + "is not multicast"); + } + socket.joinGroup(address); // TODO select correct NetworkInterface + } + } // end networkModeString + else if (networkMode == NetworkMode.MULTICAST) + { + networkModeString = "multicast"; + } + else if (networkMode == NetworkMode.UNICAST) + { + networkModeString = "unicast"; + } + else if (networkMode == NetworkMode.BROADCAST) + { + networkModeString = "broadcast"; + } + } + catch (IOException | RuntimeException e) + { + System.out.println(TRACE_PREFIX + "Unable to initialize network correctly, exiting."); + System.out.println(e); + System.exit(-1); // outta here + } + System.out.println(TRACE_PREFIX + " sending " + networkModeString + " ESPDU packets to " + + address.getHostAddress() + " port " + port); + + // Initialize values in the Entity State PDU object. The exercise ID is + // a way to differentiate between different virtual worlds on one network. + // Note that some values (such as the PDU type and PDU family) are set + // automatically when you create the ESPDU. + espdu.setExerciseID((byte)1); //(short) 1); + + // The EID is the unique identifier for objects in the world. This + // EID should match up with the ID for the object specified in the + // VMRL/x3d/virtual world. + + EntityID entityID = espdu.getEntityID(); // initialize, reset, override + // TODO check: 0 is apparently not a valid site number, per DIS specification + entityID.setSiteID ((short)1); // TODO utility method to allow int values + entityID.setApplicationID((short)2); + entityID.setEntityID ((short)3); + espdu.setEntityID(entityID); // TODO utility method to allow setting all three at once + + // Set the entity type. SISO has a big list of enumerations, so that by + // specifying various numbers we can say this is an M1A2 American tank, + // the USS Enterprise, and so on. We'll make this a tank. There is a + // separate project elsehwhere in this project that implements DIS + // enumerations in C++ and Java, but to keep things simple we just use + // numbers here. + + // Manual way to override platform information: + EntityType entityType = espdu.getEntityType() + .setEntityKind (EntityKind.PLATFORM).setEntityKind (EntityKind.PLATFORM) //(short) 1); // Platform (vs lifeform, munition, sensor, etc.); //(short) 1); // Platform (vs lifeform, munition, sensor, etc.) + .setCountry (Country.UNITED_STATES_OF_AMERICA_USA) // 225 USA + .setDomain (Domain.inst(PlatformDomain.LAND)) // Land (vs air, surface, subsurface, space) + .setCategory ((byte) 7) // Large vic + .setSubCategory((byte) 39) // MTVR + .setSpecific ((byte) 2); // MTVR + + // New way using entity jar(s) + espdu.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.land.MTVRMK25Cargo()); + // or simply use an enumeration by name, with accompanying import statement above + espdu.setEntityType(new MTVRMK25Cargo()); + + // Inspecting an enumeration + System.out.println("==============="); + System.out.println("espdu entityType information:"); + System.out.println(" EntityKind =" + espdu.getEntityType().getEntityKind()); + System.out.println(" Country =" + espdu.getEntityType().getCountry()); + System.out.println(" Domain =" + espdu.getEntityType().getDomain()); + System.out.println(" Category =" + espdu.getEntityType().getCategory()); + System.out.println(" SubCategory=" + espdu.getEntityType().getSubCategory()); + System.out.println(" Specific =" + espdu.getEntityType().getCountry()); + System.out.println("I am an MTVRMK25 Cargo Truck"); + // TODO round trip lookup + + Set<InetAddress> localNetworkAddresses; + + try // Loop through sending N ESPDUs + { + System.out.println(TRACE_PREFIX + "sending " + NUMBER_OF_LOOPS + " sets of packets:"); // + address.toString() + + for (int index = 0; index < NUMBER_OF_LOOPS; index++) + { + // DIS time is a pain in the uh, neck. DIS time units are 2^31-1 units per + // hour, and time is set to DIS time units from the top of the hour. + // This means that if you start sending just before the top of the hour + // the time units can roll over to zero as you are sending. The receivers + // (escpecially homegrown ones) are often not able to detect rollover + // and may start discarding packets as dupes or out of order. We use + // an NPS timestamp here, hundredths of a second since the start of the + // year. The DIS standard for time is often ignored in the wild; I've seen + // people use Unix time (seconds since 1970) and more. Or you can + // just stuff idx into the timestamp field to get something that is monotonically + // increasing. + + // Note that timestamp is used to detect duplicate and out of order packets. + // That means if you DON'T change the timestamp, many implementations will simply + // discard subsequent packets that have an identical timestamp. Also, if they + // receive a PDU with an timestamp lower than the last one they received, they + // may discard it as an earlier, out-of-order PDU. So it is a good idea to + // update the timestamp on ALL packets sent. + // An alterative approach: actually follow the standard. It's a crazy concept, + // but it might just work. + int timestamp = disTime.getDisAbsoluteTimestamp(); + espdu.setTimestamp(timestamp); + + // Set the position of the entity in the world. DIS uses a cartesian + // coordinate system with the origin at the center of the earth, the x + // axis out at the equator and prime meridian, y out at the equator and + // 90 deg east, and z up and out the north pole. To place an object on + // the earth's surface you also need a model for the shape of the earth + // (it's not a sphere.) All the fancy math necessary to do this is in + // the SEDRIS SRM package. There are also some one-off formulas for + // doing conversions from, for example, lat/lon/altitude to DIS coordinates. + // Here we use those one-off formulas. + // Modify the position of the object. This will send the object a little + // due east by adding some to the longitude every iteration. Since we + // are on the Pacific coast, this sends the object east. Assume we are + // at zero altitude. In other worlds you'd use DTED to determine the + // local ground altitude at that lat/lon, or you'd just use ground clamping. + // The x and y values will change, but the z value should not. + //lon = lon + (double)((double)idx / 100000.0); + //System.out.println("lla=" + lat + "," + lon + ", 0.0"); + double direction = Math.pow(-1.0, index); + longitude = longitude + (direction * 0.00006); + + double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(latitude, longitude, 1.0); + Vector3Double location = espdu.getEntityLocation(); + location.setX(disCoordinates[0]); + location.setY(disCoordinates[1]); + location.setZ(disCoordinates[2]); + System.out.println("==============="); + System.out.println("Create new PDUs"); + System.out.println(" latitude, longitude: [" + latitude + ", " + longitude + "]"); + System.out.println(" coordinate conversion: [" + disCoordinates[0] + ", " + disCoordinates[1] + ", " + disCoordinates[2] + "]"); + + location = espdu.getEntityLocation(); + + System.out.println("Espdu #" + index + " entityID=[" + entityID.getSiteID()+ "," + entityID.getApplicationID()+ "," + entityID.getEntityID()+ "]"); + double c[] = {location.getX(), location.getY(), location.getZ()}; + double lla[] = CoordinateConversions.xyzToLatLonDegrees(c); +// System.out.println(" DIS entityLocation: [" + location.getX() + "," + location.getY() + "," + location.getZ() + "]"); + String debugString = " Location (latitude/longitude/altitude): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]"; +// System.out.println(debugString); + + // Optionally, we can do some rotation of the entity + /* + Orientation orientation = espdu.getEntityOrientation(); + float psi = orientation.getPsi(); + psi = psi + idx; + orientation.setPsi(psi); + orientation.setTheta((float)(orientation.getTheta() + idx /2.0)); + */ + // You can set other ESPDU values here, such as the velocity, acceleration, + // and so on. + // Marshal out the espdu object to a byte array, then send a datagram + // packet with that data in it. + + double[] locOr = {location.getX(),location.getY(),location.getZ(), 5,6,1, 1,2,1, 0,0,0, 0,0,0}; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + DatagramPacket packet; + + // The byte array here is the packet in DIS format. We put that into a + // datagram and send it. + espdu.marshal(dos); + byte[] espduArray = baos.toByteArray(); + + FirePdu firePdu = new FirePdu(); + firePdu.setLocationInWorldCoordinates(espdu.getEntityLocation()); + byte[] fireArray = firePdu.marshal(); + +// CommentPdu newCommentPdu = new CommentPdu(); +// ArrayList<VariableDatum> payloadList = new ArrayList<>(); +// ArrayList<String> commentsList = new ArrayList<>(); +// commentsList.add("Hello CommentPDU"); +// commentsList.add("Here is a second line of text in this comment."); +// if (!commentsList.isEmpty()) +// System.out.println("Preparing CommentPDU:"); +// +// for (String comment : commentsList) +// { +// VariableDatum newVariableDatum = new VariableDatum(); +// newVariableDatum.setVariableDatumValue (comment.getBytes()); // conversion +// newVariableDatum.setVariableDatumLengthInBytes(comment.getBytes().length); // also available in bits, see spec and javadoc +// // alternatively, you do not need to set this and the marshaller will figure it out from the byte array +// // (see javadoc for VariableDatum.setVariableDatumLength()) +// payloadList.add(newVariableDatum); +// System.out.println(" \"" + comment + "\""); +// } +// newCommentPdu.setVariableDatums(payloadList); +// byte[] commentArray = newCommentPdu.marshal(); + + localNetworkAddresses = getBroadcastAddresses(); + for (InetAddress networkAddress : localNetworkAddresses) { + if (espduArray.length > 0) + { + System.out.println(TRACE_PREFIX + "sending datagram packet [" + espdu.getPduType().toString() + "] to " + + String.format("%-15s", networkAddress.getHostAddress()) + " port " + port); + packet = new DatagramPacket(espduArray, espduArray.length, networkAddress, port); + socket.send(packet); + } + // TODO experiment with these! 8) + if (fireArray.length > 0) + { + System.out.println(TRACE_PREFIX + "sending datagram packet [" + firePdu.getPduType().toString() + " ] to " + + String.format("%-15s", networkAddress.getHostAddress()) + " port " + port); + packet = new DatagramPacket(fireArray, fireArray.length, networkAddress, port); // alternate + socket.send(packet); + } +// // TODO experiment with these! 8) +// if (newCommentPdu != null) +// { +// System.out.println(TRACE_PREFIX + "sending datagram packet [" + newCommentPdu.getPduType().toString() + " ] to " + +// String.format("%-15s", networkAddress.getHostAddress()) + " port " + port); +// packet = new DatagramPacket(commentArray, commentArray.length, networkAddress, port); // alternate +// socket.send(packet); +// } + } + // Send every 1 second within loop. Otherwise all this will be all over in a fraction of a second. + Thread.sleep(1000); // msec + } + } + catch (Exception e) + { + System.out.println(TRACE_PREFIX + "Problem with " + e + ", see exception trace:"); + System.out.println(e); + } + System.out.println("==============="); + System.out.println(TRACE_PREFIX + "complete."); + } + + /** + * A number of sites get all snippy about using 255.255.255.255 for a + * broadcast address; it trips their security software and they kick you off + * their network. (Comcast, NPS, etc.) This determines the broadcast address for + * all connected interfaces, based on the IP and subnet mask. If you have a + * dual-homed host it will return a broadcast address for both. If you have + * some VMs running on your host this will pick up the addresses for those + * as well--e.g. running VMWare on your laptop with a local IP this will also + * pick up a 192.168 address assigned to the VM by the host OS. + * + * @return set of all broadcast addresses + */ + public static Set<InetAddress> getBroadcastAddresses() + { + Set<InetAddress> broadcastAddresses = new HashSet<>(); + Enumeration interfaces; + + try { + interfaces = NetworkInterface.getNetworkInterfaces(); + + while (interfaces.hasMoreElements()) + { + NetworkInterface anInterface = (NetworkInterface) interfaces.nextElement(); + + if (anInterface.isUp()) + { + for (InterfaceAddress anAddress : anInterface.getInterfaceAddresses()) { + if ((anAddress == null || anAddress.getAddress().isLinkLocalAddress())) + { + continue; + } + + //System.out.println("Getting broadcast address for " + anAddress); + InetAddress broadcastAddress = anAddress.getBroadcast(); + if (broadcastAddress != null) + { + broadcastAddresses.add(broadcastAddress); + } + } + } + } + } + catch (SocketException e) + { + System.out.println(TRACE_PREFIX + "Problem with .getBroadcastAddresses(), see exception trace:" + e); + System.out.println(e); + } + return broadcastAddresses; + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/ExampleSimulationProgram.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/ExampleSimulationProgram.java new file mode 100644 index 0000000000000000000000000000000000000000..0b3f007706a8281e7f1e02ed0d72b463329a86bf --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/ExampleSimulationProgram.java @@ -0,0 +1,399 @@ +/** + * 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.Britt; + +import edu.nps.moves.dis7.enumerations.*; +import edu.nps.moves.dis7.pdus.*; +import edu.nps.moves.dis7.utilities.*; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ExampleSimulationProgram +{ + // class variables + PduFactory pduFactory = new PduFactory(); + DisThreadedNetworkInterface disNetworkInterface; + DisThreadedNetworkInterface.PduListener pduListener; + Pdu receivedPdu; + + private String networkAddress = "239.1.2.3"; + private int networkPort = 3000; + + /** + * Constructor design goal: additional built-in initialization conveniences can go here + * to keep student efforts focused on the runSimulation() method. + */ + public ExampleSimulationProgram() + { + // Under consideration. Constructor is not currently needed. + } + + /** + * Utility Constructor + * @param address network address to use + * @param port corresponding network port to use + */ + public ExampleSimulationProgram(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) + sendSinglePdu(entityStatePdu); + + if (firePdu != null) + sendSinglePdu(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); + 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) + { + ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // 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 + + // 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 M49ID = new EntityID(); + M49ID.setSiteID(1); + M49ID.setApplicationID(13); + M49ID.setEntityID(26); + entityStatePdu.setEntityID(M49ID); + EntityType M49Type = new EntityType(); //1.1.225.7.1.11 Platform,Ground,USA,M49 + M49Type.setEntityKind(EntityKind.PLATFORM); + M49Type.setDomain(Domain.inst(PlatformDomain.LAND)); + M49Type.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); + M49Type.setCategory(7); + M49Type.setSubCategory(1); + M49Type.setSpecific(11); + entityStatePdu.setEntityType(M49Type); + 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 NPSID = new EntityID(); + NPSID.setSiteID(1); + NPSID.setApplicationID(13); + NPSID.setEntityID(27); + entityStatePdu2.setEntityID(NPSID); + 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 + NPSType.setEntityKind(EntityKind.CULTURAL_FEATURE); + NPSType.setDomain(Domain.inst(PlatformDomain.LAND)); + NPSType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); + NPSType.setCategory(14); + NPSType.setSubCategory(1); + NPSType.setSpecific(3); + entityStatePdu2.setEntityType(NPSType); + 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) 0.0); + 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(26); + EntityID targetID = new EntityID(); + targetID.setSiteID(1); + targetID.setApplicationID(13); + targetID.setEntityID(27); + + 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); + + //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() - 10); // 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 + " " +dx + " "+ dy); + // etc. etc. your code goes here + + if(range < 100){ + 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() + "."+ " at Entity#"+ firePdu.getTargetEntityID().getEntityID()); + } + + if(firePduNull.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"; + } + } + + + // 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); + 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 + { + 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(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/PDUReciever.java new file mode 100644 index 0000000000000000000000000000000000000000..5ab4c55cfd26e8629ba58a195b819a1c0d3c3fbe --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Britt/PDUReciever.java @@ -0,0 +1,128 @@ +package MV3500Cohort2020JulySeptember.homework4.White; + +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() + "]"); + } + 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonArtillerySimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonArtillerySimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..cc43ae2fd99bc4f5bf3b6800a75011cefb7fbbcc --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonArtillerySimulation.java @@ -0,0 +1,440 @@ +/** + * 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.Cannon; + +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; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class CannonArtillerySimulation { + + // class variables + PduFactory pduFactory = new PduFactory(); + DisThreadedNetworkInterface disNetworkInterface; + DisThreadedNetworkInterface.PduListener pduListener; + Pdu receivedPdu; + + private String networkAddress = "localhost";//"10.1.105.8"; + 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 CannonArtillerySimulation() { + // Under consideration. Constructor is not currently needed. + } + + /** + * Utility Constructor + * + * @param address network address to use + * @param port corresponding network port to use + */ + public CannonArtillerySimulation(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) { + sendSinglePdu(entityStatePdu); + } + + if (firePdu != null) { + sendSinglePdu(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); + 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) { + CannonArtillerySimulation thisProgram = new CannonArtillerySimulation(); // 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.6327591, -121.9232494, 0);//My house will be the firing location of the paladin. 36.6327591,-121.9232494,18.71z + 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 paladinID = new EntityID(); + paladinID.setSiteID(2); + paladinID.setApplicationID(10); + paladinID.setEntityID(3); + entityStatePdu.setEntityID(paladinID); + EntityType paladinType = new EntityType(); //1.1.225.4.3.7 M109A6 Paladin (Self-proppelled howitzer) + paladinType.setEntityKind(EntityKind.PLATFORM); + paladinType.setDomain(Domain.inst(PlatformDomain.LAND)); + paladinType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); + paladinType.setCategory(4); //4 + paladinType.setSubCategory(3); //3 + paladinType.setSpecific(7); //7 + + entityStatePdu.setEntityType(paladinType); + Vector3Double eloc2 = new Vector3Double(); + double[] loc2 = CoordinateConversions.getXYZfromLatLonDegrees(36.6327591, -121.9232494, 0);//My house will be the firing location of the paladin. 36.6327591,-121.9232494,18.71z + 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) 0.0); + orient1.setPsi((float) 0.0); + orient1.setTheta((float) 0.0); + entityStatePdu2.setEntityOrientation(orient1); + + int BMPHitsReceived = 0; + System.out.println("Firing Position Location"); + System.out.println(eloc2.toString()); + System.out.println("****************"); + System.out.println("BMP Location"); + System.out.println(eloc1.toString()); + + //FirePdu firePduNull = new FirePdu(); + FirePdu firePdu = pduFactory.makeFirePdu(); + EntityID fireID = new EntityID(); + fireID.setSiteID(2); + fireID.setApplicationID(10); + fireID.setEntityID(3); + 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.14.20.2 HE 795 W/ PD FUZE + HEType.setEntityKind(EntityKind.MUNITION); + HEType.setDomain(Domain.inst(PlatformDomain.AIR)); + HEType.setCountry(Country.UNITED_STATES_OF_AMERICA_USA); + HEType.setCategory(2); //HE is 2 + HEType.setSubCategory(14); //14 + HEType.setSpecific(1); //20 + HEType.setExtra(2); //HE PD + MunitionDescriptor HEIT = new MunitionDescriptor(); + HEIT.setMunitionType(HEType); + HEIT.setQuantity(6); + HEIT.setFuse(MunitionDescriptorFuse.CONTACT_GRAZE); + HEIT.setRate(600); + firePdu.setDescriptor(HEIT); + EntityID HEID = new EntityID(); + HEID.setEntityID(1); + firePdu.setMunitionExpendibleID(HEID); + + Double dx = eloc2.getX() - eloc1.getX(); + 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 M109A6 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; + 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? + + 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; + } + } + + // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) + narrativeMessage1 = "MV3500 ExampleSimulationProgram"; + narrativeMessage2 = "runSimulation() loop " + loopCount; + narrativeMessage3 = "M109A6 Paladin"; // intentionally blank for testing + if (narrativeMessage4.isEmpty()) { + narrativeMessage4 = "BMP2"; + } + // your loop termination condition goes here + if (loopCount > 5) // 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("Cannon 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(CannonArtillerySimulation.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); + } + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonOutputLog.md b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonOutputLog.md new file mode 100644 index 0000000000000000000000000000000000000000..3645bf9e062f9f972aa1218b248ab73c40c09f28 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/CannonOutputLog.md @@ -0,0 +1,69 @@ +[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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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: [2, 10, 3] + 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 M109A6 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java new file mode 100755 index 0000000000000000000000000000000000000000..6a601f054ecbf0077a875085a05f2df6b1b654d1 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java @@ -0,0 +1,128 @@ +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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/PDUReciever.java new file mode 100755 index 0000000000000000000000000000000000000000..5ab4c55cfd26e8629ba58a195b819a1c0d3c3fbe --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/PDUReciever.java @@ -0,0 +1,128 @@ +package MV3500Cohort2020JulySeptember.homework4.White; + +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() + "]"); + } + 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/Receiver Output Log.md b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/Receiver Output Log.md new file mode 100644 index 0000000000000000000000000000000000000000..a8efe650eb11ff581caeb9df98b2d1f61b0ba690 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/Receiver Output Log.md @@ -0,0 +1,46 @@ +[MV3500Cohort2020JulySeptember.homework4.White.working.PDUReciever] started... +[MV3500Cohort2020JulySeptember.homework4.White.working.PDUReciever] listening for PDU packets on port 2317 +==================================================== + 1. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] +New Entity: EntityKind 1 PLATFORM Land Country 225 UNITED_STATES_OF_AMERICA_USA 2 41 3 + Location in DIS coordinates: [-2707501.7340396782, -4353408.810297934, 3781834.0645668795] + 2. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707521.7340396782, -4353483.810297934, 3781834.0645668795] + 3. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + 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] + 4. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707541.7340396782, -4353558.810297934, 3781834.0645668795] + 5. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 6. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE" + 7. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707561.7340396782, -4353633.810297934, 3781834.0645668795] + 8. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + 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 /10.1.105.7 + firingEntityID triplet: [1, 13, 25] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.2.1] + 10. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707581.7340396782, -4353708.810297934, 3781834.0645668795] + 11. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + 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 /10.1.105.7 + firingEntityID triplet: [1, 13, 25] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.2.1] + 13. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "BMP2 DESTROYED BY LAV25-A2 AFTER TWO BURSTS OF 25mm HEI-T ON TARGET" + 14. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "White Simulation Completed" \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..9d95854ece52932f2ad8ae6bde531820449d9ff1 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java @@ -0,0 +1,462 @@ +/** + * 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/PDUReciever.java new file mode 100755 index 0000000000000000000000000000000000000000..7e586acc7965d1b4f1fa37a0982425a8c7a6d22a --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/PDUReciever.java @@ -0,0 +1,144 @@ +package MV3500Cohort2020JulySeptember.homework4.White.test; + +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() + "]"); + } + 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(); + }// + } + else if (aPdu instanceof ResupplyOfferPdu){ + System.out.println(receiptMessage); + EntityID receiverID = ((ResupplyOfferPdu) aPdu).getReceivingEntityID(); + EntityID supplierID = ((ResupplyOfferPdu) aPdu).getSupplyingEntityID(); + List<SupplyQuantity> supplyList = ((ResupplyOfferPdu) aPdu).getSupplies(); + System.out.println(" Resupply Offer from Entity [" + supplierID.getSiteID() + ", "+ supplierID.getApplicationID() + ", " + supplierID.getEntityID() + "] to resupply Entity ["+ receiverID.getSiteID() + ", "+ receiverID.getApplicationID() + ", " + receiverID.getEntityID() + "]" ); + for (int i = 0; i < supplyList.size(); i++){ + SupplyQuantity supplyQ = supplyList.get(i); + EntityType entityType = supplyQ.getSupplyType(); + float supplyQuantity = supplyQ.getQuantity(); + System.out.println(" Supplies Offered: [Quantity: " + supplyQuantity + ", Type: " +entityType.getEntityKind() + " "+ entityType.getDomain() + " "+ entityType.getCountry() + " "+ entityType.getCategory() + " "+ entityType.getSubCategory() + " "+ entityType.getSpecific() + " ]"); + + } + } + + //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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/WhiteSimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/WhiteSimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..56ac5e1371a9142ea905d470b683d91b2b6662a0 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/test/WhiteSimulation.java @@ -0,0 +1,479 @@ +/** + * 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.test; + +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.List; +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()) + + + ResupplyOfferPdu resupplyOfferPdu = pduFactory.makeResupplyOfferPdu(); + ArrayList<SupplyQuantity> pSupplies = new ArrayList<SupplyQuantity>(); + SupplyQuantity ammoSupplyQ = new SupplyQuantity(); + ammoSupplyQ.setSupplyType(HEType); + ammoSupplyQ.setQuantity(500); + + //EntityType ammoSupply = new EntityType(); + // ammoSupply.setEntityKind(EntityKind.MUNITION); + //ammoSupply.setDomain(Entity) + //ammoSupplyQ.setSupplyType(EntityType.) + pSupplies.add(ammoSupplyQ); + resupplyOfferPdu.setSupplies(pSupplies); + resupplyOfferPdu.setReceivingEntityID(lavID); + resupplyOfferPdu.setSupplyingEntityID(bmpID); + + // 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; + + } + } + } + + + // 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); + sendPDU(resupplyOfferPdu); + } + + 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/PDUReciever.java new file mode 100755 index 0000000000000000000000000000000000000000..d33465055cf548e178cb93b1540cdfe08e8fb261 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/PDUReciever.java @@ -0,0 +1,143 @@ +package 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() + "]"); + } + 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(); + }// + } + else if (aPdu instanceof ResupplyOfferPdu){ + System.out.println(receiptMessage); + EntityID receiverID = ((ResupplyOfferPdu) aPdu).getReceivingEntityID(); + EntityID supplierID = ((ResupplyOfferPdu) aPdu).getSupplyingEntityID(); + List<SupplyQuantity> supplyList = ((ResupplyOfferPdu) aPdu).getSupplies(); + System.out.println(" Resupply Offer from Entity [" + supplierID.getSiteID() + ", "+ supplierID.getApplicationID() + ", " + supplierID.getEntityID() + "] to resupply Entity ["+ receiverID.getSiteID() + ", "+ receiverID.getApplicationID() + ", " + receiverID.getEntityID() + "]" ); + for (int i = 0; i < supplyList.size(); i++){ + SupplyQuantity supplyQ = supplyList.get(i); + EntityType entityType = supplyQ.getSupplyType(); + float supplyQuantity = supplyQ.getQuantity(); + System.out.println(" Supplies Offered: [Quantity: " + supplyQuantity + ", Type: " +entityType.getEntityKind() + " "+ entityType.getDomain() + " "+ entityType.getCountry() + " "+ entityType.getCategory() + " "+ entityType.getSubCategory() + " "+ entityType.getSpecific() + " ]"); + + } + } + + //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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/Receiver Output Log.md b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/Receiver Output Log.md new file mode 100644 index 0000000000000000000000000000000000000000..e5cdc8b7a150c0754dd02229930316eb88062760 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/Receiver Output Log.md @@ -0,0 +1,152 @@ +[MV3500Cohort2020JulySeptember.homework4.White.working.PDUReciever] started... +[MV3500Cohort2020JulySeptember.homework4.White.working.PDUReciever] listening for PDU packets on port 2317 +==================================================== + 1. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 26] + New Entity: EntityKind 1 PLATFORM Land Country 225 UNITED_STATES_OF_AMERICA_USA 7 39 3 + Location in DIS coordinates: [-2707501.7340396782, -4353408.810297934, 3781834.0645668795] + 2. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 26] + Location in DIS coordinates: [-2707521.7340396782, -4353483.810297934, 3781834.0645668795] + 3. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 25] + New Entity: EntityKind 1 PLATFORM Land Country 225 UNITED_STATES_OF_AMERICA_USA 2 41 3 + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 4. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 26] + Location in DIS coordinates: [-2707541.7340396782, -4353558.810297934, 3781834.0645668795] + 5. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 6. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.11 + messages: "LAV25-A2 IS WITHIN DISTANCE OF RESUPPLY" + 7. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 26] + Location in DIS coordinates: [-2707561.7340396782, -4353633.810297934, 3781834.0645668795] + 8. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 9. received PDU type 6=RESUPPLY_OFFER edu.nps.moves.dis7.pdus.ResupplyOfferPdu from /10.1.105.11 + Resupply Offer from Entity [1, 13, 26] to resupply Entity [1, 13, 25] + Supplies Offered: [Quantity: 500.0, Type: EntityKind 2 MUNITION Air Country 225 UNITED_STATES_OF_AMERICA_USA 2 2 1 ] + 10. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 26] + Location in DIS coordinates: [-2707581.7340396782, -4353708.810297934, 3781834.0645668795] + 11. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.11 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 12. received PDU type 6=RESUPPLY_OFFER edu.nps.moves.dis7.pdus.ResupplyOfferPdu from /10.1.105.11 + Resupply Offer from Entity [1, 13, 26] to resupply Entity [1, 13, 25] + Supplies Offered: [Quantity: 500.0, Type: EntityKind 2 MUNITION Air Country 225 UNITED_STATES_OF_AMERICA_USA 2 2 1 ] + 13. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.11 + messages: "Britt Simulation Completed" + 14. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + 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] + 15. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + 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] + 16. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 17. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564] + 18. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 19. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 20. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564] + 21. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 22. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 23. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564] + 24. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 25. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 26. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564] + 27. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 28. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 29. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [2, 10, 3] + Location in DIS coordinates: [-2709702.5301454174, -4349384.2159421, 3784766.9772485564] + 30. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.8 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 31. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.8 + firingEntityID triplet: [2, 10, 3] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.14.1] + 32. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.8 + messages: "BMP2 DESTROYED BY M109A6 AFTER SIX VOLLEYS OF HE/PD ON TARGET" + 33. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.8 + messages: "Cannon Simulation Completed" + 34. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707501.7340396782, -4353408.810297934, 3781834.0645668795] + 35. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707521.7340396782, -4353483.810297934, 3781834.0645668795] + 36. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 37. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707541.7340396782, -4353558.810297934, 3781834.0645668795] + 38. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 39. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "LAV25-A2 ACQUIRES TARGET BMP2 WITHIN FIRING DISTANCE" + 40. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707561.7340396782, -4353633.810297934, 3781834.0645668795] + 41. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 42. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.7 + firingEntityID triplet: [1, 13, 25] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.2.1] + 43. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 25] + Location in DIS coordinates: [-2707581.7340396782, -4353708.810297934, 3781834.0645668795] + 44. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu from /10.1.105.7 + entityID triplet: [1, 13, 2] + Location in DIS coordinates: [-2707576.630668249, -4353720.04383471, 3781324.902449432] + 45. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu from /10.1.105.7 + firingEntityID triplet: [1, 13, 25] + targetEntityID triplet: [1, 13, 2] + Munition Information: [Air.Country 225 UNITED_STATES_OF_AMERICA_USA.2.2.1] + 46. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "BMP2 DESTROYED BY LAV25-A2 AFTER TWO BURSTS OF 25mm HEI-T ON TARGET" + 47. received PDU type 6=RESUPPLY_OFFER edu.nps.moves.dis7.pdus.ResupplyOfferPdu from /10.1.105.7 + Resupply Offer from Entity [1, 13, 2] to resupply Entity [1, 13, 25] + Supplies Offered: [Quantity: 500.0, Type: EntityKind 2 MUNITION Air Country 225 UNITED_STATES_OF_AMERICA_USA 2 2 1 ] + 48. received PDU type 62=COMMENT_RELIABLE edu.nps.moves.dis7.pdus.CommentReliablePdu from /10.1.105.7 + messages: "White Simulation Completed" \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/WhiteSimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/WhiteSimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..b6c6768cdadb83aec9e3013d219e615d26b15590 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/WhiteSimulation.java @@ -0,0 +1,463 @@ +/** + * 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.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.List; +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; + + } + } + } + + + // 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 diff --git a/lib/README.md b/lib/README.md index cd707cfc5df225401b304009e248c96e7c74d8f7..235c7b488556a0daf333dcf0a75a0bf11f73f0ed 100644 --- a/lib/README.md +++ b/lib/README.md @@ -1,3 +1,8 @@ ## 'lib' project library directory This directory contains library jar files used by each of the course project directories. + +Of note: + +* open-dis7-enumerations jar files contain all [SISO-REF-010](../specifications/README.md) values with sources automatically created from authoritative SISO XML as part of [open-dis7-source-generator](https://github.com/open-dis/open-dis7-source-generator) project +* open-dis7-java jar files contain all [IEEE PDU packet types](../specifications/IeeeDisPduColorFigures.pdf), with sources automatically created from [XML descriptions](https://github.com/open-dis/open-dis7-source-generator/tree/master/xml) and results collected as part of [open-dis7-java](https://github.com/open-dis/open-dis7-java) project diff --git a/nbproject/nbjdk.properties b/nbproject/nbjdk.properties index 36c61d256912b0eee079250242aeec091ea750e2..3033a8abbe8c94d80e50e14c39c869aee52fbf88 100644 --- a/nbproject/nbjdk.properties +++ b/nbproject/nbjdk.properties @@ -1 +1 @@ -nbjdk.active=default_platform +nbjdk.active=default_platform