diff --git a/src/edu/nps/moves/dis7/examples/EntityUse.java b/src/edu/nps/moves/dis7/examples/EntityUse.java index 855012079fae89c3e234483fae9dd385df10c165..298c8e9fbf22176eaf95ad6919da09ae932b1268 100644 --- a/src/edu/nps/moves/dis7/examples/EntityUse.java +++ b/src/edu/nps/moves/dis7/examples/EntityUse.java @@ -75,6 +75,11 @@ public class EntityUse */ public static void exampleUse() throws Exception { + DisThreadedNetIF netif = new DisThreadedNetIF(); // uses defaults + + // We want to listen also, so add a listener, using JDK8+ lambda grammar + netif.addListener(pdu->handleReceivedPdu(pdu)); + PduFactory pduFactory = new PduFactory(); // uses defaults: usa, exercise id 1, site id 2, app id 3, absolute time EntityStatePdu espdu = pduFactory.makeEntityStatePdu(); @@ -88,27 +93,28 @@ public class EntityUse espdu.setEntityType(et); - DisThreadedNetIF netif = new DisThreadedNetIF(); // uses defaults - - // We want to listen also, so add a listener, using JDK8+ lambda grammar - netif.addListener(pdu->handleReceivedPdu(pdu)); netif.send(espdu); // possibly throws IOException + sleep(100L); /* Do the same for the second way of creating a Shenandoah entity type and show an alternate way of creating an ESPDU */ - EntityStatePdu espdu2 = (EntityStatePdu)pduFactory.createPdu(DISPDUType.ENTITY_STATE); + espdu = (EntityStatePdu)pduFactory.createPdu(DISPDUType.ENTITY_STATE); /* set desired entity state fields here */ edu.nps.moves.dis7.entities.usa.platform.surface.AD44Shenandoah et2 = new edu.nps.moves.dis7.entities.usa.platform.surface.AD44Shenandoah(); /* Use import statement to make the code above more readable */ - espdu2.setEntityType(et2); - netif.send(espdu2); // possibly throws IOException - - // Wait a bit to see output - try { - Thread.sleep(250L); - } catch(InterruptedException ex) {} + espdu.setEntityType(et2); + netif.send(espdu); // possibly throws IOException + sleep(100L); + } + + private static void sleep(long ms) + { + try { + Thread.sleep(ms); + } + catch (InterruptedException ex) {} } private static void handleReceivedPdu(Pdu pdu)