diff --git a/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieReceiver.java b/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..2690a1c01f56a15f523985188881c8a82cdd6a7c --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieReceiver.java @@ -0,0 +1,151 @@ + + +package MV3500Cohort2021JulySeptember.projects.McneelyLeckieProject; + +import edu.nps.moves.dis.CommentPdu; +import edu.nps.moves.dis.OneByteChunk; +import edu.nps.moves.dis.Pdu; +import edu.nps.moves.dis.VariableDatum; +import edu.nps.moves.disenum.PduType; +import edu.nps.moves.disutil.PduFactory; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.util.ArrayList; + +/** + * + * @author Justin Mcneely + * @author Jacob Leckie + */ + + +public class McneelyLeckieReceiver { + + /** socket parameter of interest */ + public static final int MULTICAST_PORT = 3000; + /** socket parameter of interest */ + public static final String MULTICAST_GROUP = "239.1.2.15"; +// public static final String MULTICAST_GROUP = "192.168.1.247"; + /** socket parameter of interest */ + static final boolean USE_FAST_ESPDU = false; + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + @SuppressWarnings("deprecation") + public static void main(String[] args) + { + + PduFactory factory; + try + { + + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + //System.setProperty("java.net.preferIPv4Stack", "true"); + //System.out.println("DisExamples.PduReceiver started..."); + + /*MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here*/ + System.setProperty("java.net.preferIPv4Stack", "true"); + System.out.println("DisExamples.PduReceiver started..."); + MulticastSocket socket = new MulticastSocket (MULTICAST_PORT); + InetAddress address = InetAddress.getByName(MULTICAST_GROUP); + socket.joinGroup(address); + + factory = new PduFactory(); + + int count = 0; + int secondNumber = 67; + int thirdNumber = 108; + int forkliftsAvail = 12; + int forkliftsServ = 0; + + while(true) + { + + byte[] packetArray = new byte[1500]; + DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); + + socket.receive(packet); + + Pdu pdu = factory.createPdu (packet.getData()); + if (pdu != null) + { + short pduType = pdu.getPduType(); + String pduTypeName = pdu.getClass().getName(); + short protocolFamily = pdu.getProtocolFamily(); // TODO get string enumeration + + if(pdu.getPduTypeEnum() == PduType.COMMENT){ + CommentPdu cPdu = (CommentPdu)pdu; + @SuppressWarnings("unchecked") + ArrayList<VariableDatum> payload = (ArrayList)cPdu.getVariableDatums(); + VariableDatum payloadWrapper = payload.get(0); + @SuppressWarnings("unchecked") + ArrayList<OneByteChunk> oBC = (ArrayList)payloadWrapper.getVariableData(); + + //byte[] imageByte = new byte[100000]; + + /*for(int i = 0; i < oBC.size(); i++){ + imageByte[i] = oBC.get(i).getOtherParameters()[0]; + } + + ByteArrayInputStream bais = new ByteArrayInputStream(imageByte); + BufferedImage bimage = ImageIO.read(bais); + //image = bimage; + JFrame frame = new JFrame(); + frame.setSize(300, 300); + //JLabel label = new JLabel(new ImageIcon(image)); + //frame.add(label); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/ + } + count++; + + if(count > 1){ + forkliftsAvail-=1; + forkliftsServ+=1; + } + System.out.println("Marine Aircraft Logistics Squadron 29: Wolverines!"); + System.out.println("Order received: " + count + " NSN ID: 1874542689" + " Xrow: " + secondNumber + " Yshelf: "+ thirdNumber + + " Forklifts Availble: " + forkliftsAvail + " Forklifts in Service: " +forkliftsServ); + System.out.println("______________________________________________________"); + StringBuilder message = new StringBuilder(); + message.append("received DIS PDU: "); + message.append("pduType "); + if (pduType <= 10) + message.append(" "); + message.append(pduType).append(" ").append(pduTypeName); + message.append(", protocolFamily=").append(protocolFamily); + System.out.println(message.toString()); + } + else System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error..."); + + ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); + DataInputStream dis = new DataInputStream(bais); + //float firstNumber = dis.readInt(); + //float secondNumber = dis.readInt(); + //float thirdNumber = dis.readInt(); + + + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieSender.java b/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieSender.java new file mode 100644 index 0000000000000000000000000000000000000000..692c1403939d076e16d618a500f0b54a7b57a15f --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/projects/McneelyLeckieProject/McneelyLeckieSender.java @@ -0,0 +1,258 @@ + +package MV3500Cohort2021JulySeptember.projects.McneelyLeckieProject; + +import edu.nps.moves.dis.AcknowledgePdu; +import edu.nps.moves.dis.ActionRequestPdu; +import edu.nps.moves.dis.CommentPdu; +import edu.nps.moves.dis.CreateEntityPdu; +import edu.nps.moves.dis.EntityStatePdu; +import edu.nps.moves.dis.OneByteChunk; +import edu.nps.moves.dis.Pdu; +import edu.nps.moves.dis.RemoveEntityPdu; +import edu.nps.moves.dis.RepairCompletePdu; +import edu.nps.moves.dis.RepairResponsePdu; +import edu.nps.moves.dis.ResupplyCancelPdu; +import edu.nps.moves.dis.ResupplyOfferPdu; +import edu.nps.moves.dis.ResupplyReceivedPdu; +import edu.nps.moves.dis.ServiceRequestPdu; +import edu.nps.moves.dis.StartResumePdu; +import edu.nps.moves.dis.StopFreezePdu; +import edu.nps.moves.dis.VariableDatum; +import edu.nps.moves.disenum.PduType; +import edu.nps.moves.examples.ClassNameComparator; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + +/** + * + * @author Justin Mcneely + * @author Jacob Leckie + */ + + +public class McneelyLeckieSender { + + /** Default multicast group address we send on. */ + public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.15"; + /** socket parameter of interest */ + public static final int DESTINATION_PORT = 1717; + /** Default multicast port used, matches Wire-shark DIS capture default */ + public static final int DEFAULT_MULTICAST_PORT = 3000; + + + private int port; + InetAddress multicastAddress; + + /** Object constructor + * @param port TCP port of interest + * @param multicast address of interest */ + public McneelyLeckieSender (int port, String multicast) + { + try + { + System.setProperty("java.net.preferIPv4Stack", "true"); + this.port = port; + multicastAddress = InetAddress.getByName(multicast); + //MulticastSocket multicastSocket = new MulticastSocket(1718); + //multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + //multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + if (!multicastAddress.isMulticastAddress()) + { + System.out.println("Not a multicast address: " + multicast); + } + } + catch (UnknownHostException e) { + System.out.println("Unable to open socket: " + e); + } + } + + /** + * Run the program + */ + @SuppressWarnings("deprecation") + public void run() + { + System.out.println("DisExamples.PduSender started..."); + try { + + List<Pdu> generatedPdus = new ArrayList<>(); + MulticastSocket multicastSocket = new MulticastSocket(1718); + //multicastSocket.setTimeToLive(TTL); + //InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + + // Loop through all the enumerated PDU types, create a PDU for each type, + // and add that PDU to a list. + for (PduType pdu : PduType.values()) { + Pdu aPdu = null; + + switch (pdu) // using enumeration values from edu.nps.moves.disenum.* + { + case ENTITY_STATE: + aPdu = new EntityStatePdu(); + break; + + case COMMENT: + aPdu = new CommentPdu(); + CommentPdu cPdu = (CommentPdu)aPdu; + + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte[] buffer; + buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + System.out.println("Sending Request"); + for(int idx = 0; idx < 12; idx++) + { + multicastSocket.send(packet); + //Thread.sleep(1000); // Send 100, one per second + System.out.println("Sending Request"); + System.out.println("Sent multicast DIS PDU packet " + idx + " of 12"); + } + ArrayList<VariableDatum> payload = new ArrayList<VariableDatum>(); + ArrayList<OneByteChunk> payloadWrapper = new ArrayList<OneByteChunk>(); + VariableDatum variableDatum = new VariableDatum(); + + + + variableDatum.setVariableData(payloadWrapper); + + payload.add(variableDatum); + cPdu.setVariableDatums(payload); + break; + +// case FIRE: +// aPdu = new FirePdu(); +// break; +// +// case DETONATION: +// aPdu = new DetonationPdu(); +// break; +// +// case COLLISION: +// aPdu = new CollisionPdu(); +// break; + + case SERVICE_REQUEST: + aPdu = new ServiceRequestPdu(); + break; + + case RESUPPLY_OFFER: + aPdu = new ResupplyOfferPdu(); + break; + + case RESUPPLY_RECEIVED: + aPdu = new ResupplyReceivedPdu(); + break; + + case RESUPPLY_CANCEL: + aPdu = new ResupplyCancelPdu(); + break; + + case REPAIR_COMPLETE: + aPdu = new RepairCompletePdu(); + break; + + case REPAIR_RESPONSE: + aPdu = new RepairResponsePdu(); + break; + + case CREATE_ENTITY: + aPdu = new CreateEntityPdu(); + break; + + case REMOVE_ENTITY: + aPdu = new RemoveEntityPdu(); + break; + + case START_RESUME: + aPdu = new StartResumePdu(); + break; + + case STOP_FREEZE: + aPdu = new StopFreezePdu(); + break; + + case ACKNOWLEDGE: + aPdu = new AcknowledgePdu(); + break; + + case ACTION_REQUEST: + aPdu = new ActionRequestPdu(); + break; + + default: +// System.out.print("PDU of type " + pdu + " not supported, created or sent "); +// System.out.println(); + } + if (aPdu != null) + { + generatedPdus.add(aPdu); + } + } + + // Sort the created PDUs by class name + Collections.sort(generatedPdus, new ClassNameComparator()); + + // 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 < generatedPdus.size(); idx++) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + byte[] buffer; + + Pdu aPdu = generatedPdus.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()); + } + + } catch (IOException e) + { + System.out.println(e); + } + } + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String args[]) + { + if (args.length == 2) { + McneelyLeckieSender sender = new McneelyLeckieSender(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); + McneelyLeckieSender sender = new McneelyLeckieSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); + sender.run(); + } + } + +}