Skip to content
Snippets Groups Projects
Commit 01feffc9 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

Merge origin/master

parents f03ce3aa 3529bd0e
No related branches found
No related tags found
No related merge requests found
/**
* 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

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