mediator/src/Client/MediatorCommandInitiatorBody.cpp
changeset 0 4e1aa6a622a0
child 10 1fc153c72b60
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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 issuing Mediator Service commands.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 #include    "MediatorCommandInitiatorBody.h"
       
    22 #include    "MediatorServerClient.h"
       
    23 #include    "Debug.h"
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 CMediatorCommandInitiatorBody::CMediatorCommandInitiatorBody(
       
    29                 MMediatorCommandResponseObserver* aObserver )
       
    30     : CActive( EPriorityStandard ),
       
    31       iObserver( aObserver ),
       
    32       iCommandDataPtr( NULL, 0 ),
       
    33       iCategoryBuffer( iCategory ),
       
    34       iStatusBuffer( iResponseStatus ),
       
    35       iCommandBuffer( iCommand )
       
    36     {
       
    37     }
       
    38 
       
    39 void CMediatorCommandInitiatorBody::ConstructL()
       
    40     {
       
    41     CActiveScheduler::Add( this );
       
    42     User::LeaveIfError( iMediatorServer.Connect() );
       
    43     ResetDataBufferL( KMaxCommandData ); 
       
    44     }
       
    45 
       
    46 CMediatorCommandInitiatorBody* CMediatorCommandInitiatorBody::NewL(
       
    47                 MMediatorCommandResponseObserver* aObserver )
       
    48     {
       
    49     CMediatorCommandInitiatorBody* self 
       
    50                 = new( ELeave ) CMediatorCommandInitiatorBody( aObserver );
       
    51     
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop( self );
       
    55 
       
    56     return self;
       
    57     }
       
    58     
       
    59 CMediatorCommandInitiatorBody::~CMediatorCommandInitiatorBody()
       
    60     {
       
    61     Cancel();
       
    62     iMediatorServer.Close();
       
    63     delete iCommandData;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CMediatorCommandInitiatorBody::DoCancel
       
    68 //  
       
    69 // (other items were commented in a header).
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void CMediatorCommandInitiatorBody::DoCancel()
       
    73     {
       
    74     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandInitiatorBody::DoCancel\n")));
       
    75     iMediatorServer.Cancel();  
       
    76     }
       
    77 
       
    78 
       
    79 
       
    80 
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CMediatorCommandInitiatorBody::IssueCommandL
       
    84 //  
       
    85 // (other items were commented in a header).
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 TInt CMediatorCommandInitiatorBody::IssueCommand( TUid aDomain,
       
    89                                                   TUid aCategory, 
       
    90                                                   TInt aCommandId,
       
    91                                                   TVersion aVersion, 
       
    92                                                   const TDesC8& aData )
       
    93     {
       
    94     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandInitiatorBody::IssueCommandL\n"))); 
       
    95     
       
    96     // Check that we are not already active
       
    97     if ( IsActive() )
       
    98         {
       
    99         ERROR_LOG(_L("[Mediator] CMediatorCommandIntiatorBody::IssueCommand: IsActive returned KErrInUse\n"));
       
   100         return KErrInUse;
       
   101         }
       
   102     
       
   103     // Initiate response waiting before command, otherwise there will be a concurrency problem
       
   104     // with clients that issue response in command handler
       
   105     WaitForCommandResponse();
       
   106     
       
   107     // Send the command to Mediator                                 
       
   108     TInt status = iMediatorServer.IssueCommand( aDomain, 
       
   109                                                 aCategory,
       
   110                                                 aCommandId, 
       
   111                                                 aVersion,
       
   112                                                 aData );
       
   113     
       
   114     // Cancel command waiting request
       
   115     if ( status != KErrNone )
       
   116         {
       
   117         ERROR_TRACE(Print(_L("[Mediator] CMediatorCommandIntiatorBody::IssueCommand: status=%d\n"), status ) );
       
   118         // Cancel wait for response in server
       
   119         iMediatorServer.CancelCommand ( aDomain, aCategory, aCommandId );
       
   120         Cancel();
       
   121         }
       
   122     
       
   123     return status;
       
   124     }
       
   125     
       
   126 // -----------------------------------------------------------------------------
       
   127 // CMediatorCommandInitiatorBody::CancelCommand
       
   128 //  
       
   129 // (other items were commented in a header).
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CMediatorCommandInitiatorBody::CancelCommand( TUid aDomain, 
       
   133                                                         TUid aCategory, 
       
   134                                                         TInt aCommandId )
       
   135     {
       
   136     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandInitiatorBody::CancelCommand\n")));
       
   137     
       
   138     // Send the cancellation
       
   139     iMediatorServer.CancelCommand( aDomain, aCategory, aCommandId );
       
   140     Cancel(); // cancel also the pending request, so that subsequent commands will be served 
       
   141     
       
   142     }
       
   143 // -----------------------------------------------------------------------------
       
   144 // CMediatorCommandInitiatorBody::RunL
       
   145 //  
       
   146 // (other items were commented in a header).
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CMediatorCommandInitiatorBody::RunL()
       
   150     {
       
   151     TRACE(Print(_L("[Mediator Server]\t CMediatorCommandInitiatorBody::RunL status %d\n"), iStatus.Int() )); 
       
   152     
       
   153     // in case of error, no callbacks
       
   154     if ( iStatus.Int() >= 0 && iObserver )
       
   155         {
       
   156         // Check the parameter data size. If bigger than expected, fetch it synchronously
       
   157         TInt dataSize = iStatus.Int();
       
   158         if ( dataSize > iCommandDataPtr.MaxLength() )
       
   159             {
       
   160             // Reset data buffer for bigger size
       
   161             ResetDataBufferL( dataSize );
       
   162             
       
   163             // Fetch data from Mediator
       
   164             iMediatorServer.FetchParameterData( iCommandDataPtr );
       
   165             }
       
   166         iObserver->CommandResponseL( iCategory.iDomain,
       
   167                                      iCategory.iCategory,
       
   168                                      iCommand.iCommandId, 
       
   169                                      iResponseStatus,
       
   170                                      *iCommandData );                                  
       
   171         }
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CMediatorCommandInitiatorBody::WaitForCommandResponse
       
   176 //  
       
   177 // (other items were commented in a header).
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CMediatorCommandInitiatorBody::WaitForCommandResponse()
       
   181     {
       
   182     // To be sure no duplicate requests are raised
       
   183     if ( IsActive() )
       
   184         {
       
   185         return;
       
   186         }
       
   187     iMediatorServer.WaitForCommandResponse( iStatus,
       
   188                                             iCategoryBuffer,
       
   189                                             iCommandBuffer,
       
   190                                             iCommandDataPtr,
       
   191                                             iStatusBuffer );
       
   192     SetActive();                                            
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CMediatorCommandResponder::ResetDataBufferL
       
   197 // Starts to receive commands asynchronously
       
   198 // (other items were commented in a header).
       
   199 // -----------------------------------------------------------------------------
       
   200 //    
       
   201 void CMediatorCommandInitiatorBody::ResetDataBufferL( TInt aSize )
       
   202     {
       
   203     if ( iCommandData )
       
   204         {
       
   205         delete iCommandData;
       
   206         iCommandData = NULL;
       
   207         }
       
   208     iCommandData = HBufC8::NewL( aSize );
       
   209     iCommandDataPtr.Set( iCommandData->Des() );
       
   210        
       
   211     }
       
   212 
       
   213 
       
   214 //  End of File