diff --git a/examples/src/OpenDis7Examples/SimulationManager.java b/examples/src/OpenDis7Examples/SimulationManager.java index c9ebff296520a8c88fec1f371623aa77dc250409..6e28e064372fce9c97f0b70fe41fa69deec435eb 100644 --- a/examples/src/OpenDis7Examples/SimulationManager.java +++ b/examples/src/OpenDis7Examples/SimulationManager.java @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. package OpenDis7Examples; +import edu.nps.moves.dis7.pdus.CreateEntityPdu; +import edu.nps.moves.dis7.pdus.RemoveEntityPdu; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import java.util.ArrayList; @@ -42,17 +44,18 @@ import java.util.ArrayList; * Manage overall simulation choreography for a DIS exercise. * TODO once operation is working satisfactorily, this class will be moved into the open-dis7-java distribution utilities. * @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/specifications/README.md" target="_blank">Networked Graphics MV3500, Specification Documents, IEEE and SISO</a> - * @see <a href="https://ieeexplore.ieee.org/document/6387564" target="_blank">1278.1-2012. IEEE Standard for Distributed Interactive Simulation (DIS) - Application Protocols</a> + * @see <a href="https://ieeexplore.ieee.org/document/6387564" target="_blank">1278.1-2012. IEEE Standard for Distributed Interactive Simulation (DIS) - Application Protocols</a> 5.6.3 The simulation management computer * @see <a href="https://ieeexplore.ieee.org/document/587529" target="_blank">1278.3-1996. IEEE Recommended Practice for Distributed Interactive Simulation - Exercise Management and Feedback</a> * @author brutzman */ public class SimulationManager { - private DisThreadedNetworkInterface disThreadedNetworkInterface; - private ArrayList<RecordType> entityRecordList = new ArrayList<>(); - private ArrayList<RecordType> hostRecordList = new ArrayList<>(); - private ArrayList<RecordType> applicationRecordList = new ArrayList<>(); - private String descriptor = new String(); + private DisThreadedNetworkInterface disThreadedNetworkInterface; + private static ArrayList<RecordType> entityRecordList = new ArrayList<>(); + private static ArrayList<RecordType> hostRecordList = new ArrayList<>(); + private static ArrayList<RecordType> applicationRecordList = new ArrayList<>(); + private String descriptor = new String(); + private static int hostID = 0; private String TRACE_PREFIX = "[" + (SimulationManager.class.getSimpleName()) + "] "; @@ -62,7 +65,8 @@ public class SimulationManager */ public SimulationManager (String newDescriptor) { - this.descriptor = newDescriptor; + if (newDescriptor != null) + descriptor = newDescriptor.trim(); } /** * Object constructor @@ -122,8 +126,10 @@ public class SimulationManager { private int id = -1; private String name = new String(); + private String alias = new String(); private String description = new String(); private String reference = new String(); + private boolean isHostType = false; /** * Constructor for new record @@ -138,6 +144,7 @@ public class SimulationManager this.name = name; this.description = description; this.reference = reference; + // TODO create alias: if IP address then check for hostname, and vice versa } /** * Utility constructor for new record, description and reference remain blank @@ -150,6 +157,22 @@ public class SimulationManager this.name = name; this.description = ""; this.reference = ""; + // TODO create alias: if IP address then check for hostname, and vice versa + } + /** + * Utility constructor for new record, description and reference remain blank + * @param id identifying number + * @param name common name + * @param isHostType + */ + public RecordType (int id, String name, boolean isHostType) + { + this.id = id; + this.name = name; + this.description = ""; + this.reference = ""; + this.isHostType = isHostType; + // TODO create alias: if IP address then check for hostname, and vice versa } /** @@ -171,9 +194,11 @@ public class SimulationManager /** * @param newID the id to set + * @return same object to permit progressive setters */ - public void setId(int newID) { + public RecordType setId(int newID) { this.id = newID; + return this; } /** @@ -185,9 +210,11 @@ public class SimulationManager /** * @param newName the name to set + * @return same object to permit progressive setters */ - public void setName(String newName) { + public RecordType setName(String newName) { this.name = newName; + return this; } /** @@ -199,9 +226,11 @@ public class SimulationManager /** * @param newDescription the description to set + * @return same object to permit progressive setters */ - public void setDescription(String newDescription) { + public RecordType setDescription(String newDescription) { this.description = newDescription; + return this; } /** @@ -213,9 +242,43 @@ public class SimulationManager /** * @param newReference the reference to set + * @return same object to permit progressive setters */ - public void setReference(String newReference) { + public RecordType setReference(String newReference) { this.reference = newReference; + return this; + } + + /** + * @return the alias + */ + public String getAlias() { + return alias; + } + + /** + * @param alias the alias to set + * @return same object to permit progressive setters + */ + public RecordType setAlias(String alias) { + this.alias = alias; + return this; + } + + /** + * Does record represent a network address + * @return whether record is a network address + */ + public boolean isNetworkAddress() { + return isHostType; + } + + /** + * Set whether record represents a network address + * @param isAddress the isAddress to set + */ + public void setNetworkAddress(boolean isAddress) { + this.isHostType = isAddress; } } @@ -374,12 +437,21 @@ public class SimulationManager /** * Set the disThreadedNetworkInterface singleton to match other classes * @param disThreadedNetworkInterface the disThreadedNetworkInterface to set + * @return same object to permit progressive setters */ - public void setDisThreadedNetworkInterface(DisThreadedNetworkInterface disThreadedNetworkInterface) { + public SimulationManager setDisThreadedNetworkInterface(DisThreadedNetworkInterface disThreadedNetworkInterface) { this.disThreadedNetworkInterface = disThreadedNetworkInterface; + return this; + } + /** + * Check for disThreadedNetworkInterface + */ + protected boolean hasDisThreadedNetworkInterface() + { + return (this.disThreadedNetworkInterface != null); } /** - * Constructor for disThreadedNetworkInterface + * Create disThreadedNetworkInterface */ protected void createDisThreadedNetworkInterface() { @@ -424,13 +496,136 @@ public class SimulationManager /** * Set new simple descriptor (such as parent class name) for this SimulationManager, used in trace statements * @param newDescriptor simple descriptor name for this interface - */ - public void setDescriptor(String newDescriptor) + * @return same object to permit progressive setters */ + public SimulationManager setDescriptor(String newDescriptor) + { + if (newDescriptor != null) + this.descriptor = newDescriptor.trim(); + TRACE_PREFIX = "[" + DisThreadedNetworkInterface.class.getSimpleName() + " " + descriptor + "] "; + return this; + } + /** + * Reset descriptor + * @return same object to permit progressive setters */ + public SimulationManager clearDescriptor() + { + setDescriptor(""); + return this; + } + /** + * clear all lists + * @return same object to permit progressive setters*/ + public SimulationManager clearAll() + { + entityRecordList.clear(); + hostRecordList.clear(); + applicationRecordList.clear(); + clearDescriptor(); + return this; + } + /** + * Add entity to simulation list, if this is first occurrence + * @param newEntity new entity to add + * @return same object to permit progressive setters*/ + public SimulationManager addEntity(RecordType newEntity) + { + if (!entityRecordList.contains(newEntity)) + { + // TODO check record type + entityRecordList.add(newEntity); + if (hasDisThreadedNetworkInterface()) + { + CreateEntityPdu createEntityPdu = new CreateEntityPdu(); + // TODO set record parameters + getDisThreadedNetworkInterface().send(createEntityPdu); + } + else + { + System.err.println(TRACE_PREFIX + "addEntity() unable to send CreateEntityPdu since no disThreadedNetworkInterface found"); + // TODO consider queue for unsent entities + } + } + return this; + } + /** + * Remove entity from simulation list, if found + * @param oldEntity old entity to remove + * @return same object to permit progressive setters*/ + public SimulationManager removeEntity(RecordType oldEntity) + { + if (!entityRecordList.contains(oldEntity)) + { + // TODO check record type + entityRecordList.remove(oldEntity); + if (hasDisThreadedNetworkInterface()) + { + RemoveEntityPdu removeEntityPdu = new RemoveEntityPdu(); + // TODO set record parameters + getDisThreadedNetworkInterface().send(removeEntityPdu); + } + else + { + System.err.println(TRACE_PREFIX + "removeEntity() unable to send RemoveEntityPdu since no disThreadedNetworkInterface found"); + // TODO consider queue for unsent entities + } + } + return this; + } + /** + * Add host to simulation list, if this is first occurrence + * @param newHost new host to add + * @return same object to permit progressive setters*/ + public SimulationManager addHost(String newHost) { - this.descriptor = newDescriptor; - TRACE_PREFIX = "[" + (DisThreadedNetworkInterface.class.getSimpleName() + " " + descriptor).trim() + "] "; + boolean nameFound = false; + boolean aliasFound = false; + for (RecordType nextRecord : hostRecordList) + { + if ( nextRecord.name.equalsIgnoreCase(newHost.trim())) + nameFound = true; + if (nextRecord.alias.equalsIgnoreCase(newHost.trim())) + aliasFound = true; + if ((nameFound || aliasFound) && !nextRecord.isHostType) + nextRecord.isHostType = true; // make sure + } + if (!nameFound && !aliasFound) + { + RecordType newRecord = new RecordType(hostID, newHost, true); + // TODO set alias to IP number + hostRecordList.add(newRecord); + hostID++; + // no PDU sent + } + return this; + } + /** + * Remove host from simulation list, if found + * @param oldHost old host to remove + * @return same object to permit progressive setters*/ + public SimulationManager removeHost(String oldHost) + { + boolean nameFound = false; + boolean aliasFound = false; + for (RecordType nextRecord : hostRecordList) + { + if ( nextRecord.name.equalsIgnoreCase(oldHost.trim())) + nameFound = true; + if (nextRecord.alias.equalsIgnoreCase(oldHost.trim())) + aliasFound = true; + if ((nameFound || aliasFound) && !nextRecord.isHostType) + nextRecord.isHostType = true; // make sure + + if (nameFound || aliasFound) + { + hostRecordList.remove(nextRecord); + // no PDU sent + break; + } + } + return this; } + /** Self test to check basic operation, invoked by main() */ public void selfTest() { createDisThreadedNetworkInterface();