Mercurial > pub > ImplabNet
annotate Implab/Components/RunnableComponent.cs @ 187:dd4a3590f9c6 ref20160224
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
Any unhandled OperationCanceledException will cause the promise cancelation
| author | cin |
|---|---|
| date | Tue, 19 Apr 2016 17:35:20 +0300 |
| parents | 75103928da09 |
| children | 40d7fed4a09e |
| rev | line source |
|---|---|
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
1 using System; |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
2 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
3 namespace Implab.Components { |
| 185 | 4 public abstract class RunnableComponent : IDisposable, IRunnable, IInitializable { |
| 184 | 5 enum Commands { |
| 6 Ok = 0, | |
| 7 Fail, | |
| 8 Init, | |
| 9 Start, | |
| 10 Stop, | |
| 11 Dispose, | |
| 12 Last = Dispose | |
| 13 } | |
| 14 | |
| 15 class StateMachine { | |
| 16 static readonly ExecutionState[,] _transitions; | |
| 17 | |
| 18 static StateMachine() { | |
| 19 _transitions = new ExecutionState[(int)ExecutionState.Last + 1, (int)Commands.Last + 1]; | |
| 20 | |
| 185 | 21 Edge(ExecutionState.Created, ExecutionState.Initializing, Commands.Init); |
| 22 Edge(ExecutionState.Created, ExecutionState.Disposed, Commands.Dispose); | |
| 23 | |
| 24 Edge(ExecutionState.Initializing, ExecutionState.Ready, Commands.Ok); | |
| 25 Edge(ExecutionState.Initializing, ExecutionState.Failed, Commands.Fail); | |
| 184 | 26 |
| 27 Edge(ExecutionState.Ready, ExecutionState.Starting, Commands.Start); | |
| 28 Edge(ExecutionState.Ready, ExecutionState.Disposed, Commands.Dispose); | |
| 29 | |
| 30 Edge(ExecutionState.Starting, ExecutionState.Running, Commands.Ok); | |
| 31 Edge(ExecutionState.Starting, ExecutionState.Failed, Commands.Fail); | |
| 32 Edge(ExecutionState.Starting, ExecutionState.Stopping, Commands.Stop); | |
| 33 Edge(ExecutionState.Starting, ExecutionState.Disposed, Commands.Dispose); | |
| 34 | |
| 35 Edge(ExecutionState.Running, ExecutionState.Failed, Commands.Fail); | |
| 36 Edge(ExecutionState.Running, ExecutionState.Stopping, Commands.Stop); | |
| 37 Edge(ExecutionState.Running, ExecutionState.Disposed, Commands.Dispose); | |
| 38 | |
| 39 Edge(ExecutionState.Stopping, ExecutionState.Failed, Commands.Fail); | |
| 40 Edge(ExecutionState.Stopping, ExecutionState.Disposed, Commands.Ok); | |
| 185 | 41 |
| 42 Edge(ExecutionState.Failed, ExecutionState.Disposed, Commands.Dispose); | |
| 184 | 43 } |
| 44 | |
| 45 static void Edge(ExecutionState s1, ExecutionState s2, Commands cmd) { | |
| 46 _transitions[(int)s1, (int)cmd] = s2; | |
| 47 } | |
| 48 | |
| 49 public ExecutionState State { | |
| 50 get; | |
| 51 private set; | |
| 52 } | |
| 53 | |
| 54 public StateMachine(ExecutionState initial) { | |
| 55 State = initial; | |
| 56 } | |
| 57 | |
| 58 public bool Move(Commands cmd) { | |
| 59 var next = _transitions[(int)State, (int)cmd]; | |
| 60 if (next == ExecutionState.Undefined) | |
| 61 return false; | |
| 62 State = next; | |
| 63 return true; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 IPromise m_pending; | |
| 68 Exception m_lastError; | |
| 69 | |
| 70 readonly StateMachine m_stateMachine; | |
| 71 | |
| 72 protected RunnableComponent(bool initialized) { | |
| 73 m_stateMachine = new StateMachine(initialized ? ExecutionState.Ready : ExecutionState.Created); | |
| 74 } | |
| 75 | |
| 185 | 76 protected virtual int DisposeTimeout { |
| 77 get { | |
| 78 return 10000; | |
| 79 } | |
| 80 } | |
| 81 | |
| 184 | 82 void ThrowInvalidCommand(Commands cmd) { |
| 185 | 83 if (m_stateMachine.State == ExecutionState.Disposed) |
| 84 throw new ObjectDisposedException(ToString()); | |
| 85 | |
| 184 | 86 throw new InvalidOperationException(String.Format("Commnd {0} is not allowed in the state {1}", cmd, m_stateMachine.State)); |
| 87 } | |
| 88 | |
| 185 | 89 void Move(Commands cmd) { |
| 90 if (!m_stateMachine.Move(cmd)) | |
| 91 ThrowInvalidCommand(cmd); | |
| 184 | 92 } |
| 93 | |
| 185 | 94 void Invoke(Commands cmd, Action action) { |
| 95 lock (m_stateMachine) | |
| 96 Move(cmd); | |
| 97 | |
| 184 | 98 try { |
| 99 action(); | |
| 185 | 100 lock(m_stateMachine) |
| 101 Move(Commands.Ok); | |
| 102 | |
| 184 | 103 } catch (Exception err) { |
| 185 | 104 lock (m_stateMachine) { |
| 105 Move(Commands.Fail); | |
| 106 m_lastError = err; | |
| 107 } | |
| 184 | 108 throw; |
| 109 } | |
| 110 } | |
| 111 | |
| 185 | 112 IPromise InvokeAsync(Commands cmd, Func<IPromise> action, Action<IPromise, IDeferred> chain) { |
| 113 IPromise promise = null; | |
| 114 IPromise prev; | |
| 184 | 115 |
| 185 | 116 var task = new ActionChainTask(action, null, null, true); |
| 117 | |
| 118 lock (m_stateMachine) { | |
| 119 Move(cmd); | |
| 120 | |
| 121 prev = m_pending; | |
| 184 | 122 |
| 185 | 123 promise = task.Then( |
| 124 () => { | |
| 125 lock(m_stateMachine) { | |
| 126 if (m_pending == promise) { | |
| 127 Move(Commands.Ok); | |
| 128 m_pending = null; | |
| 129 } | |
| 184 | 130 } |
| 185 | 131 }, e => { |
| 132 lock(m_stateMachine) { | |
| 133 if (m_pending == promise) { | |
| 134 Move(Commands.Fail); | |
| 135 m_pending = null; | |
| 136 m_lastError = e; | |
| 137 } | |
| 138 } | |
| 139 throw new PromiseTransientException(e); | |
| 184 | 140 } |
| 185 | 141 ); |
| 157 | 142 |
| 185 | 143 m_pending = promise; |
| 144 } | |
| 157 | 145 |
| 185 | 146 if (prev == null) |
| 147 task.Resolve(); | |
| 148 else | |
| 149 chain(prev, task); | |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
150 |
| 185 | 151 return promise; |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
152 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
153 |
| 184 | 154 |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
155 #region IInitializable implementation |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
156 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
157 public void Init() { |
| 184 | 158 Invoke(Commands.Init, OnInitialize); |
| 159 } | |
| 160 | |
| 161 protected virtual void OnInitialize() { | |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
162 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
163 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
164 #endregion |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
165 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
166 #region IRunnable implementation |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
167 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
168 public IPromise Start() { |
| 185 | 169 return InvokeAsync(Commands.Start, OnStart, null); |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
170 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
171 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
172 protected virtual IPromise OnStart() { |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
173 return Promise.SUCCESS; |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
174 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
175 |
| 185 | 176 public IPromise Stop() { |
| 177 return InvokeAsync(Commands.Stop, OnStop, StopPending).Then(Dispose); | |
| 178 } | |
| 179 | |
| 180 protected virtual IPromise OnStop() { | |
| 181 return Promise.SUCCESS; | |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
182 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
183 |
| 185 | 184 /// <summary> |
| 185 /// Stops the current operation if one exists. | |
| 186 /// </summary> | |
| 187 /// <param name="current">Current.</param> | |
| 188 /// <param name="stop">Stop.</param> | |
| 189 protected virtual void StopPending(IPromise current, IDeferred stop) { | |
| 190 if (current == null) { | |
| 191 stop.Resolve(); | |
| 192 } else { | |
|
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
193 // связваем текущую операцию с операцией остановки |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
194 current.On( |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
195 stop.Resolve, // если текущая операция заверщилась, то можно начинать остановку |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
196 stop.Reject, // если текущая операция дала ошибку - то все плохо, нельзя продолжать |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
197 e => stop.Resolve() // если текущая отменилась, то можно начинать остановку |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
198 ); |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
199 // посылаем текущей операции сигнал остановки |
| 185 | 200 current.Cancel(); |
| 201 } | |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
202 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
203 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
204 public ExecutionState State { |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
205 get { |
| 185 | 206 return m_stateMachine.State; |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
207 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
208 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
209 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
210 public Exception LastError { |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
211 get { |
| 185 | 212 return m_lastError; |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
213 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
214 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
215 |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
216 #endregion |
| 185 | 217 |
| 218 #region IDisposable implementation | |
| 219 | |
| 220 public void Dispose() { | |
| 221 IPromise pending; | |
| 222 lock (m_stateMachine) { | |
| 223 if (m_stateMachine.State == ExecutionState.Disposed) | |
| 224 return; | |
| 225 | |
| 226 Move(Commands.Dispose); | |
| 227 | |
| 228 GC.SuppressFinalize(this); | |
| 229 | |
| 230 pending = m_pending; | |
| 231 m_pending = null; | |
| 232 } | |
| 233 if (pending != null) { | |
| 234 pending.Cancel(); | |
| 235 pending.Timeout(DisposeTimeout).On( | |
| 236 () => Dispose(true, null), | |
| 237 err => Dispose(true, err), | |
| 238 reason => Dispose(true, new OperationCanceledException("The operation is cancelled", reason)) | |
| 239 ); | |
| 240 } else { | |
| 241 Dispose(true, m_lastError); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 ~RunnableComponent() { | |
| 246 Dispose(false, null); | |
| 247 } | |
| 248 | |
| 249 #endregion | |
| 250 | |
| 251 protected virtual void Dispose(bool disposing, Exception lastError) { | |
| 252 | |
| 253 } | |
| 254 | |
|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
255 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
256 } |
|
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
257 |
