123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- /*
- * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Generic/TimeoutState.cs $
- *
- * 6 08-07-30 11:35 roger.månsson
- *
- * 5 08-05-07 15:35 roger.månsson
- * Obsoleted constructor with main object reference again. Instead, use
- * WritableMain property. When this is set on a composite state object,
- * the abstract method "ConfigureCompositeStateMachine" is called, and the
- * configuration of the state machine can be made.
- *
- * 4 08-04-24 15:45 Mattias.larsson
- *
- * 3 07-10-04 15:31 roger.månsson
- * Fixed glitch that let a timeout event slip out of the state even though
- * the state was left. It occured if the timeout was fired at the same
- * time the state exited for another reason.
- */
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Wayne.Lib.StateEngine.Generic
- {
- /// <summary>
- /// Generic timeout state class that has a main object of a generic type.
- /// </summary>
- /// <typeparam name="TMain">Specifies the type of the main object.</typeparam>
- public abstract class TimeoutState<TMain> : State<TMain>
- {
- #region Fields
- Wayne.Lib.StateEngine.Timer timer;
- object myToken;
- #endregion
-
- #region Methods
- /// <summary>
- /// See State.Enter
- /// </summary>
- /// <param name="stateEntry"></param>
- /// <param name="transition"></param>
- protected override void Enter(Wayne.Lib.StateEngine.StateEntry stateEntry, ref Wayne.Lib.StateEngine.Transition transition)
- {
- base.Enter(stateEntry, ref transition);
- myToken = new object();
- var timeoutInterval = TimeoutInterval;
- if (timeoutInterval > 0)
- {
- timer = new Wayne.Lib.StateEngine.Timer(this, GenericEventType.Timeout, timeoutInterval, myToken);
- ActivateTimer(timer);
- }
- }
- /// <summary>
- /// HandleEvent is sealed, use HandleNonTimeoutEvent method to override instead.
- /// </summary>
- /// <param name="stateEngineEvent"></param>
- /// <param name="transition"></param>
- protected sealed override void HandleEvent(Wayne.Lib.StateEngine.StateEngineEvent stateEngineEvent, ref Wayne.Lib.StateEngine.Transition transition)
- {
- base.HandleEvent(stateEngineEvent, ref transition);
- if (stateEngineEvent.Type.Equals(GenericEventType.Timeout))
- {
- Wayne.Lib.StateEngine.TimerEvent timerEvent = stateEngineEvent as Wayne.Lib.StateEngine.TimerEvent;
- if (timerEvent != null)
- {
- if (timerEvent.UserToken.Equals(myToken))
- {
- Timeout(ref transition);
- timerEvent.Handled = true;
- timer = null;
- }
- }
- }
- if (!stateEngineEvent.Handled)
- HandleNonTimeoutEvent(stateEngineEvent, ref transition);
- }
- /// <summary>
- /// See State.Exit
- /// </summary>
- protected override void Exit()
- {
- base.Exit();
- myToken = null;
- if (this.timer != null)
- {
- this.timer.Disable();
- this.timer = null;
- }
- }
- /// <summary>
- /// Method that is implemented by descendant classes to receive events that were not the timeout event.
- /// </summary>
- /// <param name="stateEngineEvent"></param>
- /// <param name="transition"></param>
- protected abstract void HandleNonTimeoutEvent(Wayne.Lib.StateEngine.StateEngineEvent stateEngineEvent, ref Wayne.Lib.StateEngine.Transition transition);
- /// <summary>
- /// Method that is used by descendant classes to set the timeout of the state.
- /// </summary>
- /// <returns></returns>
- protected abstract int TimeoutInterval { get;}
- /// <summary>
- /// Method that is used by descendant classes to be signaled when the timeout has fired.
- /// </summary>
- /// <param name="transition"></param>
- protected abstract void Timeout(ref Wayne.Lib.StateEngine.Transition transition);
- /// <summary>
- /// Cancels the currently running timer.
- /// </summary>
- protected void CancelTimer()
- {
- if (this.timer != null)
- {
- this.timer.Disable();
- this.timer = null;
- }
- }
- /// <summary>
- /// Restarts the timer. If a timer is active, it is restarted, and if the timer is not active anymore, it is started again.
- /// </summary>
- protected void ResetTimer()
- {
- CancelTimer();
- timer = new Wayne.Lib.StateEngine.Timer(this, GenericEventType.Timeout, TimeoutInterval, myToken);
- ActivateTimer(timer);
- }
- #endregion
- }
- }
|