|
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.IO; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Collections; |
|
22 using SymbianUtils.Tracer; |
|
23 |
|
24 namespace SymbianUtils |
|
25 { |
|
26 public abstract class AsyncReaderBase : DisposableObject, ITracer |
|
27 { |
|
28 #region Events |
|
29 public enum TEvent |
|
30 { |
|
31 EReadingStarted = 0, |
|
32 EReadingProgress, |
|
33 EReadingComplete |
|
34 } |
|
35 |
|
36 public delegate void Observer( TEvent aEvent, AsyncReaderBase aSender ); |
|
37 public event Observer iObserver; |
|
38 |
|
39 public delegate void ExceptionHandler( Exception aException, AsyncReaderBase aSender ); |
|
40 public event ExceptionHandler iExceptionHandler; |
|
41 #endregion |
|
42 |
|
43 #region Construct & destruct |
|
44 protected AsyncReaderBase() |
|
45 : this( null ) |
|
46 { |
|
47 } |
|
48 |
|
49 protected AsyncReaderBase( ITracer aTracer ) |
|
50 { |
|
51 iTracer = aTracer; |
|
52 |
|
53 // Make sure the thread has a unique name... |
|
54 iWorkerThread = new Thread( new System.Threading.ThreadStart( WorkerThreadFunction ) ); |
|
55 iWorkerThread.Name = "WorkerThreadFunction_" + SymbianUtils.Strings.StringUtils.MakeRandomString(); |
|
56 iWorkerThread.Priority = System.Threading.ThreadPriority.BelowNormal; |
|
57 iWorkerThread.IsBackground = true; |
|
58 } |
|
59 #endregion |
|
60 |
|
61 #region API |
|
62 protected virtual void StartRead( TSynchronicity aSynchronicity ) |
|
63 { |
|
64 switch ( aSynchronicity ) |
|
65 { |
|
66 default: |
|
67 case TSynchronicity.EAsynchronous: |
|
68 AsyncRead(); |
|
69 break; |
|
70 case TSynchronicity.ESynchronous: |
|
71 SyncRead(); |
|
72 break; |
|
73 } |
|
74 } |
|
75 |
|
76 protected virtual void AsyncRead() |
|
77 { |
|
78 lock( this ) |
|
79 { |
|
80 iWorkerThread.Start(); |
|
81 } |
|
82 } |
|
83 |
|
84 protected virtual void SyncRead() |
|
85 { |
|
86 WorkerThreadFunction(); |
|
87 } |
|
88 #endregion |
|
89 |
|
90 #region From DisposableObject - Cleanup Framework |
|
91 protected override void CleanupManagedResources() |
|
92 { |
|
93 try |
|
94 { |
|
95 } |
|
96 finally |
|
97 { |
|
98 base.CleanupManagedResources(); |
|
99 } |
|
100 } |
|
101 |
|
102 protected override void CleanupUnmanagedResources() |
|
103 { |
|
104 try |
|
105 { |
|
106 } |
|
107 finally |
|
108 { |
|
109 base.CleanupUnmanagedResources(); |
|
110 } |
|
111 } |
|
112 #endregion |
|
113 |
|
114 #region Properties |
|
115 public bool IsReady |
|
116 { |
|
117 get |
|
118 { |
|
119 lock(this) |
|
120 { |
|
121 return iReady; |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 public int Progress |
|
127 { |
|
128 get |
|
129 { |
|
130 lock(this) |
|
131 { |
|
132 int progress = 0; |
|
133 // |
|
134 if ( Size == 0 ) |
|
135 { |
|
136 progress = 0; |
|
137 } |
|
138 else |
|
139 { |
|
140 progress = CalculateProgress(); |
|
141 } |
|
142 // |
|
143 return progress; |
|
144 } |
|
145 } |
|
146 } |
|
147 |
|
148 public object Tag |
|
149 { |
|
150 get { return iTag; } |
|
151 set { iTag = value; } |
|
152 } |
|
153 #endregion |
|
154 |
|
155 #region Read handlers |
|
156 protected virtual void HandleReadStarted() |
|
157 { |
|
158 } |
|
159 |
|
160 protected virtual void HandleReadCompleted() |
|
161 { |
|
162 } |
|
163 |
|
164 protected virtual void HandleReadException( Exception aException ) |
|
165 { |
|
166 if ( iExceptionHandler != null ) |
|
167 { |
|
168 iExceptionHandler( aException, this ); |
|
169 } |
|
170 } |
|
171 #endregion |
|
172 |
|
173 #region Abstract reading framework |
|
174 protected abstract void PerformOperation(); |
|
175 protected abstract long Size { get; } |
|
176 protected abstract long Position { get; } |
|
177 #endregion |
|
178 |
|
179 #region Framework methods |
|
180 protected virtual int CalculateProgress() |
|
181 { |
|
182 float positionAsFloat = (float)Position; |
|
183 float sizeAsFloat = (float)Size; |
|
184 int progress = (int)((positionAsFloat / sizeAsFloat) * 100.0); |
|
185 // |
|
186 return System.Math.Max(1, System.Math.Min(100, progress)); |
|
187 } |
|
188 |
|
189 protected virtual void NotifyEvent( TEvent aEvent ) |
|
190 { |
|
191 // Prevents reporting the same progress repeatedly... |
|
192 if ( aEvent == TEvent.EReadingProgress ) |
|
193 { |
|
194 int progress = Progress; |
|
195 if ( progress == iLastProgress ) |
|
196 { |
|
197 return; |
|
198 } |
|
199 iLastProgress = progress; |
|
200 } |
|
201 |
|
202 if ( iObserver != null ) |
|
203 { |
|
204 iObserver( aEvent, this ); |
|
205 } |
|
206 } |
|
207 #endregion |
|
208 |
|
209 #region Internal methods |
|
210 private void WorkerThreadFunction() |
|
211 { |
|
212 try |
|
213 { |
|
214 lock( this ) |
|
215 { |
|
216 iLastProgress = -1; |
|
217 iReady = false; |
|
218 HandleReadStarted(); |
|
219 NotifyEvent( TEvent.EReadingStarted ); |
|
220 } |
|
221 |
|
222 // Record start time |
|
223 iOperationStartTime = DateTime.Now; |
|
224 |
|
225 PerformOperation(); |
|
226 } |
|
227 catch( Exception exception ) |
|
228 { |
|
229 lock( this ) |
|
230 { |
|
231 HandleReadException( exception ); |
|
232 } |
|
233 } |
|
234 finally |
|
235 { |
|
236 Dispose(); |
|
237 // |
|
238 lock( this ) |
|
239 { |
|
240 try |
|
241 { |
|
242 HandleReadCompleted(); |
|
243 } |
|
244 catch( Exception exception ) |
|
245 { |
|
246 HandleReadException( exception ); |
|
247 } |
|
248 iReady = true; |
|
249 NotifyEvent( TEvent.EReadingComplete ); |
|
250 } |
|
251 } |
|
252 } |
|
253 #endregion |
|
254 |
|
255 #region ITracer Members |
|
256 public void Trace( string aMessage ) |
|
257 { |
|
258 if ( iTracer != null ) |
|
259 { |
|
260 iTracer.Trace( aMessage ); |
|
261 } |
|
262 } |
|
263 |
|
264 public void Trace( string aFormat, params object[] aParams ) |
|
265 { |
|
266 Trace( string.Format( aFormat, aParams ) ); |
|
267 } |
|
268 #endregion |
|
269 |
|
270 #region Internal data members |
|
271 protected bool iReady = true; |
|
272 protected DateTime iOperationStartTime; |
|
273 #endregion |
|
274 |
|
275 #region Data members |
|
276 private readonly ITracer iTracer; |
|
277 private readonly Thread iWorkerThread; |
|
278 private object iTag = null; |
|
279 private int iLastProgress = -1; |
|
280 #endregion |
|
281 } |
|
282 } |