diff --git a/examples/src/OpenDis7Examples/EspduSender.java b/examples/src/OpenDis7Examples/EspduSender.java index 661f16b23c566126711063e123c2424d723317c3..0c81c79733a6ca423c5d93f82b4d7e8496a6847b 100644 --- a/examples/src/OpenDis7Examples/EspduSender.java +++ b/examples/src/OpenDis7Examples/EspduSender.java @@ -23,7 +23,7 @@ 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_PDUS_TO_SEND = 5; // 5000 + public static final int NUMBER_OF_LOOPS = 1; // 5 /** * Default multicast group address we send on. @@ -38,6 +38,11 @@ public class EspduSender public enum NetworkMode { UNICAST, MULTICAST, BROADCAST }; + + /** + * Output prefix to identify this class + */ + private final static String TRACE_PREFIX = "[" + EspduSender.class.getName() + "] "; /** * Possible system properties, passed in via -Dattr=val networkMode: @@ -53,16 +58,16 @@ public class EspduSender @SuppressWarnings("SleepWhileInLoop") // allows Thread.sleep(value) without warning in code public static void main(String args[]) { - System.out.println("OpenDis7Examples.EspduSender started..."); + 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.BROADCAST; - 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(); + // 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.595517; @@ -73,7 +78,7 @@ public class EspduSender } catch (UnknownHostException e) { - System.out.println(e + " Cannot create multicast address"); + System.out.println(TRACE_PREFIX + e + " Cannot create multicast address"); System.exit(0); } @@ -125,14 +130,27 @@ public class EspduSender 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("Unable to initialize network correctly, exiting."); + System.out.println(TRACE_PREFIX + "Unable to initialize network correctly, exiting."); System.out.println(e); System.exit(-1); // outta here } - System.out.println("EspduPduSender: sending ESPDU packets to " + address.getHostAddress() + " port " + port); + 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. @@ -158,21 +176,22 @@ public class EspduSender // enumerations in C++ and Java, but to keep things simple we just use // numbers here. - // Manual way to identify platform information: - EntityType entityType = espdu.getEntityType(); - entityType.setEntityKind (EntityKind.PLATFORM); //(short) 1); // Platform (vs lifeform, munition, sensor, etc.) - entityType.setCountry (Country.UNITED_STATES_OF_AMERICA_USA); // 225 USA - entityType.setDomain (Domain.inst(PlatformDomain.LAND)); // Land (vs air, surface, subsurface, space) - entityType.setCategory ((byte) 1); // Tank - entityType.setSubCategory((byte) 1); // M1 Abrams - entityType.setSpecific ((byte) 3); // M1A2 Abrams + // 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) 1) // Tank + .setSubCategory((byte) 1) // M1 Abrams + .setSpecific ((byte) 3); // M1A2 Abrams // New way using entity jar(s) espdu.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.land.M1A2()); - // or simply use import statement above + // or simply use an enumeration by name, with accompanying import statement above espdu.setEntityType(new M1A2()); - // Inspecting an enumeration: + // 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()); @@ -186,9 +205,9 @@ public class EspduSender try // Loop through sending N ESPDUs { - System.out.println("Sending " + NUMBER_PDUS_TO_SEND + " sets of packets:"); // + address.toString() + System.out.println(TRACE_PREFIX + "sending " + NUMBER_OF_LOOPS + " sets of packets:"); // + address.toString() - for (int index = 0; index < NUMBER_PDUS_TO_SEND; index++) + 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. @@ -276,22 +295,53 @@ public class EspduSender 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("Sending espdu datagram packet to " + String.format("%-15s", networkAddress.getHostAddress()) + " port " + port); + 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("Sending fire datagram packet to " + String.format("%-15s", networkAddress.getHostAddress()) + " port " + port); + 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 @@ -299,11 +349,11 @@ public class EspduSender } catch (Exception e) { - System.out.println("Problem with OpenDis7Examples.EspduSender, see exception trace:"); + System.out.println(TRACE_PREFIX + "Problem with " + e.getMessage() + ", see exception trace:"); System.out.println(e); } System.out.println("==============="); - System.out.println("OpenDis7Examples.EspduSender complete."); + System.out.println(TRACE_PREFIX + "complete."); } /** @@ -350,7 +400,7 @@ public class EspduSender } catch (SocketException e) { - System.out.println("Problem with OpenDis7Examples.EspduSender.getBroadcastAddresses(), see exception trace:"); + System.out.println(TRACE_PREFIX + "Problem with .getBroadcastAddresses(), see exception trace:" + e.getMessage()); System.out.println(e); } return broadcastAddresses;