From 30acbbbeee096fdbd7645fdb70572419a03460f8 Mon Sep 17 00:00:00 2001 From: Mike Bailey <jmbailey@nps.edu> Date: Tue, 10 Sep 2019 13:09:03 -0700 Subject: [PATCH] Add example to show use of DisThreadedNetIF --- .../dis7/examples/ThreadedNetExample.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/edu/nps/moves/dis7/examples/ThreadedNetExample.java diff --git a/src/edu/nps/moves/dis7/examples/ThreadedNetExample.java b/src/edu/nps/moves/dis7/examples/ThreadedNetExample.java new file mode 100644 index 0000000000..dcd3d05517 --- /dev/null +++ b/src/edu/nps/moves/dis7/examples/ThreadedNetExample.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved. + * This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html + */ +package edu.nps.moves.dis7.examples; + +import edu.nps.moves.dis7.Pdu; +import edu.nps.moves.dis7.enumerations.Country; +import edu.nps.moves.dis7.util.DisThreadedNetIF; +import edu.nps.moves.dis7.util.PduFactory; + +/** + * ThreadedNetExample.java created on Sep 9, 2019 + * MOVES Institute Naval Postgraduate School, Monterey, CA, USA www.nps.edu + * + * Shows how to use DisThreadedNetIF from an application class + * + * @author Mike Bailey, jmbailey@nps.edu + * @version $Id$ + */ +public class ThreadedNetExample +{ + public static void main(String[] args) + { + // Create an instance of DisThreadedNetIF using default port 3000, mcast 225.4.5.6, use other constructor to specific port and ip + DisThreadedNetIF netif = new DisThreadedNetIF(); + + // Internally, the DisThreadeNetIF class has constructed whatever threads it needs to operated + + // We want to listen also, so add a listener, using JDK8+ lambda grammar + netif.addListener(pdu->handleReceivedPdu(pdu)); + + // Use PduFactory to make pdus, default country = Deutschland, exercise, site, app, absolute timestamps + + PduFactory factory = new PduFactory(Country.GERMANY_DEU, (byte) 1, (short) 2, (short) 3, true); + + // Make and send 3 pdus with no delay between, testing threaded receiver performance + netif.send(factory.makeEntityStatePdu()); + System.out.println("Sent EntityStatePdu"); + netif.send(factory.makeFirePdu()); + System.out.println("Sent FirePdu"); + netif.send(factory.makeCollisionPdu()); + System.out.println("Sent CollisionPdu"); + + // Wait a bit to see output + try{ Thread.sleep(2000L);}catch(InterruptedException ex){} + + System.out.println("End of example"); + } + + private static void handleReceivedPdu(Pdu pdu) + { + // Do something here with the pdu you received + System.out.println("Received "+pdu.getClass().getSimpleName()); + } +} -- GitLab