mediator/src/Client/MediatorCommandResponderBody.cpp
changeset 0 4e1aa6a622a0
child 7 1fc153c72b60
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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:  An implementation class for responding to Mediator Service commands.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 #include    "MediatorCommandResponderBody.h"
       
    22 #include    "MediatorServerClient.h"
       
    23 #include    "Debug.h"
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 CMediatorCommandResponderBody::CMediatorCommandResponderBody( MMediatorCommandObserver* aObserver )
       
    29     : CActive( EPriorityStandard ),
       
    30       iObserver( aObserver ),
       
    31       iCategoryBuffer( iCategory ), 
       
    32       iCommandBuffer( iCommand ),
       
    33       iCommandDataPtr( NULL, 0 ),
       
    34       iDestroyed( NULL )
       
    35     {
       
    36     }
       
    37 
       
    38 void CMediatorCommandResponderBody::ConstructL()
       
    39     {
       
    40     CActiveScheduler::Add( this );
       
    41     User::LeaveIfError( iMediatorServer.Connect() );
       
    42     ResetDataBufferL( KMaxCommandData );
       
    43     }
       
    44 
       
    45 CMediatorCommandResponderBody* CMediatorCommandResponderBody::NewL(
       
    46                 MMediatorCommandObserver* aObserver )
       
    47     {
       
    48     CMediatorCommandResponderBody* self = new( ELeave ) CMediatorCommandResponderBody( aObserver );
       
    49     
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53 
       
    54     return self;
       
    55     }
       
    56     
       
    57 CMediatorCommandResponderBody::~CMediatorCommandResponderBody()
       
    58     {
       
    59     Cancel();
       
    60     iMediatorServer.Close();
       
    61     delete iCommandData;
       
    62     
       
    63     if ( iDestroyed ) // RunL is being executed
       
    64         {
       
    65         *iDestroyed = ETrue;
       
    66         }
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CMediatorCommandResponderBody::DoCancel
       
    71 //  
       
    72 // (other items were commented in a header).
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void CMediatorCommandResponderBody::DoCancel()
       
    76     {
       
    77     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandResponderBody::DoCancel\n")));
       
    78     iMediatorServer.Cancel();    
       
    79     }
       
    80 
       
    81 
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CMediatorCommandResponderBody::RunL
       
    85 //  
       
    86 // (other items were commented in a header).
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CMediatorCommandResponderBody::RunL()
       
    90     {
       
    91     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandResponderBody::RunL status %d\n"), iStatus.Int() ));
       
    92     
       
    93     if ( iObserver )
       
    94         {
       
    95         
       
    96         // Set the flag to the member variable that is updated by the destructor 
       
    97         // in case this instance is destroyed by observer callback.
       
    98         // Otherwise an attempt to manipulate member data after destruction will cause a panic.
       
    99         TBool destroyed = EFalse;
       
   100                 
       
   101         // Check if there is an error --> Cancel command
       
   102         if ( iStatus < 0 )
       
   103             {
       
   104             ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::RunL: iStatus=%d\n"), iStatus.Int() ) );
       
   105             iDestroyed = &destroyed;
       
   106             iObserver->CancelMediatorCommand( iCategory.iDomain,
       
   107                                               iCategory.iCategory, 
       
   108                                               iCommand.iCommandId );
       
   109             }
       
   110         else    // Everything should be ok
       
   111             {
       
   112             // Check the parameter data size. If bigger than expected, fetch it synchronously
       
   113             TInt dataSize = iStatus.Int();
       
   114             if ( dataSize > iCommandDataPtr.MaxLength() )
       
   115                 {
       
   116                 // Reset data buffer for bigger size
       
   117                 ResetDataBufferL( dataSize );
       
   118                 
       
   119                 // Fetch data from Mediator
       
   120                 iMediatorServer.FetchParameterData( iCommandDataPtr );
       
   121                 }
       
   122             
       
   123             iDestroyed = &destroyed; // can't set earlier because leaving functions are called
       
   124                 
       
   125             // Issue callback to client. 
       
   126             // For some reason error handling can't be done in RunError because server stops forwarding commands
       
   127             TRAPD( err, iObserver->MediatorCommandL( iCategory.iDomain,
       
   128                                          iCategory.iCategory, 
       
   129                                          iCommand.iCommandId,
       
   130                                          iCommand.iVersion, 
       
   131                                          *iCommandData ) );
       
   132             
       
   133             if ( err != KErrNone ) // Errors only are propagated back to command initiator
       
   134                 {
       
   135                 
       
   136                 ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::RunL: err=%d\n"), err ) );
       
   137                 
       
   138                 if ( !destroyed )
       
   139                     {
       
   140                     iMediatorServer.IssueResponse ( iCategory.iDomain,
       
   141                                                     iCategory.iCategory,
       
   142                                                     iCommand.iCommandId,
       
   143                                                     err,
       
   144                                                     KNullDesC8 );    
       
   145                     }
       
   146                 }
       
   147             }
       
   148         
       
   149         if ( destroyed ) // instance does not exist any longer, don't proceed to command receiving
       
   150             {
       
   151             return;
       
   152             }
       
   153         
       
   154         iDestroyed = NULL; // set to NULL, because variable goes out of scope soon            
       
   155         
       
   156         }
       
   157     
       
   158     
       
   159     
       
   160     // Continue command receiving
       
   161     // Wait for next commands                                      
       
   162     StartCommandReceiving();           
       
   163     
       
   164     }
       
   165     
       
   166 // -----------------------------------------------------------------------------
       
   167 // CMediatorCommandResponderBody::RegisterCommandL
       
   168 // Register a command category.
       
   169 // (other items were commented in a header).
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TInt CMediatorCommandResponderBody::RegisterCommand( 
       
   173                                         TUid aDomain,
       
   174                                         TUid aCategory, 
       
   175                                         const RCommandList& aCommands )
       
   176     {
       
   177     LOG(_L("[Mediator Server]\t CMediatorCommandResponderBody::RegisterCommand list"));
       
   178     // Send the registration
       
   179     TInt status = iMediatorServer.RegisterCommandList( aDomain, aCategory, aCommands );
       
   180     
       
   181     if ( status == KErrNone )
       
   182         {
       
   183         // Wait for incoming commands
       
   184         StartCommandReceiving();                        
       
   185         }
       
   186     else
       
   187         {
       
   188         ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::RegisterCommand: status=%d\n"), status ) );
       
   189         }
       
   190                 
       
   191     return status;
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CMediatorCommandResponderBody::RegisterCommandL
       
   196 // Register a command.
       
   197 // (other items were commented in a header).
       
   198 // -----------------------------------------------------------------------------
       
   199 //        
       
   200 TInt CMediatorCommandResponderBody::RegisterCommand( TUid aDomain, 
       
   201                                                      TUid aCategory, 
       
   202                                                      TInt aCommandId, 
       
   203                                                      TVersion aVersion,
       
   204                                                      TCapabilitySet aCaps, 
       
   205                                                      TInt aTimeOut )
       
   206     {
       
   207     LOG(_L("[Mediator Server]\t CMediatorCommandResponderBody::RegisterCommand"));
       
   208     
       
   209     // Create a list to contain one command
       
   210     RCommandList commandList;
       
   211     TCommand newCommand;
       
   212     newCommand.iCommandId   = aCommandId;
       
   213     newCommand.iVersion     = aVersion;
       
   214     newCommand.iCaps        = aCaps;
       
   215     newCommand.iTimeout     = aTimeOut;
       
   216     TInt error = commandList.Append( newCommand );
       
   217         
       
   218     if ( error == KErrNone )
       
   219         {
       
   220         // Register command
       
   221         error = iMediatorServer.RegisterCommandList( aDomain,
       
   222                                                   aCategory,
       
   223                                                   commandList );
       
   224         if ( error == KErrNone ) 
       
   225             {
       
   226             // Wait for incoming commands
       
   227             StartCommandReceiving();    
       
   228             }
       
   229         else
       
   230             {
       
   231             ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::RegisterCommand (ln%d): error=%d\n"), __LINE__, error ) );
       
   232             }
       
   233         }
       
   234     else
       
   235         {
       
   236         ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::RegisterCommand (ln%d): error=%d\n"), __LINE__, error ) );    
       
   237         }        
       
   238     
       
   239     
       
   240             
       
   241     commandList.Reset();
       
   242     commandList.Close();
       
   243     
       
   244     return error;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CMediatorCommandResponderBody::UnregisterCommand
       
   249 // Unregister a command list.
       
   250 // (other items were commented in a header).
       
   251 // -----------------------------------------------------------------------------
       
   252 //        
       
   253 TInt CMediatorCommandResponderBody::UnregisterCommand( 
       
   254                                             TUid aDomain, 
       
   255                                             TUid aCategory,
       
   256                                             const RCommandList& aCommands )
       
   257     {
       
   258     LOG(_L("[Mediator Server]\t CMediatorCommandResponderBody::UnregisterCommand list"));
       
   259     
       
   260     return iMediatorServer.UnregisterCommandList( aDomain, aCategory, aCommands );
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CMediatorCommandResponderBody::UnregisterCommand
       
   265 // Unregister a command.
       
   266 // (other items were commented in a header).
       
   267 // -----------------------------------------------------------------------------
       
   268 //        
       
   269 TInt CMediatorCommandResponderBody::UnregisterCommand( 
       
   270                                          TUid aDomain, 
       
   271                                          TUid aCategory, 
       
   272                                          TInt aCommandId )
       
   273     {
       
   274     LOG(_L("[Mediator Server]\t CMediatorCommandResponderBody::UnregisterCommand"));
       
   275         
       
   276     // Create a list to contain one command
       
   277     RCommandList commandList;
       
   278     TCommand command;
       
   279     command.iCommandId   = aCommandId;
       
   280     TInt error = commandList.Append( command );
       
   281         
       
   282     if ( !error )
       
   283         {
       
   284         // Unregister command
       
   285         error = iMediatorServer.UnregisterCommandList( aDomain,
       
   286                                                        aCategory,
       
   287                                                        commandList );
       
   288         }
       
   289     else
       
   290         {
       
   291         ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandResponderBody::UnregisterCommand: error=%d\n"), error ) );
       
   292         }
       
   293                 
       
   294     commandList.Reset();
       
   295     commandList.Close();
       
   296     
       
   297     return error;
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // CMediatorCommandResponderBody::IssueResponseL
       
   302 // Issue a response to a command.
       
   303 // (other items were commented in a header).
       
   304 // -----------------------------------------------------------------------------
       
   305 //        
       
   306 TInt CMediatorCommandResponderBody::IssueResponse( TUid aDomain,
       
   307                                                    TUid aCategory, 
       
   308                                                    TInt aCommandId,
       
   309                                                    TInt aStatus, 
       
   310                                                    const TDesC8& aData )
       
   311     {
       
   312     LOG(_L("[Mediator Server]\t CMediatorCommandResponderBody::IssueResponse"));
       
   313     return iMediatorServer.IssueResponse( aDomain,
       
   314                                           aCategory,
       
   315                                           aCommandId,
       
   316                                           aStatus,
       
   317                                           aData );
       
   318     }
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CMediatorCommandResponderBody::StartCommandReceiving
       
   322 // Starts to receive commands asynchronously
       
   323 // (other items were commented in a header).
       
   324 // -----------------------------------------------------------------------------
       
   325 //    
       
   326 void CMediatorCommandResponderBody::StartCommandReceiving()
       
   327     {
       
   328     if ( !IsActive() )
       
   329         {
       
   330         iMediatorServer.ReceiveCommands( iStatus,
       
   331                                          iCategoryBuffer,
       
   332                                          iCommandBuffer,
       
   333                                          iCommandDataPtr );
       
   334         SetActive();   
       
   335         }         
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CMediatorCommandResponderBody::ResetDataBufferL
       
   340 // Starts to receive commands asynchronously
       
   341 // (other items were commented in a header).
       
   342 // -----------------------------------------------------------------------------
       
   343 //    
       
   344 void CMediatorCommandResponderBody::ResetDataBufferL( TInt aSize )
       
   345     {
       
   346     if ( iCommandData )
       
   347         {
       
   348         delete iCommandData;
       
   349         iCommandData = NULL;
       
   350         }
       
   351     iCommandData = HBufC8::NewL( aSize );
       
   352     iCommandDataPtr.Set( iCommandData->Des() );
       
   353        
       
   354     }
       
   355 
       
   356 //  End of File