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
{
    /** 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.
     */
    public static final int MAX_PDU_SIZE = 8192;

    /** Default multicast group address we send on. */
    public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3";
	
    /** Default multicast port used, matches Wireshark DIS capture default */
    public static final int    DEFAULT_MULTICAST_PORT    = 3000;

    public static void main(String args[])
	{
		System.out.println("DisExamples.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().getName());
                    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 DisExamples.EspduReceiver, see exception trace:");
            System.out.println(e);
        }
    } // end main
} // end class