package MV3500Cohort2018JanuaryMarch.homework2;

/**
 * This assignment uses the code from MV3500 example "multicastExample1"  
 * modifications were made to way the packet is read to facilitate the double  
 * instead of the float.
 * 
 * @author codyt
 */
import java.io.*;
import java.net.*;


/** Program description goes here */
public class TackettMultiCastReceiver
{
    /**
     * 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 TackettMultiCastReceiver ()
    {
        // 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;
    /** How many routers can be crossed */
    static final int TTL = 10; 
    
    /** run the program
     * @param args command-line arguments, string parameters (unused) */
    @SuppressWarnings("deprecation")
    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);
                double firstNumber = dis.readDouble();
                double secondNumber = dis.readDouble();
                
                System.out.println("Number received: " + count + " X Pos: " + firstNumber + " Y Pos: " + secondNumber);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    
}