Skip to content
Snippets Groups Projects
YamashitaDeMouraMulticastSender.java 3.82 KiB
package MV3500Cohort2018JanuaryMarch.homework2;


import java.io.*;
import java.net.*;
import java.util.Random;

/**
 * MV3500
 * 
 * Multicast Sender
 * 
 * @author Douglas Yamashita de Moura
 * @version 20180227
 * 
 */
public class YamashitaDeMouraMulticastSender
{
    /**
     * Default constructor to silence javadoc warning
     * @see <a href="https://stackoverflow.com/questions/4488716/java-default-constructor" target="_blank">StackOverflow: Java default constructor</a>
     */
    public YamashitaDeMouraMulticastSender ()
    {
        // default initializations
    }
    /** socket parameter of interest */
    public static final String MULTICAST_ADDRESS = "239.1.2.15";
    /** socket parameter of interest */
    public static final int DESTINATION_PORT = 1717;
    static final int TTL = 10;
    
    
    /** Program invocation, execution starts here
     * @param args command-line arguments  */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) 
    {
        YamashitaDeMouraMulticastEntity entityA = new YamashitaDeMouraMulticastEntity("Alpha", 0, 0, 0, 2);
        YamashitaDeMouraMulticastEntity entityB = new YamashitaDeMouraMulticastEntity("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);
        }
    }
}