Skip to content

Commit

Permalink
Simplify and make Espdu Sender and Receiver examples work
Browse files Browse the repository at this point in the history
Using multicast connection by default, because shouldn't be encouraging
use of broadcast. And overall simplified the examples.
  • Loading branch information
leif81 committed Nov 10, 2022
1 parent 782b9f9 commit 5574b27
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 281 deletions.
74 changes: 28 additions & 46 deletions src/main/java/edu/nps/moves/examples/EspduReceiver.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package edu.nps.moves.examples;

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

import edu.nps.moves.disutil.*;

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

/**
* Receives PDUs from the network in IEEE format.
Expand All @@ -15,59 +13,43 @@
*/
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
/**
* 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;

public static void main(String args[]) {
public static final int DIS_PORT = 3000;

public static void main(String args[]) throws IOException {
MulticastSocket socket;
DatagramPacket packet;
InetAddress address;
PduFactory pduFactory = new PduFactory();

try {
// Specify the socket to receive data
socket = new MulticastSocket(3001);
socket.setBroadcast(true);

//address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
//socket.joinGroup(address);

// Loop infinitely, receiving datagrams
while (true) {
byte buffer[] = new byte[MAX_PDU_SIZE];
packet = new DatagramPacket(buffer, buffer.length);
socket = new MulticastSocket(DIS_PORT);
address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
socket.joinGroup(address);

socket.receive(packet);
// Loop infinitely, receiving datagrams
while (true) {
byte buffer[] = new byte[MAX_PDU_SIZE];
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);

List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData());
System.out.println("Bundle size is " + pduBundle.size());

Iterator it = pduBundle.iterator();
Pdu aPdu = pduFactory.createPdu(packet.getData());

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();
Vector3Double position = ((EntityStatePdu)aPdu).getEntityLocation();
System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
}
System.out.println();
} // end trop through PDU bundle
System.out.println("Received PDU of type: " + aPdu.getClass().getName());
if (aPdu instanceof EntityStatePdu) {
EntityID eid = ((EntityStatePdu) aPdu).getEntityID();
Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation();
System.out.println(" Site,App,Id:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
System.out.println(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");

} // end while
} // End try
catch (Exception e) {

System.out.println(e);
final double[] latlon = CoordinateConversions.xyzToLatLonDegrees(position.toArray());
System.out.println(" Location in Latitude Longitude Elevation: [" + latlon[0] + ", " + latlon[1] + ", " + latlon[2] + "]");
}
}


} // end main
} // end class
}
}
Loading

0 comments on commit 5574b27

Please sign in to comment.