StateManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using Wayne.Lib;
  4. using Wayne.Lib.Log;
  5. using Wayne.Lib.StateEngine;
  6. using Wayne.Lib.StateEngine.Generic;
  7. namespace SinochemInternetPlusApp
  8. {
  9. public enum TimeoutType
  10. {
  11. }
  12. /// <summary>
  13. /// The base class for the Managers in Eps. It contains some support functionality that can be shared by the
  14. /// descendant classes.
  15. /// </summary>
  16. public abstract class StateManager : IDisposable, IStateFactory, IIdentifiableEntity
  17. {
  18. static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("SinochemEpsApp");
  19. #region Fields
  20. protected StateMachine stateMachine;
  21. protected DebugLogger debugLogger;
  22. private bool disposed = false;
  23. private int id;
  24. public int Id { get { return id; } }
  25. #endregion
  26. public DebugLogger DebugLogger { get { return debugLogger; } }
  27. #region Constuction
  28. /// <summary>
  29. /// Initializes a new instance of the CustomManager class.
  30. /// </summary>
  31. protected StateManager(int id, string name)
  32. {
  33. this.id = id;
  34. debugLogger = new DebugLogger(this);
  35. stateMachine = StateMachine.Create(name, StateMachineType.Synchronous, debugLogger, null);
  36. stateMachine.AddStateFactory(new StateFactory<StateManager>(this, null));
  37. }
  38. /// <summary>
  39. /// Finalizer
  40. /// </summary>
  41. ~StateManager()
  42. {
  43. Dispose(false);
  44. }
  45. #endregion
  46. #region IDisposable Members
  47. public void Dispose()
  48. {
  49. Dispose(true);
  50. GC.SuppressFinalize(this);
  51. }
  52. protected virtual void Dispose(bool disposing)
  53. {
  54. if (disposed) return;
  55. disposed = true;
  56. if (disposing)
  57. {
  58. stateMachine.Dispose();
  59. debugLogger.Dispose();
  60. }
  61. }
  62. #endregion
  63. #region Methods
  64. // States.CONFIGURATION.Config(stateMachine.StateTransitionLookup);
  65. protected abstract void ConfigStates();
  66. /// <summary>
  67. /// Starts the manager.
  68. /// </summary>
  69. public virtual void Start()
  70. {
  71. ConfigStates();
  72. stateMachine.Start();
  73. }
  74. public int GetTimeout(State state, TimeoutType timeoutType)
  75. {
  76. return 1000;
  77. }
  78. #endregion
  79. #region IStateFactory
  80. /// <summary>
  81. /// This method should be overridden for each manager that is defined in another assembly than it's parent manager. I.e. an extension of EpsManager
  82. /// defined in another assembly than Wayne.Eps must override this method and add the same code there also.
  83. /// </summary>
  84. /// <param name="stateFactoryName"></param>
  85. /// <returns></returns>
  86. public virtual State CreateState(string stateFactoryName)
  87. {
  88. Type stateType = Type.GetType(stateFactoryName);
  89. if (stateType != null)
  90. {
  91. System.Reflection.ConstructorInfo constructorInfo = stateType.GetConstructor(new Type[0]);
  92. object stateObject = constructorInfo.Invoke(new object[0]);
  93. PrepareState(stateObject);
  94. return stateObject as State;
  95. }
  96. return null;
  97. }
  98. /// <summary>
  99. /// This method should be overridden by each manager type that defines generic states with it's own type.
  100. /// </summary>
  101. /// <param name="stateObject"></param>
  102. protected virtual void PrepareState(object stateObject)
  103. {
  104. IGenericState<StateManager> genericClientState = stateObject as IGenericState<StateManager>;
  105. if (genericClientState != null)
  106. genericClientState.WritableMain = this;
  107. CompositeState<StateManager> compositeState = stateObject as CompositeState<StateManager>;
  108. if (compositeState != null)
  109. compositeState.StateMachine.AddStateFactory(this);
  110. }
  111. public string Name
  112. {
  113. get { return this.GetType().Name; }
  114. }
  115. public abstract string EntityType { get; }
  116. public string FullEntityName { get => EntityType + "_" + Name; set => throw new NotImplementedException(); }
  117. public abstract string EntitySubType { get; }
  118. public IIdentifiableEntity ParentEntity => null;
  119. #endregion
  120. }
  121. }