Class StateMachine

  • All Implemented Interfaces:
    StateHandlerContext

    public class StateMachine
    extends java.lang.Object
    implements StateHandlerContext
    SHIP state machine, handles connection setup until Connection Data Exchange is reached.
    • Field Detail

      • state

        protected State state
      • extraDataType

        protected java.lang.Class<?> extraDataType
      • stateExtraData

        protected java.lang.Object stateExtraData
      • timeoutExecutor

        protected java.util.concurrent.ScheduledExecutorService timeoutExecutor
    • Method Detail

      • begin

        public void begin()
      • swapState

        protected State swapState​(State newState)
      • sendMessage

        public void sendMessage​(byte[] msg)
        Description copied from interface: StateHandlerContext
        Send a message to the communication partner.
        Specified by:
        sendMessage in interface StateHandlerContext
        Parameters:
        msg - the message to send
      • messageReceived

        public void messageReceived​(byte[] msg)
      • processMessage

        public void processMessage()
        Description copied from interface: StateHandlerContext
        Indicate that the handler is ready to process a waiting message from the communication partner.
        Specified by:
        processMessage in interface StateHandlerContext
      • getState

        public State getState()
        Specified by:
        getState in interface StateHandlerContext
        Returns:
        the current state. Mostly useful for handlers that handle multiple states.
      • getDefaultTimeoutSeconds

        public int getDefaultTimeoutSeconds​(SpecifiedTimeout which)
      • setTimeoutExecutor

        public void setTimeoutExecutor​(java.util.concurrent.ScheduledExecutorService executor)
        Replace the executor used for scheduling timeouts (this is mainly useful for testing). Cannot be called while there are already timeouts scheduled.
        Parameters:
        executor - the new executor to use
      • startTimeout

        public void startTimeout​(SpecifiedTimeout which,
                                 int fixedDelay)
        Description copied from interface: StateHandlerContext
        Start a specific timeout with the given duration.
        Specified by:
        startTimeout in interface StateHandlerContext
        Parameters:
        which - which timeout to start
        fixedDelay - how long it should run
      • startTimeout

        public void startTimeout​(SpecifiedTimeout which,
                                 int amount,
                                 java.util.concurrent.TimeUnit unit)
        Description copied from interface: StateHandlerContext
        Start a specific timeout with the given duration.
        Specified by:
        startTimeout in interface StateHandlerContext
        Parameters:
        which - which timeout to start
        amount - how long it should run
        unit - the unit for amount
      • getTimeoutStatus

        public java.lang.Integer getTimeoutStatus​(SpecifiedTimeout which)
        Description copied from interface: StateHandlerContext
        Query the status of a specific timeout.
        Specified by:
        getTimeoutStatus in interface StateHandlerContext
        Parameters:
        which - which timeout to get the status of
        Returns:
        time remaining (in seconds), or null if not active.
      • isServerSide

        public boolean isServerSide()
        Specified by:
        isServerSide in interface StateHandlerContext
        Returns:
        whether the caller is on the server side of a connection.
      • requireTrust

        public boolean requireTrust​(int minimumTrust)
        Specified by:
        requireTrust in interface StateHandlerContext
        Parameters:
        minimumTrust - required trust level
        Returns:
        whether the communication partner is trusted at least this much
      • getTrustLevel

        public int getTrustLevel()
      • setExtraData

        public void setExtraData​(java.lang.Object stateExtraData)
        Description copied from interface: StateHandlerContext
        Store some arbitrary extra data.

        The handler must be marked with a StateHandler.UsesExtraData annotation indicating the type of the extra data it uses.

        Specified by:
        setExtraData in interface StateHandlerContext
        Parameters:
        stateExtraData - the data to store.
      • setCommPartnerTrusted

        public void setCommPartnerTrusted()