Mercurial > pub > ImplabNet
annotate Implab/Components/RunnableComponent.cs @ 185:822aab37b107 ref20160224
runnable component, work in progress
author | cin |
---|---|
date | Mon, 18 Apr 2016 16:41:17 +0300 |
parents | d6a8cba73acc |
children | 75103928da09 |
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); | |
140 }, | |
141 r => { | |
142 lock(m_stateMachine) { | |
143 if (m_pending == promise) { | |
144 Move(Commands.Fail); | |
145 m_pending = null; | |
146 m_lastError = new OperationCanceledException("The operation has been cancelled", r); | |
147 } | |
148 | |
149 } | |
150 throw new OperationCanceledException("The operation has been cancelled", r); | |
184 | 151 } |
185 | 152 ); |
157 | 153 |
185 | 154 m_pending = promise; |
155 } | |
157 | 156 |
185 | 157 if (prev == null) |
158 task.Resolve(); | |
159 else | |
160 chain(prev, task); | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
161 |
185 | 162 return promise; |
156
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 |
184 | 165 |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
166 #region IInitializable 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 void Init() { |
184 | 169 Invoke(Commands.Init, OnInitialize); |
170 } | |
171 | |
172 protected virtual void OnInitialize() { | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
173 } |
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 #endregion |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
176 |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
177 #region IRunnable implementation |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
178 |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
179 public IPromise Start() { |
185 | 180 return InvokeAsync(Commands.Start, OnStart, null); |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
181 } |
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 protected virtual IPromise OnStart() { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
184 return Promise.SUCCESS; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
185 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
186 |
185 | 187 public IPromise Stop() { |
188 return InvokeAsync(Commands.Stop, OnStop, StopPending).Then(Dispose); | |
189 } | |
190 | |
191 protected virtual IPromise OnStop() { | |
192 return Promise.SUCCESS; | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
193 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
194 |
185 | 195 /// <summary> |
196 /// Stops the current operation if one exists. | |
197 /// </summary> | |
198 /// <param name="current">Current.</param> | |
199 /// <param name="stop">Stop.</param> | |
200 protected virtual void StopPending(IPromise current, IDeferred stop) { | |
201 if (current == null) { | |
202 stop.Resolve(); | |
203 } else { | |
204 current.On(stop.Resolve, stop.Reject, stop.CancelOperation); | |
205 current.Cancel(); | |
206 } | |
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 public ExecutionState State { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
210 get { |
185 | 211 return m_stateMachine.State; |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
212 } |
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 public Exception LastError { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
216 get { |
185 | 217 return m_lastError; |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
218 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
219 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
220 |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
221 #endregion |
185 | 222 |
223 #region IDisposable implementation | |
224 | |
225 public void Dispose() { | |
226 IPromise pending; | |
227 lock (m_stateMachine) { | |
228 if (m_stateMachine.State == ExecutionState.Disposed) | |
229 return; | |
230 | |
231 Move(Commands.Dispose); | |
232 | |
233 GC.SuppressFinalize(this); | |
234 | |
235 pending = m_pending; | |
236 m_pending = null; | |
237 } | |
238 if (pending != null) { | |
239 pending.Cancel(); | |
240 pending.Timeout(DisposeTimeout).On( | |
241 () => Dispose(true, null), | |
242 err => Dispose(true, err), | |
243 reason => Dispose(true, new OperationCanceledException("The operation is cancelled", reason)) | |
244 ); | |
245 } else { | |
246 Dispose(true, m_lastError); | |
247 } | |
248 } | |
249 | |
250 ~RunnableComponent() { | |
251 Dispose(false, null); | |
252 } | |
253 | |
254 #endregion | |
255 | |
256 protected virtual void Dispose(bool disposing, Exception lastError) { | |
257 | |
258 } | |
259 | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
260 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
261 } |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
diff
changeset
|
262 |