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

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


/**
 * Receives PDUs from the network in IEEE format and creates a TCP connection, 
 * then sends information through the connection.
 *
 * @author Angelopoulos/Blankenbeker
 * @version 8 MAR 2018
 */
public class AngelBlankEspduReceiverBtoTCP {

    public static final String TCP_DESTINATION_IP = "172.20.144.187";
    public static final int DIS_DESTINATION_PORT = 2800;
    public static final int TCP_DESTINATION_PORT = 2998;
    public static final int MAX_PDU_SIZE = 8192;
    public static final String GROUP = "239.1.2.3";
    public static void main(String args[]) {
        MulticastSocket socket;
        DatagramPacket packet;
        DatagramSocket dataGram;
        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() == 200){
                            dataGram = new DatagramSocket();
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            DataOutputStream dos = new DataOutputStream(baos);  
                            aPdu.marshal(dos);
                            byte[] data = baos.toByteArray();
                                
                            DatagramPacket DpSend =  new DatagramPacket(data, data.length,  InetAddress.getByName(TCP_DESTINATION_IP), TCP_DESTINATION_PORT);
                            dataGram.send(DpSend);                                    // Create TCP Bridge
                            System.out.println("Bravo briding complete to: "+ TCP_DESTINATION_IP);
                            //EntityType entityType = ((EntityStatePdu)aPdu).getEntityType();
                            }
                            Vector3Double location = ((EntityStatePdu)aPdu).getEntityLocation();
                            System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                            //System.out.print(" Location in DIS coordinates: [" + location.getX() + ", " + location.getY() + ", " + position.getZ() + "]");
                            double c[] = {location.getX(), location.getY(), location.getZ()};
                            double lla[] = CoordinateConversions.xyzToLatLonDegrees(c);
                            System.out.println(" Location (lat/lon/alt): [" + lla[0] + ", " + lla[1] + ", " + lla[2] + "]");
                        }
                        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