diff --git a/src/edu/nps/moves/dis7/utilities/PduFactory.java b/src/edu/nps/moves/dis7/utilities/PduFactory.java index c8d2996281e24c6282bc0d3a20136802b28713f8..de37edba7a4563416929634b5c9c6a2ab9375d9f 100644 --- a/src/edu/nps/moves/dis7/utilities/PduFactory.java +++ b/src/edu/nps/moves/dis7/utilities/PduFactory.java @@ -5,11 +5,10 @@ package edu.nps.moves.dis7.utilities; -import edu.nps.moves.dis7.pdus.*; +import edu.nps.moves.dis7.utilities.DisTime.*; import edu.nps.moves.dis7.enumerations.*; +import edu.nps.moves.dis7.pdus.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -33,26 +32,10 @@ import java.util.stream.Stream; */ public class PduFactory { - /** Supported timestamp styles. TODO consider moving to Pdu abstract class. - * @see edu.nps.moves.dis7.pdus.DisTime - */ - public enum TimestampStyle { - /** Clock ticks since top of hour, host synchronized to UTC via Network Time Protocol (NTP) */ - IEEE_ABSOLUTE, - /** Clock ticks since top of hour, host not synchronized to UTC via Network Time Protocol (NTP) */ - IEEE_RELATIVE, - /** Unix time (seconds since 1 January 1970) */ - UNIX, - /** hundreds of a second since the start of the year */ - YEAR }; - private edu.nps.moves.dis7.enumerations.Country country = Country.UNITED_STATES_OF_AMERICA_USA; private byte defaultExerciseId = 1; private short defaultSiteId = 2; private short defaultAppId = 3; - private final edu.nps.moves.dis7.pdus.DisTime disTime = new DisTime(); - - private Method getTimeMethod; /** We can marshal the PDU with a timestamp set to any of several styles. * Remember, you MUST set a timestamp. DIS will regard multiple packets sent @@ -68,7 +51,7 @@ public class PduFactory public PduFactory(TimestampStyle newTimestampStyle) { timestampStyle = newTimestampStyle; - setTimeStampMethod(); + DisTime.setTimestampMethod(); } /** @@ -98,70 +81,16 @@ public class PduFactory this.defaultSiteId = siteId; this.defaultAppId = applicationId; - setTimestampStyle(timestampStyle); + DisTime.setTimestampStyle(timestampStyle); } - /** Set one of four time references as timestampStyle: IEEE_ABSOLUTE, IEEE_RELATIVE, UNIX, or YEAR. - * @see TimestampStyle - * @param newtimestampStyle the timestamp style to set for this PDU - */ - public final void setTimestampStyle(TimestampStyle newtimestampStyle) { - timestampStyle = newtimestampStyle; - setTimeStampMethod(); - } - - private void setTimeStampMethod() { - try { - switch (timestampStyle) { - case IEEE_ABSOLUTE: - getTimeMethod = DisTime.class.getDeclaredMethod("getCurrentDisAbsoluteTimestamp", new Class<?>[0]); - break; - - case IEEE_RELATIVE: - getTimeMethod = DisTime.class.getDeclaredMethod("getCurrentDisRelativeTimestamp", new Class<?>[0]); - break; - - case UNIX: - getTimeMethod = DisTime.class.getDeclaredMethod("getCurrentUnixTimestamp", new Class<?>[0]); - break; - - case YEAR: // formerly NPS: - getTimeMethod = DisTime.class.getDeclaredMethod("getCurrentYearTimestamp", new Class<?>[0]); - break; - - default: - getTimeMethod = DisTime.class.getDeclaredMethod("getCurrentDisAbsoluteTimestamp", new Class<?>[0]); - break; - } - } catch (NoSuchMethodException ex) { - throw new RuntimeException(ex); - } - } - - /** Retrieve the current timestamp in the time stamp style set at factory - * instantiation. - * @return the current timestamp in the time stamp style set at factory - * instantiation - */ - public int getTimestamp() - { - try { - if (getTimeMethod == null) - setTimeStampMethod(); // avoid NPE - return (int) getTimeMethod.invoke(disTime, (Object[]) null); - } - catch (IllegalAccessException | InvocationTargetException ex) { - throw new RuntimeException(ex); - } - } - /* ***************************************************/ /* utility methods*/ private PduBase addBoilerPlate(PduBase pdu) { pdu.getPduStatus().setValue((byte) (PduStatus.AII_ACTIVE | PduStatus.CEI_COUPLED)); pdu.setExerciseID(defaultExerciseId) - .setTimestamp(getTimestamp()) + .setTimestamp(DisTime.getTimestamp()) .setLength((short) pdu.getMarshalledSize()); //todo check if should be done in Pdu class return pdu; @@ -170,7 +99,7 @@ public class PduFactory private LiveEntityFamilyPdu addBoilerPlate(LiveEntityFamilyPdu pdu) { pdu.setExerciseID(defaultExerciseId) - .setTimestamp(getTimestamp()) + .setTimestamp(DisTime.getTimestamp()) .setLength((short) pdu.getMarshalledSize()); //todo check if should be done in Pdu class return pdu;