diff --git a/examples/src/OpenDis7Examples/EspduReceiver.java b/examples/src/OpenDis7Examples/EspduReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..c4cb5cfd8cd84bd414371bb8ab51202496b0c2f6 --- /dev/null +++ b/examples/src/OpenDis7Examples/EspduReceiver.java @@ -0,0 +1,81 @@ +package OpenDis7Examples; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis7.*; +import edu.nps.moves.dis7.util.*; + +/** + * Receives PDUs from the network in IEEE DIS format. + * Adapted from OpenDIS library example package edu.nps.moves.examples + * + * @author DMcG + * @version $Id:$ + */ +public class EspduReceiver +{ + /** 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 multicast group address we send on. */ + public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; + + /** Default multicast port used, matches Wireshark DIS capture default */ + public static final int DEFAULT_MULTICAST_PORT = 3000; + + public static void main(String args[]) + { + System.out.println("DisExamplesOpenDis7.EspduReceiver started..."); + + MulticastSocket socket; + DatagramPacket packet; + InetAddress address; + PduFactory pduFactory = new PduFactory(); + + try { + // Specify the socket to receive data + socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); + socket.setBroadcast(true); + + // address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP); + // socket.joinGroup(address); + + while (true) // Loop infinitely, receiving datagrams + { + byte buffer[] = new byte[MAX_PDU_SIZE]; + packet = new DatagramPacket(buffer, buffer.length); + + socket.receive(packet); + + List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData()); + System.out.println("Bundle size is " + pduBundle.size()); + + Iterator iterator = pduBundle.iterator(); + + while(iterator.hasNext()) + { + Pdu aPdu = (Pdu)iterator.next(); + + System.out.print("got PDU of type: " + aPdu.getClass().getName()); + if(aPdu instanceof EntityStatePdu) + { + EntityID eid = ((EntityStatePdu)aPdu).getEntityID(); + Vector3Double position = ((EntityStatePdu)aPdu).getEntityLocation(); + System.out.print(" EID:[" + eid.getSiteID()+ ", " + eid.getApplicationID()+ ", " + eid.getEntityID()+ "] "); + System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); + } + System.out.println(); + } // end trop through PDU bundle + } // end while + } // End try + catch (IOException e) + { + System.out.println("Problem with DisExamplesOpenDis7.EspduReceiver, see exception trace:"); + System.out.println(e); + } + } // end main +} // end class diff --git a/examples/src/OpenDis7Examples/EspduSender.java b/examples/src/OpenDis7Examples/EspduSender.java new file mode 100644 index 0000000000000000000000000000000000000000..4e7a6f8a3231e51d22d12a2a83f28f489df78f5e --- /dev/null +++ b/examples/src/OpenDis7Examples/EspduSender.java @@ -0,0 +1,325 @@ +package OpenDis7Examples; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis7.*; +import edu.nps.moves.dis7.util.*; + +//import edu.nps.moves.disutil.CoordinateConversions; +//import edu.nps.moves.disutil.DisTime; + +/** + * Creates and sends ESPDUs in IEEE binary format. Adapted from OpenDIS library + * example package edu.nps.moves.examples + * + * @author DMcG + */ +public class EspduSender { + + public static final int NUMBER_TO_SEND = 5000; + + public enum NetworkMode { + UNICAST, MULTICAST, BROADCAST + }; + + /** + * 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; + + /** + * Possible system properties, passed in via -Dattr=val networkMode: + * unicast, broadcast, multicast destinationIp: where to send the packet. If + * in multicast mode, this can be multicast. To determine broadcast + * destination IP, use an online broadcast address calculator, for example + * http://www.remotemonitoringsystems.ca/broadcast.php If in multicast mode, + * a join() will be done on the multicast address. port: port used for both + * source and destination. + * + * @param args + */ + public static void main(String args[]) + { + System.out.println("DisExamplesOpenDis7.EspduSender started... send " + NUMBER_TO_SEND + " ESPDUs, initial index=0"); + /** + * an entity state pdu + */ + EntityStatePdu espdu = new EntityStatePdu(); + MulticastSocket socket = null; // must be initialized, even if null + DisTime disTime = DisTime.getInstance(); // TODO explain + int alternator = -1; + + // ICBM coordinates for my office + double lat = 36.595517; + double lon = -121.877000; + + // Default settings. These are used if no system properties are set. + // If system properties are passed in, these are over ridden. + int port = DEFAULT_MULTICAST_PORT; + NetworkMode mode = NetworkMode.BROADCAST; + InetAddress destinationIp = null; // must be initialized, even if null + + try { + destinationIp = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); + } catch (UnknownHostException e) { + System.out.println(e + " Cannot create multicast address"); + System.exit(0); + } + + // All system properties, passed in on the command line via -Dattribute=value + Properties systemProperties = System.getProperties(); + + // IP address we send to + String destinationIpString = systemProperties.getProperty("destinationIp"); + + // Port we send to, and local port we open the socket on + String portString = systemProperties.getProperty("port"); + + // Network mode: unicast, multicast, broadcast + String networkModeString = systemProperties.getProperty("networkMode"); // unicast or multicast or broadcast + + // Set up a socket to send information + try { + // Port we send to + if (portString != null) + { + port = Integer.parseInt(portString); + } + + socket = new MulticastSocket(port); + + // Where we send packets to, the destination IP address + if (destinationIpString != null) + { + destinationIp = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + // TODO convert to String constants + if (networkModeString != null) { + if (networkModeString.equalsIgnoreCase("unicast")) + { + mode = NetworkMode.UNICAST; + } + else if (networkModeString.equalsIgnoreCase("broadcast")) { + mode = NetworkMode.BROADCAST; + } + else if (networkModeString.equalsIgnoreCase("multicast")) { + mode = NetworkMode.MULTICAST; + if (!destinationIp.isMulticastAddress()) + { + throw new RuntimeException("Sending to multicast address, but destination address " + destinationIp.toString() + "is not multicast"); + } + socket.joinGroup(destinationIp); + } + } // end networkModeString + } + catch (IOException | RuntimeException e) + { + System.out.println("Unable to initialize networking. Exiting."); + System.out.println(e); + System.exit(-1); + } + + // Initialize values in the Entity State PDU object. The exercise ID is + // a way to differentiate between different virtual worlds on one network. + // Note that some values (such as the PDU type and PDU family) are set + // automatically when you create the ESPDU. + espdu.setExerciseID((short) 1); + + // The EID is the unique identifier for objects in the world. This + // EID should match up with the ID for the object specified in the + // VMRL/x3d/virtual world. + EntityID entityID = espdu.getEntityID(); + entityID.setSiteID(1); // TODO check: 0 is apparently not a valid site number, per the spec + entityID.setApplicationID(1); + entityID.setEntityID(2); + + // Set the entity type. SISO has a big list of enumerations, so that by + // specifying various numbers we can say this is an M1A2 American tank, + // the USS Enterprise, and so on. We'll make this a tank. There is a + // separate project elsehwhere in this project that implements DIS + // enumerations in C++ and Java, but to keep things simple we just use + // numbers here. + EntityType entityType = espdu.getEntityType(); + entityType.setEntityKind((short) 1); // Platform (vs lifeform, munition, sensor, etc.) + entityType.setCountry(225); // USA + entityType.setDomain((short) 1); // Land (vs air, surface, subsurface, space) + entityType.setCategory((short) 1); // Tank + entityType.setSubcategory((short) 1); // M1 Abrams + entityType.setSpecific((short) 3); // M1A2 Abrams + + Set<InetAddress> broadcastAddresses; + // Loop through sending N ESPDUs + try + { + System.out.println("Sending " + NUMBER_TO_SEND + " ESPDU packets to " + destinationIp.toString()); + + for (int index = 0; index < NUMBER_TO_SEND; index++) { + // DIS time is a pain in the uh, neck. DIS time units are 2^31-1 units per + // hour, and time is set to DIS time units from the top of the hour. + // This means that if you start sending just before the top of the hour + // the time units can roll over to zero as you are sending. The receivers + // (escpecially homegrown ones) are often not able to detect rollover + // and may start discarding packets as dupes or out of order. We use + // an NPS timestamp here, hundredths of a second since the start of the + // year. The DIS standard for time is often ignored in the wild; I've seen + // people use Unix time (seconds since 1970) and more. Or you can + // just stuff idx into the timestamp field to get something that is monotonically + // increasing. + + // Note that timestamp is used to detect duplicate and out of order packets. + // That means if you DON'T change the timestamp, many implementations will simply + // discard subsequent packets that have an identical timestamp. Also, if they + // receive a PDU with an timestamp lower than the last one they received, they + // may discard it as an earlier, out-of-order PDU. So it is a good idea to + // update the timestamp on ALL packets sent. + // An alterative approach: actually follow the standard. It's a crazy concept, + // but it might just work. + int timestamp = disTime.getDisAbsoluteTimestamp(); + espdu.setTimestamp(timestamp); + + // Set the position of the entity in the world. DIS uses a cartesian + // coordinate system with the origin at the center of the earth, the x + // axis out at the equator and prime meridian, y out at the equator and + // 90 deg east, and z up and out the north pole. To place an object on + // the earth's surface you also need a model for the shape of the earth + // (it's not a sphere.) All the fancy math necessary to do this is in + // the SEDRIS SRM package. There are also some one-off formulas for + // doing conversions from, for example, lat/lon/altitude to DIS coordinates. + // Here we use those one-off formulas. + // Modify the position of the object. This will send the object a little + // due east by adding some to the longitude every iteration. Since we + // are on the Pacific coast, this sends the object east. Assume we are + // at zero altitude. In other worlds you'd use DTED to determine the + // local ground altitude at that lat/lon, or you'd just use ground clamping. + // The x and y values will change, but the z value should not. + //lon = lon + (double)((double)idx / 100000.0); + //System.out.println("lla=" + lat + "," + lon + ", 0.0"); + double direction = Math.pow((double) (-1.0), (double) (index)); + lon = lon + (direction * 0.00006); + System.out.println(lon); + + double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 1.0); + Vector3Double location = espdu.getEntityLocation(); + location.setX(disCoordinates[0]); + location.setY(disCoordinates[1]); + location.setZ(disCoordinates[2]); + System.out.println("lat, lon:" + lat + ", " + lon); + System.out.println("DIS coord:" + disCoordinates[0] + ", " + disCoordinates[1] + ", " + disCoordinates[2]); + + // Optionally, we can do some rotation of the entity + /* + Orientation orientation = espdu.getEntityOrientation(); + float psi = orientation.getPsi(); + psi = psi + idx; + orientation.setPsi(psi); + orientation.setTheta((float)(orientation.getTheta() + idx /2.0)); + */ + // You can set other ESPDU values here, such as the velocity, acceleration, + // and so on. + // Marshal out the espdu object to a byte array, then send a datagram + // packet with that data in it. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + espdu.marshal(dos); + + FirePdu fire = new FirePdu(); + byte[] fireArray = fire.marshal(); + + // The byte array here is the packet in DIS format. We put that into a + // datagram and send it. + byte[] data = baos.toByteArray(); + + broadcastAddresses = getBroadcastAddresses(); + Iterator iterator = broadcastAddresses.iterator(); + while (iterator.hasNext()) + { + InetAddress broadcast = (InetAddress) iterator.next(); + System.out.println("Sending broadcast datagram packet to " + broadcast); + DatagramPacket packet = new DatagramPacket(data, data.length, broadcast, port); + socket.send(packet); + // TODO experiment with these! 8) + packet = new DatagramPacket(fireArray, fireArray.length, broadcast, port); // alternate + socket.send(packet); + } + + // Send every 1 sec. Otherwise all this will be all over in a fraction of a second. + Thread.sleep(1000); // msec + + location = espdu.getEntityLocation(); + + System.out.println("Espdu #" + index + " EID=[" + entityID.getSiteID()+ "," + entityID.getApplicationID()+ "," + entityID.getEntityID()+ "]"); + System.out.println(" DIS coordinates location=[" + location.getX() + "," + location.getY() + "," + location.getZ() + "]"); + double c[] = {location.getX(), location.getY(), location.getZ()}; + double lla[] = CoordinateConversions.xyzToLatLonDegrees(c); +// debug: System.out.println(" Location (lat/lon/alt): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]"); + } + } + catch (IOException | InterruptedException e) + { + System.out.println("Problem with DisExamplesOpenDis7.EspduSender, see exception trace:"); + System.out.println(e); + } + } + + /** + * A number of sites get all snippy about using 255.255.255.255 for a + * broadcast address; it trips their security software and they kick you off + * their network. (Comcast, NPS.) This determines the broadcast address for + * all connected interfaces, based on the IP and subnet mask. If you have a + * dual-homed host it will return a broadcast address for both. If you have + * some VMs running on your host this will pick up the addresses for those + * as well--e.g. running VMWare on your laptop with a local IP this will also + * pick up a 192.168 address assigned to the VM by the host OS. + * + * @return set of all broadcast addresses + */ + public static Set<InetAddress> getBroadcastAddresses() + { + Set<InetAddress> broadcastAddresses = new HashSet<>(); + Enumeration interfaces; + + try { + interfaces = NetworkInterface.getNetworkInterfaces(); + + while (interfaces.hasMoreElements()) + { + NetworkInterface anInterface = (NetworkInterface) interfaces.nextElement(); + + if (anInterface.isUp()) + { + Iterator iterator = anInterface.getInterfaceAddresses().iterator(); + while (iterator.hasNext()) + { + InterfaceAddress anAddress = (InterfaceAddress) iterator.next(); + if ((anAddress == null || anAddress.getAddress().isLinkLocalAddress())) + { + continue; + } + + //System.out.println("Getting broadcast address for " + anAddress); + InetAddress broadcastAddress = anAddress.getBroadcast(); + if (broadcastAddress != null) + { + broadcastAddresses.add(broadcastAddress); + } + } + } + } + } + catch (SocketException e) + { + System.out.println("Problem with DisExamplesOpenDis7.EspduSender.getBroadcastAddresses(), see exception trace:"); + System.out.println(e); + } + return broadcastAddresses; + } +} diff --git a/examples/src/OpenDis7Examples/EspduTerminalLog.txt b/examples/src/OpenDis7Examples/EspduTerminalLog.txt new file mode 100644 index 0000000000000000000000000000000000000000..1925747ec59f0115aad9587ca9efe3b43d85a204 --- /dev/null +++ b/examples/src/OpenDis7Examples/EspduTerminalLog.txt @@ -0,0 +1,93 @@ +Invocation instructions: +1. run/debug EspduReceiver.java (since sender does not block, be ready to listen) +2. run/debug EspduSender.java + +Program responses: + +=================================================== +run: +DisExamples.EspduSender started... send 5 ESPDUs, initial index=0 +Sending 5 ESPDU packets to /239.1.2.3 +-121.87693999999999 +lat, lon:36.595517, -121.87693999999999 +DIS coord:-2707488.3677768684, -4353666.735244379, 3781450.3202754413 +Sending broadcast datagram packet to /127.255.255.255 +Sending broadcast datagram packet to /172.20.83.255 +Espdu #0 EID=[1,1,2] + DIS coordinates location=[-2707488.3677768684,-4353666.735244379,3781450.3202754413] +-121.877 +lat, lon:36.595517, -121.877 +DIS coord:-2707492.9269245286, -4353663.899966802, 3781450.3202754413 +Sending broadcast datagram packet to /127.255.255.255 +Sending broadcast datagram packet to /172.20.83.255 +Espdu #1 EID=[1,1,2] + DIS coordinates location=[-2707492.9269245286,-4353663.899966802,3781450.3202754413] +-121.87693999999999 +lat, lon:36.595517, -121.87693999999999 +DIS coord:-2707488.3677768684, -4353666.735244379, 3781450.3202754413 +Sending broadcast datagram packet to /127.255.255.255 +Sending broadcast datagram packet to /172.20.83.255 +Espdu #2 EID=[1,1,2] + DIS coordinates location=[-2707488.3677768684,-4353666.735244379,3781450.3202754413] +-121.877 +lat, lon:36.595517, -121.877 +DIS coord:-2707492.9269245286, -4353663.899966802, 3781450.3202754413 +Sending broadcast datagram packet to /127.255.255.255 +Sending broadcast datagram packet to /172.20.83.255 +Espdu #3 EID=[1,1,2] + DIS coordinates location=[-2707492.9269245286,-4353663.899966802,3781450.3202754413] +-121.87693999999999 +lat, lon:36.595517, -121.87693999999999 +DIS coord:-2707488.3677768684, -4353666.735244379, 3781450.3202754413 +Sending broadcast datagram packet to /127.255.255.255 +Sending broadcast datagram packet to /172.20.83.255 +Espdu #4 EID=[1,1,2] + DIS coordinates location=[-2707488.3677768684,-4353666.735244379,3781450.3202754413] +BUILD SUCCESSFUL (total time: 5 seconds) + +=================================================== +run: +DisExamples.EspduReceiver started... +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.EntityStatePdu EID:[1, 1, 2] Location in DIS coordinates: [-2707488.3677768684, -4353666.735244379, 3781450.3202754413] +Bundle size is 1 +got PDU of type: edu.nps.moves.dis.FirePdu + +=================================================== + diff --git a/examples/src/OpenDis7Examples/PduReceiver.java b/examples/src/OpenDis7Examples/PduReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..216525b3c10199a4948753101fb2687ea7dbda25 --- /dev/null +++ b/examples/src/OpenDis7Examples/PduReceiver.java @@ -0,0 +1,81 @@ +package OpenDis7Examples; + +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 PduReceiver +{ + public static final int DEFAULT_MULTICAST_PORT = PduSender.DEFAULT_MULTICAST_PORT; + public static final String DEFAULT_MULTICAST_ADDRESS = 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("DisExamplesOpenDis7.PduReceiver started..."); + if (args.length == 2) + { + socket = new MulticastSocket (Integer.parseInt(args[0])); + address = InetAddress.getByName(args[1]); + } + else + { + System.out.println("Usage: PduReceiver <port> <multicast group>"); + System.out.println("Default: PduReceiver " + 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) + { + short currentPduType = pdu.getPduType(); + String currentPduTypeName = pdu.getClass().getName(); + short currentProtocolFamilyID = pdu.getProtocolFamily(); + String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName(); + + StringBuilder message = new StringBuilder(); + message.append("received DIS PDU: "); + message.append("pduType "); + if (currentPduType < 10) + message.append(" "); + message.append(currentPduType).append(" ").append(currentPduTypeName); + message.append(", protocolFamily ").append(currentProtocolFamilyID); + message.append(" ").append(currentPduFamilyName); + 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 DisExamplesOpenDis7.PduReceiver, see exception trace:"); + System.out.println(e); + } + finally + { + System.out.println("DisExamplesOpenDis7.PduReceiver complete."); + } + } +} diff --git a/examples/src/OpenDis7Examples/PduReceiverTerminalLog.txt b/examples/src/OpenDis7Examples/PduReceiverTerminalLog.txt new file mode 100644 index 0000000000000000000000000000000000000000..dccf843e373d7da9e0c7f9c874e0cae0203c4938 --- /dev/null +++ b/examples/src/OpenDis7Examples/PduReceiverTerminalLog.txt @@ -0,0 +1,4 @@ +run: +DisExamples.PduReceiver started... + + diff --git a/examples/src/OpenDis7Examples/PduSender.java b/examples/src/OpenDis7Examples/PduSender.java new file mode 100644 index 0000000000000000000000000000000000000000..ec4e975b2e08a2096d4b6e9621e1ed374713d6fa --- /dev/null +++ b/examples/src/OpenDis7Examples/PduSender.java @@ -0,0 +1,458 @@ +package OpenDis7Examples; + +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 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 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() + { + System.out.println("DisExamplesOpenDis7.PduSender started..."); + try { + List<Pdu> generatedPdusList = new ArrayList<>(); + + // Loop through all the enumerated PDU types, create a PDU for each type, + // add that PDU to generatedPdusList, and 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 + { + case OTHER: // 0 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); // TODO explain + break; + + case ENTITY_STATE: //1 + aPdu = new EntityStatePdu(); + EntityStatePdu espdu = (EntityStatePdu) aPdu; + EntityMarking entityMarking = new EntityMarking (); + entityMarking.setCharacters(Byte.valueOf("0")); // 11 characters max? + // TODO entityMarking.setCharactersString("PduSender"); // 11 characters max + + espdu.setMarking(entityMarking); + Vector3Double espduLocation = new Vector3Double(); + espduLocation.setX(1.0); + espduLocation.setY(2.0); + espduLocation.setZ(3.0); + espdu.setEntityLocation(espduLocation); + // 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(); + 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 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); + // aPdu = new ResupplyCancelPdu(); // TODO exception + 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 ElectronicEmissionsPdu(); // TODO misnamed as 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 IFF: // 28 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new IffPdu(); // TODO misnamed as IFFPdu(); + break; + + case UNDERWATER_ACOUSTIC: // 29 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new UnderwaterAcousticPdu(); + break; + + case SUPPLEMENTAL_EMISSION_ENTITY_STATE: // 30 + aPdu = new SeesPdu(); // TODO misnamed as SupplementalEmission/EntityStatePdu(); + break; + + case INTERCOM_SIGNAL: // 31 + aPdu = new IntercomSignalPdu(); + break; + + case INTERCOM_CONTROL: // 32 + aPdu = new IntercomControlPdu(); + break; + + case AGGREGATE_STATE: // 33 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new AggregateStatePdu(); + break; + + case ISGROUPOF: // 34 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new IsGroupOfPdu(); + break; + + case TRANSFER_OWNERSHIP: // 35 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new TransferOwnershipPdu(); + break; + + case ISPARTOF: // 36 + aPdu = new IsPartOfPdu(); + break; + + case MINEFIELD_STATE: // 37 + aPdu = new MinefieldStatePdu(); + break; + + case MINEFIELD_QUERY: // 38 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new MinefieldQueryPdu(); + break; + + case MINEFIELD_DATA: // 39 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new MinefieldDataPdu(); + break; + + case MINEFIELD_RESPONSE_NACK: // 40 + aPdu = new MinefieldResponseNackPdu(); // TODO misnamed as MinefieldResponseNACKPdu(); + break; + + case ENVIRONMENTAL_PROCESS: // 41 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new EnvironmentalProcessPdu(); + break; + + case GRIDDED_DATA: // 42 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// 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 TSPI: // 46 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); + // aPdu = new TspiPdu(); // TODO misnamed as TSPIPdu(); also extends LiveEntityFamilyPdu but cannot be cast to class Pdu + break; + + case APPEARANCE: // 47 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); + // aPdu = new AppearancePdu(); // TODO extends LiveEntityFamilyPdu but cannot be cast to class Pdu + break; + + case ARTICULATED_PARTS: // 48 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); + // aPdu = new ArticulatedPartsPdu(); // TODO extends LiveEntityFamilyPdu but cannot be cast to class Pdu + break; + + case LE_FIRE: // 49 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); + // aPdu = new LEFirePdu(); // TODO extends LiveEntityFamilyPdu but cannot be cast to class Pdu + break; + + case LE_DETONATION: // 50 + System.out.println ("DISPDUType." + pdu.name() + " not supported"); + // aPdu = new LEDetonationPdu(); // TODO extends LiveEntityFamilyPdu but cannot be cast to class Pdu + break; + + case CREATE_ENTITY_R: // 51 + aPdu = new CreateEntityReliablePdu(); // TODO misnamed as CreateEntityRPdu(); + break; + + case REMOVE_ENTITY_R: // 52 + aPdu = new RemoveEntityReliablePdu(); // TODO misnamed as RemoveEntityRPdu(); + break; + + case START_RESUME_R: // 53 + aPdu = new StartResumeReliablePdu(); // TODO misnamed as StartResumeRPdu(); + break; + + case STOP_FREEZE_R: // 54 + aPdu = new StopFreezeReliablePdu(); // TODO misnamed as StopFreezeRPdu(); + break; + + case ACKNOWLEDGE_R: // 55 + aPdu = new AcknowledgeReliablePdu(); // TODO misnamed as AcknowledgeRPdu(); + break; + + case ACTION_REQUEST_R: // 56 + aPdu = new ActionRequestReliablePdu(); // TODO misnamed as ActionRequestRPdu(); + break; + + case ACTION_RESPONSE_R: // 57 + aPdu = new ActionResponseReliablePdu(); // TODO misnamed as ActionResponseRPdu(); + break; + + case DATA_QUERY_R: // 58 + aPdu = new DataQueryReliablePdu(); // TODO misnamed as DataQueryRPdu(); + break; + + case SET_DATA_R: // 59 + aPdu = new SetDataReliablePdu(); // TODO misnamed as SetDataRPdu(); + break; + + case DATA_R: // 60 + aPdu = new DataReliablePdu(); // TODO misnamed as DataRPdu(); + break; + + case EVENT_REPORT_R: // 61 + aPdu = new EventReportReliablePdu(); // TODO misnamed as EventReportRPdu(); + break; + + case COMMENT_R: // 62 + aPdu = new CommentReliablePdu(); // TODO misnamed as CommentRPdu(); + break; + + case RECORD_R: // 63 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new RecordReliablePdu(); // TODO misnamed as RecordRPdu(); + break; + + case SET_RECORD_R: // 64 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new SetRecordReliablePdu(); // TODO misnamed as SetRecordRPdu(); + break; + + case RECORD_QUERY_R: // 65 + aPdu = new RecordQueryReliablePdu(); // TODO misnamed as RecordQueryRPdu(); + 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 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new InformationOperationsActionPdu(); + break; + + case INFORMATION_OPERATIONS_REPORT: // 71 + System.out.println ("DISPDUType." + pdu.name() + " not supported, throws exception"); +// aPdu = new InformationOperationsReportPdu(); + break; + + case ATTRIBUTE: // 72 + aPdu = new AttributePdu(); + break; + + // TODO unused?? + case COMMENT: + aPdu = new CommentPdu(); + CommentPdu newCommentPdu = (CommentPdu)aPdu; + VariableDatum newVariableDatum = new VariableDatum(); + // etc. see Garrett and Pete's code + break; + + default: + System.out.print("PDU " + pdu.getValue() + " " + pdu + " not supported, created or sent "); + + // code generation block for this class follow: + // 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 + } + } + System.out.println("Send the " + generatedPdusList.size() + " PDUs we created..."); + + // Send the PDUs we created + 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); + System.out.println("Sent PDU of type " + aPdu.getClass().getName()); + } + // write the PDUs out to an XML file. + //PduContainer container = new PduContainer(); + //container.setPdus(generatedPdus); + //container.marshallToXml("examplePdus.xml"); + } + catch (IOException e) + { + System.out.println(e); + } + } + + public static void main(String args[]) + { + if (args.length == 2) + { + PduSender sender = new PduSender(Integer.parseInt(args[0]), args[1]); + sender.run(); + } + else + { + System.out.println("Usage: PduSender <port> <multicast group>"); + System.out.println("Default: PduSender " + DEFAULT_MULTICAST_PORT + " " + DEFAULT_MULTICAST_ADDRESS); + PduSender sender = new PduSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); + sender.run(); + } + } +} diff --git a/examples/src/OpenDis7Examples/PduSenderTerminalLog.txt b/examples/src/OpenDis7Examples/PduSenderTerminalLog.txt new file mode 100644 index 0000000000000000000000000000000000000000..2b4f3fb4546b1375e0bdcb04f042a68e42c301c9 --- /dev/null +++ b/examples/src/OpenDis7Examples/PduSenderTerminalLog.txt @@ -0,0 +1,85 @@ +Invocation instructions: +1. run/debug PduSender.java + +Program response: + +=================================================== + +Usage: PduSender <port> <multicast group> +Default: PduSender 3000 239.1.2.3 +DisExamplesOpenDis7.PduSender started... +DISPDUType.OTHER not supported +DISPDUType.RESUPPLY_CANCEL not supported, throws exception +DISPDUType.IFF not supported, throws exception +DISPDUType.UNDERWATER_ACOUSTIC not supported, throws exception +DISPDUType.AGGREGATE_STATE not supported, throws exception +DISPDUType.ISGROUPOF not supported, throws exception +DISPDUType.TRANSFER_OWNERSHIP not supported, throws exception +DISPDUType.MINEFIELD_QUERY not supported, throws exception +DISPDUType.MINEFIELD_DATA not supported, throws exception +DISPDUType.ENVIRONMENTAL_PROCESS not supported, throws exception +DISPDUType.GRIDDED_DATA not supported, throws exception +DISPDUType.TSPI not supported +DISPDUType.APPEARANCE not supported +DISPDUType.ARTICULATED_PARTS not supported +DISPDUType.LE_FIRE not supported +DISPDUType.LE_DETONATION not supported +DISPDUType.RECORD_R not supported, throws exception +DISPDUType.SET_RECORD_R not supported, throws exception +DISPDUType.INFORMATION_OPERATIONS_ACTION not supported, throws exception +DISPDUType.INFORMATION_OPERATIONS_REPORT not supported, throws exception +Send the 53 PDUs we created... +Sent PDU of type edu.nps.moves.dis7.EntityStatePdu +Sent PDU of type edu.nps.moves.dis7.FirePdu +Sent PDU of type edu.nps.moves.dis7.DetonationPdu +Sent PDU of type edu.nps.moves.dis7.CollisionPdu +Sent PDU of type edu.nps.moves.dis7.ServiceRequestPdu +Sent PDU of type edu.nps.moves.dis7.ResupplyOfferPdu +Sent PDU of type edu.nps.moves.dis7.ResupplyReceivedPdu +Sent PDU of type edu.nps.moves.dis7.RepairCompletePdu +Sent PDU of type edu.nps.moves.dis7.RepairResponsePdu +Sent PDU of type edu.nps.moves.dis7.CreateEntityPdu +Sent PDU of type edu.nps.moves.dis7.RemoveEntityPdu +Sent PDU of type edu.nps.moves.dis7.StartResumePdu +Sent PDU of type edu.nps.moves.dis7.StopFreezePdu +Sent PDU of type edu.nps.moves.dis7.AcknowledgePdu +Sent PDU of type edu.nps.moves.dis7.ActionRequestPdu +Sent PDU of type edu.nps.moves.dis7.ActionResponsePdu +Sent PDU of type edu.nps.moves.dis7.DataQueryPdu +Sent PDU of type edu.nps.moves.dis7.SetDataPdu +Sent PDU of type edu.nps.moves.dis7.DataPdu +Sent PDU of type edu.nps.moves.dis7.EventReportPdu +Sent PDU of type edu.nps.moves.dis7.CommentPdu +Sent PDU of type edu.nps.moves.dis7.ElectronicEmissionsPdu +Sent PDU of type edu.nps.moves.dis7.DesignatorPdu +Sent PDU of type edu.nps.moves.dis7.TransmitterPdu +Sent PDU of type edu.nps.moves.dis7.SignalPdu +Sent PDU of type edu.nps.moves.dis7.ReceiverPdu +Sent PDU of type edu.nps.moves.dis7.SeesPdu +Sent PDU of type edu.nps.moves.dis7.IntercomSignalPdu +Sent PDU of type edu.nps.moves.dis7.IntercomControlPdu +Sent PDU of type edu.nps.moves.dis7.IsPartOfPdu +Sent PDU of type edu.nps.moves.dis7.MinefieldStatePdu +Sent PDU of type edu.nps.moves.dis7.MinefieldResponseNackPdu +Sent PDU of type edu.nps.moves.dis7.PointObjectStatePdu +Sent PDU of type edu.nps.moves.dis7.LinearObjectStatePdu +Sent PDU of type edu.nps.moves.dis7.ArealObjectStatePdu +Sent PDU of type edu.nps.moves.dis7.CreateEntityReliablePdu +Sent PDU of type edu.nps.moves.dis7.RemoveEntityReliablePdu +Sent PDU of type edu.nps.moves.dis7.StartResumeReliablePdu +Sent PDU of type edu.nps.moves.dis7.StopFreezeReliablePdu +Sent PDU of type edu.nps.moves.dis7.AcknowledgeReliablePdu +Sent PDU of type edu.nps.moves.dis7.ActionRequestReliablePdu +Sent PDU of type edu.nps.moves.dis7.ActionResponseReliablePdu +Sent PDU of type edu.nps.moves.dis7.DataQueryReliablePdu +Sent PDU of type edu.nps.moves.dis7.SetDataReliablePdu +Sent PDU of type edu.nps.moves.dis7.DataReliablePdu +Sent PDU of type edu.nps.moves.dis7.EventReportReliablePdu +Sent PDU of type edu.nps.moves.dis7.CommentReliablePdu +Sent PDU of type edu.nps.moves.dis7.RecordQueryReliablePdu +Sent PDU of type edu.nps.moves.dis7.CollisionElasticPdu +Sent PDU of type edu.nps.moves.dis7.EntityStateUpdatePdu +Sent PDU of type edu.nps.moves.dis7.DirectedEnergyFirePdu +Sent PDU of type edu.nps.moves.dis7.EntityDamageStatusPdu +Sent PDU of type edu.nps.moves.dis7.AttributePdu +BUILD SUCCESSFUL (total time: 1 second) diff --git a/examples/src/OpenDis7Examples/README.md b/examples/src/OpenDis7Examples/README.md new file mode 100644 index 0000000000000000000000000000000000000000..89d90724bc17cff98112589508dcace4ba72345a --- /dev/null +++ b/examples/src/OpenDis7Examples/README.md @@ -0,0 +1,7 @@ +# DIS Protocol Examples using Open-DIS-Java Library v7 + +TODO under development! + +Course examples using the [Open-DIS-Java](https://github.com/open-dis/open-dis-java) library are presented in this package. + +See [specifications](../../../specifications) directory to obtain reference copies of DIS and RPRFOM standards.