Skip to content
Snippets Groups Projects
Commit bf58167d authored by brutzman's avatar brutzman
Browse files

remove duplicates

parent 9a28948d
No related branches found
No related tags found
No related merge requests found
/*
* 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.projects.LoeffelmanSeverson;
import edu.nps.moves.dis.CommentPdu;
import edu.nps.moves.dis.EntityID;
import edu.nps.moves.dis.EntityStatePdu;
import edu.nps.moves.dis.Pdu;
import edu.nps.moves.dis.OneByteChunk;
import edu.nps.moves.dis.VariableDatum;
import edu.nps.moves.dis.Vector3Double;
import edu.nps.moves.disutil.PduFactory;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import edu.nps.moves.disenum.PduType;
import java.util.ArrayList;
/**
*
* @author garrettloeffelman
*/
public class LoeffelmanSeversonDISImageReceiver {
public static final int MULTICAST_PORT = 3000;
public static final String MULTICAST_GROUP = "239.1.2.15";
public static final boolean USE_FAST_ESPDU = false;
public static void main(String args[])
{
Image image = null;
PduFactory factory;
MulticastSocket socket = null;
InetAddress address = null;
DatagramPacket packet;
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
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
{
//buffer needs to be resized to be able to accept image sizes
byte buffer[] = new byte[100000]; // typical MTU size
packet = new DatagramPacket(buffer, buffer.length); // reset
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 we receive a comment PDU, we are going to look for a picture specifically
if(pdu.getPduTypeEnum() == PduType.COMMENT){
//Set up data structures, get the list of variableDatums, and
//get the first and only VariableDatum, and the arrayList
//of OneByteChunks from the variableDatum
CommentPdu cPdu = (CommentPdu)pdu;
ArrayList<VariableDatum> payload = (ArrayList)cPdu.getVariableDatums();
VariableDatum payloadWrapper = payload.get(0);
ArrayList<OneByteChunk> oBC = (ArrayList)payloadWrapper.getVariableData();
byte[] imageByte = new byte[100000];
//Loop through the arrayList of OneByteChunks and
//populate the byte array representation of the image
for(int i = 0; i < oBC.size(); i++){
imageByte[i] = oBC.get(i).getOtherParameters()[0];
}
//Convert that byte array into an input stream and into
//an image using a buffered image reader.
ByteArrayInputStream bais = new ByteArrayInputStream(imageByte);
BufferedImage bimage = ImageIO.read(bais);
image = bimage;
//Display the image using java swing utilities
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);
}
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...");
}
}
catch (IOException e)
{
System.out.println("Problem with DisExamples.PduReceiver, see exception trace:");
System.out.println(e);
}
finally
{
System.out.println("DisExamples.PduReceiver complete.");
}
}
}
/*
* 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.projects.LoeffelmanSeverson;
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.EntityID;
import edu.nps.moves.dis.EntityStatePdu;
import edu.nps.moves.dis.EntityType;
import edu.nps.moves.dis.FirePdu;
import edu.nps.moves.dis.Pdu;
import edu.nps.moves.dis.VariableDatum;
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.Vector3Double;
import edu.nps.moves.disenum.PduType;
import edu.nps.moves.disutil.CoordinateConversions;
import edu.nps.moves.disutil.DisTime;
import edu.nps.moves.dis.OneByteChunk;
import edu.nps.moves.examples.ClassNameComparator;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.MalformedURLException;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author garrettloeffelman
*/
public class LoeffelmanSeversonDISImageSender {
/** Default multicast group address we send on. */
public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.15";
/** Default multicast port used, matches Wire-shark DIS capture default */
public static final int DEFAULT_MULTICAST_PORT = 3000;
private int port;
InetAddress multicastAddress;
public LoeffelmanSeversonDISImageSender (int port, String multicast) {
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
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 {
URL url1 = new URL("https://cdn1.iconfinder.com/data/icons/cute-monkey-emoticon/595/MONKEY_EMOTICON-05-512.png");
URL url2 = new URL("https://images.all-free-download.com/images/graphicthumb/yammi_banana_99225.jpg");
List<Pdu> generatedPdus = new ArrayList<>();
// 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:
//Initialize PDU and image
aPdu = new CommentPdu();
CommentPdu cPdu = (CommentPdu)aPdu;
BufferedImage image = ImageIO.read(url1);
//Convert image to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] buffer;
buffer = baos.toByteArray();
//Instaniate data structures for holding bytes
ArrayList<VariableDatum> payload = new ArrayList<VariableDatum>();
ArrayList<OneByteChunk> payloadWrapper = new ArrayList<OneByteChunk>();
VariableDatum variableDatum = new VariableDatum();
//Loop through, create OneByteChunk arrays of size 1
//and place them in ArrayList of OneByteChunks
for(int i = 0; i < buffer.length; i++){
OneByteChunk oBC = new OneByteChunk();
byte[] oneBite = new byte[1];
oneBite[0] = buffer[i];
oBC.setOtherParameters(oneBite);
payloadWrapper.add(oBC);
}
System.out.println("Sending Picture");
//Add the arrayList of OneByteChunks to the variable datum
variableDatum.setVariableData(payloadWrapper);
//Add the variableDatum to the arraylist
payload.add(variableDatum);
//Set the variableDatum for the PDU to be sent
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());
}
// 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) {
LoeffelmanSeversonDISImageSender sender = new LoeffelmanSeversonDISImageSender(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);
LoeffelmanSeversonDISImageSender sender = new LoeffelmanSeversonDISImageSender(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS);
sender.run();
}
}
}
To run this program run the receiver first, and then run the sender.
The Sender will send a picture specified by the URL at the top of the try block
You can change the url to a different image, but keep in mind the image must be smaller then
100,000 bytes or 100 kb.
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