diff --git a/projects/Assignments/homework2/YamashitaDeMouraEntity.java b/projects/Assignments/homework2/YamashitaDeMouraEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..4ad6f592bbf1e02c349d4e9e8e0478e8cb0f85b1 --- /dev/null +++ b/projects/Assignments/homework2/YamashitaDeMouraEntity.java @@ -0,0 +1,99 @@ + +/** + * 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; + } + + + +} diff --git a/projects/Assignments/homework2/YamashitaDeMouraMulticastReceiver.java b/projects/Assignments/homework2/YamashitaDeMouraMulticastReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..8cb018ba6f006609a3668ff1a1b524520598f5b9 --- /dev/null +++ b/projects/Assignments/homework2/YamashitaDeMouraMulticastReceiver.java @@ -0,0 +1,77 @@ + +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); + } + } +} diff --git a/projects/Assignments/homework2/YamashitaDeMouraMulticastSender.java b/projects/Assignments/homework2/YamashitaDeMouraMulticastSender.java new file mode 100644 index 0000000000000000000000000000000000000000..725621ddb603e6c7aa43e7e1e328a681897e1dc2 --- /dev/null +++ b/projects/Assignments/homework2/YamashitaDeMouraMulticastSender.java @@ -0,0 +1,85 @@ + +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); + } + } +} diff --git a/projects/Assignments/homework2/YamashitaDeMouraOutput.jpg b/projects/Assignments/homework2/YamashitaDeMouraOutput.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ea97fa9be246b0dadc110583cbd7032f1bac3886 Binary files /dev/null and b/projects/Assignments/homework2/YamashitaDeMouraOutput.jpg differ