diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduReceiver.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduReceiver.java deleted file mode 100644 index d4a5390c49692ec07292714f04deb1a76fdff54e..0000000000000000000000000000000000000000 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduReceiver.java +++ /dev/null @@ -1,104 +0,0 @@ -package MV3500Cohort2020JulySeptember.homework4.White; - -import java.net.*; -import java.io.*; - -import edu.nps.moves.dis7.pdus.*; -import edu.nps.moves.dis7.enumerations.*; -import edu.nps.moves.dis7.utilities.PduFactory; -import java.util.ArrayList; - -public class AllPduReceiver -{ - public static final String DEFAULT_MULTICAST_ADDRESS = AllPduSender.DEFAULT_MULTICAST_ADDRESS; - public static final int DEFAULT_MULTICAST_PORT = AllPduSender.DEFAULT_MULTICAST_PORT; - public static final boolean USE_FAST_ESPDU = false; - - public static void main(String args[]) - { - PduFactory factory; - MulticastSocket socket; - InetAddress address; - DatagramPacket packet; - - try { - System.out.println("OpenDis7Examples.AllPduReceiver started..."); - if (args.length == 2) { - address = InetAddress.getByName(args[0]); - socket = new MulticastSocket(Integer.parseInt(args[1])); - } - else { - System.out.println("Usage: AllPduReceiver <multicast group> <port>"); - System.out.println("Default: AllPduReceiver " + DEFAULT_MULTICAST_ADDRESS + " " + DEFAULT_MULTICAST_PORT); - socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); - address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); - } - socket.joinGroup(address); - - factory = new PduFactory(); - - while (true) // Loop infinitely, receiving datagrams - { - byte buffer[] = new byte[1500]; // typical MTU size - - packet = new DatagramPacket(buffer, buffer.length); // reset - - socket.receive(packet); - - Pdu pdu = factory.createPdu(packet.getData()); // packet.getData() returns byte[] array data buffer - if (pdu != null) - { - DISPDUType currentPduType = pdu.getPduType(); //short currentPduType = pdu.getPduType(); - String currentPduTypeName = pdu.getClass().getName(); - DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short currentProtocolFamilyID = pdu.getProtocolFamily(); - String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName(); - - StringBuilder message = new StringBuilder(); - message.append(DisTime.timeStampToString(pdu.getTimestamp()) + " "); - message.append("received DIS PDU "); - String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace - message.append(" " ).append(currentPduTypePadded); - if (currentPduType.getValue() < 10) - message.append(" "); // column spacing -// String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace -// message.append(" of type ").append(currentPduTypeNamePadded); // package.class name - message.append(" (").append(currentProtocolFamilyID); -// message.append(" ").append(currentPduFamilyName); // class name is also available - message.append(")"); - System.out.println(message.toString()); - if (currentPduType == DISPDUType.FIRE){ - FirePdu firePdu = new FirePdu(); - firePdu = (FirePdu) pdu; - if(firePdu.getFiringEntityID().getEntityID()!= 0){ - System.out.println("Entity#" + firePdu.getFiringEntityID().getEntityID() + " is firing Muition [" + firePdu.getDescriptor().getMunitionType().getDomain() + "."+firePdu.getDescriptor().getMunitionType().getCountry() + "." + firePdu.getDescriptor().getMunitionType().getCategory() + "."+ firePdu.getDescriptor().getMunitionType().getSubCategory() + "." + firePdu.getDescriptor().getMunitionType().getSpecific() + "]"+ " at Entity#"+ firePdu.getTargetEntityID().getEntityID()); - } - } - - switch (currentPduType) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType - { - case COMMENT: - CommentPdu commentPdu = (CommentPdu)pdu; // cast to precise type - ArrayList<VariableDatum> payloadList = (ArrayList)commentPdu.getVariableDatums(); - if (!payloadList.isEmpty()) - System.out.print (" messages: "); - for (VariableDatum variableDatum : payloadList) - { - String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String - System.out.print (" \"" + nextComment + "\""); - } - System.out.println(); - } - } - else - System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error..."); - } - } - catch (IOException e) { - System.out.println("Problem with OpenDis7Examples.AllPduReceiver, see exception trace:"); - System.out.println(e); - } - finally { - System.out.println("OpenDis7Examples.AllPduReceiver complete."); - } - } -} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduSender.java deleted file mode 100755 index 5d4b58b7e1f395e1503a2f713a5f4f0c26f346ce..0000000000000000000000000000000000000000 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/AllPduSender.java +++ /dev/null @@ -1,562 +0,0 @@ -package MV3500Cohort2020JulySeptember.homework4.White; - -import java.io.*; -import java.net.*; -import java.util.*; - -import edu.nps.moves.dis7.pdus.*; -import edu.nps.moves.dis7.enumerations.*; -import edu.nps.moves.dis7.utilities.*; - -/** - * This is an application example that sends all types of PDUs. Useful for testing standards - * compliance or getting a full set of PDUs. It also writes the generated PDUs to an XML file. - * Adapted from OpenDIS library example package edu.nps.moves.examples - * - * @author DMcG - * @version $Id:$ - */ -public class AllPduSender -{ - /** Default multicast group address we send on. */ - public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; // PduRecorder "225.4.5.6"; // - - /** Default multicast port used, matches Wireshark DIS capture default */ - public static final int DEFAULT_MULTICAST_PORT = 3000; - - /** Duration in milliseconds, set to 0 to avoid pausing between PDU sends */ - private long THREAD_SLEEP_INTERVAL = 0; - - /** Number of complete loops to perform */ - private int SEND_LOOPS_TO_PERFORM = 1; - - private static InetAddress multicastAddress; - private static int port; - - public AllPduSender(String newMulticastAddress, int newMulticastPort) { - this.port = DEFAULT_MULTICAST_PORT; - try - { - multicastAddress = InetAddress.getByName(newMulticastAddress); - if (!multicastAddress.isMulticastAddress()) - { - System.out.println("Not a multicast address: " + newMulticastAddress); - } - this.port = newMulticastPort; - } - catch (UnknownHostException e) { - System.out.println("Unable to open socket: " + e); - } - } - - @SuppressWarnings("SleepWhileInLoop") - public int run() - { - System.out.println("OpenDis7Examples.AllPduSender started..."); - if (SEND_LOOPS_TO_PERFORM != 1) - { - float waitIntervalSeconds = ((float)THREAD_SLEEP_INTERVAL / 1000); - float loopIntervalSeconds = ((float)THREAD_SLEEP_INTERVAL / 1000) * 72; // 72 PDUs - float totalDurationSeconds = loopIntervalSeconds * SEND_LOOPS_TO_PERFORM ; - System.out.println("... THREAD_SLEEP_INTERVAL = " + THREAD_SLEEP_INTERVAL + " milliseconds = " + waitIntervalSeconds + " seconds"); - System.out.print ("... running for "); - if (SEND_LOOPS_TO_PERFORM > 1) - System.out.print (SEND_LOOPS_TO_PERFORM + " loops, "); - if (THREAD_SLEEP_INTERVAL > 0) - System.out.println("expected loop interval = " + loopIntervalSeconds + " seconds, total duration = " + - totalDurationSeconds + " seconds = " + (totalDurationSeconds/60.0) + " minutes"); - } - - System.out.println("Generate list of all PDU types and note issues, if any..."); - List<Pdu> generatedPdusList = new ArrayList<>(); - - for (int i = 0; i < SEND_LOOPS_TO_PERFORM; i++) { - - try { - - // Loop through all the enumerated PDU types, create a PDU for each type, - // add that PDU to generatedPdusList, then send each one - for (DISPDUType pdu : DISPDUType.values()) - { - // System.out.println("PDU " + pdu.getValue() + " " + pdu.name() + " " + pdu.getDescription()); // diagnostic - - Pdu aPdu = null; // edu.​nps.​moves7.​dis.PDU superclass for all PDUs, in preparation for custom assignment - - try { - switch (pdu) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType - { - // each case value is DISPDUType - case OTHER: // 0 - System.out.println ("*** Note: DISPDUType." + pdu.name() + "=" + pdu.getValue() + " not supported"); // TODO why was this received? - break; // nothing to send - - case ENTITY_STATE: // 1 - aPdu = new EntityStatePdu(); - - EntityStatePdu espdu = (EntityStatePdu) aPdu; - EntityMarking entityMarking = new EntityMarking (); - entityMarking.setCharacters("AllPduSender".getBytes()); //entityMarking.setCharacters(Byte.valueOf("0")); // 11 characters max? - - espdu.setMarking(entityMarking); - EntityID lavID = new EntityID(); - lavID.setSiteID(1); - lavID.setApplicationID(13); - lavID.setEntityID(25); - espdu.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); - espdu.setEntityType(lavType); - Vector3Double eloc1 = new Vector3Double(); - double[] loc1 = CoordinateConversions.getXYZfromLatLonDegrees(36.599831, -121.878842, 0); - eloc1.setX(loc1[0]); - eloc1.setY(loc1[1]); - eloc1.setZ(loc1[2]); - espdu.setEntityLocation(eloc1); - EulerAngles orient1 = new EulerAngles(); - orient1.setPhi((float) 0.0); - orient1.setPsi((float) 0.0); - orient1.setTheta((float) 0.0); - espdu.setEntityOrientation(orient1); - // it is important to identify questions as you think of them - // TODO how to set azimuth, i.e. course direction over ground? - break; - - case FIRE: // 2 - aPdu = new FirePdu(); - FirePdu firePdu = (FirePdu) aPdu; - - 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); - firePdu.setDescriptor(HEIT); - EntityID HEID = new EntityID(); - HEID.setEntityID(1); - firePdu.setMunitionExpendibleID(HEID); - break; - - case DETONATION: // 3 - aPdu = new DetonationPdu(); - break; - - case COLLISION: // 4 - aPdu = new CollisionPdu(); - break; - - case SERVICE_REQUEST: // 5 - aPdu = new ServiceRequestPdu(); - break; - - case RESUPPLY_OFFER: // 6 - aPdu = new ResupplyOfferPdu(); - break; - - case RESUPPLY_RECEIVED: // 7 - aPdu = new ResupplyReceivedPdu(); - break; - - case RESUPPLY_CANCEL: //8 - aPdu = new ResupplyCancelPdu(); - break; - - case REPAIR_COMPLETE: // 9 - aPdu = new RepairCompletePdu(); - break; - - case REPAIR_RESPONSE: // 10 - aPdu = new RepairResponsePdu(); - break; - - case CREATE_ENTITY: // 11 - aPdu = new CreateEntityPdu(); - break; - - case REMOVE_ENTITY: // 12 - aPdu = new RemoveEntityPdu(); - break; - - case START_RESUME: // 13 - aPdu = new StartResumePdu(); - break; - - case STOP_FREEZE: // 14 - aPdu = new StopFreezePdu(); - break; - - case ACKNOWLEDGE: // 15 - aPdu = new AcknowledgePdu(); - break; - - case ACTION_REQUEST: // 16 - aPdu = new ActionRequestPdu(); - break; - - case ACTION_RESPONSE: // 17 - aPdu = new ActionResponsePdu(); - break; - - case DATA_QUERY: // 18 - aPdu = new DataQueryPdu(); - break; - - case SET_DATA: // 19 - aPdu = new SetDataPdu(); - break; - - case DATA: // 20 - aPdu = new DataPdu(); - break; - - case EVENT_REPORT: // 21 - aPdu = new EventReportPdu(); - break; - - case ELECTROMAGNETIC_EMISSION: // 23 - aPdu = new ElectromagneticEmissionPdu(); - break; - - case DESIGNATOR: // 24 - aPdu = new DesignatorPdu(); - break; - - case TRANSMITTER: // 25 - aPdu = new TransmitterPdu(); - break; - - case SIGNAL: // 26 - aPdu = new SignalPdu(); - break; - - case RECEIVER: // 27 - aPdu = new ReceiverPdu(); - break; - - case IDENTIFICATION_FRIEND_OR_FOE: // 28 - aPdu = new IdentificationFriendOrFoePdu(); - break; - - case UNDERWATER_ACOUSTIC: // 29 - aPdu = new UnderwaterAcousticPdu(); - break; - - case SUPPLEMENTAL_EMISSION_ENTITY_STATE: // 30 - aPdu = new SupplementalEmissionEntityStatePdu(); - break; - - case INTERCOM_SIGNAL: // 31 - aPdu = new IntercomSignalPdu(); - break; - - case INTERCOM_CONTROL: // 32 - aPdu = new IntercomControlPdu(); - break; - - case AGGREGATE_STATE: // 33 - aPdu = new AggregateStatePdu(); - break; - - case ISGROUPOF: // 34 - aPdu = new IsGroupOfPdu(); - break; - - case TRANSFER_OWNERSHIP: // 35 - aPdu = new TransferOwnershipPdu(); - break; - - case ISPARTOF: // 36 - aPdu = new IsPartOfPdu(); - break; - - case MINEFIELD_STATE: // 37 - aPdu = new MinefieldStatePdu(); - break; - - case MINEFIELD_QUERY: // 38 - aPdu = new MinefieldQueryPdu(); - break; - - case MINEFIELD_DATA: // 39 - aPdu = new MinefieldDataPdu(); - break; - - case MINEFIELD_RESPONSE_NACK: // 40 - aPdu = new MinefieldResponseNACKPdu(); - break; - - case ENVIRONMENTAL_PROCESS: // 41 - aPdu = new EnvironmentalProcessPdu(); - break; - - case GRIDDED_DATA: // 42 - aPdu = new GriddedDataPdu(); - break; - - case POINT_OBJECT_STATE: // 43 - aPdu = new PointObjectStatePdu(); - break; - - case LINEAR_OBJECT_STATE: // 44 - aPdu = new LinearObjectStatePdu(); - break; - - case AREAL_OBJECT_STATE: // 45 - aPdu = new ArealObjectStatePdu(); - break; - - case TIME_SPACE_POSITION_INFORMATION: // 46 - aPdu = new TimeSpacePositionInformationPdu(); - break; - - case APPEARANCE: // 47 - aPdu = new AppearancePdu(); - break; - - case ARTICULATED_PARTS: // 48 - aPdu = new ArticulatedPartsPdu(); - break; - - case LIVE_ENTITY_FIRE: // 49 - aPdu = new LiveEntityFirePdu(); - break; - - case LIVE_ENTITY_DETONATION: // 50 - aPdu = new LiveEntityDetonationPdu(); - break; - - case CREATE_ENTITY_RELIABLE: // 51 - aPdu = new CreateEntityReliablePdu(); - break; - - case REMOVE_ENTITY_RELIABLE: // 52 - aPdu = new RemoveEntityReliablePdu(); - break; - - case START_RESUME_RELIABLE: // 53 - aPdu = new StartResumeReliablePdu(); - break; - - case STOP_FREEZE_RELIABLE: // 54 - aPdu = new StopFreezeReliablePdu(); - break; - - case ACKNOWLEDGE_RELIABLE: // 55 - aPdu = new AcknowledgeReliablePdu(); - break; - - case ACTION_REQUEST_RELIABLE: // 56 - aPdu = new ActionRequestReliablePdu(); - break; - - case ACTION_RESPONSE_RELIABLE: // 57 - aPdu = new ActionResponseReliablePdu(); - break; - - case DATA_QUERY_RELIABLE: // 58 - aPdu = new DataQueryReliablePdu(); - break; - - case SET_DATA_RELIABLE: // 59 - aPdu = new SetDataReliablePdu(); - break; - - case DATA_RELIABLE: // 60 - aPdu = new DataReliablePdu(); - break; - - case EVENT_REPORT_RELIABLE: // 61 - aPdu = new EventReportReliablePdu(); - break; - - case COMMENT_RELIABLE: // 62 - aPdu = new CommentReliablePdu(); - break; - - case RECORD_RELIABLE: // 63 - aPdu = new RecordReliablePdu(); - break; - - case SET_RECORD_RELIABLE: // 64 - aPdu = new SetRecordReliablePdu(); - break; - - case RECORD_QUERY_RELIABLE: // 65 - aPdu = new RecordQueryReliablePdu(); - break; - - case COLLISION_ELASTIC: // 66 - aPdu = new CollisionElasticPdu(); - break; - - case ENTITY_STATE_UPDATE: // 67 - aPdu = new EntityStateUpdatePdu(); - break; - - case DIRECTED_ENERGY_FIRE: // 68 - aPdu = new DirectedEnergyFirePdu(); - break; - - case ENTITY_DAMAGE_STATUS: // 69 - aPdu = new EntityDamageStatusPdu(); - break; - - case INFORMATION_OPERATIONS_ACTION: // 70 - aPdu = new InformationOperationsActionPdu(); - break; - - case INFORMATION_OPERATIONS_REPORT: // 71 - aPdu = new InformationOperationsReportPdu(); - break; - - case ATTRIBUTE: // 72 - aPdu = new AttributePdu(); - break; - - case COMMENT: - // aPdu = new CommentPdu(); // default for this switch logic - - // see Garrett Loffelman and Pete Severson's code for OpenDis version 4 example - // https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/tree/master/assignments/src/MV3500Cohort2018JulySeptember/projects/LoeffelmanSeverson - - 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); - - aPdu = newCommentPdu; // hand off for sending - break; - - default: - System.out.println("*** Warning: PDU " + pdu.getValue() + " " + pdu + " not supported, created or sent "); - - // code generation block for this class follows: - // System.out.println(" case " + pdu + ": // " + pdu.getValue()); - // System.out.println(" aPdu = new " + pdu.getDescription().replace(" ","").replace("-","").replace("/","") + - // "Pdu();"); - // System.out.println(" break;"); - // System.out.println(); - } - if (aPdu != null) - { - generatedPdusList.add(aPdu); - } - } - catch (Exception e) - { - System.out.print("Exception thrown for PDU " + pdu.getValue() + " " + pdu); - System.out.print(Arrays.toString(e.getStackTrace())); - // continue looping - } - } - if (generatedPdusList.size() != 72) // TODO create an enumeration DISType.TOTAL_PDU_TYPES - System.out.println("Error: " + generatedPdusList.size() + " PDUs generated but 72 PDUs expected."); - - // Send the PDUs we created - System.out.println("Send the " + generatedPdusList.size() + " PDUs we created..."); - - InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); - MulticastSocket multicastSocket = new MulticastSocket(DEFAULT_MULTICAST_PORT); - multicastSocket.joinGroup(localMulticastAddress); - - // keep object instantiations outside of loops for best performance - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - byte[] buffer; - Pdu aPdu; - DatagramPacket packet; - - for (int idx = 0; idx < generatedPdusList.size(); idx++) - { - aPdu = generatedPdusList.get(idx); - try - { - aPdu.marshal(dos); - - buffer = baos.toByteArray(); - packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT); - multicastSocket.send(packet); - String currentPduTypeValuePadded = String.format("%2s", aPdu.getPduType().getValue()); - String currentPduTypePadded = String.format("%-50s", aPdu.getPduType().toString()); // - indicates right padding of whitespace - System.out.print ("Sent DIS PDU " + currentPduTypeValuePadded + " " + currentPduTypePadded ); - System.out.println(" of type " + aPdu.getClass().getName()); - - if (THREAD_SLEEP_INTERVAL > 0) - Thread.sleep(THREAD_SLEEP_INTERVAL); // pause for debugging - } - catch (Exception ex) { - System.out.println("Marshaling error" + ex); - } - } - } - catch (IOException e) - { - System.out.println(e); - return -1; - } - } // end repetion loop - - // write the PDUs out to an XML file. - //PduContainer container = new PduContainer(); - //container.setPdus(generatedPdus); - //container.marshallToXml("examplePdus.xml"); - return generatedPdusList.size(); - } - - public static void main(String args[]) - { - AllPduSender allPduSender; - int totalSentPdus; - - if (args.length == 2) - { - allPduSender = new AllPduSender(args[0], Integer.parseInt(args[1])); - System.out.println("Usage: AllPduSender <multicast group> <port>"); - System.out.println("Actual: AllPduSender " + multicastAddress.getHostAddress() + " " + port); - totalSentPdus = allPduSender.run(); - } - else - { - System.out.println("Usage: AllPduSender <multicast group> <port>"); - System.out.println("Default: AllPduSender " + DEFAULT_MULTICAST_ADDRESS + " " + DEFAULT_MULTICAST_PORT); - allPduSender = new AllPduSender(DEFAULT_MULTICAST_ADDRESS, DEFAULT_MULTICAST_PORT); - totalSentPdus = allPduSender.run(); - } - System.out.println("OpenDis7Examples.AllPduSender complete, sent " + totalSentPdus + " PDUs total."); - } -} 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/ExampleSimulationProgram.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java similarity index 81% rename from assignments/src/MV3500Cohort2020JulySeptember/homework4/White/ExampleSimulationProgram.java rename to assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java index 2ff32fe4ae21370c3a48926fe57629c65a0cc525..9d95854ece52932f2ad8ae6bde531820449d9ff1 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/ExampleSimulationProgram.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/WhiteSimulation.java @@ -4,14 +4,24 @@ */ 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 ExampleSimulationProgram +public class WhiteSimulation { // class variables PduFactory pduFactory = new PduFactory(); @@ -19,14 +29,14 @@ public class ExampleSimulationProgram DisThreadedNetworkInterface.PduListener pduListener; Pdu receivedPdu; - private String networkAddress = "239.1.2.3"; - private int networkPort = 3000; + 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 ExampleSimulationProgram() + public WhiteSimulation() { // Under consideration. Constructor is not currently needed. } @@ -36,7 +46,7 @@ public class ExampleSimulationProgram * @param address network address to use * @param port corresponding network port to use */ - public ExampleSimulationProgram(String address, int port) + public WhiteSimulation(String address, int port) { setNetworkAddress(address); @@ -136,10 +146,10 @@ public class ExampleSimulationProgram String... comments) { if (entityStatePdu != null) - sendSinglePdu(entityStatePdu); + sendPDU(entityStatePdu); if (firePdu != null) - sendSinglePdu(firePdu); // bang + sendPDU(firePdu); // bang if ((comments != null) && (comments.length > 0)) { @@ -154,7 +164,7 @@ public class ExampleSimulationProgram if (commentType == null) commentType = VariableRecordType.OTHER; CommentPdu commentPdu = pduFactory.makeCommentPdu(commentType, newCommentsList.toArray(new String[0])); // comments); - sendSinglePdu(commentPdu); + sendPDU(commentPdu); } } } @@ -166,7 +176,7 @@ public class ExampleSimulationProgram */ public static void main(String[] args) { - ExampleSimulationProgram thisProgram = new ExampleSimulationProgram(); // creates instance + WhiteSimulation thisProgram = new WhiteSimulation(); // creates instance // initial execution: can handle args array of initialization arguments here if (args.length == 2) @@ -184,11 +194,11 @@ public class ExampleSimulationProgram } // OK here we go... - thisProgram.setUpNetworkInterface(); + //thisProgram.setUpNetworkInterface(); thisProgram.runSimulation (); // customization code goes in there - thisProgram.tearDownNetworkInterface(); + //thisProgram.tearDownNetworkInterface(); } /** @@ -206,7 +216,8 @@ public class ExampleSimulationProgram 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? @@ -236,6 +247,7 @@ public class ExampleSimulationProgram 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]); @@ -272,7 +284,7 @@ public class ExampleSimulationProgram entityStatePdu2.setEntityLocation(eloc1); EulerAngles orient1 = new EulerAngles(); - orient1.setPhi((float) 0.0); + orient1.setPhi((float) 3.1415); orient1.setPsi((float) 0.0); orient1.setTheta((float) 0.0); entityStatePdu2.setEntityOrientation(orient1); @@ -282,7 +294,7 @@ public class ExampleSimulationProgram System.out.println(eloc2.toString()); System.out.println(eloc1.toString()); - FirePdu firePduNull = new FirePdu(); + //FirePdu firePduNull = new FirePdu(); FirePdu firePdu = pduFactory.makeFirePdu(); EntityID fireID = new EntityID(); fireID.setSiteID(1); @@ -308,11 +320,15 @@ public class ExampleSimulationProgram 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? @@ -337,24 +353,28 @@ public class ExampleSimulationProgram 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); + System.out.println("range" + range + " dx:" +dx + " dy:"+ dy); // etc. etc. your code goes here if(range < 100){ - firePduNull = firePdu; + 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(firePduNull.getTargetEntityID().getEntityID() == 2){ + 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"; @@ -378,14 +398,23 @@ public class ExampleSimulationProgram // send the status PDUs for this loop and continue System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); - sendAllPdus(entityStatePdu, firePduNull, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); - sendAllPdus(entityStatePdu2, null, null, narrativeMessage1, narrativeMessage2, narrativeMessage4); + //sendAllPdus(entityStatePdu, firePduNull, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); + //sendAllPdus(entityStatePdu2, null, null, narrativeMessage1, narrativeMessage2, narrativeMessage4); + sendPDU(entityStatePdu); + sendPDU(entityStatePdu2); + if (fireBool) + sendPDU(firePdu); + if (destBool) + sendPDU(bmpDestroyedComment); + System.out.println ("... PDUs successfully sent"); // =============================== // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good { + CommentReliablePdu completionPdu = pduFactory.makeCommentReliablePdu("White Simulation Completed"); + sendPDU(completionPdu); System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); break; } @@ -393,7 +422,41 @@ public class ExampleSimulationProgram } catch (Exception ex) // handle any exception that your code might choose to provoke! { - Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, ex); + 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..a0ac43f61913b35cbee0f3dbd4b09ff854065405 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/PDUReciever.java @@ -0,0 +1,127 @@ +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(); + } + } //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..a8efe650eb11ff581caeb9df98b2d1f61b0ba690 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/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/working/WhiteSimulation.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/WhiteSimulation.java new file mode 100644 index 0000000000000000000000000000000000000000..5208527bf2f09a1950a4649107d58dfd7daaaa4a --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/White/working/WhiteSimulation.java @@ -0,0 +1,461 @@ +/** + * 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.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