diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderA.java b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderA.java new file mode 100644 index 0000000000000000000000000000000000000000..5f7a6eaad88a5889e521e41dcafa862e01493a6f --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderA.java @@ -0,0 +1,158 @@ + +package MV3500Cohort2018JulySeptember.FinalProject.CainThomersonFinal; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.util.Properties; +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis.*; +import edu.nps.moves.disutil.CoordinateConversions; +import edu.nps.moves.disutil.DisTime; + +/** + * + * @author danielcain + */ +public class C_T_EspduSenderA { + + public static final int NUMBER_TO_SEND = 5000; + + public enum NetworkMode{UNICAST, MULTICAST, BROADCAST}; + + /** default multicast group we send on */ + public static final String DEFAULT_MULTICAST_GROUP="239.1.2.4"; + + /** Port we send on */ + public static final int DIS_DESTINATION_PORT = 3000; + + +public static void main(String args[]) +{ + /** an entity state pdu */ + EntityStatePdu espdu = new EntityStatePdu(); + MulticastSocket socket = null; + DisTime disTime = DisTime.getInstance(); + int alternator = -1; + // ICBM coordinates for my office + double lat = 36.595517; + double lon = -121.877000; + int port = DIS_DESTINATION_PORT; + NetworkMode mode = NetworkMode.MULTICAST; + InetAddress destinationIp = null; + + try + { + destinationIp = InetAddress.getByName(DEFAULT_MULTICAST_GROUP); + } + catch(Exception 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(DIS_DESTINATION_PORT); + + // Where we send packets to, the destination IP address + if(destinationIpString != null) + { + destinationIp = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + 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(Exception e) + { + System.out.println("Unable to initialize networking. Exiting."); + System.out.println(e); + System.exit(-1); + } + espdu.setExerciseID((short)1); + EntityID eid = espdu.getEntityID(); + eid.setSite(100); // 0 is apparently not a valid site number, per the spec + eid.setApplication(1); + eid.setEntity(2); + 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.setSpec((short)3); // M1A2 Abrams + try + { + System.out.println("Sending " + NUMBER_TO_SEND + " ESPDU packets to " + destinationIp.toString()); + for(int idx = 0; idx < NUMBER_TO_SEND; idx++) + { + eid.setEntity(idx+1); + int ts = disTime.getDisAbsoluteTimestamp(); + espdu.setTimestamp(ts); + double direction = Math.pow((double)(-1.0), (double)(idx)); + lon = lon + (direction * 0.00006); + double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 1.0); + Vector3Double location = espdu.getEntityLocation(); + location.setX(disCoordinates[0]); + location.setY(disCoordinates[1]); + location.setZ(disCoordinates[2]); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + espdu.marshal(dos); + byte[] data = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(DEFAULT_MULTICAST_GROUP), DIS_DESTINATION_PORT); + socket.send(packet); + Thread.sleep(3000); + location = espdu.getEntityLocation(); + System.out.println("Espdu #" + idx + " EID=[" + eid.getSite() + "," + eid.getApplication() + "," + eid.getEntity() + "]"); + //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); + System.out.println(" Location (lat/lon/alt): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]"); + } + } + catch(Exception e) + { + System.out.println(e); + } + +} + + +} + diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderB.java b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderB.java new file mode 100644 index 0000000000000000000000000000000000000000..f8d00539078ec173e76ab78165de7dcacab814e7 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduSenderB.java @@ -0,0 +1,212 @@ + +package MV3500Cohort2018JulySeptember.FinalProject.CainThomersonFinal; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis.*; +import edu.nps.moves.disutil.CoordinateConversions; +import edu.nps.moves.disutil.DisTime; + + +/** + * + * @author danielcain + */ +public class C_T_EspduSenderB { +public static final int NUMBER_TO_SEND = 5000; + public enum NetworkMode{UNICAST, MULTICAST, BROADCAST}; + /** default multicast group we send on */ + public static final String DEFAULT_MULTICAST_GROUP="239.1.2.3"; + /** Port we send on */ + public static final int DIS_DESTINATION_PORT = 2800; + +public static void main(String args[]) +{ + /** an entity state pdu */ + EntityStatePdu espdu = new EntityStatePdu(); + MulticastSocket socket = null; + DisTime disTime = DisTime.getInstance(); + int alternator = -1; + + // ICBM coordinates for my office + double lat = 36.595517; + double lon = -121.877000; + + int port = DIS_DESTINATION_PORT; + NetworkMode mode = NetworkMode.MULTICAST; + InetAddress destinationIp = null; + try + { + destinationIp = InetAddress.getByName(DEFAULT_MULTICAST_GROUP); + } + catch(Exception 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(DIS_DESTINATION_PORT); + + // Where we send packets to, the destination IP address + if(destinationIpString != null) + { + destinationIp = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + 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(Exception e) + { + System.out.println("Unable to initialize networking. Exiting."); + System.out.println(e); + System.exit(-1); + } + + + espdu.setExerciseID((short)1); + EntityID eid = espdu.getEntityID(); + eid.setSite(200); // 0 is apparently not a valid site number, per the spec + eid.setApplication(1); + eid.setEntity(2); + 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.setSpec((short)3); // M1A2 Abrams + + try + { + + System.out.println("Sending " + NUMBER_TO_SEND + " ESPDU packets to " + destinationIp.toString()); + for(int idx = 0; idx < NUMBER_TO_SEND; idx++) + { + eid.setEntity(idx+1); + + int ts = disTime.getDisAbsoluteTimestamp(); + espdu.setTimestamp(ts); + double direction = Math.pow((double)(-1.0), (double)(idx)); + lon = lon + (direction * 0.00006); + double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 1.0); + Vector3Double location = espdu.getEntityLocation(); + location.setX(disCoordinates[0]); + location.setY(disCoordinates[1]); + location.setZ(disCoordinates[2]); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + espdu.marshal(dos); + byte[] data = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(DEFAULT_MULTICAST_GROUP), DIS_DESTINATION_PORT); + socket.send(packet); + Thread.sleep(3000); + location = espdu.getEntityLocation(); + System.out.println("Espdu #" + idx + " EID=[" + eid.getSite() + "," + eid.getApplication() + "," + eid.getEntity() + "]"); + //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); + System.out.println(" Location (lat/lon/alt): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]"); + } + } + catch(Exception e) + { + System.out.println(e); + } + +} + + /** + * A number of sites get all snippy about using 255.255.255.255 for a bcast + * address; it trips their security software and they kick you off their + * network. (Comcast, NPS.) This determines the bcast address for all + * connected interfaces, based on the IP and subnet mask. If you have + * a dual-homed host it will return a bcast address for both. If you have + * some VMs running on your host this will pick up the addresses for those + * as well--eg 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 bcast addresses + */ + public static Set<InetAddress> getBroadcastAddresses() + { + Set<InetAddress> bcastAddresses = new HashSet<InetAddress>(); + Enumeration interfaces; + + try + { + interfaces = NetworkInterface.getNetworkInterfaces(); + + while(interfaces.hasMoreElements()) + { + NetworkInterface anInterface = (NetworkInterface)interfaces.nextElement(); + + if(anInterface.isUp()) + { + Iterator it = anInterface.getInterfaceAddresses().iterator(); + while(it.hasNext()) + { + InterfaceAddress anAddress = (InterfaceAddress)it.next(); + if((anAddress == null || anAddress.getAddress().isLinkLocalAddress())) + continue; + + //System.out.println("Getting bcast address for " + anAddress); + InetAddress abcast = anAddress.getBroadcast(); + if(abcast != null) + bcastAddresses.add(abcast); + } + } + } + + } + catch(Exception e) + { + e.printStackTrace(); + System.out.println(e); + } + + return bcastAddresses; + } + +} + + diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecA_SendB.java b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecA_SendB.java new file mode 100644 index 0000000000000000000000000000000000000000..f02c5bbf388bb8e2161364cea3fc1ad6fe60e2b3 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecA_SendB.java @@ -0,0 +1,144 @@ + +package MV3500Cohort2018JulySeptember.FinalProject.CainThomersonFinal; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis.*; +import edu.nps.moves.disutil.DisTime; +import edu.nps.moves.disutil.PduFactory; + +public class C_T_EspduTCP_RecA_SendB { + +public static final int NUMBER_TO_SEND = 5000; + public enum NetworkMode{UNICAST, MULTICAST, BROADCAST}; + /** default multicast group we send on */ + public static final String DEFAULT_MULTICAST_GROUP="239.1.2.3"; + public static final int MAX_PDU_SIZE = 8192; + public static final int TCP_DESTINATION_PORT = 2999; + /** Port we send on */ + public static final int DIS_DESTINATION_PORT = 2800; +/** 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 mcast. + * To determine bcast destination IP, use an online bcast address + * caclulator, for example http://www.remotemonitoringsystems.ca/broadcast.php + * If in mcast mode, a join() will be done on the mcast address. + * port: port used for both source and destination. + * @param args + */ +public static void main(String args[]) +{ + /** an entity state pdu */ + EntityStatePdu espdu = new EntityStatePdu(); + MulticastSocket socket = null; + DisTime disTime = DisTime.getInstance(); + int alternator = -1; + int port = DIS_DESTINATION_PORT; + NetworkMode mode = NetworkMode.MULTICAST; + InetAddress destinationIp = null; + PduFactory pduFactory = new PduFactory(); + try + { + destinationIp = InetAddress.getByName(DEFAULT_MULTICAST_GROUP); + } + catch(Exception e) + { + System.out.println(e + " Cannot create multicast address"); + System.exit(0); + } + Properties systemProperties = System.getProperties(); + String destinationIpString = systemProperties.getProperty("destinationIp"); + String portString = systemProperties.getProperty("port"); + String networkModeString = systemProperties.getProperty("networkMode"); // unicast or multicast or broadcast + try + { + + // Port we send to + if(portString != null) + port = Integer.parseInt(portString); + + socket = new MulticastSocket(DIS_DESTINATION_PORT); + + // Where we send packets to, the destination IP address + if(destinationIpString != null) + { + destinationIp = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + 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(Exception e) + { + System.out.println("Unable to initialize networking. Exiting."); + System.out.println(e); + System.exit(-1); + } + + try + { + int connectionCount = 0; + DatagramSocket ds = new DatagramSocket(TCP_DESTINATION_PORT); + + while(true){ + try + { + byte buffer[] = new byte[MAX_PDU_SIZE]; + DatagramPacket tcpPacket = new DatagramPacket(buffer, buffer.length); + ds.receive(tcpPacket); + connectionCount++; + System.out.println("Current PDUs transferred over TCP: "+ connectionCount); + List<Pdu> pduBundle = pduFactory.getPdusFromBundle(tcpPacket.getData()); + //System.out.println("Bundle size is " + pduBundle.size()); + Iterator it = pduBundle.iterator(); + + while(it.hasNext()){ + //System.out.println("Entity ID transferred: "); + Pdu aPdu = (Pdu)it.next(); + //System.out.print("got PDU of type: " + aPdu.getClass().getName()); + if(aPdu instanceof EntityStatePdu){ + EntityID eid = ((EntityStatePdu)aPdu).getEntityID(); + System.out.println("Entity ID transferred: "+eid.getEntity()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + aPdu.marshal(dos); + byte[] data = baos.toByteArray(); + DatagramPacket udpPacket = new DatagramPacket(data, data.length, InetAddress.getByName(DEFAULT_MULTICAST_GROUP), DIS_DESTINATION_PORT); + socket.send(udpPacket); + } + } + } + catch(Exception e) + { + System.out.println(e); + } + } + } + catch(Exception e) + { + System.out.println(e); + } + +} + + +} diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecB_SendA.java b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecB_SendA.java new file mode 100644 index 0000000000000000000000000000000000000000..57b43054e7c66eef16847c9ff75b96fef869974e --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/CainThomersonFinal/C_T_EspduTCP_RecB_SendA.java @@ -0,0 +1,150 @@ + +package MV3500Cohort2018JulySeptember.FinalProject.CainThomersonFinal; + +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis.*; +import edu.nps.moves.disutil.DisTime; +import edu.nps.moves.disutil.PduFactory; + +public class C_T_EspduTCP_RecB_SendA { + +public static final int NUMBER_TO_SEND = 5000; + + public enum NetworkMode{UNICAST, MULTICAST, BROADCAST}; + public static final int MAX_PDU_SIZE = 8192; + /** default multicast group we send on */ + public static final String DEFAULT_MULTICAST_GROUP="239.1.2.4"; + public static final int TCP_DESTINATION_PORT = 2998; + /** Port we send on */ + public static final int DIS_DESTINATION_PORT = 3000; + +public static void main(String args[]) +{ + /** an entity state pdu */ + EntityStatePdu espdu = new EntityStatePdu(); + MulticastSocket socket = null; + DisTime disTime = DisTime.getInstance(); + int alternator = -1; + + int port = DIS_DESTINATION_PORT; + NetworkMode mode = NetworkMode.MULTICAST; + InetAddress destinationIp = null; + PduFactory pduFactory = new PduFactory(); + + try + { + destinationIp = InetAddress.getByName(DEFAULT_MULTICAST_GROUP); + } + catch(Exception 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(DIS_DESTINATION_PORT); + + // Where we send packets to, the destination IP address + if(destinationIpString != null) + { + destinationIp = InetAddress.getByName(destinationIpString); + } + + // Type of transport: unicast, broadcast, or multicast + 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(Exception e) + { + System.out.println("Unable to initialize networking. Exiting."); + System.out.println(e); + System.exit(-1); + } + + try + { + int connectionCount = 0; + DatagramSocket ds = new DatagramSocket(TCP_DESTINATION_PORT); + + while(true){ + try + { + byte buffer[] = new byte[MAX_PDU_SIZE]; + DatagramPacket tcpPacket = new DatagramPacket(buffer, buffer.length); + ds.receive(tcpPacket); + //Socket clientConnection = serverSocket.accept(); + connectionCount++; + System.out.println("Current PDUs transferred over TCP: "+ connectionCount); + List<Pdu> pduBundle = pduFactory.getPdusFromBundle(tcpPacket.getData()); + //System.out.println("Bundle size is " + pduBundle.size()); + Iterator it = pduBundle.iterator(); + + while(it.hasNext()){ + //System.out.println("Entity ID transferred: "); + Pdu aPdu = (Pdu)it.next(); + //System.out.print("got PDU of type: " + aPdu.getClass().getName()); + if(aPdu instanceof EntityStatePdu){ + EntityID eid = ((EntityStatePdu)aPdu).getEntityID(); + System.out.println("Entity ID transferred: "+eid.getEntity()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + aPdu.marshal(dos); + byte[] data = baos.toByteArray(); + DatagramPacket udpPacket = new DatagramPacket(data, data.length, InetAddress.getByName(DEFAULT_MULTICAST_GROUP), DIS_DESTINATION_PORT); + socket.send(udpPacket); + } + } + } + catch(Exception e) + { + System.out.println(e); + } + } + } + catch(Exception e) + { + System.out.println(e); + } + +} + + + +}