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("OpenDis7Examples.EspduReceiver started..."); MulticastSocket socket; DatagramPacket packet; InetAddress address; PduFactory pduFactory = new PduFactory(); int pduCount = 0; 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) { pduCount++; String receiptMessage = String.format("%3s", pduCount) // right justify, 3 characters + ". 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 OpenDis7Examples.EspduReceiver, see exception trace:"); System.out.println(e); } System.out.println("OpenDis7Examples.EspduReceiver complete."); } // end main } // end class