|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.pogamut.base.component.controller.AbstractComponentControllerBase<COMPONENT> cz.cuni.amis.pogamut.base.component.controller.SharedComponentController<COMPONENT>
public class SharedComponentController<COMPONENT extends ISharedComponent>
Shared component controller is meant to provide management of lifecycle methods of the ISharedComponent
while providing methods that has to be implemented by any ISharedComponent
.
You can't actualy create ISharedComponent
by extending this class, you have to create your own class implementing
ISharedComponent
interface and instantiate this class (wrap it) for yourself (and delegete all calls to methods
from ISharedComponent
to this class).
This decision has been deliberately made in order to allow anybody to create their shared components out of blue and add lifecycle-management later on using this object.
To get the impression of what this class is doing, first read javadoc for ComponentController
. Read? Good! So this
object does something similar as ComponentController
but not for simple IComponent
but for ISharedComponent
which
means that we're dealing with the component that is shared by multiple agent instances, i.e., component that will be active on
multiple ILifecycleBus
es. Which means that we must auto-start/stop controlled component differently than ComponentController
is
doing.
The component should be started when any agent using it is starting and should be stop when the last agent that is using it is stopping. Similar things applies for pause/resume. Simple to say, harder to perform. Nevertheless, the implementation is here ;-)
Nested Class Summary | |
---|---|
protected class |
SharedComponentController.ComponentStateListener
|
protected class |
SharedComponentController.ControlHelper
|
protected class |
SharedComponentController.LocalController
Used as IComponentControlHelper that is passed to every ILifecycleBus.addLifecycleManagement(IComponent, IComponentControlHelper, ComponentDependencies)
sensing decisions of ordinary ComponentController that signalizes when the component should be started for a given bus. |
Nested classes/interfaces inherited from class cz.cuni.amis.pogamut.base.component.controller.AbstractComponentControllerBase |
---|
AbstractComponentControllerBase.AwaitState |
Field Summary | |
---|---|
protected Map<ComponentState,Integer> |
componentStateCount
Map tracking count of states of dependencies. |
protected Map<IAgentId,ComponentState> |
componentStates
This map holds the currently desired AbstractComponentControllerBase.component state inside the agent's bus. |
protected Object |
ctrlMutex
Mutex that is synchronizing access to internal data structures of the controller. |
protected IFatalErrorEvent |
fatalError
Last fatal error sensed. |
protected Map<IAgentId,SharedComponentController.ControlHelper> |
localControlHelpers
Control helpers that are used to signalizes starting/stopping of the controlled AbstractComponentControllerBase.component inside
bus of respective agents. |
protected Map<IAgentId,SharedComponentController.LocalController> |
localControllers
Controllers that are used to store IComponentController provided by respective ILifecycleBus.addLifecycleManagement(IComponent, IComponentControlHelper, ComponentDependencies)
of agents using the component. |
Fields inherited from class cz.cuni.amis.pogamut.base.component.controller.AbstractComponentControllerBase |
---|
broadcastingEvents, component, componentState, control, controllerId, log |
Constructor Summary | |
---|---|
SharedComponentController(COMPONENT component,
ISharedComponentControlHelper componentControl,
Logger log)
Default constructor. |
Method Summary | |
---|---|
void |
addComponentBus(IAgentId agentId,
ILifecycleBus bus,
ComponentDependencies dependencies)
Informs the component that it is part of another ILifecycleBus , i.e., it has become used by new agent with 'agentId'. |
protected void |
agentStoppedUsingTheComponent(IAgentId agentId,
ComponentState oldState)
|
protected int |
alterStateCount(ComponentState state,
int change)
Changes the count (by 'change') of the 'state', DOES NOT TRIGGER SharedComponentController#componentStateCountChanged() . |
protected void |
checkStateCount(int newCount,
ComponentState state)
Checks sanity of the 'state' count 'newCount' |
protected void |
componentStateChanged(IAgentId origin,
ComponentState oldState,
ComponentState newState)
|
protected void |
componentStateCountChanged(IAgentId origin,
ComponentState oldState,
ComponentState newState)
|
protected int |
decreaseStateCount(ComponentState state)
Decreases (-1) count of the 'state', DOES NOT TRIGGER SharedComponentController#componentStateCountChanged() . |
protected int |
decreaseStateCount(ComponentState state,
int n)
Decreases (-n) count of the 'state', DOES NOT TRIGGER SharedComponentController#componentStateCountChanged() . |
void |
fatalError(String message)
Broadcasts fatal error with controlled component as source. |
void |
fatalError(String message,
Throwable e)
Broadcasts fatal error with controlled component as source. |
ISharedComponentControlHelper |
getComponentControl()
Returns component control with lifecycle methods of the component controlled by this instance. |
IFatalErrorEvent |
getFatalError()
Returns last fatal error event that has triggered the system failure. |
int |
getStateCount(ComponentState... states)
Return how many components are in one of 'states' |
protected int |
increaseStateCount(ComponentState state)
Increases (+1) count of the 'state', DOES NOT TRIGGER SharedComponentController#componentStateCountChanged() . |
protected int |
increaseStateCount(ComponentState state,
int n)
Increases count (+n) of the 'state', DOES NOT TRIGGER SharedComponentController#componentStateCountChanged() . |
boolean |
isDependent(IAgentId agentId,
IComponent component)
Whether the controlled component is dependent on 'component' of the agent identified by 'agentId'. |
boolean |
isDependent(IAgentId agentId,
cz.cuni.amis.utils.token.IToken componentId)
Whether the controlled component is dependent on component (identified by 'componentId') of the agent identified by 'agentId'. |
boolean |
isUsedBy(IAgentId agentId)
Tells whether the agent identified by 'agentId' is currently using the controlled component, i.e., this component controller registers the component to agent's ILifecycleBus and is watching it for auto start/stop/pause/resume/... |
protected void |
kill(IAgentId agentId,
String message,
Throwable cause)
AgentId has broadcast IFatalErrorEvent , tear down the whole system! |
protected void |
localKill(IAgentId agentId)
|
protected void |
localPause(IAgentId agentId)
|
protected void |
localPrePause(IAgentId agentId)
|
protected void |
localPreResume(IAgentId agentId)
|
protected void |
localPreStart(IAgentId agentId)
|
protected void |
localPreStartPaused(IAgentId agentId)
|
protected void |
localPreStop(IAgentId agentId)
|
protected void |
localReset(IAgentId agentId)
|
protected void |
localResume(IAgentId agentId)
|
protected void |
localStart(IAgentId agentId)
|
protected void |
localStartPaused(IAgentId agentId)
|
protected void |
localStop(IAgentId agentId)
|
void |
manualKill(String reason)
Provides the way to kill the component (constructor of this controller). |
void |
manualPause(String reason)
Provides the way to pause the component (constructor of this controller). |
void |
manualResume(String reason)
Provides the way to pause the component (constructor of this controller). |
void |
manualStart(String reason)
Provides the way to manually start the component. |
void |
manualStartPaused(String reason)
Provides the way to manually start the component into paused state. |
void |
manualStop(String reason)
Provides the way to stop the component (constructor of this controller). |
protected void |
newAgentIsUsingTheComponent(IAgentId agentId,
ComponentState state)
|
protected void |
pause()
|
void |
removeComponentBus(IAgentId agentId,
ILifecycleBus bus)
Informs the component that it ceased to be the part of the ILifecycleBus , i.e., it has stopped to be used by agent with 'agentId'. |
protected void |
reset()
|
protected void |
resume()
|
protected void |
setState(IAgentId agentId,
ComponentState newState)
Changes the state of the component for given agentId, triggers SharedComponentController#componentStateCountChanged() . |
protected void |
setState(IAgentId agentId,
ComponentState oldState,
ComponentState newState)
Changes the state of the component for given agentId, triggers SharedComponentController#componentStateCountChanged() . |
protected void |
start()
|
protected void |
startPaused()
|
protected void |
stateChanged(IAgentId agentId,
ComponentState changedValue)
Signal that the component in the bus of agent identified by 'agentId' has changed into 'changedValue'. |
protected void |
stop()
|
Methods inherited from class cz.cuni.amis.pogamut.base.component.controller.AbstractComponentControllerBase |
---|
awaitState, awaitState, getComponent, getComponentId, getLog, getState, id, inState, isBroadcastingEvents, isPaused, isRunning, notInState, setBroadcastingEvents, setState, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface cz.cuni.amis.pogamut.base.component.controller.IComponentControllerBase |
---|
awaitState, awaitState, getComponent, getState, inState, isBroadcastingEvents, isPaused, isRunning, notInState, setBroadcastingEvents |
Methods inherited from interface cz.cuni.amis.pogamut.base.component.IComponent |
---|
getComponentId |
Field Detail |
---|
protected Object ctrlMutex
protected Map<IAgentId,SharedComponentController.ControlHelper> localControlHelpers
AbstractComponentControllerBase.component
inside
bus of respective agents.
protected Map<IAgentId,SharedComponentController.LocalController> localControllers
IComponentController
provided by respective ILifecycleBus.addLifecycleManagement(IComponent, IComponentControlHelper, ComponentDependencies)
of agents using the component.
protected Map<IAgentId,ComponentState> componentStates
AbstractComponentControllerBase.component
state inside the agent's bus.
protected Map<ComponentState,Integer> componentStateCount
protected IFatalErrorEvent fatalError
Constructor Detail |
---|
public SharedComponentController(COMPONENT component, ISharedComponentControlHelper componentControl, Logger log)
component
- componentControl
- log
- Method Detail |
---|
public ISharedComponentControlHelper getComponentControl()
IComponentControllerBase
IComponentControllerBase.getComponent()
.
IT IS DISCOURAGED TO USE METHODS OF THE IComponentControlHelper
DIRECTLY! IT DEFIES THE PURPOSE OF THE CONTROLLER TOTALLY
AND THE CONTROLLER WILL PROBABLY WON'T COPE WITH SUCH BEHAVIOR.
But what the hell, if it solves your problem, go ahead ;-)
getComponentControl
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
getComponentControl
in class AbstractComponentControllerBase<COMPONENT extends ISharedComponent>
public void addComponentBus(IAgentId agentId, ILifecycleBus bus, ComponentDependencies dependencies)
ISharedComponent
ILifecycleBus
, i.e., it has become used by new agent with 'agentId'.
The component is obliged to register to that bus a watch for the lifecycle state of various components inside the bus.
addComponentBus
in interface ISharedComponent
public void removeComponentBus(IAgentId agentId, ILifecycleBus bus)
ISharedComponent
ILifecycleBus
, i.e., it has stopped to be used by agent with 'agentId'.
removeComponentBus
in interface ISharedComponent
public boolean isUsedBy(IAgentId agentId)
ISharedComponentController
ILifecycleBus
and is watching it for auto start/stop/pause/resume/...
isUsedBy
in interface ISharedComponentController<COMPONENT extends ISharedComponent>
public boolean isDependent(IAgentId agentId, cz.cuni.amis.utils.token.IToken componentId)
ISharedComponentController
Note that two IComponent
belonging to different agents may have the same 'componentId'.
isDependent
in interface ISharedComponentController<COMPONENT extends ISharedComponent>
public boolean isDependent(IAgentId agentId, IComponent component)
ISharedComponentController
isDependent
in interface ISharedComponentController<COMPONENT extends ISharedComponent>
public void fatalError(String message)
IComponentControllerBase
Sets state to KILLING, broadcasts IFatalErrorEvent
and then sets the state to KILLED.
WARNING: Note that the ComponentController assumes that you will kill your component yourself before or after you call this method. Therefore the components kill() method won't be called. That's because whenever fatal error occurs, the component is in undefined state and you have to decide what to do based on that fatal error.
fatalError
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
public void fatalError(String message, Throwable e)
IComponentControllerBase
Sets state to KILLING, broadcasts IFatalErrorEvent
and then sets the state to KILLED.
WARNING: Note that the ComponentController assumes that you will kill your component yourself before or after you call this method. Therefore the components kill() method won't be called. That's because whenever fatal error occurs, the component is in undefined state and you have to decide what to do based on that fatal error.
fatalError
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
public IFatalErrorEvent getFatalError()
IComponentControllerBase
getFatalError
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
public void manualStart(String reason)
IComponentControllerBase
manualStart
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
public void manualStartPaused(String reason)
IComponentControllerBase
manualStartPaused
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
public void manualPause(String reason)
IComponentControllerBase
Note that you should not use IComponentControlHelper
.pause() alone to stop your component
as it won't produce IPausingEvent
and IPausedEvent
.
manualPause
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
reason
- why the component is pausingpublic void manualResume(String reason)
IComponentControllerBase
Note that you should not use IComponentControlHelper
.pause() alone to stop your component
as it won't produce IPausingEvent
and IPausedEvent
.
manualResume
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
reason
- why the component is pausingpublic void manualStop(String reason)
IComponentControllerBase
Note that you should not use IComponentControlHelper
.stop() alone to stop your component
as it won't produce IStoppingEvent
and IStoppedEvent
.
If you require your component to stop prematurely - call this method.
manualStop
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
reason
- why the component is stoppingpublic void manualKill(String reason)
IComponentControllerBase
Note that you should not use IComponentControlHelper
.kill() alone to stop your component
as it won't produce IFatalErrorEvent
.
If you require your component to stop prematurely - call this method.
manualKill
in interface IComponentControllerBase<COMPONENT extends ISharedComponent>
reason
- why the component is stoppingpublic int getStateCount(ComponentState... states)
states
-
protected void checkStateCount(int newCount, ComponentState state)
newCount
- state
- protected int alterStateCount(ComponentState state, int change)
SharedComponentController#componentStateCountChanged()
.
state
- protected int increaseStateCount(ComponentState state)
SharedComponentController#componentStateCountChanged()
.
state
- protected int increaseStateCount(ComponentState state, int n)
SharedComponentController#componentStateCountChanged()
.
state
- protected int decreaseStateCount(ComponentState state)
SharedComponentController#componentStateCountChanged()
.
state
- protected int decreaseStateCount(ComponentState state, int n)
SharedComponentController#componentStateCountChanged()
.
state
- protected void localPreStart(IAgentId agentId)
protected void localStart(IAgentId agentId)
protected void localPreStartPaused(IAgentId agentId)
protected void localStartPaused(IAgentId agentId)
protected void localPrePause(IAgentId agentId)
protected void localPause(IAgentId agentId)
protected void localPreResume(IAgentId agentId)
protected void localResume(IAgentId agentId)
protected void localPreStop(IAgentId agentId)
protected void localStop(IAgentId agentId)
protected void localKill(IAgentId agentId)
protected void localReset(IAgentId agentId)
protected void stateChanged(IAgentId agentId, ComponentState changedValue)
protected void setState(IAgentId agentId, ComponentState newState)
SharedComponentController#componentStateCountChanged()
.
agentId
- newState
- may be null (== agentId has been deleted)protected void setState(IAgentId agentId, ComponentState oldState, ComponentState newState)
SharedComponentController#componentStateCountChanged()
.
agentId
- oldState
- may be null (== there was no previous state stored), MUST BE CORRECT!newState
- may be null (== agentId has been deleted)protected void componentStateCountChanged(IAgentId origin, ComponentState oldState, ComponentState newState)
protected void newAgentIsUsingTheComponent(IAgentId agentId, ComponentState state)
protected void agentStoppedUsingTheComponent(IAgentId agentId, ComponentState oldState)
protected void componentStateChanged(IAgentId origin, ComponentState oldState, ComponentState newState)
protected void start()
protected void startPaused()
protected void pause()
protected void resume()
protected void stop()
protected void kill(IAgentId agentId, String message, Throwable cause)
IFatalErrorEvent
, tear down the whole system!
agentId
- may be nullprotected void reset()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |