Skip to content
Snippets Groups Projects
Commit 40f13627 authored by coltonfetterolf's avatar coltonfetterolf
Browse files

hw 4 complete

parent efe77cc1
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2019JulySeptember.homework4.Fetterolf;
import java.net.*;
import java.io.*;
import edu.nps.moves.dis7.*;
import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.util.*;
import java.util.ArrayList;
public class AllPduReceiver
{
public static final int DEFAULT_MULTICAST_PORT = AllPduSender.DEFAULT_MULTICAST_PORT;
public static final String DEFAULT_MULTICAST_ADDRESS = AllPduSender.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.AllPduReceiver started...");
if (args.length == 2) {
socket = new MulticastSocket(Integer.parseInt(args[0]));
address = InetAddress.getByName(args[1]);
}
else {
System.out.println("Usage: AllPduReceiver <port> <multicast group>");
System.out.println("Default: AllPduReceiver " + 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)
{
DISPDUType currentPduType = pdu.getPduType(); //short currentPduType = pdu.getPduType();
String currentPduTypeName = pdu.getClass().getName();
DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short currentProtocolFamilyID = pdu.getProtocolFamily();
String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName();
StringBuilder message = new StringBuilder();
message.append("received DIS PDU ");
if (currentPduType.getValue() < 10)
message.append(" "); // column spacing
message.append(currentPduType.getValue());
String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
message.append(" " ).append(currentPduTypePadded);
String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
message.append(" (protocolFamily ").append(currentProtocolFamilyID);
// message.append(" ").append(currentPduFamilyName); // class name is also available
message.append(")");
System.out.println(message.toString());
switch (currentPduType) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType
{
case COMMENT:
CommentPdu commentPdu = (CommentPdu)pdu; // cast to precise type
ArrayList<VariableDatum> payloadList = (ArrayList)commentPdu.getVariableDatums();
for (VariableDatum variableDatum : payloadList)
{
String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String
System.out.println("\"" + nextComment + "\"");
}
}
}
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.AllPduReceiver, see exception trace:");
System.out.println(e);
}
finally {
System.out.println("DisExamplesOpenDis7.AllPduReceiver complete.");
}
}
}
package MV3500Cohort2019JulySeptember.homework4.Fetterolf;
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 AllPduSender
{
/** 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 AllPduSender(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.AllPduSender started...");
try
{
System.out.println("Generate PDUs and note issues, if any...");
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 ("*** Note: DISPDUType." + pdu.name() + " not supported"); // TODO why was this received?
// nothing to send
break;
case ENTITY_STATE: // 1
aPdu = new EntityStatePdu();
EntityStatePdu espdu = (EntityStatePdu) aPdu;
EntityMarking entityMarking = new EntityMarking ();
entityMarking.setCharacters("AllPduSender".getBytes()); //entityMarking.setCharacters(Byte.valueOf("0")); // 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;
//added in different PDUs from the jar file.
case COLLISION:
aPdu = new CollisionPdu();
break;
case TRANSMITTER:
aPdu = new TransmitterPdu();
break;
case ACKNOWLEDGE:
aPdu = new AcknowledgePdu();
break;
case RECEIVER:
aPdu = new ReceiverPdu();
break;
case COMMENT:
// aPdu = new CommentPdu(); // default for this switch logic
// see Garrett Loffelman and Pete Severson's code for OpenDis version 4 example
// https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/tree/master/assignments/src/MV3500Cohort2018JulySeptember/projects/LoeffelmanSeverson
CommentPdu newCommentPdu = new CommentPdu();
ArrayList<VariableDatum> payloadList = new ArrayList<VariableDatum>();
ArrayList<String> commentsList = new ArrayList<>();
commentsList.add("Hello CommentPDU");
commentsList.add("Here is a new message");
if (!commentsList.isEmpty())
System.out.println("Preparing CommentPDU:");
for (String comment : commentsList)
{
VariableDatum newVariableDatum = new VariableDatum();
newVariableDatum.setVariableDatumValue (comment.getBytes()); // conversion
newVariableDatum.setVariableDatumLength(comment.getBytes().length * 8); // bits, not bytes, 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);
aPdu = newCommentPdu; // hand off for sending
break;
//commented out this warning message
// default:
// System.out.println("*** Warning: PDU " + pdu.getValue() + " " + pdu + " not supported, created or sent ");
// code generation block for this class follows:
// 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
}
}
// Send the PDUs we created
System.out.println("Send the " + generatedPdusList.size() + " 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);
try
{
aPdu.marshal(dos);
buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT);
socket.send(packet);
try {
Thread.sleep(100L);
} catch (InterruptedException ex) {
}
String currentPduTypeValuePadded = String.format("%2s", aPdu.getPduType().getValue());
String currentPduTypePadded = String.format("%-34s", aPdu.getPduType()); // - indicates right padding of whitespace
System.out.print ("Sent DIS PDU " + currentPduTypeValuePadded + " " + currentPduTypePadded );
System.out.println(" of type " + aPdu.getClass().getName());
}
catch (Exception ex) {
System.out.println("Marshaling error" + ex);
}
}
// 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)
{
AllPduSender sender = new AllPduSender(Integer.parseInt(args[0]), args[1]);
sender.run();
}
else
{
System.out.println("Usage: AllPduSender <port> <multicast group>");
System.out.println("Default: AllPduSender " + DEFAULT_MULTICAST_PORT + " " + DEFAULT_MULTICAST_ADDRESS);
AllPduSender sender = new AllPduSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS);
sender.run();
}
System.out.println("DisExamplesOpenDis7.AllPduSender complete.");
}
}
/**
* Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*/
package MV3500Cohort2019JulySeptember.homework4.Fetterolf;
import edu.nps.moves.dis7.util.playerrecorder.Recorder;
import java.io.IOException;
import java.util.Scanner;
/**
* PduSaver.java created on Aug 21, 2019
* MOVES Institute Naval Postgraduate School, Monterey, CA, USA www.nps.edu
*
* @author Mike Bailey, jmbailey@nps.edu
* @version $Id$
*/
public class PduListenerSaver
{
private final static String DEFAULT_OUTPUTDIR = "pduLog";
private final static String MCAST_ADDR = "239.1.2.3";
private final static int DIS_PORT = 3000;
private enum mystate
{
RUNNING,
PAUSED;
}
public static void main(String[] args)
{
String outDir = DEFAULT_OUTPUTDIR;
String mcast = MCAST_ADDR;
int port = DIS_PORT;
System.out.println("DisExamplesOpenDis7.PduListenerSaver started...");
switch (args.length) {
case 0:
break;
case 1:
outDir = args[0];
break;
case 3:
outDir = args[0];
mcast = args[1];
port = Integer.parseInt(args[2]);
break;
default:
System.err.println("Usage: PduListener() or PduListener(\"outputdir\") or PduListener(\"outputdir\",\"multicast address\", ipPort");
System.exit(1);
}
System.out.println("Beginning pdu save to directory " + outDir);
try {
Recorder recorder = new Recorder(outDir, mcast, port);
recorder.startResume();
mystate state = mystate.RUNNING;
System.out.println("* recorder.startResume(), state=RUNNING, recording in progress...");
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Warning: you must quit when complete, otherwise recorded PDUs are lost!");
System.out.println("Type p/enter to pause, r/enter to resume, q/enter to stop recording, save and quit");
String line = scan.nextLine();
if (line.equalsIgnoreCase("p") && state == mystate.RUNNING) {
recorder.stopPause();
state = mystate.PAUSED;
System.out.println("* recorder.stopPause(), state=PAUSED, recording paused...");
}
else if (line.equalsIgnoreCase("r") && state == mystate.PAUSED) {
recorder.startResume();
state = mystate.RUNNING;
System.out.println("* recorder.startResume(), state=RUNNING, recording in progress...");
}
else if (line.equalsIgnoreCase("q")) {
recorder.end();
System.out.println("* recorder.end(), recording complete.");
break;
}
}
System.out.println("Ending pdu save to "+recorder.getLogFile());
}
catch (IOException ex) {
System.err.println("*** Exception: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage());
}
}
}
package MV3500Cohort2019JulySeptember.homework4.Fetterolf;
// originally edu.nps.moves.examples.ReceiverPerformance
import java.net.*;
import edu.nps.moves.dis.*;
import edu.nps.moves.disutil.PduFactory;
import java.io.IOException;
public class PduReceiver {
public static final int MULTICAST_PORT = 3000;
public static final String MULTICAST_GROUP = "239.1.2.3";
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("DisExamples.PduReceiver started...");
socket = new MulticastSocket(MULTICAST_PORT);
address = InetAddress.getByName(MULTICAST_GROUP);
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 DisExamples.PduReceiver, see exception trace:");
System.out.println(e);
} finally {
System.out.println("DisExamples.PduReceiver complete.");
}
}
}
package MV3500Cohort2019JulySeptember.homework4.Fetterolf;
import java.io.*;
import java.net.*;
import java.util.*;
import edu.nps.moves.dis.*;
import edu.nps.moves.disenum.*;
import edu.nps.moves.examples.ClassNameComparator;
/**
* 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("DisExamples.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 (PduType pdu : PduType.values()) {
Pdu aPdu = null; // edu.​nps .​moves.​dis,PDU superclass for all PDUs,
switch (pdu) // using enumeration values from edu.nps.moves.disenum.*
{
case ENTITY_STATE:
aPdu = new EntityStatePdu();
EntityStatePdu espdu = (EntityStatePdu) aPdu;
Marking marking = new Marking();
marking.setCharactersString("PduSender"); // 11 characters max
espdu.setMarking(marking);
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 COMMENT:
aPdu = new CommentPdu();
CommentPdu newCommentPdu = (CommentPdu) aPdu;
VariableDatum newVariableDatum = new VariableDatum();
// etc. see Garrett and Pete's code
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) {
generatedPdusList.add(aPdu);
}
}
// Sort the created PDUs by class name, if desired
// Collections.sort(generatedPdusList, new ClassNameComparator());
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();
}
}
}
!Begin!Beginning of DIS capture file, Pdusave1.dislog.
AAAAAAAAAAA=,BwABAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/AAAAAAAABAAAAAAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQWxsUGR1U2VuZGUAAAAA
AAAAACQr8hg=,BwAbBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
!End!End of DIS capture file, Pdusave1.dislog.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment