natfw/natfwstunturnclient/src/casynccallback.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2005 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 
       
    18 
       
    19 
       
    20 
       
    21 #include "natfwstunclientobserver.h"
       
    22 #include "casynccallback.h"
       
    23 #include "stunassert.h"
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CAsyncCallback::NewL
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 CAsyncCallback* CAsyncCallback::NewL( MSTUNClientObserver& aObserver )
       
    30     {
       
    31     CAsyncCallback* self = new (ELeave) CAsyncCallback( aObserver );
       
    32     CActiveScheduler::Add( self );
       
    33     self->WaitForRequests();
       
    34     return self;
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CAsyncCallback::CAsyncCallback
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CAsyncCallback::CAsyncCallback( MSTUNClientObserver& aObserver ) :
       
    42     CActive( EPriorityStandard ),
       
    43     iObserver( aObserver ),
       
    44     iPendingCallbacks( _FOFF( TSTUNCallbackInfo, iLink ) )
       
    45     {
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CAsyncCallback::CAsyncCallback
       
    50 // Dummy implementation. Default constructor is declared private and not used.
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CAsyncCallback::CAsyncCallback() :
       
    54     CActive( EPriorityStandard ),
       
    55     iObserver( *( MSTUNClientObserver* )0x1 )
       
    56     {
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CAsyncCallback::CAsyncCallback
       
    61 // Dummy implementation, as copy constructor is declared private and not used.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CAsyncCallback::CAsyncCallback( const CAsyncCallback& aAsyncCallback ) :
       
    65     CActive( EPriorityStandard ),
       
    66     iObserver( aAsyncCallback.iObserver )
       
    67     {    
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CAsyncCallback::~CAsyncCallback
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CAsyncCallback::~CAsyncCallback()
       
    75     {
       
    76     Cancel();
       
    77 
       
    78     TSTUNCallbackInfo* callback = iPendingCallbacks.First();
       
    79     while ( iPendingCallbacks.IsFirst( callback ) &&
       
    80             !iPendingCallbacks.IsEmpty() )
       
    81         {
       
    82         iPendingCallbacks.Remove( *callback );
       
    83         delete callback;
       
    84         callback = iPendingCallbacks.First();
       
    85         }
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CAsyncCallback::DoCancel
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CAsyncCallback::DoCancel()
       
    93     {
       
    94     if ( iStatus == KRequestPending )
       
    95         {
       
    96         TRequestStatus* status = &iStatus;    
       
    97         User::RequestComplete( status, KErrCancel );
       
    98         }
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CAsyncCallback::RunL
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CAsyncCallback::RunL()
       
   106     {
       
   107     WaitForRequests();
       
   108     ExecuteFirstCallback();
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CAsyncCallback::RunError
       
   113 // Currently RunL cannot leave.
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 TInt CAsyncCallback::RunError( TInt aError )
       
   117     {
       
   118     if ( aError == KErrNoMemory )
       
   119         {
       
   120         return aError;
       
   121         }
       
   122     return KErrNone;
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CAsyncCallback::MakeCallbackL
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CAsyncCallback::MakeCallbackL( TSTUNCallbackInfo::TFunction aFunction,
       
   130                                     const CBinding* aBinding,
       
   131                                     TInt aErrorCode,
       
   132                                     const CSTUNClient* aClient )
       
   133     {    
       
   134     TSTUNCallbackInfo* callback = new ( ELeave ) TSTUNCallbackInfo( aFunction,
       
   135                                                                     aBinding,
       
   136                                                                     aErrorCode,
       
   137                                                                     aClient );
       
   138     CleanupStack::PushL( callback );
       
   139     __STUN_ASSERT_L( callback->Validate(), KErrArgument );
       
   140     CleanupStack::Pop( callback );
       
   141 
       
   142     //CActive::iActive won't tell whether User::RequestComplete has already
       
   143     //been called, so iStatus is inspected to see whether a previous call to
       
   144     //AddDeleteRequestL has already called User::RequestComplete.
       
   145     if ( iStatus == KRequestPending )
       
   146         {
       
   147         Wakeup();
       
   148         }
       
   149 
       
   150     iPendingCallbacks.AddLast( *callback );
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CAsyncCallback::CancelCallback
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CAsyncCallback::CancelCallback( const CBinding& aBinding )
       
   158     {
       
   159     TSglQueIter<TSTUNCallbackInfo> iter( iPendingCallbacks );
       
   160 
       
   161     for ( TSTUNCallbackInfo* callback = iter++; callback; callback = iter++ )
       
   162         {
       
   163         if ( callback->iBinding == &aBinding )
       
   164             {
       
   165             iPendingCallbacks.Remove( *callback );
       
   166             delete callback;
       
   167             }
       
   168         }
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CAsyncCallback::ExecuteFirstCallback
       
   173 // Handle one callback request and wait for the next one. If there are more
       
   174 // callbacks, complete the asynchronous request so that RunL will soon be called
       
   175 // again. Don't call more than one callback, since upper layer might've deleted
       
   176 // STUN client, causing CAsyncCallback to be deleted too. For the same reason
       
   177 // nothing is done after executing the callback.
       
   178 // -----------------------------------------------------------------------------
       
   179 //    
       
   180 void CAsyncCallback::ExecuteFirstCallback()
       
   181     {
       
   182     TSTUNCallbackInfo* callback = iPendingCallbacks.First();
       
   183     if ( iPendingCallbacks.IsFirst( callback ) &&
       
   184          !iPendingCallbacks.IsEmpty() )
       
   185         {
       
   186         iPendingCallbacks.Remove( *callback );
       
   187         
       
   188         if ( !iPendingCallbacks.IsEmpty() )
       
   189             {
       
   190             Wakeup();
       
   191             }
       
   192 
       
   193         callback->Execute( iObserver );
       
   194         delete callback;
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CAsyncCallback::WaitForRequests
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CAsyncCallback::WaitForRequests()
       
   203     {
       
   204     __STUN_ASSERT_RETURN( !IsActive(), KErrNotReady );
       
   205 
       
   206     iStatus = KRequestPending;
       
   207     SetActive();
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CAsyncCallback::Wakeup
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CAsyncCallback::Wakeup()
       
   215     {
       
   216     TRequestStatus* status = &iStatus;
       
   217     User::RequestComplete( status, KErrNone );
       
   218     }