localconnectivityservice/dun/utils/src/DunDataPusher.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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* aDataToPush,
       
   103                                       MDunCompletionReporter* aCallback )
       
   104     {
       
   105     FTRACE(FPrint( _L("CDunDataPusher::AddToQueue()" )));
       
   106     if ( !aDataToPush || aDataToPush->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( aDataToPush );
       
   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.iDataToPush = aDataToPush;
       
   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 (count=%d)" ), iEventQueue.Count() ));
       
   129     return KErrNone;
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Finds an event from queue
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 TInt CDunDataPusher::FindEventFromQueue( const TDesC8* aDataToPush )
       
   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].iDataToPush == aDataToPush )
       
   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* aDataToPush )
       
   158     {
       
   159     FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent()" )));
       
   160     if ( !aDataToPush )
       
   161         {
       
   162         FTRACE(FPrint( _L("CDunDataPusher::StopOneEvent() (unknown data) complete" )));
       
   163         return KErrGeneral;
       
   164         }
       
   165     TInt foundIndex = FindEventFromQueue( aDataToPush );
       
   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     iSocket = NULL;
       
   316     iComm = NULL;
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // Manages one event's data push
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 TInt CDunDataPusher::ManageOneEvent()
       
   324     {
       
   325     FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent()" )));
       
   326     if ( IsActive() )
       
   327         {
       
   328         FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (not ready) complete" )));
       
   329         return KErrNotReady;
       
   330         }
       
   331     if ( iEventIndex < 0 ||
       
   332          iEventIndex >= iEventQueue.Count() )
       
   333         {
       
   334         FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (buffer mismatch) complete" )));
       
   335         return KErrGeneral;
       
   336         }
       
   337     const TDesC8* dataToPush = iEventQueue[iEventIndex].iDataToPush;
       
   338     if ( iComm )
       
   339         {
       
   340         iStatus = KRequestPending;
       
   341         iComm->Write( iStatus, *dataToPush );
       
   342         FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() RComm Write() requested (buffer=0x%08X)" ), dataToPush ));
       
   343         }
       
   344     else if ( iSocket )
       
   345         {
       
   346         iStatus = KRequestPending;
       
   347         iSocket->Send( *dataToPush, 0, iStatus );
       
   348         FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() RSocket Send() requested (buffer=0x%08X)" ), dataToPush ));
       
   349         }
       
   350     else
       
   351         {
       
   352         FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() (ERROR) complete" )));
       
   353         return KErrGeneral;
       
   354         }
       
   355     SetActive();
       
   356     FTRACE(FPrint( _L("CDunDataPusher::ManageOneEvent() complete" )));
       
   357     return KErrNone;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Check whether an error code is severe error or not
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 TInt CDunDataPusher::ProcessErrorCondition( TInt aError, TBool& aIsError )
       
   365     {
       
   366     FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d)" ), EDunWriterDownstream));
       
   367     aIsError = EFalse;
       
   368     if ( aError != KErrNone )
       
   369         {
       
   370         aIsError = ETrue;
       
   371         TInt retTemp = iParent.iOkErrorsW.Find( aError );
       
   372         if ( retTemp == KErrNotFound )
       
   373             {
       
   374             FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d) (%d=ETrue) complete" ), EDunWriterDownstream, aError));
       
   375             return ETrue;
       
   376             }
       
   377         }
       
   378     FTRACE(FPrint( _L("CDunDataPusher::ProcessErrorCondition() (Dir=%d) (%d=EFalse) complete" ), EDunWriterDownstream, aError));
       
   379     return EFalse;
       
   380     }
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // From class CActive.
       
   384 // Gets called when endpoint data write complete
       
   385 // ---------------------------------------------------------------------------
       
   386 //
       
   387 void CDunDataPusher::RunL()
       
   388     {
       
   389     FTRACE(FPrint( _L("CDunDataPusher::RunL() (buffer=0x%08X)" ), iEventQueue[iEventIndex].iDataToPush ));
       
   390 
       
   391     TBool isError;
       
   392     TInt retTemp = iStatus.Int();
       
   393     TInt stop = ProcessErrorCondition( retTemp, isError );
       
   394 
       
   395     if ( !stop )  // no real error detected -> continue
       
   396         {
       
   397         if ( !isError )
       
   398             {
       
   399             iEventIndex++;
       
   400             }
       
   401         if ( iEventIndex < iEventQueue.Count() )
       
   402             {
       
   403             // More to serve so start again
       
   404             ManageOneEvent();
       
   405             }
       
   406         else
       
   407             {
       
   408             // Last was served so stop processing and notify
       
   409             iPushState = EDunStateIdle;
       
   410             iStreamCallback->NotifyDataPushComplete( ETrue );
       
   411             }
       
   412         }  // if ( !stop )
       
   413     else  // stop -> tear down connection
       
   414         {
       
   415         TDunConnectionReason connReason;
       
   416         connReason.iReasonType = EDunReasonTypeRW;
       
   417         connReason.iContext = EDunMediaContextLocal;
       
   418         connReason.iSignalType = 0;
       
   419         connReason.iSignalHigh = EFalse;
       
   420         connReason.iDirection = EDunWriterDownstream;
       
   421         connReason.iErrorCode = retTemp;
       
   422         iParent.iUtility->DoNotifyConnectionNotOk( iComm,
       
   423                                                    iSocket,
       
   424                                                    connReason,
       
   425                                                    iParent.iCallbacksW );
       
   426         }  // else
       
   427 
       
   428     FTRACE(FPrint( _L("CDunDataPusher::RunL() complete" )));
       
   429     }
       
   430 
       
   431 // ---------------------------------------------------------------------------
       
   432 // From class CActive.
       
   433 // Gets called on cancel
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CDunDataPusher::DoCancel()
       
   437     {
       
   438     FTRACE(FPrint( _L("CDunDataPusher::DoCancel()" )));
       
   439     if ( iComm )
       
   440         {
       
   441         iComm->WriteCancel();
       
   442         FTRACE(FPrint( _L("CDunDataPusher::DoCancel() (RComm) cancelled" )));
       
   443         }
       
   444     else if ( iSocket )
       
   445         {
       
   446         iSocket->CancelWrite();
       
   447         FTRACE(FPrint( _L("CDunDataPusher::DoCancel() (RSocket) cancelled" )));
       
   448         }
       
   449     FTRACE(FPrint( _L("CDunDataPusher::DoCancel() complete" )));
       
   450     }