|
1 /* |
|
2 * Copyright (c) 2008 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 the License "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 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "VCXTestMessageWait.h" |
|
21 #include "VCXTestLog.h" |
|
22 #include "CIptvTestActiveWait.h" |
|
23 #include "CIptvTestTimer.h" |
|
24 |
|
25 // EXTERNAL DATA STRUCTURES |
|
26 |
|
27 // EXTERNAL FUNCTION PROTOTYPES |
|
28 |
|
29 // CONSTANTS |
|
30 const TInt KTimeoutSecond = 1000000; |
|
31 |
|
32 // MACROS |
|
33 |
|
34 // ============================ MEMBER FUNCTIONS =============================== |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // CVCXTestMessageWait:: |
|
38 // C++ default constructor can NOT contain any code, that |
|
39 // might leave. |
|
40 // ----------------------------------------------------------------------------- |
|
41 // |
|
42 EXPORT_C CVCXTestMessageWait* CVCXTestMessageWait::NewL( MVCXTestMessageWaitObserver* aObserver ) |
|
43 { |
|
44 VCXLOGLO1(">>>CVCXTestMessageWait::NewL"); |
|
45 CVCXTestMessageWait* self = new (ELeave) CVCXTestMessageWait( aObserver ); |
|
46 CleanupStack::PushL(self); |
|
47 self->ConstructL(); |
|
48 CleanupStack::Pop(); |
|
49 VCXLOGLO1("<<<CVCXTestMessageWait::NewL"); |
|
50 return self; |
|
51 } |
|
52 |
|
53 // ----------------------------------------------------------------------------- |
|
54 // CVCXTestMessageWait::~CVCXTestMessageWait |
|
55 // destructor |
|
56 // ----------------------------------------------------------------------------- |
|
57 // |
|
58 EXPORT_C CVCXTestMessageWait::~CVCXTestMessageWait() |
|
59 { |
|
60 VCXLOGLO1(">>>CVCXTestMessageWait::~CVCXTestMessageWait"); |
|
61 |
|
62 iActiveWaitBlocking->Stop(); |
|
63 delete iActiveWaitBlocking; |
|
64 iActiveWaitBlocking = NULL; |
|
65 |
|
66 if( iTimeoutTimer ) |
|
67 { |
|
68 iTimeoutTimer->CancelTimer(); |
|
69 delete iTimeoutTimer; |
|
70 iTimeoutTimer = NULL; |
|
71 } |
|
72 |
|
73 iReceivedMessages.Reset(); |
|
74 iWaitedMessages.Reset(); |
|
75 |
|
76 VCXLOGLO1("<<<CVCXTestMessageWait::~CVCXTestMessageWait"); |
|
77 } |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // CVCXTestMessageWait::~CVCXTestMessageWait |
|
81 // destructor |
|
82 // ----------------------------------------------------------------------------- |
|
83 // |
|
84 EXPORT_C CVCXTestMessageWait::CVCXTestMessageWait( MVCXTestMessageWaitObserver* aObserver ) |
|
85 : iObserver( aObserver ) |
|
86 { |
|
87 VCXLOGLO1(">>>CVCXTestMessageWait::CVCXTestMessageWait"); |
|
88 |
|
89 VCXLOGLO1("<<<CVCXTestMessageWait::CVCXTestMessageWait"); |
|
90 } |
|
91 |
|
92 // ----------------------------------------------------------------------------- |
|
93 // CVCXTestMessageWait::ConstructL |
|
94 // ----------------------------------------------------------------------------- |
|
95 // |
|
96 EXPORT_C void CVCXTestMessageWait::ConstructL() |
|
97 { |
|
98 VCXLOGLO1(">>>CVCXTestMessageWait::ConstructL"); |
|
99 |
|
100 iActiveWaitBlocking = CIptvTestActiveWait::NewL(); |
|
101 iTimeoutTimer = CIptvTestTimer::NewL(*this, 0); |
|
102 |
|
103 VCXLOGLO1("<<<CVCXTestMessageWait::ConstructL"); |
|
104 } |
|
105 |
|
106 // ----------------------------------------------------------------------------- |
|
107 // CVCXTestMessageWait::Reset |
|
108 // ----------------------------------------------------------------------------- |
|
109 // |
|
110 EXPORT_C void CVCXTestMessageWait::Reset() |
|
111 { |
|
112 VCXLOGLO1(">>>CVCXTestMessageWait::Reset"); |
|
113 iCoolingDown = EFalse; |
|
114 iWaitStarted = EFalse; |
|
115 iWaitedMessages.Reset(); |
|
116 iReceivedMessages.Reset(); |
|
117 iTimeoutTimer->Cancel(); |
|
118 iActiveWaitBlocking->Stop(); |
|
119 VCXLOGLO1("<<<CVCXTestMessageWait::Reset"); |
|
120 } |
|
121 |
|
122 // ----------------------------------------------------------------------------- |
|
123 // CVCXTestMessageWait::ResetReceivedMessages |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 EXPORT_C void CVCXTestMessageWait::ResetReceivedMessages() |
|
127 { |
|
128 VCXLOGLO1(">>>CVCXTestMessageWait::ResetReceivedMessages"); |
|
129 iReceivedMessages.Reset(); |
|
130 VCXLOGLO1("<<<CVCXTestMessageWait::ResetReceivedMessages"); |
|
131 } |
|
132 |
|
133 // ----------------------------------------------------------------------------- |
|
134 // CVCXTestMessageWait::AddMessage |
|
135 // ----------------------------------------------------------------------------- |
|
136 // |
|
137 EXPORT_C void CVCXTestMessageWait::AddMessage( TInt32 aMsg ) |
|
138 { |
|
139 VCXLOGLO1(">>>CVCXTestMessageWait::AddMessage"); |
|
140 iWaitedMessages.Append( aMsg ); |
|
141 VCXLOGLO1("<<<CVCXTestMessageWait::AddMessage"); |
|
142 } |
|
143 |
|
144 // ----------------------------------------------------------------------------- |
|
145 // CVCXTestMessageWait::WaitForMessageL |
|
146 // ----------------------------------------------------------------------------- |
|
147 // |
|
148 EXPORT_C void CVCXTestMessageWait::WaitForMessageL( TInt32 aMsg, TInt aTimeoutSeconds, TBool aDoBlock ) |
|
149 { |
|
150 VCXLOGLO1(">>>CVCXTestMessageWait::WaitForMessageL"); |
|
151 |
|
152 iWaitedMessages.Reset(); |
|
153 iWaitedMessages.Append( aMsg ); |
|
154 |
|
155 iTimeoutSeconds = aTimeoutSeconds; |
|
156 WaitForAllL( aTimeoutSeconds, aDoBlock ); |
|
157 |
|
158 VCXLOGLO1("<<<CVCXTestMessageWait::WaitForMessageL"); |
|
159 } |
|
160 |
|
161 // ----------------------------------------------------------------------------- |
|
162 // CVCXTestMessageWait::WaitForAllL |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 EXPORT_C void CVCXTestMessageWait::WaitForAllL( TInt aTimeoutSeconds, TBool aDoBlock ) |
|
166 { |
|
167 VCXLOGLO1(">>>CVCXTestMessageWait::WaitForAllL"); |
|
168 |
|
169 iWaitStarted = ETrue; |
|
170 |
|
171 iTimeoutSeconds = aTimeoutSeconds; |
|
172 |
|
173 // Nothing to wait. |
|
174 if( iWaitedMessages.Count() <= 0 ) |
|
175 { |
|
176 VCXLOGLO1("<<<CVCXTestMessageWait::WaitForAllL"); |
|
177 User::Leave( KErrNotReady ); |
|
178 } |
|
179 |
|
180 // Check already received messages. |
|
181 for( TInt i = iReceivedMessages.Count()-1; i >= 0; i-- ) |
|
182 { |
|
183 for( TInt e = iWaitedMessages.Count()-1; e >= 0; e-- ) |
|
184 { |
|
185 if( iReceivedMessages[i] == iWaitedMessages[e] ) |
|
186 { |
|
187 iWaitedMessages.Remove( e ); |
|
188 iReceivedMessages.Remove( i ); |
|
189 break; |
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 if( iWaitedMessages.Count() > 0 ) |
|
195 { |
|
196 // There's messages to wait. Start timeout timer. |
|
197 iTimeoutTimer->After( aTimeoutSeconds * KTimeoutSecond ); |
|
198 |
|
199 // Client wants blocking call. |
|
200 if( aDoBlock ) |
|
201 { |
|
202 iActiveWaitBlocking->Start(); |
|
203 User::LeaveIfError( iError ); |
|
204 } |
|
205 } |
|
206 else |
|
207 { |
|
208 iWaitStarted = EFalse; |
|
209 |
|
210 // Nothing to wait for. |
|
211 iTimeoutTimer->CancelTimer(); |
|
212 |
|
213 // Not blocking, inform client with callback. |
|
214 if( !aDoBlock ) |
|
215 { |
|
216 iObserver->MessageWaitComplete( KErrNone ); |
|
217 } |
|
218 } |
|
219 |
|
220 VCXLOGLO1("<<<CVCXTestMessageWait::WaitForAllL"); |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // CVCXTestMessageWait::CoolDownL |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 EXPORT_C void CVCXTestMessageWait::CoolDownL( TInt aSeconds ) |
|
228 { |
|
229 VCXLOGLO1(">>>CVCXTestMessageWait::CoolDownL"); |
|
230 |
|
231 Reset(); |
|
232 |
|
233 iTimeoutSeconds = aSeconds; |
|
234 |
|
235 iCoolingDown = ETrue; |
|
236 |
|
237 iTimeoutTimer->After( iTimeoutSeconds * KTimeoutSecond ); |
|
238 |
|
239 iActiveWaitBlocking->Start(); |
|
240 |
|
241 VCXLOGLO1("<<<CVCXTestMessageWait::CoolDownL"); |
|
242 } |
|
243 |
|
244 // ----------------------------------------------------------------------------- |
|
245 // CVCXTestMessageWait::ReceiveMessage |
|
246 // ----------------------------------------------------------------------------- |
|
247 // |
|
248 EXPORT_C void CVCXTestMessageWait::ReceiveMessage( TInt32 aMsg, TInt aError ) |
|
249 { |
|
250 if( !iWaitStarted ) |
|
251 { |
|
252 VCXLOGLO1("CVCXTestMessageWait::ReceiveMessage: Wait not active."); |
|
253 return; |
|
254 } |
|
255 |
|
256 VCXLOGLO1(">>>CVCXTestMessageWait::ReceiveMessage"); |
|
257 |
|
258 if( aError != KErrNone ) |
|
259 { |
|
260 iWaitStarted = EFalse; |
|
261 if( iActiveWaitBlocking->IsWaiting() ) |
|
262 { |
|
263 iError = aError; |
|
264 iActiveWaitBlocking->Stop(); |
|
265 } |
|
266 else |
|
267 { |
|
268 iObserver->MessageWaitComplete( aError ); |
|
269 } |
|
270 iTimeoutTimer->CancelTimer(); |
|
271 VCXLOGLO1("<<<CVCXTestMessageWait::ReceiveMessage"); |
|
272 return; |
|
273 } |
|
274 |
|
275 if( iCoolingDown ) |
|
276 { |
|
277 iTimeoutTimer->After( iTimeoutSeconds * KTimeoutSecond ); |
|
278 VCXLOGLO1("<<<CVCXTestMessageWait::ReceiveMessage"); |
|
279 return; |
|
280 } |
|
281 |
|
282 if( !iWaitStarted ) |
|
283 { |
|
284 iReceivedMessages.Append( aMsg ); |
|
285 VCXLOGLO1("<<<CVCXTestMessageWait::ReceiveMessage"); |
|
286 return; |
|
287 } |
|
288 |
|
289 if( iWaitedMessages.Count() > 0 ) |
|
290 { |
|
291 // Check if message is in wait queue. |
|
292 for( TInt i = iWaitedMessages.Count()-1; i >= 0; i-- ) |
|
293 { |
|
294 if( iWaitedMessages[i] == aMsg ) |
|
295 { |
|
296 iWaitedMessages.Remove( i ); |
|
297 break; |
|
298 } |
|
299 } |
|
300 |
|
301 if( iWaitedMessages.Count() <= 0 ) |
|
302 { |
|
303 iWaitStarted = EFalse; |
|
304 // Stop wait. |
|
305 if( iActiveWaitBlocking->IsWaiting() ) |
|
306 { |
|
307 iError = KErrNone; |
|
308 iActiveWaitBlocking->Stop(); |
|
309 } |
|
310 // Not waiting, client wants callback. |
|
311 else |
|
312 { |
|
313 iObserver->MessageWaitComplete( KErrNone ); |
|
314 } |
|
315 // Stop timer too. |
|
316 iTimeoutTimer->CancelTimer(); |
|
317 } |
|
318 } |
|
319 |
|
320 VCXLOGLO1("<<<CVCXTestMessageWait::ReceiveMessage"); |
|
321 } |
|
322 |
|
323 // ----------------------------------------------------------------------------- |
|
324 // CVCXTestMessageWait::TimerComplete |
|
325 // ----------------------------------------------------------------------------- |
|
326 // |
|
327 void CVCXTestMessageWait::TimerComplete( TInt /* aTimerId */, TInt aError ) |
|
328 { |
|
329 if( aError != KErrCancel ) |
|
330 { |
|
331 if( iCoolingDown ) |
|
332 { |
|
333 VCXLOGLO1("CVCXTestMessageWait:: Cooldown done."); |
|
334 iActiveWaitBlocking->Stop(); |
|
335 iCoolingDown = EFalse; |
|
336 } |
|
337 else |
|
338 if( iWaitedMessages.Count() > 0 ) |
|
339 { |
|
340 iWaitStarted = EFalse; |
|
341 VCXLOGLO2("CVCXTestMessageWait:: Timeout. Messages left: %d", iWaitedMessages.Count()); |
|
342 if( iActiveWaitBlocking->IsWaiting() ) |
|
343 { |
|
344 iActiveWaitBlocking->Stop(); |
|
345 iError = KErrTimedOut; |
|
346 } |
|
347 else |
|
348 { |
|
349 // Client wants callback. |
|
350 iObserver->MessageWaitComplete( KErrTimedOut ); |
|
351 } |
|
352 } |
|
353 } |
|
354 } |
|
355 |
|
356 // End of File |