From 207c72d29f0cba8d0c45d6c0c00f3e91c9974355 Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@nps.edu>
Date: Thu, 1 Jul 2021 17:40:12 -0700
Subject: [PATCH] package info, package rename

---
 examples/DisDemo/build.run.log.txt            |   7 +-
 examples/DisDemo/src/DisDemo/DisDemo.java     | 153 ++++++++++++++++++
 examples/DisDemo/src/DisDemo/EntityMover.java |  29 ++++
 .../src/DisDemo/EntityMoverSquare.java        |  75 +++++++++
 4 files changed, 259 insertions(+), 5 deletions(-)
 create mode 100644 examples/DisDemo/src/DisDemo/DisDemo.java
 create mode 100644 examples/DisDemo/src/DisDemo/EntityMover.java
 create mode 100644 examples/DisDemo/src/DisDemo/EntityMoverSquare.java

diff --git a/examples/DisDemo/build.run.log.txt b/examples/DisDemo/build.run.log.txt
index 32cd48707c..8d5f9aab2c 100644
--- a/examples/DisDemo/build.run.log.txt
+++ b/examples/DisDemo/build.run.log.txt
@@ -1,15 +1,12 @@
-ant -f C:\\x-nps-gitlab\\NetworkedGraphicsMV3500\\examples\\DisDemo -Dnb.internal.action.name=run run
+ant -f C:\\x-nps-gitlab\\NetworkedGraphicsMV3500\\examples\\DisDemo -Dnb.internal.action.name=run -Duser.properties.file=C:\\Users\\brutzman.IT154928\\AppData\\Roaming\\NetBeans\\12.4\\build.properties run
 init:
 Deleting: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\DisDemo\build\built-jar.properties
 deps-jar:
 Updating property file: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\DisDemo\build\built-jar.properties
-Compiling 1 source file to C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\DisDemo\build\classes
-warning: [options] system modules path not set in conjunction with -source 11
-1 warning
 compile:
 run:
 Sending 20 ESPDU packets at 1000 msec (1.0 second) intervals on
-  broadcast address=[/172.20.209.165, /172.16.0.255] port 3000
+  broadcast address=[/172.16.0.255, /172.20.209.219, /172.24.159.255] port 3000
 Current position: 0.0, 1.0, 0.0
 ... sent 1
 Current position: 0.0, 2.0, 0.0
diff --git a/examples/DisDemo/src/DisDemo/DisDemo.java b/examples/DisDemo/src/DisDemo/DisDemo.java
new file mode 100644
index 0000000000..6f002c7873
--- /dev/null
+++ b/examples/DisDemo/src/DisDemo/DisDemo.java
@@ -0,0 +1,153 @@
+package DisDemo;
+
+import edu.nps.moves.dis7.enumerations.Country;
+import edu.nps.moves.dis7.enumerations.EntityKind;
+import edu.nps.moves.dis7.enumerations.ForceID;
+import edu.nps.moves.dis7.enumerations.PlatformDomain;
+import edu.nps.moves.dis7.pdus.DisTime;
+import edu.nps.moves.dis7.pdus.Domain;
+import edu.nps.moves.dis7.pdus.EntityStatePdu;
+import edu.nps.moves.dis7.pdus.Vector3Double;
+import edu.nps.moves.spatial.*;
+
+import java.io.IOException;
+import java.net.*;
+import java.nio.ByteBuffer;
+import java.util.*;
+
+/**
+ *
+ * @author DMcG
+ */
+public class DisDemo {
+
+    /**
+     * Program invocation, execution starts here
+     * @param args command-line arguments
+     * @throws java.lang.Exception execution error
+     */
+    public static void main(String[] args) throws Exception 
+    {
+        EntityStatePdu espdu = new EntityStatePdu();
+
+        int NUMBER_OF_SENDS =   20;
+        int PORT            = 3000;
+        int INTERVAL_MSEC   = 1000; // 1 second
+        
+        // Create a new coordinate system at the give latitude, longitude, and altitude
+        RangeCoordinates rangeCoordinates = new RangeCoordinates(31.435, 45.223, 17.0);
+        
+        // Convert from the coodinate system with the origin above to DIS coordinates
+        Vector3Double disCoordinates = rangeCoordinates.DISCoordFromLocalFlat(0, 0, 0);
+        espdu.setEntityLocation(disCoordinates);
+        
+        // Set various fields--entityID, entity type, etc.
+        espdu.setExerciseID((byte)1);
+        
+        espdu.getEntityID().setSiteID(17);
+        espdu.getEntityID().setApplicationID(42);
+        espdu.getEntityID().setEntityID(104);
+        
+        espdu.getEntityType().setEntityKind(EntityKind.PLATFORM); // platform
+        espdu.getEntityType().setDomain(Domain.inst(PlatformDomain.LAND)); // land
+        espdu.getEntityType().setCategory((short)2);
+        espdu.getEntityType().setSubCategory(1);
+        espdu.getEntityType().setSpecific((short)1);
+        espdu.getEntityType().setCountry(Country.UNITED_STATES_OF_AMERICA_USA);
+        
+        espdu.getMarking().setCharacters("lulz".getBytes());
+        espdu.setForceId(ForceID.FRIENDLY);
+        
+        try (DatagramSocket socket = new DatagramSocket(PORT))
+        {
+           
+           // We set the starting position to (0,0,0) in the local coordinate
+           // system, which is equivalent to the (lat, lon, alt) set above as
+           // the origin, which also corresponds to some DIS geocentric point.
+           // All three coordinate system points correspond to the same point
+           // in space.
+           Vector3Double startPosition = new Vector3Double();
+           startPosition.setX(0.0);
+           startPosition.setY(0.0);
+           startPosition.setZ(0.0);
+           
+           // Our mover is told to start at (0,0,0) in the local coordinate system
+           EntityMoverSquare mover = new EntityMoverSquare(startPosition);
+           
+           List<InetAddress> allBcasts = getBroadcastAddresses();
+           Vector3Double currentPosition, currentPositionDisCoords;
+           ByteBuffer buffer;
+           DatagramPacket packet;
+               
+           // Do some movement for a while
+           System.out.println ("Sending " + NUMBER_OF_SENDS + " ESPDU packets at " +
+               INTERVAL_MSEC + " msec (" + INTERVAL_MSEC/1000.0 +
+               " second) intervals on");
+           System.out.println ("  broadcast address=" + allBcasts + " port " + PORT);
+           for(int index = 1; index <= NUMBER_OF_SENDS; index++)
+           {
+               mover.step();
+               
+               // get the current position of the enity in the local coordinate system
+               currentPosition = mover.getPosition();
+               
+               // Convert that point to the DIS geocentric coordinate system
+               currentPositionDisCoords = rangeCoordinates.DISCoordFromLocalFlat(currentPosition.getX(), currentPosition.getY(), currentPosition.getZ());
+               
+               // Set entity position in the espdu
+               espdu.setEntityLocation(currentPositionDisCoords);
+               
+               // Marshall it, send it
+               espdu.setTimestamp(new DisTime().getDisRelativeTimestamp());
+               buffer = ByteBuffer.wrap(espdu.marshal());
+               
+               for(InetAddress aBcast: allBcasts)
+               {
+                    packet = new DatagramPacket(buffer.array(), buffer.array().length, aBcast, PORT);
+                    socket.send(packet);
+               }
+              
+               Thread.sleep(INTERVAL_MSEC);
+               System.out.println("... sent " + index);
+           }
+        }
+        catch(IOException | InterruptedException e)
+        {
+            System.err.println(e);
+        }
+        
+    }
+    
+    public static List<InetAddress> getBroadcastAddresses()
+    {
+        List<InetAddress> broadcastAddresses = new ArrayList<>();
+
+        try
+        {
+            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+            while (interfaces.hasMoreElements()) 
+            {
+                NetworkInterface networkInterface = interfaces.nextElement();
+                if (networkInterface.isLoopback())
+                    continue;    // Don't want to broadcast to the loopback interface
+
+                for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) 
+                {
+                    InetAddress broadcast = interfaceAddress.getBroadcast();
+                    if (broadcast == null)
+                        continue;
+
+                    // Use the address
+                    broadcastAddresses.add(broadcast);
+                }
+            }
+        }
+        catch(SocketException e)
+        {
+            System.err.println(e);
+        }
+
+        return broadcastAddresses;
+    }
+    
+}
diff --git a/examples/DisDemo/src/DisDemo/EntityMover.java b/examples/DisDemo/src/DisDemo/EntityMover.java
new file mode 100644
index 0000000000..db9fa5a527
--- /dev/null
+++ b/examples/DisDemo/src/DisDemo/EntityMover.java
@@ -0,0 +1,29 @@
+package DisDemo;
+
+import edu.nps.moves.dis7.pdus.*;
+
+/**
+ * Abstract superclass for moving entities. Initialized with a starting
+ * position (IN LOCAL COORDINATES). Step() performs one quanta of moving
+ * the thing around. After movement you can retrieve the new position with
+ * getPosition(), convert it to global DIS coordinates, and be on your way.
+ * 
+ * @author DMcG
+ */
+public abstract class EntityMover 
+{
+    Vector3Double position;
+    
+    public EntityMover(Vector3Double startPosition)
+    {
+        this.position = startPosition;
+    }
+    
+    public abstract void step();
+    
+    public Vector3Double getPosition()
+    {
+        return position;
+    }
+    
+}
diff --git a/examples/DisDemo/src/DisDemo/EntityMoverSquare.java b/examples/DisDemo/src/DisDemo/EntityMoverSquare.java
new file mode 100644
index 0000000000..e57577368d
--- /dev/null
+++ b/examples/DisDemo/src/DisDemo/EntityMoverSquare.java
@@ -0,0 +1,75 @@
+package DisDemo;
+
+import edu.nps.moves.dis7.pdus.*;
+
+/**
+ * Logic for moving something around in a square using a local coordinate system,
+ * ENU. East is positive X, north is positive Y, positive Z is "up". 
+ * 
+ * @author DMcG
+ */
+public class EntityMoverSquare extends EntityMover
+{
+    /** How much an entity moves every quanta */
+    private static final int STEP_SIZE = 1;
+    /** How big the square we move around is */
+    private static final int SQUARE_SIZE = 50;  // size of a side to the square, in meters
+    /** Directions we can travel. */
+    public enum Direction{NORTH, EAST, SOUTH, WEST};
+    
+    public Direction currentDirection;
+    
+    public EntityMoverSquare(Vector3Double position)
+    {
+        super(position);
+        this.currentDirection = Direction.NORTH;
+    }
+    
+    @Override
+    public void step()
+    {
+        switch(currentDirection)
+        {
+            case NORTH:
+                position.setY( position.getY() + STEP_SIZE);
+                if(position.getY() >= SQUARE_SIZE)
+                {
+                    currentDirection = Direction.EAST;
+                }
+                break;
+                
+            case EAST:
+                position.setX( position.getX() + STEP_SIZE);
+                if(position.getX() >= SQUARE_SIZE)
+                {
+                    currentDirection = Direction.SOUTH;
+                }
+                break;
+                
+            case SOUTH: 
+                position.setY( position.getY() - STEP_SIZE);
+                if(position.getY() <= 0.0)
+                {
+                    currentDirection = Direction.WEST;
+                }
+                break;
+                
+                
+            case WEST:
+                
+                position.setX( position.getX() - STEP_SIZE);
+                if(position.getX() <= 0.0)
+                {
+                    currentDirection = Direction.NORTH;
+                }
+                break;
+                
+            default:
+                System.out.println("Unrecognized direction");
+        }
+        
+        System.out.println("Current position: " + position.getX() + ", " + position.getY() + ", " + position.getZ());
+        
+    }
+
+}
-- 
GitLab