Skip to content
Snippets Groups Projects
Commit 5f1a4cc0 authored by codyt's avatar codyt
Browse files

Merge origin/master

parents 57fb8b0a 01feffc9
No related branches found
No related tags found
No related merge requests found
Showing
with 913 additions and 0 deletions
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.
*/
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author Michael
*/
public class BlankenbekerMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
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");
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
int count = 0;
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
count++;
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
float firstNumber = dis.readFloat();
float secondNumber = dis.readFloat();
float thirdNumber = dis.readFloat();
System.out.println("Number received: " + count + " Vehicle position is: (" + firstNumber + ", " + secondNumber+", "+thirdNumber+")");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
* 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.
*/
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author Michael
*/
public class BlankenbekerMulticastSender {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
Truck truck = new Truck();
while (true) {
truck.move();
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");
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
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
float x = truck.getX();
float y = truck.getY();
float z = truck.getZ();
dos.writeFloat(x);
dos.writeFloat(y);
dos.writeFloat(z);
byte[] buffer = baos.toByteArray();
// Put together a packet to send
// muticast group we are sending to--not a single host
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
// How fast does this go? Does UDP try to slow it down, or does
// this cause network problems? (hint: yes for an unlimited send
// rate, unlike TCP). How do you know on the receiving side
// that you haven't received a duplicate UDP packet, out of
// order packet, or dropped packet?
for(int idx = 0; idx < 1; idx++)
{
multicastSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent new multicast packet 1 of 1");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
\ No newline at end of file
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
*
* @author emilyconard
*/
public class ConardMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
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");
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
int count = 0;
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
count++;
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
float firstNumber = dis.readInt();
float secondNumber = dis.readInt();
float thirdNumber = dis.readInt();
System.out.println("Number received: " + count + " ID: " + firstNumber + " xcord: " + secondNumber + " ycord: "+ thirdNumber);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author emilyconard
*/
public class ConardMulticastSender {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
try
{
int ID = 27;
int xcord = 5;
int ycord = 7;
System.setProperty("java.net.preferIPv4Stack", "true");
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
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(ID);
dos.writeInt(xcord);
dos.writeInt(ycord);
byte[] buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
for(int idx = 0; idx < 100; idx++)
{
multicastSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent multicast packet " + idx + " of 100");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
File added
import java.io.*;
import java.net.*;
/**
*
* @author mcgredo
*/
public class SasalaMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
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");
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
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
float temperature = dis.readFloat();
String DTG = dis.readLine();
System.out.println("Time: " + DTG + " Temperature:" + temperature);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
import java.time.LocalDateTime;
/**
* Looks a lot like a UDP sender.
*
* @author mcgredo
*/
public class SasalaMulticastSender {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
public static void main(String[] args)
{
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");
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
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
for(int idx = 0; idx < 100; idx++)
{
double temperature = 100 * Math.random();
LocalDateTime LDT = LocalDateTime.now();
String time = LDT.toString();
dos.writeFloat((float) temperature);
dos.writeChars(time);
// Put together a packet to send
// muticast group we are sending to--not a single host
byte[] buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
multicastSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent multicast packet " + idx + " of 100");
baos.reset();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
* 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 mv3500code;
/**
*
* @author Michael
*/
public class Truck {
private float x;
private float y;
private float z;
public Truck(){
x = 0;
y=0;
z=0;
}
public void move(){
x+=1.5;
y+=1.2;
z-=.2;
}
/**
*
* @return
*/
public float getX(){
return x;
}
public float getY(){
return y;
}
public float getZ(){
return z;
}
}
/**
* MV3500
*
* Entity
*
* @author Douglas Yamashita de Moura
* @version 20180227
*
*/
public class YamashitaDeMouraEntity {
private String name;
private float x;
private float y;
private float z;
private float speed;
public YamashitaDeMouraEntity (String name, float x, float y, float z, float speed) {
this.name = name;
this.x = x;
this.y = y;
this.z = z;
this.speed = speed;
}
/**
* @return the x
*/
public float getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(float x) {
this.x = x;
}
/**
* @return the y
*/
public float getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(float y) {
this.y = y;
}
/**
* @return the z
*/
public float getZ() {
return z;
}
/**
* @param z the z to set
*/
public void setZ(float z) {
this.z = z;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the velocity
*/
public float getVelocity() {
return speed;
}
/**
* @param velocity the velocity to set
*/
public void setVelocity(float velocity) {
this.speed = velocity;
}
}
import java.io.*;
import java.net.*;
import java.util.Random;
/**
* MV3500
*
* Multicast Receiver
*
* @author Douglas Yamashita de Moura
* @version 20180227
*
*/
public class YamashitaDeMouraMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
public static final int TTL = 10;
public static void main(String[] args)
{
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT);
multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println(multicastAddress);
multicastSocket.joinGroup(multicastAddress);
int count = 0;
int randomReceiver = new Random().nextInt(100);
System.out.println("=== MULTICAST RECEIVER " + randomReceiver + " ===");
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
count++;
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
char entity = dis.readChar();
String name = "";
int nameLength = 0;
switch(entity) {
case 'A':
nameLength = 4;
name = "A";
break;
case 'B':
nameLength = 4;
name = "B";
break;
}
// Read name
for (int i = 0; i < nameLength; i++) {
name += dis.readChar();
}
// Read Position
float xPos = dis.readFloat();
float yPos = dis.readFloat();
float zPos = dis.readFloat();
System.out.println("Entity '" + name + "' with position (" + xPos +
", " + yPos + ", " + zPos +").");
System.out.println("Number received: " + count);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
import java.util.Random;
/**
* MV3500
*
* Multicast Sender
*
* @author Douglas Yamashita de Moura
* @version 20180227
*
*/
public class YamashitaDeMouraMulticastSender {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
public static final int TTL = 10;
public static void main(String[] args)
{
YamashitaDeMouraEntity entityA = new YamashitaDeMouraEntity("Alpha", 0, 0, 0, 2);
YamashitaDeMouraEntity entityB = new YamashitaDeMouraEntity("Bravo", 0, 0, 0, 3);
try
{
System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket multicastSocket = new MulticastSocket(1718);
multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println(multicastAddress);
multicastSocket.joinGroup(multicastAddress);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
Random randomProb = new Random();
System.out.println("=== MULTICAST SENDER ===");
for(int idx = 1; idx < 100; idx++)
{
int prob = randomProb.nextInt(5);
if(prob < 2) {
dos.writeChars(entityA.getName());
// Move only in x direction
float newPos = entityA.getX()+entityA.getVelocity();
entityA.setX(newPos);
dos.writeFloat(entityA.getX());
dos.writeFloat(entityA.getY());
dos.writeFloat(entityA.getZ());
System.out.println("Entity '" + entityA.getName() +
"' with position (" + entityA.getX() + ", " +
entityA.getY() + ", " + entityA.getZ() + ")");
} else if (prob < 5) {
dos.writeChars(entityB.getName());
dos.writeFloat(entityB.getX());
// Move only in y direction
float newPos = entityB.getY()+entityB.getVelocity();
entityB.setY(newPos);
dos.writeFloat(entityB.getY());
dos.writeFloat(entityB.getZ());
System.out.println("Entity '" + entityB.getName() +
"' with position (" + entityB.getX() + ", " +
entityB.getY() + ", " + entityB.getZ() + ")");
}
byte[] buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
multicastSocket.send(packet);
baos.reset();
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent multicast packet " + idx + " of 100");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
projects/Assignments/homework2/YamashitaDeMouraOutput.jpg

616 KiB

package edu.nps.moves.examples;
import java.io.*;
import java.net.*;
import java.util.*;
import edu.nps.moves.dis.*;
import edu.nps.moves.disenum.*;
/**
* 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.
*
* @author DMcG
* @version $Id:$
*/
public class PduSender {
public static final int PORT = 62040;
public static final String MULTICAST_ADDRESS = "239.1.2.3";
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 (Exception e) {
System.out.println("Unable to open socket");
}
}
public void run() {
try {
List<Pdu> generatedPdus = new ArrayList<Pdu>();
// 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) {
case ENTITY_STATE:
aPdu = new EntityStatePdu();
break;
case COMMENT:
aPdu = new CommentPdu();
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 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(MULTICAST_ADDRESS);
MulticastSocket socket = new MulticastSocket(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, 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 (Exception 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>");
}
}
}
# Homework Assignment 3 (draft)
1. Add X3D-Edit as Netbeans Plugin from https://savage.nps.edu/X3D-Edit/#Downloads
1. Checkout Open-DIS-Java library from https://github.com/open-dis/open-dis-java
1. Plan a track of interest, described in YournameREADME.md documentation file...
1. Modify example file OpenDisPduSender...
1. Generate PDUs...
1. Record PDU file using X3D-Edit...
1. Playback PDU file using X3D-Edit...
\ No newline at end of file
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