From 4f1e1d19b0b3db24d349479286917df5bae231e0 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@DESKTOP-2S09UKA> Date: Mon, 5 Jul 2021 16:48:31 -0700 Subject: [PATCH] multiple refinements for clarity, correctness, consistency --- .../src/OpenDis7Examples/AllPduReceiver.java | 72 +++++++++++-------- .../src/OpenDis7Examples/AllPduSender.java | 19 +++-- .../src/OpenDis7Examples/EspduReceiver.java | 34 +++++---- .../src/OpenDis7Examples/EspduSender.java | 12 ++-- 4 files changed, 76 insertions(+), 61 deletions(-) diff --git a/examples/src/OpenDis7Examples/AllPduReceiver.java b/examples/src/OpenDis7Examples/AllPduReceiver.java index 80b54da677..20b443df78 100644 --- a/examples/src/OpenDis7Examples/AllPduReceiver.java +++ b/examples/src/OpenDis7Examples/AllPduReceiver.java @@ -7,16 +7,20 @@ import edu.nps.moves.dis7.pdus.*; import edu.nps.moves.dis7.enumerations.*; import edu.nps.moves.dis7.utilities.PduFactory; import java.util.ArrayList; +import java.util.List; /** Listen to all kinds of PDUs and report them */ public class AllPduReceiver { - /** @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */ - public static final String DEFAULT_MULTICAST_ADDRESS = AllPduSender.DEFAULT_MULTICAST_ADDRESS; - /** @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */ - public static final int DEFAULT_MULTICAST_PORT = AllPduSender.DEFAULT_MULTICAST_PORT; - /** TODO whether to use Fast ESPDU */ - public static final boolean USE_FAST_ESPDU = false; + /** @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */ + public static final String DEFAULT_MULTICAST_ADDRESS = AllPduSender.DEFAULT_MULTICAST_ADDRESS; + /** @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */ + public static final int DEFAULT_MULTICAST_PORT = AllPduSender.DEFAULT_MULTICAST_PORT; + /** TODO whether to use Fast ESPDU */ + public static final boolean USE_FAST_ESPDU = false; + + /** Output prefix to identify this class */ + private final static String TRACE_PREFIX = "[" + AllPduReceiver.class.getName() + "] "; /** * Program invocation, execution starts here @@ -24,61 +28,71 @@ public class AllPduReceiver */ public static void main(String args[]) { - PduFactory factory; MulticastSocket socket; - InetAddress address; - DatagramPacket packet; + DatagramPacket packet; + InetAddress address; + PduFactory pduFactory = new PduFactory(); + + System.out.println(TRACE_PREFIX + "started..."); try { - System.out.println("OpenDis7Examples.AllPduReceiver started..."); - if (args.length == 2) { + if (args.length == 2) + { address = InetAddress.getByName(args[0]); socket = new MulticastSocket(Integer.parseInt(args[1])); } - else { + 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); + socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); } + System.out.println("To quit: stop or kill this process"); socket.joinGroup(address); - factory = new PduFactory(); - while (true) // Loop infinitely, receiving datagrams { - byte buffer[] = new byte[1500]; // typical MTU size + byte buffer[] = new byte[1500]; // typical MTU size in bytes + packet = new DatagramPacket(buffer, buffer.length); // reset packet each time - packet = new DatagramPacket(buffer, buffer.length); // reset + socket.receive(packet); // process blocks here until receipt of network packet with PDU - socket.receive(packet); +// Pdu pdu = pduFactory.createPdu(packet.getData()); // packet.getData() returns byte[] array data buffer - 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(); + List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(),packet.getLength()); + if (pduBundle.size() > 1) + System.out.println("Received PDU bundle size is " + pduBundle.size()); + else if (pduBundle.isEmpty()) + System.out.println("Received PDU bundle is empty, packet.getData().length=" + packet.getData().length + ", error..."); + for (Pdu nextPdu : pduBundle) // iterator loop through PDU bundle + { + DISPDUType currentPduType = nextPdu.getPduType(); //short currentPduType = nextPdu.getPduType(); + String currentPduTypeName = nextPdu.getClass().getName(); + String currentPduFamilyName = nextPdu.getClass().getSuperclass().getSimpleName(); + DISProtocolFamily currentProtocolFamilyID = nextPdu.getProtocolFamily(); //short currentProtocolFamilyID = nextPdu.getProtocolFamily(); + StringBuilder message = new StringBuilder(); - message.append(DisTime.timeStampToString(pdu.getTimestamp()) + " "); + message.append(DisTime.timeStampToString(nextPdu.getTimestamp())).append(" "); 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 + // optional, verbose // 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()); + System.out.println(message.toString()); // diagnostic information helps + // additional message information of interest, if any switch (currentPduType) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType { case COMMENT: - CommentPdu commentPdu = (CommentPdu)pdu; // cast to precise type + CommentPdu commentPdu = (CommentPdu)nextPdu; // cast to precise type ArrayList<VariableDatum> payloadList = (ArrayList)commentPdu.getVariableDatums(); if (!payloadList.isEmpty()) System.out.print (" messages: "); @@ -90,8 +104,6 @@ public class AllPduReceiver System.out.println(); } } - else - System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error..."); } } catch (IOException e) { diff --git a/examples/src/OpenDis7Examples/AllPduSender.java b/examples/src/OpenDis7Examples/AllPduSender.java index 867fdc5eaf..e5162ed721 100755 --- a/examples/src/OpenDis7Examples/AllPduSender.java +++ b/examples/src/OpenDis7Examples/AllPduSender.java @@ -28,7 +28,8 @@ public class AllPduSender /** Duration in milliseconds, set to 0 to avoid pausing between PDU sends */ private long THREAD_SLEEP_INTERVAL = 0; - /** Number of complete loops to perform */ + /** Number of complete loops to perform. + * Putting any upper limit on # packets sent avoids possibility of non-terminating infinite loops that continue sending packets. */ private int SEND_LOOPS_TO_PERFORM = 1; private static InetAddress multicastAddress; @@ -37,7 +38,8 @@ public class AllPduSender /** Object constructor * @param newMulticastAddress address of interest * @param newMulticastPort port of interest */ - public AllPduSender(String newMulticastAddress, int newMulticastPort) { + public AllPduSender(String newMulticastAddress, int newMulticastPort) + { this.port = DEFAULT_MULTICAST_PORT; try { @@ -452,6 +454,10 @@ public class AllPduSender InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); MulticastSocket multicastSocket = new MulticastSocket(DEFAULT_MULTICAST_PORT); + if (!localMulticastAddress.isMulticastAddress()) + { + throw new RuntimeException("*** Error: sending to multicast address, but destination address " + localMulticastAddress.toString() + "is not multicast"); + } multicastSocket.joinGroup(localMulticastAddress); // keep object instantiations outside of loops for best performance @@ -476,8 +482,7 @@ public class AllPduSender 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 + Thread.sleep(THREAD_SLEEP_INTERVAL); // pause for debugging, if zero this process still yields } catch (Exception ex) { System.out.println("Marshaling error" + ex); @@ -487,11 +492,11 @@ public class AllPduSender catch (IOException e) { System.out.println(e); - return -1; + return -1; // error condition } } // end repetion loop - // write the PDUs out to an XML file. + // TODO write the PDUs out to an XML file. //PduContainer container = new PduContainer(); //container.setPdus(generatedPdus); //container.marshallToXml("examplePdus.xml"); @@ -509,9 +514,9 @@ public class AllPduSender 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); + allPduSender = new AllPduSender(args[0], Integer.parseInt(args[1])); totalSentPdus = allPduSender.run(); } else diff --git a/examples/src/OpenDis7Examples/EspduReceiver.java b/examples/src/OpenDis7Examples/EspduReceiver.java index 0bd1413c36..8aa82cad29 100755 --- a/examples/src/OpenDis7Examples/EspduReceiver.java +++ b/examples/src/OpenDis7Examples/EspduReceiver.java @@ -29,9 +29,7 @@ public class EspduReceiver * @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */ public static final int DEFAULT_MULTICAST_PORT = EspduSender.DEFAULT_MULTICAST_PORT; - /** - * Output prefix to identify this class - */ + /** Output prefix to identify this class */ private final static String TRACE_PREFIX = "[" + EspduReceiver.class.getName() + "] "; /** @@ -40,54 +38,54 @@ public class EspduReceiver */ public static void main(String args[]) { - System.out.println(TRACE_PREFIX + "started..."); - MulticastSocket socket; DatagramPacket packet; InetAddress address; PduFactory pduFactory = new PduFactory(); int pduCount = 0; + System.out.println(TRACE_PREFIX + "started..."); + try { // Specify the socket to receive data socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); // socket.setBroadcast(true); address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); -////// socket.joinGroup(address); // TODO not needed! + socket.joinGroup(address); System.out.println(TRACE_PREFIX + "listening for PDU packets on " + address.getHostAddress() + " port " + DEFAULT_MULTICAST_PORT); + System.out.println("To quit: stop or kill this process"); System.out.println("==============="); while (true) // Loop infinitely, receiving datagrams { byte buffer[] = new byte[MAX_PDU_SIZE]; - packet = new DatagramPacket(buffer, buffer.length); + packet = new DatagramPacket(buffer, buffer.length); // reset packet each time - socket.receive(packet); + socket.receive(packet); // process blocks here until receipt of network packet with PDU List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(),packet.getLength()); if (pduBundle.size() > 1) - System.out.println("Bundle size is " + pduBundle.size()); - - // end iterator loop through PDU bundle - for (Pdu aPdu : pduBundle) + System.out.println("Received PDU bundle size is " + pduBundle.size()); + + for (Pdu nextPdu : pduBundle) // iterator loop through PDU bundle { pduCount++; String receiptMessage = String.format("%3s", pduCount) // right justify, 3 characters - + ". received PDU type " + aPdu.getPduType().getValue() + "=" + aPdu.getPduType().name() + " " + aPdu.getClass().getName(); - if (aPdu instanceof EntityStatePdu) + + ". received PDU type " + nextPdu.getPduType().getValue() + "=" + nextPdu.getPduType().name() + " " + nextPdu.getClass().getName(); + if (nextPdu instanceof EntityStatePdu) { System.out.println(receiptMessage); - EntityID entityID = ((EntityStatePdu)aPdu).getEntityID(); - Vector3Double position = ((EntityStatePdu)aPdu).getEntityLocation(); + EntityID entityID = ((EntityStatePdu)nextPdu).getEntityID(); + Vector3Double position = ((EntityStatePdu)nextPdu).getEntityLocation(); System.out.println(" entityID triplet: [" + entityID.getSiteID()+ ", " + entityID.getApplicationID()+ ", " + entityID.getEntityID()+ "] "); System.out.println(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); } - else if (aPdu instanceof FirePdu) + else if (nextPdu instanceof FirePdu) { System.out.println(receiptMessage); - Vector3Double position = ((FirePdu)aPdu).getLocationInWorldCoordinates(); + Vector3Double position = ((FirePdu)nextPdu).getLocationInWorldCoordinates(); System.out.println(" FirePdu locationInWorldCoordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); System.out.println("==============="); } diff --git a/examples/src/OpenDis7Examples/EspduSender.java b/examples/src/OpenDis7Examples/EspduSender.java index 67d743a2c1..b18ae8d025 100644 --- a/examples/src/OpenDis7Examples/EspduSender.java +++ b/examples/src/OpenDis7Examples/EspduSender.java @@ -21,9 +21,9 @@ import edu.nps.moves.dis7.entities.usa.platform.land.M1A2; 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 = 1; // 5 + * Number of complete loops to perform. + * Putting any upper limit on # packets sent avoids possibility of non-terminating infinite loops that continue sending packets. */ + public static final int SEND_LOOPS_TO_PERFORM = 1; // 5 /** * Default multicast group address we send on. @@ -76,7 +76,7 @@ public class EspduSender DisTime disTime = new DisTime(); // ICBM coordinates for my office - double latitude = 36.595517; + double latitude = 36.595517; double longitude = -121.877000; try { @@ -211,9 +211,9 @@ public class EspduSender try // Loop through sending N ESPDUs { - System.out.println(TRACE_PREFIX + "sending " + NUMBER_OF_LOOPS + " sets of packets:"); // + address.toString() + System.out.println(TRACE_PREFIX + "sending " + SEND_LOOPS_TO_PERFORM + " sets of packets:"); // + address.toString() - for (int index = 0; index < NUMBER_OF_LOOPS; index++) + for (int index = 0; index < SEND_LOOPS_TO_PERFORM; 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. -- GitLab