Skip to content
Snippets Groups Projects
EspduReceiver.java 3.77 KiB
package OpenDis7Examples; 

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

import edu.nps.moves.dis7.*;
import edu.nps.moves.dis7.utilities.*;

/**
 * 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 = EspduSender.DEFAULT_MULTICAST_ADDRESS;
	
    /** Default multicast port used, matches Wireshark DIS capture default */
    public static final int    DEFAULT_MULTICAST_PORT    = EspduSender.DEFAULT_MULTICAST_PORT;

    public static void main(String args[])
	{
		System.out.println("DisExamplesOpenDis7.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(DEFAULT_MULTICAST_ADDRESS);
            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(),packet.getLength());
                if (pduBundle.size() > 1)
                    System.out.println("Bundle size is " + pduBundle.size());
                
                // end iterator loop through PDU bundle
                for (Pdu aPdu : pduBundle) {
                    String receiptMessage = "received PDU type " + aPdu.getPduType().getValue() + "=" + aPdu.getPduType().name() + " " + aPdu.getClass().getName();
                    if (aPdu instanceof EntityStatePdu)
                    {
                        System.out.println("===============");
                        System.out.println(receiptMessage);
                        EntityID      entityID = ((EntityStatePdu)aPdu).getEntityID();
                        Vector3Double position = ((EntityStatePdu)aPdu).getEntityLocation();
                        System.out.println("  entityID triplet: [" + entityID.getSiteID()+ ", " + entityID.getApplicationID()+ ", " + entityID.getEntityID()+ "] ");
                        System.out.println("  Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
                    }
                    else if (aPdu instanceof FirePdu)
                    {    
                        System.out.println(receiptMessage);
                        Vector3Double position = ((FirePdu)aPdu).getLocationInWorldCoordinates();
                        System.out.println("  FirePdu locationInWorldCoordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
                        
                    }
                    else
                    {    
                        System.out.println(receiptMessage);
                    }
                }
            } // end while
        } // end try block
        catch (IOException e)
		{
            System.out.println("Problem with DisExamplesOpenDis7.EspduReceiver, see exception trace:");
            System.out.println(e);
        }
		System.out.println("DisExamplesOpenDis7.EspduReceiver complete.");
    } // end main
} // end class