Skip to content
Snippets Groups Projects
LoeffelmanSeversonDISImageReceiver.java 4.63 KiB
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MV3500Cohort2018JulySeptember.FinalProject.LoeffelmanSeverson;

import edu.nps.moves.dis.CommentPdu;
import edu.nps.moves.dis.EntityID;
import edu.nps.moves.dis.EntityStatePdu;
import edu.nps.moves.dis.Pdu;
import edu.nps.moves.dis.OneByteChunk;
import edu.nps.moves.dis.VariableDatum;
import edu.nps.moves.dis.Vector3Double;
import edu.nps.moves.disutil.PduFactory;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import edu.nps.moves.disenum.PduType;
import java.util.ArrayList;

/**
 *
 * @author garrettloeffelman
 */
public class LoeffelmanSeversonDISImageReceiver {

   public static final int    MULTICAST_PORT  = 3000;
    public static final String MULTICAST_GROUP = "239.1.2.15";
    public static final boolean USE_FAST_ESPDU = false;
    
    public static void main(String args[])
    {
        Image image = null;
        PduFactory      factory;
        MulticastSocket socket = null;
        InetAddress     address = null;
		DatagramPacket  packet;
		
        try
        {
            System.setProperty("java.net.preferIPv4Stack", "true");
            System.out.println("DisExamples.PduReceiver started...");
            socket  = new MulticastSocket  (MULTICAST_PORT);
            address = InetAddress.getByName(MULTICAST_GROUP);
            socket.joinGroup(address);
            
            factory = new PduFactory();
            
            while (true) // Loop infinitely, receiving datagrams
            {
                byte buffer[] = new byte[100000]; // typical MTU size
                
                packet = new DatagramPacket(buffer, buffer.length); // reset
                
                socket.receive(packet);
                
                Pdu pdu = factory.createPdu (packet.getData());
		if (pdu != null)
		{
                    short pduType        = pdu.getPduType();
                    String pduTypeName   = pdu.getClass().getName();
                    short protocolFamily = pdu.getProtocolFamily(); // TODO get string enumeration
                    
                    if(pdu.getPduTypeEnum() == PduType.COMMENT){
                        CommentPdu cPdu = (CommentPdu)pdu;
                        ArrayList<VariableDatum> payload = (ArrayList)cPdu.getVariableDatums();
                        VariableDatum payloadWrapper = payload.get(0);
                        ArrayList<OneByteChunk> oBC = (ArrayList)payloadWrapper.getVariableData();
                        
                        byte[] imageByte = new byte[100000];
                        
                        for(int i = 0; i < oBC.size(); i++){
                            imageByte[i] = oBC.get(i).getOtherParameters()[0];
                        }
                        
                        ByteArrayInputStream bais = new ByteArrayInputStream(imageByte);
                        BufferedImage bimage = ImageIO.read(bais);
                        image = bimage;
                        JFrame frame = new JFrame();
                        frame.setSize(300, 300);
                        JLabel label = new JLabel(new ImageIcon(image));
                        frame.add(label);
                        frame.setVisible(true);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    }
                    
                    
                    StringBuilder message = new StringBuilder();
                    message.append("received DIS PDU: ");
                    message.append("pduType ");
                    if (pduType < 10)
			message.append(" ");
			message.append(pduType).append(" ").append(pduTypeName);
			message.append(", protocolFamily=").append(protocolFamily);
                        System.out.println(message.toString());
                    }
                    else System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error...");

            }
        }
        catch (IOException e)
        {
            System.out.println("Problem with DisExamples.PduReceiver, see exception trace:");
            System.out.println(e);
        }
        finally
        {
            System.out.println("DisExamples.PduReceiver complete.");
        }
    }
}