|
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: Pushes data to existing stream from outside |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "DunDataPusher.h" |
|
20 #include "DunDownstream.h" |
|
21 #include "DunDebug.h" |
|
22 |
|
23 // --------------------------------------------------------------------------- |
|
24 // Two-phased constructor. |
|
25 // --------------------------------------------------------------------------- |
|
26 // |
|
27 CDunDataPusher* CDunDataPusher::NewL( CDunDownstream& aParent, |
|
28 MDunCompletionReporter* aStreamCallback ) |
|
29 { |
|
30 CDunDataPusher* self = new (ELeave) CDunDataPusher( aParent, |
|
31 aStreamCallback ); |
|
32 CleanupStack::PushL( self ); |
|
33 self->ConstructL(); |
|
34 CleanupStack::Pop( self ); |
|
35 return self; |
|
36 } |
|
37 |
|
38 // --------------------------------------------------------------------------- |
|
39 // Destructor. |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 CDunDataPusher::~CDunDataPusher() |
|
43 { |
|
44 FTRACE(FPrint( _L("CDunDataPusher::~CDunDataPusher()" ))); |
|
45 ResetData(); |
|
46 FTRACE(FPrint( _L("CDunDataPusher::~CDunDataPusher() complete" ))); |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // Resets data to initial values |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 void CDunDataPusher::ResetData() |
|
54 { |
|
55 // APIs affecting this: |
|
56 // SendQueuedData() |
|
57 Stop(); |
|
58 // AddToEventQueue() |
|
59 iEventQueue.Close(); |
|
60 // Internal |
|
61 Initialize(); |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // Sets media to be used for this endpoint |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 TInt CDunDataPusher::SetMedia( RComm* aComm ) |
|
69 { |
|
70 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RComm)" ))); |
|
71 if ( !aComm ) |
|
72 { |
|
73 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RComm) (not initialized!) complete" ))); |
|
74 return KErrGeneral; |
|
75 } |
|
76 iComm = aComm; |
|
77 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RComm) complete" ))); |
|
78 return KErrNone; |
|
79 } |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // Sets media to be used for this endpoint |
|
83 // --------------------------------------------------------------------------- |
|
84 // |
|
85 TInt CDunDataPusher::SetMedia( RSocket* aSocket ) |
|
86 { |
|
87 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RSocket)" ))); |
|
88 if ( !aSocket ) |
|
89 { |
|
90 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RSocket) (not initialized!) complete" ))); |
|
91 return KErrGeneral; |
|
92 } |
|
93 iSocket = aSocket; |
|
94 FTRACE(FPrint( _L("CDunDataPusher::SetMedia() (RSocket) complete" ))); |
|
95 return KErrNone; |
|
96 } |
|
97 |
|
98 // --------------------------------------------------------------------------- |
|
99 // Adds event notification to queue |
|
100 // --------------------------------------------------------------------------- |
|
101 // |
|
102 TInt CDunDataPusher::AddToEventQueue( const TDesC8 *aPushedData, |
|
103 MDunCompletionReporter* aCallback ) |
|
104 { |
|
105 FTRACE(FPrint( _L("CDunDataPusher::AddToQueue()" ))); |
|
106 if ( !aPushedData || aPushedData->Length()<0 ) |
|
107 { |
|
108 FTRACE(FPrint( _L("CDunDataPusher::AddToQueue() (unknown data) complete" ))); |
|
109 return KErrGeneral; |
|
110 } |
|
111 // Check if identical pointer to data already exists |
|
112 TInt foundIndex = FindEventFromQueue( aPushedData ); |
|
113 if ( foundIndex >= 0 ) |
|
114 { |
|
115 FTRACE(FPrint( _L("CDunDataPusher::AddToQueue() (already exists) complete" ))); |
|
116 return KErrAlreadyExists; |
|
117 } |
|
118 // Unique pointer -> add to event queue |
|
119 TDunDataPush dataPush; |
|
120 dataPush.iPushedData = aPushedData; |
|
121 dataPush.iCallback = aCallback; |
|
122 TInt retTemp = iEventQueue.Append( dataPush ); |
|
123 if ( retTemp != KErrNone ) |
|
124 { |
|
125 FTRACE(FPrint( _L("CDunDataPusher::AddToQueue() (append failed!) complete" ))); |
|
126 return retTemp; |
|
127 } |
|
128 FTRACE(FPrint( _L("CDunDataPusher::AddToQueue() complete (%d)" ), iEventQueue.Count() )); |
|
129 return KErrNone; |
|
130 } |
|
131 |
|
132 // --------------------------------------------------------------------------- |
|
133 // Finds an event from queue |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 TInt CDunDataPusher::FindEventFromQueue( const TDesC8 *aPushedData ) |
|
137 { |
|
138 FTRACE(FPrint( _L("CDunDataPusher::FindEventFromQueue()" ))); |
|
139 TInt i; |
|
140 TInt count = iEventQueue.Count(); |
|
141 for ( i=0; i<count; i++ ) |
|
142 { |
|
143 if ( iEventQueue[i].iPushedData == aPushedData ) |
|
144 { |
|
145 FTRACE(FPrint( _L("CDunDataPusher::FindEventFromQueue() complete" ))); |
|
146 return i; |
|
147 } |
|
148 } |
|
149 FTRACE(FPrint( _L("CDunDataPusher::FindEventFromQueue() (not found) complete" ))); |
|
150 return KErrNotFound; |
|
151 } |
|
152 |
|
153 // --------------------------------------------------------------------------- |
|
154 // Stops one event in the event queue |
|
155 // --------------------------------------------------------------------------- |
|
156 // |
|
157 TInt CDunDataPusher::StopOneEvent( const TDesC8 *aPushedData ) |
|
158 { |
|
159 FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent()" ))); |
|
160 if ( !aPushedData ) |
|
161 { |
|
162 FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent() (unknown data) complete" ))); |
|
163 return KErrGeneral; |
|
164 } |
|
165 TInt foundIndex = FindEventFromQueue( aPushedData ); |
|
166 if ( foundIndex >= 0 ) |
|
167 { |
|
168 if ( iEventIndex == foundIndex ) |
|
169 { |
|
170 Stop(); |
|
171 } |
|
172 FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent() complete" ))); |
|
173 return KErrNone; |
|
174 } |
|
175 FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent() (not found) complete" ))); |
|
176 return KErrNotFound; |
|
177 } |
|
178 |
|
179 // --------------------------------------------------------------------------- |
|
180 // Sends queued data in round robin |
|
181 // --------------------------------------------------------------------------- |
|
182 // |
|
183 TBool CDunDataPusher::SendQueuedData() |
|
184 { |
|
185 FTRACE(FPrint( _L("CDunDataPusher::SendQueuedData()" ))); |
|
186 if ( iPushState!=EDunStateIdle || iEventQueue.Count()==0 ) |
|
187 { |
|
188 FTRACE(FPrint( _L("CDunDataPusher::SendQueuedData() (not ready) complete" ))); |
|
189 return EFalse; |
|
190 } |
|
191 TInt retTemp = ManageOneEvent(); |
|
192 if ( retTemp != KErrNone ) |
|
193 { |
|
194 FTRACE(FPrint( _L("CDunDataPusher::SendQueuedData() (ERROR) complete" ))); |
|
195 return EFalse; |
|
196 } |
|
197 iPushState = EDunStateDataPushing; |
|
198 FTRACE(FPrint( _L("CDunDataPusher::SendQueuedData() complete (%d)" ), iEventQueue.Count() )); |
|
199 return ETrue; |
|
200 } |
|
201 |
|
202 // --------------------------------------------------------------------------- |
|
203 // Stops sending for write endpoint |
|
204 // --------------------------------------------------------------------------- |
|
205 // |
|
206 TInt CDunDataPusher::Stop() |
|
207 { |
|
208 FTRACE(FPrint( _L("CDunDataPusher::Stop()" ))); |
|
209 if ( iPushState != EDunStateDataPushing ) |
|
210 { |
|
211 FTRACE(FPrint( _L("CDunDataPusher::Stop() (not ready) complete" ))); |
|
212 return KErrNotReady; |
|
213 } |
|
214 // As the EDunStateDataPushing can be on even with multiple requests, |
|
215 // cancel the actual operation in DoCancel() |
|
216 Cancel(); |
|
217 iPushState = EDunStateIdle; |
|
218 FTRACE(FPrint( _L("CDunDataPusher::Stop() complete" ))); |
|
219 return KErrNone; |
|
220 } |
|
221 |
|
222 // --------------------------------------------------------------------------- |
|
223 // Stops sending for write endpoint and clears event queue |
|
224 // --------------------------------------------------------------------------- |
|
225 // |
|
226 TInt CDunDataPusher::StopAndClearQueue() |
|
227 { |
|
228 FTRACE(FPrint( _L("CDunDataPusher::StopAndClearQueue()" ))); |
|
229 TInt retVal = Stop(); |
|
230 iEventQueue.Reset(); |
|
231 iEventIndex = 0; |
|
232 FTRACE(FPrint( _L("CDunDataPusher::StopAndClearQueue() complete" ))); |
|
233 return retVal; |
|
234 } |
|
235 |
|
236 // --------------------------------------------------------------------------- |
|
237 // Signals completion status in round robin and clears event queue |
|
238 // --------------------------------------------------------------------------- |
|
239 // |
|
240 TInt CDunDataPusher::SignalCompletionAndClearQueue() |
|
241 { |
|
242 FTRACE(FPrint( _L("CDunDataPusher::SignalCompletionAndClearQueue()" ))); |
|
243 // First copy the event queue to temporary notitication queue and |
|
244 // reset the real event queue before notifications. This is done because |
|
245 // implementor of NotifyDataPushComplete() can call AddToEventQueue() |
|
246 // (and KErrAlreadyExists will happen there) |
|
247 TInt i; |
|
248 TInt retTemp; |
|
249 RPointerArray<MDunCompletionReporter> notify; |
|
250 TInt count = iEventQueue.Count(); |
|
251 for ( i=0; i<count; i++ ) |
|
252 { |
|
253 if ( !iEventQueue[i].iCallback ) |
|
254 { |
|
255 continue; |
|
256 } |
|
257 retTemp = notify.Append( iEventQueue[i].iCallback ); |
|
258 if ( retTemp != KErrNone ) |
|
259 { |
|
260 FTRACE(FPrint( _L("CDunDataPusher::SignalCompletionAndClearQueue() (append failed!) complete" ))); |
|
261 return retTemp; |
|
262 } |
|
263 } |
|
264 iEventQueue.Reset(); |
|
265 iEventIndex = 0; |
|
266 // Now notify |
|
267 count = notify.Count(); |
|
268 for ( i=0; i<count; i++ ) |
|
269 { |
|
270 notify[i]->NotifyDataPushComplete( EFalse ); |
|
271 } |
|
272 notify.Close(); |
|
273 FTRACE(FPrint( _L("CDunDataPusher::SignalCompletionAndClearQueue() complete (%d)" ), count )); |
|
274 return KErrNone; |
|
275 } |
|
276 |
|
277 // --------------------------------------------------------------------------- |
|
278 // CDunDataPusher::CDunDataPusher |
|
279 // --------------------------------------------------------------------------- |
|
280 // |
|
281 CDunDataPusher::CDunDataPusher( CDunDownstream& aParent, |
|
282 MDunCompletionReporter* aStreamCallback ) : |
|
283 CActive( EPriorityHigh ), |
|
284 iParent( aParent ), |
|
285 iStreamCallback( aStreamCallback ) |
|
286 { |
|
287 Initialize(); |
|
288 } |
|
289 |
|
290 // --------------------------------------------------------------------------- |
|
291 // CDunDataPusher::ConstructL |
|
292 // --------------------------------------------------------------------------- |
|
293 // |
|
294 void CDunDataPusher::ConstructL() |
|
295 { |
|
296 FTRACE(FPrint( _L("CDunDataPusher::ConstructL()" ))); |
|
297 if ( !iStreamCallback ) |
|
298 { |
|
299 User::Leave( KErrGeneral ); |
|
300 } |
|
301 CActiveScheduler::Add( this ); |
|
302 FTRACE(FPrint( _L("CDunDataPusher::ConstructL() complete" ))); |
|
303 } |
|
304 |
|
305 // --------------------------------------------------------------------------- |
|
306 // Initializes this class |
|
307 // --------------------------------------------------------------------------- |
|
308 // |
|
309 void CDunDataPusher::Initialize() |
|
310 { |
|
311 // Don't initialize iUtility here (it is set through NewL) |
|
312 // Don't initialize iStreamCallback here (it is set through NewL) |
|
313 iPushState = EDunStateIdle; |
|
314 iEventIndex = 0; |
|
315 iEPReady = EFalse; |
|
316 iSocket = NULL; |
|
317 iComm = NULL; |
|
318 } |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // Manages one event's data push |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 TInt CDunDataPusher::ManageOneEvent() |
|
325 { |
|
326 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent()" ))); |
|
327 if ( IsActive() ) |
|
328 { |
|
329 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (not ready) complete" ))); |
|
330 return KErrNotReady; |
|
331 } |
|
332 if ( iEventIndex < 0 || |
|
333 iEventIndex >= iEventQueue.Count() ) |
|
334 { |
|
335 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (buffer mismatch) complete" ))); |
|
336 return KErrGeneral; |
|
337 } |
|
338 iStatus = KRequestPending; |
|
339 if ( !iEPReady ) |
|
340 { |
|
341 SetActive(); |
|
342 TRequestStatus* requestStatus = &iStatus; |
|
343 User::RequestComplete( requestStatus, KErrNone ); |
|
344 return KErrNone; |
|
345 } |
|
346 const TDesC8 *pushedData = iEventQueue[iEventIndex].iPushedData; |
|
347 if ( iComm ) |
|
348 { |
|
349 iComm->Write( iStatus, *pushedData ); |
|
350 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() RComm Write() requested" ) )); |
|
351 } |
|
352 else if ( iSocket ) |
|
353 { |
|
354 iSocket->Send( *pushedData, 0, iStatus ); |
|
355 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() RSocket Send() requested" ) )); |
|
356 } |
|
357 else |
|
358 { |
|
359 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (ERROR) complete" ))); |
|
360 return KErrGeneral; |
|
361 } |
|
362 SetActive(); |
|
363 FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() complete" ))); |
|
364 return KErrNone; |
|
365 } |
|
366 |
|
367 // --------------------------------------------------------------------------- |
|
368 // Check whether an error code is severe error or not |
|
369 // --------------------------------------------------------------------------- |
|
370 // |
|
371 TInt CDunDataPusher::ProcessErrorCondition( TInt aError, TBool& aIsError ) |
|
372 { |
|
373 FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d)" ), EDunWriterDownstream)); |
|
374 aIsError = EFalse; |
|
375 if ( aError != KErrNone ) |
|
376 { |
|
377 aIsError = ETrue; |
|
378 TInt retTemp = iParent.iOkErrorsW.Find( aError ); |
|
379 if ( retTemp == KErrNotFound ) |
|
380 { |
|
381 FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d) (%d=ETrue) complete" ), EDunWriterDownstream, aError)); |
|
382 return ETrue; |
|
383 } |
|
384 } |
|
385 FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d) (%d=EFalse) complete" ), EDunWriterDownstream, aError)); |
|
386 return EFalse; |
|
387 } |
|
388 |
|
389 // --------------------------------------------------------------------------- |
|
390 // From class CActive. |
|
391 // Gets called when endpoint data write complete |
|
392 // --------------------------------------------------------------------------- |
|
393 // |
|
394 void CDunDataPusher::RunL() |
|
395 { |
|
396 FTRACE(FPrint( _L("CDunDataPusher::RunL()" ))); |
|
397 |
|
398 TBool isError; |
|
399 TInt retTemp = iStatus.Int(); |
|
400 TInt stop = ProcessErrorCondition( retTemp, isError ); |
|
401 |
|
402 if ( !stop ) // no real error detected -> continue |
|
403 { |
|
404 if ( !isError ) |
|
405 { |
|
406 iEventIndex++; |
|
407 } |
|
408 if ( iEventIndex < iEventQueue.Count() ) |
|
409 { |
|
410 // More to serve so start again |
|
411 ManageOneEvent(); |
|
412 } |
|
413 else |
|
414 { |
|
415 // Last was served so stop processing and notify |
|
416 iPushState = EDunStateIdle; |
|
417 iStreamCallback->NotifyDataPushComplete( ETrue ); |
|
418 } |
|
419 } // if ( !stop ) |
|
420 else // stop -> tear down connection |
|
421 { |
|
422 TDunConnectionReason connReason; |
|
423 connReason.iReasonType = EDunReasonTypeRW; |
|
424 connReason.iContext = EDunMediaContextLocal; |
|
425 connReason.iSignalType = 0; |
|
426 connReason.iSignalHigh = EFalse; |
|
427 connReason.iDirection = EDunWriterDownstream; |
|
428 connReason.iErrorCode = retTemp; |
|
429 iParent.iUtility->DoNotifyConnectionNotOk( iComm, |
|
430 iSocket, |
|
431 connReason, |
|
432 iParent.iCallbacksW ); |
|
433 } // else |
|
434 |
|
435 FTRACE(FPrint( _L("CDunDataPusher::RunL() complete" ))); |
|
436 } |
|
437 |
|
438 // --------------------------------------------------------------------------- |
|
439 // From class CActive. |
|
440 // Gets called on cancel |
|
441 // --------------------------------------------------------------------------- |
|
442 // |
|
443 void CDunDataPusher::DoCancel() |
|
444 { |
|
445 FTRACE(FPrint( _L("CDunDataPusher::DoCancel()" ))); |
|
446 if ( iComm ) |
|
447 { |
|
448 iComm->WriteCancel(); |
|
449 FTRACE(FPrint( _L("CDunDataPusher::DoCancel() (RComm) cancelled" ))); |
|
450 } |
|
451 else if ( iSocket ) |
|
452 { |
|
453 iSocket->CancelWrite(); |
|
454 FTRACE(FPrint( _L("CDunDataPusher::DoCancel() (RSocket) cancelled" ))); |
|
455 } |
|
456 FTRACE(FPrint( _L("CDunDataPusher::DoCancel() complete" ))); |
|
457 } |
|
458 |
|
459 // --------------------------------------------------------------------------- |
|
460 // From class MDunEndpointReady. |
|
461 // Gets called when endpoint is ready |
|
462 // --------------------------------------------------------------------------- |
|
463 // |
|
464 void CDunDataPusher::NotifyEndpointReady() |
|
465 { |
|
466 FTRACE(FPrint( _L("CDunDataPusher::NotifyEndpointReady()" ))); |
|
467 iEPReady = ETrue; |
|
468 FTRACE(FPrint( _L("CDunDataPusher::NotifyEndpointReady() complete" ))); |
|
469 } |
|
470 |
|
471 // --------------------------------------------------------------------------- |
|
472 // From class MDunEndpointReady. |
|
473 // Gets called when endpoint is not ready |
|
474 // --------------------------------------------------------------------------- |
|
475 // |
|
476 void CDunDataPusher::NotifyEndpointNotReady() |
|
477 { |
|
478 FTRACE(FPrint( _L("CDunDataPusher::NotifyEndpointNotReady()" ))); |
|
479 iEPReady = EFalse; |
|
480 FTRACE(FPrint( _L("CDunDataPusher::NotifyEndpointNotReady() complete" ))); |
|
481 } |