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

import edu.nps.moves.disutil.*;
import edu.nps.moves.dis.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Receives PDUs from the network in IEEE format.
 *
 * @author DMcG
 * @version $Id:$
 */
public class ABEspduReceiverAtoTCP {

    /** 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 DIS_DESTINATION_PORT = 3000;
    public static final int TCP_DESTINATION_PORT = 2999;
    public static final int MAX_PDU_SIZE = 8192;
    public static final String GROUP = "239.1.2.4";
    public static void main(String args[]) {
        MulticastSocket socket;
        DatagramPacket packet;
        InetAddress address;
        PduFactory pduFactory = new PduFactory();
        
        

        try {

            // Specify the socket to receive data
            socket = new MulticastSocket(DIS_DESTINATION_PORT);
            socket.setBroadcast(true);
       
            address = InetAddress.getByName(GROUP);
            socket.joinGroup(address);
            
            System.out.println("Joined Group.");
            // Loop infinitely, receiving datagrams
            while (true) {
                try
                {

                byte buffer[] = new byte[MAX_PDU_SIZE];
                packet = new DatagramPacket(buffer, buffer.length);

                socket.receive(packet);
                System.out.println("Packet Received");


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

                while(it.hasNext())
                {
                    Pdu aPdu = (Pdu)it.next();
                
                   // System.out.print("got PDU of type: " + aPdu.getClass().getName());
                    if(aPdu instanceof EntityStatePdu)
                    {

                        EntityID eid = ((EntityStatePdu)aPdu).getEntityID();
                        if (eid.getSite() == 100){
                                                                                        // Create TCP Bridge
                System.out.println("Opening Bridge.");

                Socket clientConnection = new Socket("localhost", TCP_DESTINATION_PORT); 
                OutputStream os = clientConnection.getOutputStream();
                PrintStream ps = new PrintStream(os);

                //ByteArrayOutputStream baos = new ByteArrayOutputStream();
                //DataOutputStream dos = new DataOutputStream(baos);
                System.out.println("Bridging Complete.");

                        EntityType entityType = ((EntityStatePdu)aPdu).getEntityType();
                        ps.println(eid.getSite());
                        ps.println(eid.getApplication());
                        ps.println(eid.getEntity());
                        ps.println(entityType.getEntityKind());      // Platform (vs lifeform, munition, sensor, etc.)
                        ps.println(entityType.getCountry());              // USA
                        ps.println(entityType.getDomain());          // Land (vs air, surface, subsurface, space)
                        ps.println(entityType.getCategory());        // Tank
                        ps.println(entityType.getSubcategory());     // M1 Abrams
                        ps.println(entityType.getSpec());            // M1A2 Abrams
                        //ps.println(packet);
                        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
                }
                catch (Exception e) {
                    System.out.println(e);
                }

            } // end while
        } // End try
        catch (Exception e) {

            System.out.println(e);
        }


    } // end main
} // end class