Skip to content
Snippets Groups Projects
PduReceiver.java 2.42 KiB
package DisExamples;

// originally edu.nps.moves.examples.ReceiverPerformance

import java.net.*;

import edu.nps.moves.dis.*;
import edu.nps.moves.disutil.PduFactory;
import java.io.IOException;

public class PduReceiver 
{
    public static final int    MULTICAST_PORT  = 3000;
    public static final String MULTICAST_GROUP = "239.1.2.3";
    public static final boolean USE_FAST_ESPDU = false;
    
    public static void main(String args[])
    {
        PduFactory      factory;
        MulticastSocket socket = null;
        InetAddress     address = null;
		DatagramPacket  packet;
		
        try
        {
			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[1500]; // typical MTU size
                
                packet = new DatagramPacket(buffer, buffer.length); // reset
                
                socket.receive(packet);
                
                Pdu pdu = factory.createPdu (packet.getData());
				if (pdu != null)
				{
					short  currentPduType          = pdu.getPduType();
					String currentPduTypeName      = pdu.getClass().getName();
					short  currentProtocolFamilyID = pdu.getProtocolFamily();
					String currentPduFamilyName    = pdu.getClass().getSuperclass().getSimpleName();
					
					StringBuilder message = new StringBuilder();
					message.append("received DIS PDU: ");
					message.append("pduType ");
					if (currentPduType < 10)
						message.append(" ");
					message.append(currentPduType).append(" ").append(currentPduTypeName);
					message.append(", protocolFamily ").append(currentProtocolFamilyID);
					message.append(" ").append(currentPduFamilyName);
				    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.");
        }
    }
}