|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.ComponentModel; |
|
22 using SymbianUtils.Tracer; |
|
23 |
|
24 namespace SymbianUtils.SerializedOperations |
|
25 { |
|
26 public class SerializedOperationManager : DisposableObject, ITracer |
|
27 { |
|
28 #region Enumerations |
|
29 public enum TState |
|
30 { |
|
31 EStateOperationsStarted = 0, |
|
32 EStateOperationsSuspended, |
|
33 EStateOperationsCompleted |
|
34 } |
|
35 #endregion |
|
36 |
|
37 #region Delegates & events |
|
38 public delegate void QueueStateHandler( TState aState ); |
|
39 public event QueueStateHandler StateHandler; |
|
40 #endregion |
|
41 |
|
42 #region Constructors |
|
43 public SerializedOperationManager() |
|
44 : this( null ) |
|
45 { |
|
46 } |
|
47 |
|
48 public SerializedOperationManager( ITracer aTracer ) |
|
49 { |
|
50 iTracer = aTracer; |
|
51 } |
|
52 #endregion |
|
53 |
|
54 #region API |
|
55 public virtual void Clear() |
|
56 { |
|
57 lock ( iQueue ) |
|
58 { |
|
59 Stop(); |
|
60 iQueue.Clear(); |
|
61 } |
|
62 } |
|
63 |
|
64 public void Start() |
|
65 { |
|
66 Enabled = true; |
|
67 StartNextOperation(); |
|
68 } |
|
69 |
|
70 public void Stop() |
|
71 { |
|
72 Enabled = false; |
|
73 } |
|
74 |
|
75 public void Queue( SerializedOperation aOperation ) |
|
76 { |
|
77 Trace( "Queue() - START - enabled: {0}, count: {1}, OpInProg: {2}", Enabled, iQueue.Count, OperationInProgress ); |
|
78 lock ( iQueue ) |
|
79 { |
|
80 Trace( "Queue() - aOperation.Enabled: {0}, aOperation: {1}", aOperation.Enabled, aOperation.GetType().Name ); |
|
81 |
|
82 aOperation.OperationManager = this; |
|
83 iQueue.Add( aOperation ); |
|
84 |
|
85 // Sort the list so that the highest priority item is first |
|
86 Comparison<SerializedOperation> sortByPriority = delegate( SerializedOperation aLeft, SerializedOperation aRight ) |
|
87 { |
|
88 // We want highest-to-lowest sort order |
|
89 int ret = aLeft.CompareTo( aRight ); |
|
90 return ret * -1; |
|
91 }; |
|
92 iQueue.Sort( sortByPriority ); |
|
93 |
|
94 Trace( "Queue() - {{{0}}} - Queue now contains {1} entries, amEnabled: {2}", aOperation.GetType(), Count, Enabled ); |
|
95 } |
|
96 |
|
97 StartNextOperation(); |
|
98 Trace( "Queue() - END - enabled: {0}, count: {1}, OpInProg: {2}", Enabled, iQueue.Count, OperationInProgress ); |
|
99 Trace( "" ); |
|
100 Trace( "" ); |
|
101 } |
|
102 #endregion |
|
103 |
|
104 #region Properties |
|
105 public int Count |
|
106 { |
|
107 get |
|
108 { |
|
109 lock( iQueue ) |
|
110 { |
|
111 return iQueue.Count; |
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 public bool Enabled |
|
117 { |
|
118 get { return iEnabled; } |
|
119 private set |
|
120 { |
|
121 lock ( iEnabledLock ) |
|
122 { |
|
123 if ( iEnabled != value ) |
|
124 { |
|
125 Trace( "Enabled - current value: {0}, new value: {1}", iEnabled, value ); |
|
126 iEnabled = value; |
|
127 // |
|
128 TState e = value ? TState.EStateOperationsStarted : TState.EStateOperationsSuspended; |
|
129 if ( StateHandler != null ) |
|
130 { |
|
131 StateHandler( e ); |
|
132 } |
|
133 } |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 public ITracer Tracer |
|
139 { |
|
140 get |
|
141 { |
|
142 ITracer ret = this; |
|
143 // |
|
144 if ( iTracer != null ) |
|
145 { |
|
146 ret = iTracer; |
|
147 } |
|
148 // |
|
149 return ret; |
|
150 } |
|
151 set { iTracer = value; } |
|
152 } |
|
153 |
|
154 public bool OperationInProgress |
|
155 { |
|
156 get |
|
157 { |
|
158 lock ( iQueue ) |
|
159 { |
|
160 return iPendingOperation; |
|
161 } |
|
162 } |
|
163 private set |
|
164 { |
|
165 lock ( iQueue ) |
|
166 { |
|
167 iPendingOperation = value; |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 public bool IsCompleteAndIdle |
|
173 { |
|
174 get |
|
175 { |
|
176 bool ret = false; |
|
177 // |
|
178 lock ( iQueue ) |
|
179 { |
|
180 int enabledCount = OperationCountEnabled; |
|
181 if ( Enabled == false ) |
|
182 { |
|
183 // To be complete when we are disabled, then there must be |
|
184 // no items that are ready to run. |
|
185 ret = ( enabledCount == 0 ); |
|
186 } |
|
187 else |
|
188 { |
|
189 // If we are enabled, then we are complete when there are |
|
190 // no more enabled items left to run. |
|
191 ret = ( enabledCount == 0 ); |
|
192 } |
|
193 } |
|
194 // |
|
195 return ret; |
|
196 } |
|
197 } |
|
198 #endregion |
|
199 |
|
200 #region ITracer Members |
|
201 public void Trace( string aMessage ) |
|
202 { |
|
203 if ( iTracer != null ) |
|
204 { |
|
205 iTracer.Trace( "[" + this.GetType().Name + "] " + aMessage ); |
|
206 } |
|
207 } |
|
208 |
|
209 public void Trace( string aFormat, params object[] aParams ) |
|
210 { |
|
211 if ( iTracer != null ) |
|
212 { |
|
213 string text = string.Format( aFormat, aParams ); |
|
214 iTracer.Trace( "[" + this.GetType().Name + "] {0}", text ); |
|
215 } |
|
216 } |
|
217 #endregion |
|
218 |
|
219 #region Internal methods |
|
220 internal void OnOperationEnabledStatusChanged( SerializedOperation aOperation ) |
|
221 { |
|
222 StartNextOperation(); |
|
223 } |
|
224 |
|
225 internal void OperationCompleted( SerializedOperation aOperation ) |
|
226 { |
|
227 OperationInProgress = false; |
|
228 Trace( "OpComplete() - Operation completed {{{0}}}, queue contains {1} more entries, amEnabled: {2}", aOperation.GetType(), Count, Enabled ); |
|
229 // |
|
230 StartNextOperation(); |
|
231 } |
|
232 |
|
233 private void StartNextOperation() |
|
234 { |
|
235 TraceQueue( string.Format( "StartNextOperation() - START - Enabled: {0}", Enabled ) ); |
|
236 |
|
237 lock ( iQueue ) |
|
238 { |
|
239 Trace( "StartNextOperation() - count: {0}, OpInProg: {1}", iQueue.Count, OperationInProgress ); |
|
240 bool enabled = Enabled; |
|
241 if ( enabled ) |
|
242 { |
|
243 if ( OperationInProgress ) |
|
244 { |
|
245 Trace( "StartNextOperation() - Already running operation!" ); |
|
246 } |
|
247 else |
|
248 { |
|
249 SerializedOperation op = FindNextOp(); |
|
250 if ( op != null ) |
|
251 { |
|
252 iQueue.Remove( op ); |
|
253 // |
|
254 Trace( string.Empty ); |
|
255 Trace( string.Empty ); |
|
256 Trace( "StartNextOperation() - ****************************************************************************" ); |
|
257 Trace( "StartNextOperation() - starting op: {0}/{1} [{2}]", 1, iQueue.Count + 1, op.GetType() + " - " + op.ToString() ); |
|
258 Trace( "StartNextOperation() - ****************************************************************************" ); |
|
259 // |
|
260 op.Start(); |
|
261 |
|
262 OperationInProgress = true; |
|
263 } |
|
264 else |
|
265 { |
|
266 Trace( "StartNextOperation() - Queue is empty or no enabled items!" ); |
|
267 if ( StateHandler != null ) |
|
268 { |
|
269 StateHandler( TState.EStateOperationsCompleted ); |
|
270 } |
|
271 } |
|
272 } |
|
273 } |
|
274 else |
|
275 { |
|
276 Trace( "StartNextOperation() - Queue is disabled!" ); |
|
277 } |
|
278 } |
|
279 |
|
280 Trace( "StartNextOperation() - END - count: {0}, OpInProg: {1}, Enabled: {2}", iQueue.Count, OperationInProgress, Enabled ); |
|
281 Trace( "" ); |
|
282 } |
|
283 |
|
284 private void TraceQueue( string aFrom ) |
|
285 { |
|
286 StringBuilder text = new StringBuilder(); |
|
287 |
|
288 text.AppendLine( string.Format( "========================== {0} ==========================", aFrom ) ); |
|
289 text.AppendLine( string.Format( "opInPrgress: {0}", OperationInProgress ) ); |
|
290 text.AppendLine( string.Format( "enabled: {0}", Enabled ) ); |
|
291 // |
|
292 lock ( iQueue ) |
|
293 { |
|
294 int count = iQueue.Count; |
|
295 text.AppendLine( string.Format( "count: {0}", count ) ); |
|
296 text.AppendLine( "" ); |
|
297 |
|
298 int i = 1; |
|
299 foreach ( SerializedOperation op in iQueue ) |
|
300 { |
|
301 text.AppendLine( string.Format( "[{0:d2}/{1:d2}] enabled: {2}, busy: {3}, pri: {4:d10}, name: {5}", i, count, System.Convert.ToInt32( op.Enabled ), System.Convert.ToInt32( op.IsBusy ), op.Priority, op.ToString() ) ); |
|
302 ++i; |
|
303 } |
|
304 } |
|
305 // |
|
306 text.AppendLine( "" ); |
|
307 text.AppendLine( "" ); |
|
308 // |
|
309 Trace( text.ToString() ); |
|
310 } |
|
311 |
|
312 private SerializedOperation FindNextOp() |
|
313 { |
|
314 TraceQueue( "FindNextOp()" ); |
|
315 SerializedOperation ret = null; |
|
316 // |
|
317 foreach ( SerializedOperation op in iQueue ) |
|
318 { |
|
319 System.Diagnostics.Debug.Assert( !op.IsBusy ); |
|
320 if ( op.Enabled ) |
|
321 { |
|
322 ret = op; |
|
323 break; |
|
324 } |
|
325 } |
|
326 // |
|
327 return ret; |
|
328 } |
|
329 |
|
330 private int OperationCountEnabled |
|
331 { |
|
332 get |
|
333 { |
|
334 int ret = 0; |
|
335 // |
|
336 lock ( iQueue ) |
|
337 { |
|
338 Action<SerializedOperation> act = delegate(SerializedOperation op ) |
|
339 { |
|
340 if ( op.Enabled ) |
|
341 { |
|
342 ++ret; |
|
343 } |
|
344 }; |
|
345 iQueue.ForEach( act ); |
|
346 } |
|
347 // |
|
348 return ret; |
|
349 } |
|
350 } |
|
351 |
|
352 private int OperationCountDisabled |
|
353 { |
|
354 get |
|
355 { |
|
356 int ret = 0; |
|
357 // |
|
358 lock ( iQueue ) |
|
359 { |
|
360 Action<SerializedOperation> act = delegate( SerializedOperation op ) |
|
361 { |
|
362 if ( !op.Enabled ) |
|
363 { |
|
364 ++ret; |
|
365 } |
|
366 }; |
|
367 iQueue.ForEach( act ); |
|
368 } |
|
369 // |
|
370 return ret; |
|
371 } |
|
372 } |
|
373 #endregion |
|
374 |
|
375 #region Data members |
|
376 private ITracer iTracer; |
|
377 private List<SerializedOperation> iQueue = new List<SerializedOperation>(); |
|
378 private bool iPendingOperation = false; |
|
379 private bool iEnabled = false; |
|
380 private object iEnabledLock = new object(); |
|
381 #endregion |
|
382 } |
|
383 } |