Package org.lsst.ccs.services
Class AgentStateService
java.lang.Object
org.lsst.ccs.services.AgentStateService
- All Implemented Interfaces:
BusMessagePreProcessor,ClusterMembershipListener,ServiceLifecycle,AgentService
public final class AgentStateService
extends Object
implements AgentService, ServiceLifecycle, BusMessagePreProcessor, ClusterMembershipListener
An AgentService to manage the internal state of an Agent.
- Author:
- The LSST CCS Team
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStateChangeListener(StateChangeListener stateChangeListener, Class... states) voidGet the name of this service.getBus()The Bus this BusMessagePreProcessor applies to.getComponentState(String component, Class stateClass) Returns the state of this Agent's component for the provided class.getState()Returns a clone of the current Agent state.Returns the state of this Agent for the provided class.getStateBundleForAgent(String agentName) Returns the object whose monitor lock guards write-access to the agent state.booleanisComponentInState(String component, Enum state) Check if a component of thisAgentis in the given state.booleanCheck if thisAgentis in the given state.booleanisInState(StateBundle state) Checks if thisAgentis in all of the states contained in the given StateBundle.voidmembersJoined(List<String> joined, boolean isMerge) voidmembersLeft(List<String> left) voidpreBuild()voidpreInit()Pre-process the incoming BusMessage and return the message to be forwarded to the bus message listeners.voidvoidpreStart()Called from the enclosingSubsystemwhenorg.lsst.ccs.Subsystem#start()has been called.final <T extends Enum<T>>
voidregisterState(Class<T> state, String description, Object obj) Register a State with the AgentStateService.voidremoveStateChangeListener(StateChangeListener stateChangeListener) booleanstartForAgent(AgentInfo agentInfo) This method is invoked after an agent service is created to see if this service is to be started for the tiven Agent.voidupdateAgentComponentState(Object component, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentComponentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Object component, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(String cause, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(String cause, org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(String cause, org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, StateBundle stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(StateBundle stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.voidupdateAgentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, StateBundle stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses.final voidupdateInternalState(String cause, AgentState... stateChanges) Updates the internal state of this Agent and publishes StateChangeNotification message.final voidupdateInternalState(AgentState... stateChanges) Updates the internal state of this Agent and publishes StateChangeNotification message.final booleanwaitFor(Predicate<StateBundle> target, long timeout, TimeUnit unit) Deprecated.final voidwaitForStatePredicate(Predicate<StateBundle> target, long timeout, TimeUnit unit) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.lsst.ccs.ServiceLifecycle
afterBuild, afterStart
-
Constructor Details
-
AgentStateService
public AgentStateService()
-
-
Method Details
-
getAgentServiceName
Description copied from interface:AgentServiceGet the name of this service. This name will be used to add the given service to the lookup tree.- Specified by:
getAgentServiceNamein interfaceAgentService- Returns:
- the AgentService name.
-
startForAgent
Description copied from interface:AgentServiceThis method is invoked after an agent service is created to see if this service is to be started for the tiven Agent.- Specified by:
startForAgentin interfaceAgentService- Returns:
- true if the service is to be added to the given agent.
-
preStart
public void preStart()Description copied from interface:ServiceLifecycleCalled from the enclosingSubsystemwhenorg.lsst.ccs.Subsystem#start()has been called.The preceding lifecycle step is
HasLifecycle.postInit()The following lifecycle step is
HasLifecycle.start()At the time it is called :-
the
PhaseStateisINITIALIZINGconnection with the messaging layer is effective, the enclosing subsystem has startedStatusHeartBeatpublication- Specified by:
preStartin interfaceServiceLifecycle
-
afterInit
public void afterInit()- Specified by:
afterInitin interfaceServiceLifecycle
-
preShutdown
public void preShutdown()- Specified by:
preShutdownin interfaceServiceLifecycle
-
preBuild
public void preBuild()- Specified by:
preBuildin interfaceServiceLifecycle
-
preInit
public void preInit()- Specified by:
preInitin interfaceServiceLifecycle
-
registerState
Register a State with the AgentStateService. Only registered states can be set on the Agent state. This registration step is only required for WORKERS.- Type Parameters:
T-- Parameters:
state- The State Enumeration Class to be registereddescription- The state descriptionobj- The object on which the state will be set.
-
getStateLock
Returns the object whose monitor lock guards write-access to the agent state. The state is guaranteed to remain unchanged while this lock is held.- Returns:
- the inner state lock to perform synchronized operations on states.
-
isInState
Check if thisAgentis in the given state.- Parameters:
state- The state to check.- Returns:
trueif thisAgentis in the specified state.
-
isComponentInState
Check if a component of thisAgentis in the given state.- Parameters:
component- The name of the componentstate- The state to check.- Returns:
trueif thisAgent's component is in the specified state.
-
isInState
Checks if thisAgentis in all of the states contained in the given StateBundle.- Parameters:
state- A StateBundle.- Returns:
trueif theAgentis in the provided StateBundle.
-
getState
Returns the state of this Agent for the provided class.- Parameters:
stateClass- The class of theEnumfor which we would like the Agent's state.- Returns:
- state for the specified class.
-
getComponentState
Returns the state of this Agent's component for the provided class.- Parameters:
component- The Agent's component namestateClass- The class of theEnumfor which we would like the Agent's state.- Returns:
- The component's state for the specified class.
-
updateAgentState
Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycleWhen this method is invoked the CCSTimeStamp.currentTime() is assigned to the state transition.- Parameters:
stateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycleWhen this method is invoked the CCSTimeStamp.currentTime() is assigned to the state transition.- Parameters:
cause- The reason the state transition happened.stateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
public void updateAgentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. The provided CCSTimeStamp is associated to the state transition. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycle- Parameters:
stateTransitionTimestamp- The CCSTimeStamp corresponding to this state transitionstateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
public void updateAgentState(String cause, org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. The provided CCSTimeStamp is associated to the state transition. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycle- Parameters:
cause- The reason the state transition happened.stateTransitionTimestamp- The CCSTimeStamp corresponding to this state transitionstateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycleWhen this method is invoked the CCSTimeStamp.currentTime() is assigned to the state transition.- Parameters:
stateChanges- The StateBundle with all the changes.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
public void updateAgentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, StateBundle stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. The provided CCSTimeStamp is associated to the state transition. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycle- Parameters:
stateTransitionTimestamp- The CCSTimeStamp corresponding to this state transitionstateChanges- The StateBundle with all the changes.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentState
public void updateAgentState(String cause, org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, StateBundle stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. The provided CCSTimeStamp is associated to the state transition. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycle- Parameters:
cause- The reason the state transition happened.stateTransitionTimestamp- The CCSTimeStamp corresponding to this state transitionstateChanges- The StateBundle with all the changes.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentComponentState
Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. When this method is invoked the CCSTimeStamp.currentTime() is assigned to the state transition. This method can be invoked at any stage ofHasLifecycle- Parameters:
component- The name of the component to be updatedstateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateAgentComponentState
public void updateAgentComponentState(org.lsst.ccs.utilities.taitime.CCSTimeStamp stateTransitionTimestamp, Object component, Enum... stateChanges) Updates the state of this Agent and publishes StateChangeNotification message only if the Agent is connected to the buses. The provided CCSTimeStamp is associated to the state transition. Note that this method should be used to modify subsystem-defined states only. Internal Agent states (AlertState, CommandState, OperationalState, PhaseState) are managed by the Agent. This method will produce a StatusStateChangeNotification to be published on the buses only if there are state changes. This method can be invoked at any stage ofHasLifecycle- Parameters:
stateTransitionTimestamp- The CCSTimeStamp corresponding to this state transitioncomponent- The name of the component to be updatedstateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the list of arguments contains internal Agent states.
-
updateInternalState
Updates the internal state of this Agent and publishes StateChangeNotification message.- Parameters:
stateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the update would bring this Agent into a prohibited state
-
updateInternalState
Updates the internal state of this Agent and publishes StateChangeNotification message.- Parameters:
cause- The reason the state transition happened.stateChanges- The list of the states that have changed.- Throws:
IllegalArgumentException- if the update would bring this Agent into a prohibited state
-
waitFor
@Deprecated public final boolean waitFor(Predicate<StateBundle> target, long timeout, TimeUnit unit) throws InterruptedException Deprecated.Block until thisAgentis in state that satisfies the specified predicate. The condition is not guaranteed to remaintruewhen the thread resumes. The predicate provided to this method should not depend on anything other than its argument, and should not have side effects.- Parameters:
target- Condition to be satisfied for this method to return.timeout- Timeout value.unit- Timeout unit.- Returns:
Trueif the condition was satisfied;falseif the method timed out.- Throws:
InterruptedException- if the thread is interrupted while waiting.
-
waitForStatePredicate
public final void waitForStatePredicate(Predicate<StateBundle> target, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException - Throws:
InterruptedExceptionTimeoutException
-
addStateChangeListener
-
removeStateChangeListener
-
getState
Returns a clone of the current Agent state. The returned object is a snapshot of the current state. It is not modified by subsequent changes to the agent state.- Returns:
- A clone of the current Agent state.
-
preProcessMessage
Description copied from interface:BusMessagePreProcessorPre-process the incoming BusMessage and return the message to be forwarded to the bus message listeners. The returned message can either be a modified version of the original message or a brand new message. The Class of the returned message can be different than the incoming message. If null is returned the message will not be sent to the BusMessage listeners, but it will still be passed to other BusMessagePreProcessors This method can be invoked by separate threads, but it's invoked by one thread at a time; there is no concurrent invocation of the method.- Specified by:
preProcessMessagein interfaceBusMessagePreProcessor- Parameters:
msg- The incoming BusMessage- Returns:
- The modified BusMessage to be forwarded to the BusMessageListeners
-
getStateBundleForAgent
-
getBus
Description copied from interface:BusMessagePreProcessorThe Bus this BusMessagePreProcessor applies to.- Specified by:
getBusin interfaceBusMessagePreProcessor- Returns:
- The Bus.
-
membersJoined
- Specified by:
membersJoinedin interfaceClusterMembershipListener
-
membersLeft
- Specified by:
membersLeftin interfaceClusterMembershipListener
-