Class StateMachine
- java.lang.Object
-
- org.openmuc.jeebus.ship.state.machine.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 Summary
Fields Modifier and Type Field Description protected StateHandleractionsprotected java.lang.Class<?>extraDataTypeprotected ShipConnectionshipConnectionprotected Statestateprotected java.lang.ObjectstateExtraDataprotected java.util.concurrent.ScheduledExecutorServicetimeoutExecutor
-
Constructor Summary
Constructors Constructor Description StateMachine(ShipConnection shipConnection, UserInterface userInterface, Configuration config)StateMachine(ShipConnection shipConnection, UserInterface userInterface, Configuration config, State initial, java.lang.Object extraData)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbegin()voidcloseConnection()Close the SPINE connection with no further messaging.voidenableConnectionDataExchange()Enable Connection Data Exchange (it cannot be disabled afterward)ConfigurationgetConfig()intgetDefaultTimeoutSeconds(SpecifiedTimeout which)<T> TgetExtraData(java.lang.Class<T> dataClass)Retrieve data previously stored withStateHandlerContext.setExtraData(Object).StategetState()java.lang.IntegergetTimeoutStatus(SpecifiedTimeout which)Query the status of a specific timeout.intgetTrustLevel()UserInterfacegetUserInterface()voidincreaseTimeout(SpecifiedTimeout which)Prolong a specific timeout by the initial value.voidincreaseTimeout(SpecifiedTimeout which, int secondsDelta)Prolong a specific timeout.booleanisServerSide()voidmessageReceived(byte[] msg)voidprocessMessage()Indicate that the handler is ready to process a waiting message from the communication partner.booleanrequireTrust(int minimumTrust)voidsendMessage(byte[] msg)Send a message to the communication partner.voidsetCommPartnerTrusted()voidsetExtraData(java.lang.Object stateExtraData)Store some arbitrary extra data.voidsetPeerSkiAuthenticated()voidsetTimeoutExecutor(java.util.concurrent.ScheduledExecutorService executor)Replace the executor used for scheduling timeouts (this is mainly useful for testing).voidstartTimeout(SpecifiedTimeout which)Start a specific timeout with the default duration.voidstartTimeout(SpecifiedTimeout which, int fixedDelay)Start a specific timeout with the given duration.voidstartTimeout(SpecifiedTimeout which, int amount, java.util.concurrent.TimeUnit unit)Start a specific timeout with the given duration.voidstopTimeouts(SpecifiedTimeout... which)Stop one or more timeouts.protected StateswapState(State newState)voidtransitionTo(State newState)Transition the state machine.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openmuc.jeebus.ship.state.machine.StateHandlerContext
stopAllTimeouts
-
-
-
-
Field Detail
-
state
protected State state
-
actions
protected StateHandler actions
-
extraDataType
protected java.lang.Class<?> extraDataType
-
stateExtraData
protected java.lang.Object stateExtraData
-
shipConnection
protected final ShipConnection shipConnection
-
timeoutExecutor
protected java.util.concurrent.ScheduledExecutorService timeoutExecutor
-
-
Constructor Detail
-
StateMachine
public StateMachine(ShipConnection shipConnection, UserInterface userInterface, Configuration config, State initial, java.lang.Object extraData)
-
StateMachine
public StateMachine(ShipConnection shipConnection, UserInterface userInterface, Configuration config)
-
-
Method Detail
-
begin
public void begin()
-
transitionTo
public void transitionTo(State newState)
Description copied from interface:StateHandlerContextTransition the state machine.- Specified by:
transitionToin interfaceStateHandlerContext- Parameters:
newState- which state to enter now
-
sendMessage
public void sendMessage(byte[] msg)
Description copied from interface:StateHandlerContextSend a message to the communication partner.- Specified by:
sendMessagein interfaceStateHandlerContext- Parameters:
msg- the message to send
-
messageReceived
public void messageReceived(byte[] msg)
-
processMessage
public void processMessage()
Description copied from interface:StateHandlerContextIndicate that the handler is ready to process a waiting message from the communication partner.- Specified by:
processMessagein interfaceStateHandlerContext
-
getState
public State getState()
- Specified by:
getStatein interfaceStateHandlerContext- Returns:
- the current state. Mostly useful for handlers that handle multiple states.
-
closeConnection
public void closeConnection()
Description copied from interface:StateHandlerContextClose the SPINE connection with no further messaging.- Specified by:
closeConnectionin interfaceStateHandlerContext
-
getConfig
public Configuration getConfig()
- Specified by:
getConfigin interfaceStateHandlerContext- Returns:
- the configuration for this node
-
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
-
stopTimeouts
public void stopTimeouts(SpecifiedTimeout... which)
Description copied from interface:StateHandlerContextStop one or more timeouts.- Specified by:
stopTimeoutsin interfaceStateHandlerContext- Parameters:
which- which timeout(s) to stop.
-
startTimeout
public void startTimeout(SpecifiedTimeout which)
Description copied from interface:StateHandlerContextStart a specific timeout with the default duration.- Specified by:
startTimeoutin interfaceStateHandlerContext- Parameters:
which- which timeout to start.
-
startTimeout
public void startTimeout(SpecifiedTimeout which, int fixedDelay)
Description copied from interface:StateHandlerContextStart a specific timeout with the given duration.- Specified by:
startTimeoutin interfaceStateHandlerContext- Parameters:
which- which timeout to startfixedDelay- how long it should run
-
startTimeout
public void startTimeout(SpecifiedTimeout which, int amount, java.util.concurrent.TimeUnit unit)
Description copied from interface:StateHandlerContextStart a specific timeout with the given duration.- Specified by:
startTimeoutin interfaceStateHandlerContext- Parameters:
which- which timeout to startamount- how long it should rununit- the unit foramount
-
getTimeoutStatus
public java.lang.Integer getTimeoutStatus(SpecifiedTimeout which)
Description copied from interface:StateHandlerContextQuery the status of a specific timeout.- Specified by:
getTimeoutStatusin interfaceStateHandlerContext- 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:
isServerSidein interfaceStateHandlerContext- Returns:
- whether the caller is on the server side of a connection.
-
requireTrust
public boolean requireTrust(int minimumTrust)
- Specified by:
requireTrustin interfaceStateHandlerContext- Parameters:
minimumTrust- required trust level- Returns:
- whether the communication partner is trusted at least this much
-
getTrustLevel
public int getTrustLevel()
-
increaseTimeout
public void increaseTimeout(SpecifiedTimeout which)
Description copied from interface:StateHandlerContextProlong a specific timeout by the initial value.- Specified by:
increaseTimeoutin interfaceStateHandlerContext- Parameters:
which- which timeout to prolong
-
increaseTimeout
public void increaseTimeout(SpecifiedTimeout which, int secondsDelta)
Description copied from interface:StateHandlerContextProlong a specific timeout.- Specified by:
increaseTimeoutin interfaceStateHandlerContext- Parameters:
which- which timeout to prolongsecondsDelta- how many seconds to add
-
setExtraData
public void setExtraData(java.lang.Object stateExtraData)
Description copied from interface:StateHandlerContextStore some arbitrary extra data.The handler must be marked with a
StateHandler.UsesExtraDataannotation indicating the type of the extra data it uses.- Specified by:
setExtraDatain interfaceStateHandlerContext- Parameters:
stateExtraData- the data to store.
-
getExtraData
public <T> T getExtraData(java.lang.Class<T> dataClass)
Description copied from interface:StateHandlerContextRetrieve data previously stored withStateHandlerContext.setExtraData(Object).The handler must be marked with a
StateHandler.UsesExtraDataannotation indicating the type of the extra data it uses.- Specified by:
getExtraDatain interfaceStateHandlerContext- Type Parameters:
T- type of the data to retrieve- Parameters:
dataClass- class of the data to retrieve- Returns:
- stored data, or null if not present.
-
getUserInterface
public UserInterface getUserInterface()
- Specified by:
getUserInterfacein interfaceStateHandlerContext- Returns:
- the interface to prompt the user about some action
-
enableConnectionDataExchange
public void enableConnectionDataExchange()
Description copied from interface:StateHandlerContextEnable Connection Data Exchange (it cannot be disabled afterward)- Specified by:
enableConnectionDataExchangein interfaceStateHandlerContext
-
setPeerSkiAuthenticated
public void setPeerSkiAuthenticated()
- Specified by:
setPeerSkiAuthenticatedin interfaceStateHandlerContext
-
setCommPartnerTrusted
public void setCommPartnerTrusted()
-
-