Skip to content
Snippets Groups Projects
Commit 1b9b18c9 authored by John Furr's avatar John Furr
Browse files

Merge origin/master

parents fa13e7fd e1e6c541
No related branches found
No related tags found
No related merge requests found
Showing
with 913 additions and 14 deletions
build.xml.data.CRC32=ba18aca8 build.xml.data.CRC32=3397fc52
build.xml.script.CRC32=f4fc0484 build.xml.script.CRC32=f4fc0484
build.xml.stylesheet.CRC32=8064a381@1.80.1.48 build.xml.stylesheet.CRC32=8064a381@1.80.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=ba18aca8 nbproject/build-impl.xml.data.CRC32=3397fc52
nbproject/build-impl.xml.script.CRC32=6a5b91f6 nbproject/build-impl.xml.script.CRC32=6a5b91f6
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
...@@ -73,6 +73,9 @@ public class PduSender ...@@ -73,6 +73,9 @@ public class PduSender
case COMMENT: case COMMENT:
aPdu = new CommentPdu(); aPdu = new CommentPdu();
CommentPdu newCommentPdu = (CommentPdu)aPdu;
VariableDatum newVariableDatum = new VariableDatum();
// etc. see Garrett and Pete's code
break; break;
case FIRE: case FIRE:
......
...@@ -31,7 +31,7 @@ public class TcpExample3Server { ...@@ -31,7 +31,7 @@ public class TcpExample3Server {
// Notice that it is outside the loop; ServerSocket // Notice that it is outside the loop; ServerSocket
// needs to be made only once. // needs to be made only once.
System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first System.out.println("TcpExample3Server has really started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
// Server is up and waiting (i.e. "blocked" or paused) // Server is up and waiting (i.e. "blocked" or paused)
...@@ -62,7 +62,7 @@ public class TcpExample3Server { ...@@ -62,7 +62,7 @@ public class TcpExample3Server {
// //
// Why is the first IP/port the same, while the second set has // Why is the first IP/port the same, while the second set has
// different ports? // different ports?
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " System.out.println("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))"); + remoteAddress.toString() + ", " + remotePort + " ))");
// Notice the use of flush() and close(). Without // Notice the use of flush() and close(). Without
......
...@@ -7,9 +7,9 @@ Program responses: ...@@ -7,9 +7,9 @@ Program responses:
=================================================== ===================================================
run: run:
TcpExample3Server has started... TcpExample3Server has started...
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49239 )) TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49239 ))
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49240 )) TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49240 ))
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49241 )) TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49241 ))
[etc.] [etc.]
[kill process to exit] [kill process to exit]
...@@ -30,3 +30,7 @@ The message the server sent was This was written by the server ...@@ -30,3 +30,7 @@ The message the server sent was This was written by the server
TcpExample3Client creating socket... TcpExample3Client creating socket...
================================================== ==================================================
[etc.] [etc.]
You must kill this process to get it to stop, each is in an infinite loop.
If you kill the server, that will kill the client automatically when the connection drops.
Final Project Notes:
We constructed code to communicate between parties via DIS over a LAN (Local Area Network).
The PDU and Espdu example files were the genesis of the two files.
To run our code you need to open two main classes
C_T_EspduRequestingUnit.java, and C_T_EspduSupplyingUnit.java
In order to get the program to run properly, execute:
First: C_T_EspduSupplyingUnit.java
Next: C_T_EspduRequestingUnit.java
Each class has the capability to both sender and receive packages. They are listening
for specific types of PDU packets, and will then send a response accordingly.
Below is the output from running the code-
-------------------- EspduSupplyingUnit output -------------------------------
SUPPLYER UNIT:
Program started
SUPPYER UNIT:
Listening
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.EntityStatePdu
EID:[1, 1, 2]
Entity Location: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413]
Entity Type: 1
SENDING ENTITY STATE PACKET
EID=[2,2,2]
Entity Location: [-1534805.049164391, 585379.2141969808, 6142318.841117044]
Entity Type: 7
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.ServiceRequestPdu
Requesting Entity ID:[1, 1, 2]
Service Type Requested: [1, Resupply]
SENDING PACKET
Packet Type: Resupply Offer
Receiving Entity ID: [1, 1, 2]
Supplying Entity ID: [2, 2, 2]
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.AcknowledgePdu
SENDING PACKET
Packet Type: Acknowledge
SUPPYER UNIT:
Listening
-------------------- EspduRequestingUnit output ------------------------------
REQUESTING UNIT:
Program started
REQUESTING UNIT:
Listening
SENDING ENTITY STATE INFORMATION
EID=[1,1,2]
Entity Location: [-2707492.9269245286, -4353663.899966802, 3781450.3202754413]
Entity Type: 1
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.EntityStatePdu
EID:[2, 2, 2]
Entity Location: [-1534805.049164391, 585379.2141969808, 6142318.841117044]
Entity Type: 7
SENDING SERVICE REQUEST
Entity Requesting:[1, 1, 2]
Service Type Requested: [1, Resupply]
SENDING SERVICE REQUEST
Entity Requesting:[1, 1, 2]
Service Type Requested: [1, Resupply]
SENDING SERVICE REQUEST
Entity Requesting:[1, 1, 2]
Service Type Requested: [1, Resupply]
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.ResupplyOfferPdu
Supplying Unit: [2, 2, 2]
Requesting Unit: [1, 1, 2]
SENDING ACKNOWLEDGE
SENDING ACKNOWLEDGE
SENDING ACKNOWLEDGE
RECEIVED PACKET
Packet Type: edu.nps.moves.dis.AcknowledgePdu
REQUEST COMPLETED - SOCKET IS NOW CLOSED
\ No newline at end of file
/*
* 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.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
{
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(pdu.getPduTypeEnum() == PduType.COMMENT){
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];
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);
}
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.FinalProject.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:
aPdu = new CommentPdu();
CommentPdu cPdu = (CommentPdu)aPdu;
BufferedImage image = ImageIO.read(url1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] buffer;
buffer = baos.toByteArray();
ArrayList<VariableDatum> payload = new ArrayList<VariableDatum>();
ArrayList<OneByteChunk> payloadWrapper = new ArrayList<OneByteChunk>();
VariableDatum variableDatum = new VariableDatum();
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");
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());
}
// 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();
}
}
}
File added
/*
* 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.FinalProjects.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.FinalProjects.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();
}
}
}
...@@ -5,3 +5,10 @@ Create a dedicated subdirectory for each individual or team project. ...@@ -5,3 +5,10 @@ Create a dedicated subdirectory for each individual or team project.
Example: `SmithJones` Example: `SmithJones`
See the [course syllabus](../../../../MV3500NetworkedGraphicsSyllabus2018JulySeptember.pdf) for details on how to document your project. See the [course syllabus](../../../../MV3500NetworkedGraphicsSyllabus2018JulySeptember.pdf) for details on how to document your project.
Typical final project deliverables:
* README.md providing a basic description of goals, running the project, files, etc.
* Source code, screen shots, console text log, and any other assets
* Powerpoint presentation, video capture (if you want)
These deliverables have great value going forward. Questions welcome, keep going!
...@@ -55,7 +55,7 @@ public class DemchkoServer { ...@@ -55,7 +55,7 @@ public class DemchkoServer {
// //
// Why is the first IP/port the same, while the second set has // Why is the first IP/port the same, while the second set has
// different ports? // different ports?
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " System.out.println("Socket pair: (( " + localAddress.toString().trim() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))"); + remoteAddress.toString() + ", " + remotePort + " ))");
InputStream is = clientConnection.getInputStream(); InputStream is = clientConnection.getInputStream();
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package MV3500Cohort2018JulySeptember.homework3.Ayres_Homework3; package MV3500Cohort2018JulySeptember.homework3.AyresDemchkoHomework3;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
...@@ -17,7 +17,7 @@ import java.net.UnknownHostException; ...@@ -17,7 +17,7 @@ import java.net.UnknownHostException;
* *
* @author kjayr * @author kjayr
*/ */
public class AyresMulticastReceiver { public class AyresDemchkoMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15"; public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717; public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */ /** How many routers can be crossed */
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package MV3500Cohort2018JulySeptember.homework3.Ayres_Homework3; package MV3500Cohort2018JulySeptember.homework3.AyresDemchkoHomework3;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
...@@ -15,7 +15,7 @@ import java.net.MulticastSocket; ...@@ -15,7 +15,7 @@ import java.net.MulticastSocket;
* *
* @author kjayr * @author kjayr
*/ */
public class AyresMulticastSender { public class AyresDemchkoMulticastSender {
public static final String MULTICAST_ADDRESS = "239.1.2.15"; public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717; public static final int DESTINATION_PORT = 1717;
......
...@@ -18,12 +18,22 @@ auxiliary.show.customizer.message=<message> ...@@ -18,12 +18,22 @@ auxiliary.show.customizer.message=<message>
<source-folder> <source-folder>
<label>Networked Graphics MV3500</label> <label>Networked Graphics MV3500</label>
<location>.</location> <location>.</location>
<encoding>UTF-8</encoding> <encoding>windows-1252</encoding>
</source-folder>
<source-folder>
<label>lib</label>
<type>java</type>
<location>lib</location>
<encoding>windows-1252</encoding>
</source-folder> </source-folder>
</folders> </folders>
<ide-actions/> <ide-actions/>
<view> <view>
<items> <items>
<source-folder style="packages">
<label>lib</label>
<location>lib</location>
</source-folder>
<source-file> <source-file>
<location>build.xml</location> <location>build.xml</location>
</source-file> </source-file>
...@@ -32,7 +42,12 @@ auxiliary.show.customizer.message=<message> ...@@ -32,7 +42,12 @@ auxiliary.show.customizer.message=<message>
</view> </view>
<subprojects/> <subprojects/>
</general-data> </general-data>
<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1"/> <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
<compilation-unit>
<package-root>lib</package-root>
<source-level>1.5</source-level>
</compilation-unit>
</java-data>
<spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1"> <spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
<word>codebases</word> <word>codebases</word>
<word>DIS</word> <word>DIS</word>
...@@ -40,6 +55,7 @@ auxiliary.show.customizer.message=<message> ...@@ -40,6 +55,7 @@ auxiliary.show.customizer.message=<message>
<word>interoperability</word> <word>interoperability</word>
<word>interoperable</word> <word>interoperable</word>
<word>TENA</word> <word>TENA</word>
<word>Wikipedia</word>
</spellchecker-wordlist> </spellchecker-wordlist>
</configuration> </configuration>
</project> </project>
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