diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduReceiver.java b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..d23bef63de7d55beaca0e06896cfbb937025ba40 --- /dev/null +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduReceiver.java @@ -0,0 +1,80 @@ +package MV3500Cohort2019JulySeptember.homework4.Knobeloch; + +import java.net.*; +import java.io.*; + +import edu.nps.moves.dis7.*; +import edu.nps.moves.dis7.enumerations.*; +import edu.nps.moves.dis7.util.*; + +public class Knobeloch_PduReceiver +{ + public static final int DEFAULT_MULTICAST_PORT = Knobeloch_PduSender.DEFAULT_MULTICAST_PORT; + public static final String DEFAULT_MULTICAST_ADDRESS = Knobeloch_PduSender.DEFAULT_MULTICAST_ADDRESS; + 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("Knobeloch_PduReceiver started..."); + if (args.length == 2) { + socket = new MulticastSocket(Integer.parseInt(args[0])); + address = InetAddress.getByName(args[1]); + } + else { + System.out.println("Usage: AllPduReceiver <port> <multicast group>"); + System.out.println("Default: AllPduReceiver " + DEFAULT_MULTICAST_PORT + " " + DEFAULT_MULTICAST_ADDRESS); + 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()); + 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("received DIS PDU "); + if (currentPduType.getValue() < 10) + message.append(" "); + message.append(currentPduType.getValue()); + String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace + message.append(" " ).append(currentPduTypePadded); + String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace + message.append(" of type ").append(currentPduTypeNamePadded); // package.class name + message.append(" (protocolFamily ").append(currentProtocolFamilyID); +// message.append(" ").append(currentPduFamilyName); // class name is also available + message.append(")"); + System.out.println(message.toString()); + } + 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 Knobeloch_PduReceiver, see exception trace:"); + System.out.println(e); + } + finally { + System.out.println("Knobeloch_PduReceiver complete."); + } + } +} diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduSender.java b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduSender.java new file mode 100755 index 0000000000000000000000000000000000000000..7294c95f1cce104089c272deb54e0cf0efc9d468 --- /dev/null +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Knobeloch/Knobeloch_PduSender.java @@ -0,0 +1,172 @@ +package MV3500Cohort2019JulySeptember.homework4.Knobeloch; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis7.*; +import edu.nps.moves.dis7.enumerations.*; + +/** + * This is an example that sends many/most 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 Knobeloch_PduSender +{ + /** 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; + + private int port; + InetAddress multicastAddress; + + public Knobeloch_PduSender(int port, String multicast) { + try + { + this.port = port; + multicastAddress = InetAddress.getByName(multicast); + if (!multicastAddress.isMulticastAddress()) + { + System.out.println("Not a multicast address: " + multicast); + } + } + catch (UnknownHostException e) { + System.out.println("Unable to open socket: " + e); + } + } + + public void run(int numbOfPDUS) + { + System.out.println("DisExamplesOpenDis7.AllPduSender started..."); + System.out.println("Generate PDUs and note issues, if any..."); + + List<Pdu> generatedPdusList = createPDU(numbOfPDUS); + + // Send the PDUs we created + System.out.println("Send the " + generatedPdusList.size() + " PDUs we created..."); + + try + { + InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); + MulticastSocket socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); + socket.joinGroup(localMulticastAddress); + + for (int idx = 0; idx < generatedPdusList.size(); idx++) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + byte[] buffer; + + Pdu aPdu = generatedPdusList.get(idx); + aPdu.marshal(dos); + + buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT); + socket.send(packet); + try { + Thread.sleep(100L); + } catch (InterruptedException ex) { + } + String currentPduTypeValuePadded = String.format("%2s", aPdu.getPduType().getValue()); + String currentPduTypePadded = String.format("%-34s", aPdu.getPduType()); // - indicates right padding of whitespace + System.out.print ("Sent DIS PDU " + currentPduTypeValuePadded + " " + currentPduTypePadded ); + System.out.println(" of type " + aPdu.getClass().getName()); + } + } + catch (IOException e) + { + System.out.println(e); + } + } + + public static void main(String args[]) + { + if (args.length == 2) + { + Knobeloch_PduSender sender = new Knobeloch_PduSender(Integer.parseInt(args[0]), args[1]); + sender.run(5); + } + else + { + System.out.println("Usage: AllPduSender <port> <multicast group>"); + System.out.println("Default: AllPduSender " + DEFAULT_MULTICAST_PORT + " " + DEFAULT_MULTICAST_ADDRESS); + Knobeloch_PduSender sender = new Knobeloch_PduSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); + sender.run(5); + } + System.out.println("Knobeloch_PduSender complete."); + } + + private List<Pdu> createPDU(int numbOfPDUs) + { + List<Pdu> list = new ArrayList<>(); + List<Short> entityIDs = new ArrayList<>(); + Random r = new Random(); + + //generate Entity ID (no douplications) + for (int i = 0; i < numbOfPDUs; i++) + { + short temp = (short) r.nextInt(); + while (entityIDs.contains(temp)) + { + temp = (short) r.nextInt(); + } + entityIDs.add(temp); + } + + // + for (int i = 0; i < numbOfPDUs; i++){ + EntityStatePdu myPdu = new EntityStatePdu(); + + //ID + EntityID tempID = new EntityID(); + tempID.setEntityID(entityIDs.get(i)); + myPdu.setEntityID(tempID); + + //Enemy or Friend + myPdu.setForceId(ForceID.FRIENDLY); + + //location + Vector3Double tempLoc = new Vector3Double(); + tempLoc.setX(r.nextInt(1000) + r.nextDouble()); + tempLoc.setY(r.nextInt(1000) + r.nextDouble()); + tempLoc.setZ(r.nextInt(1000) + r.nextDouble()); + + myPdu.setEntityLocation(tempLoc); + + //orientation + EulerAngles tempOri = new EulerAngles(); + tempOri.setPhi(r.nextFloat()); + tempOri.setPsi(r.nextFloat()); + tempOri.setTheta(r.nextFloat()); + + myPdu.setEntityOrientation(tempOri); + + + //velocity + Vector3Double tempVel = new Vector3Double(); + tempVel.setX(r.nextDouble()); + tempVel.setY(r.nextDouble()); + tempVel.setZ(r.nextDouble()); + + myPdu.setEntityLocation(tempVel); + + +// Category? Country? Domain?... +// EntityType tempType = new EntityType(); +// tempType.set +// myPdu.setEntityType(tempType) + + list.add(myPdu); + } + + + return list; + } +} +