package OpenDis4Examples; 

import java.net.*;
import java.util.*;

import edu.nps.moves.disutil.*;

import edu.nps.moves.dis.*;
import java.io.IOException;

/**
 * Receives PDUs from the network in IEEE DIS format.
 * Adapted from OpenDIS library example package edu.nps.moves.examples
 *
 * @author DMcG
 * @version $Id:$
 */
public class EspduReceiver
{
    /** Default constructor */
    public EspduReceiver()
    {
        // default constructor
    }
    /** Max size of a PDU in binary format that we can receive. This is actually
     * somewhat outdated--PDUs can be larger--but this is a reasonable starting point.
     */
    private static final int MAX_PDU_SIZE = 8192;

    /** Default multicast group address we send on.
      * @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */
    private static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3";
	
    /** Default multicast port used, matches Wireshark DIS capture default
     * @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */
    private static final int    DEFAULT_MULTICAST_PORT    = 3000;

    /**
     * Program invocation, execution starts here
     * @param args command-line arguments
     */
    public static void main(String args[])
	{
		System.out.println("OpenDis4Examples.EspduReceiver started...");
		
        MulticastSocket socket;
        DatagramPacket packet;
        InetAddress address;
        PduFactory pduFactory = new PduFactory();

        try {
            // Specify the socket to receive data
            socket = new MulticastSocket(DEFAULT_MULTICAST_PORT);
            socket.setBroadcast(true);
       
            // address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
            // socket.joinGroup(address);

            while (true) // Loop infinitely, receiving datagrams
			{
                byte buffer[] = new byte[MAX_PDU_SIZE];
                packet = new DatagramPacket(buffer, buffer.length);

                socket.receive(packet);

                List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData());
                System.out.println("Bundle size is " + pduBundle.size());
                
                Iterator iterator = pduBundle.iterator();

                while(iterator.hasNext())
                {
                    Pdu aPdu = (Pdu)iterator.next();
                
                    System.out.print("got PDU of type: " + aPdu.getClass().getSimpleName());
                    if(aPdu instanceof EntityStatePdu)
                    {
                        EntityID eid = ((EntityStatePdu)aPdu).getEntityID();
                        Vector3Double position = ((EntityStatePdu)aPdu).getEntityLocation();
                        System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                        System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
                    }
                    System.out.println();
                } // end trop through PDU bundle
            } // end while
        } // End try
        catch (IOException e)
		{
            System.out.println("Problem with OpenDis4Examples.EspduReceiver, see exception trace:");
            System.out.println(e);
        }
    } // end main
} // end class