diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/AyresDemchkoProject/AyresDemchkoSender.java b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/AyresDemchkoProject/AyresDemchkoSender.java new file mode 100644 index 0000000000000000000000000000000000000000..2d18ffe5499683fddfb7c8b048eae1a2107dcb8a --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/FinalProject/AyresDemchkoProject/AyresDemchkoSender.java @@ -0,0 +1,246 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package MV3500Cohort2018JulySeptember.FinalProject.AyresDemchko; + +import edu.nps.moves.dis.AcknowledgePdu; +import edu.nps.moves.dis.ActionRequestPdu; +import edu.nps.moves.dis.CollisionPdu; +import edu.nps.moves.dis.CommentPdu; +import edu.nps.moves.dis.CreateEntityPdu; +import edu.nps.moves.dis.DetonationPdu; +import edu.nps.moves.dis.EntityStatePdu; +import edu.nps.moves.dis.FirePdu; +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 kjayr + */ +public class AyresDemchkoSender { +/** Default multicast group address we send on. */ + public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.15"; + 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; + + public AyresDemchkoSender (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); + } + } + + 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 < 100; 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 100"); + } + 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); + } + } + + public static void main(String args[]) + { + if (args.length == 2) { + AyresDemchkoSender sender = new AyresDemchkoSender(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); + AyresDemchkoSender sender = new AyresDemchkoSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); + sender.run(); + } + } +}