mediator/src/Client/MediatorEventConsumerBody.cpp
changeset 0 4e1aa6a622a0
child 10 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 receiving Mediator events.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 #include    "MediatorEventConsumerBody.h"
       
    22 #include    "MediatorServerClient.h"
       
    23 #include    "Debug.h"
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 CMediatorEventConsumerBody::CMediatorEventConsumerBody( MMediatorEventObserver* aObserver )
       
    29     : CActive( EPriorityNormal ),
       
    30       iObserver( aObserver ), 
       
    31       iCategoryBuffer( iCategory ),
       
    32       iEventBuffer( iEvent ),
       
    33       iEventDataPtr( NULL, 0 ),
       
    34       iDestroyed ( NULL )
       
    35     {
       
    36     }
       
    37 
       
    38 void CMediatorEventConsumerBody::ConstructL()
       
    39     {
       
    40     CActiveScheduler::Add( this );
       
    41     User::LeaveIfError( iMediatorServer.Connect() );
       
    42     ResetDataBufferL( KMaxEventData );  
       
    43     }
       
    44 
       
    45 CMediatorEventConsumerBody* CMediatorEventConsumerBody::NewL(
       
    46                         MMediatorEventObserver* aObserver )
       
    47     {
       
    48     CMediatorEventConsumerBody* self = new( ELeave ) CMediatorEventConsumerBody( aObserver );
       
    49     
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53 
       
    54     return self;
       
    55     }
       
    56     
       
    57 CMediatorEventConsumerBody::~CMediatorEventConsumerBody()
       
    58     {
       
    59     Cancel();
       
    60     iMediatorServer.Close();
       
    61     delete iEventData;
       
    62     
       
    63     if ( iDestroyed ) // RunL is being executed
       
    64         {
       
    65         *iDestroyed = ETrue;
       
    66         }
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CMediatorEventConsumerBody::RunL
       
    71 //  
       
    72 // (other items were commented in a header).
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void CMediatorEventConsumerBody::RunL()
       
    76     {
       
    77     TRACE(Print(_L("[Mediator Server]\t CMediatorEventConsumerBody::RunL status %d\n"), iStatus.Int() ));
       
    78     
       
    79     if ( iObserver && ( iStatus >= KErrNone ) )
       
    80         {
       
    81         // Check the parameter data size. If bigger than expected, fetch it synchronously
       
    82         TInt dataSize = iStatus.Int();
       
    83         if ( dataSize > iEventDataPtr.MaxLength() )
       
    84             {
       
    85             // Reset data buffer for bigger size
       
    86             ResetDataBufferL( dataSize );
       
    87             
       
    88             // Fetch data from Mediator
       
    89             iMediatorServer.FetchParameterData( iEventDataPtr );
       
    90             }
       
    91         
       
    92         // Set the flag to the member variable that is updated by the destructor 
       
    93         // in case this instance is destroyed by observer callback.
       
    94         // Otherwise an attempt to manipulate member data after destruction will cause a panic.
       
    95         TBool destroyed = EFalse;
       
    96         iDestroyed = &destroyed;
       
    97         
       
    98         // must be trapped, so that leave won't cause access violation (iDestroyed pointing to a non-existing variable)
       
    99         TRAPD( err, iObserver->MediatorEventL( iCategory.iDomain,
       
   100                                                iCategory.iCategory, 
       
   101                                                iEvent.iEventId, 
       
   102                                                *iEventData ) );
       
   103         
       
   104         if ( err != KErrNone )
       
   105             {
       
   106             ERROR_TRACE(Print(_L("[Mediator] CMediatorEventConsumerBody::RunL: err=%d\n"), err ) );
       
   107             if ( !destroyed )
       
   108                 {
       
   109                 iDestroyed = NULL;
       
   110                 }
       
   111             User::Leave( err );
       
   112             }
       
   113         
       
   114         if ( destroyed )
       
   115             {
       
   116             return; // instance destroyed in MediatorEventL, don't proceed to event receiving
       
   117             }
       
   118         
       
   119         iDestroyed = NULL; // set member variable to NULL, because local variable goes out of scope            
       
   120         }
       
   121     StartEventReceiving();
       
   122     }
       
   123         
       
   124 // -----------------------------------------------------------------------------
       
   125 // CMediatorEventConsumerBody::DoCancel
       
   126 //  
       
   127 // (other items were commented in a header).
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 void CMediatorEventConsumerBody::DoCancel()
       
   131     {
       
   132     LOG(_L("[Mediator Server]\t CMediatorEventConsumerBody::DoCancel\n")); 
       
   133     iMediatorServer.Cancel();    
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CMediatorEventConsumerBody::SubscribeEventL
       
   138 // Subscribe to event category
       
   139 // (other items were commented in a header).
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 TInt CMediatorEventConsumerBody::SubscribeEvent( TUid aDomain,
       
   143                                                       TUid aCategory,
       
   144                                                       const REventList& aEvents )
       
   145     {
       
   146     LOG(_L("[Mediator Server]\t CMediatorEventConsumerBody::SubscribeEvent\n"));
       
   147     // Start to receive
       
   148     StartEventReceiving();
       
   149     // Subscribe
       
   150     return iMediatorServer.SubscribeEventList( aDomain, 
       
   151                                                aCategory,
       
   152                                                aEvents );
       
   153     }
       
   154     
       
   155 // -----------------------------------------------------------------------------
       
   156 // CMediatorEventConsumerBody::SubscribeEventL
       
   157 // Subscribe to event
       
   158 // (other items were commented in a header).
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 TInt CMediatorEventConsumerBody::SubscribeEvent( TUid aDomain,
       
   162                                                       TUid aCategory, 
       
   163                                                       TInt aEventId,
       
   164                                                       TVersion aVersion )
       
   165     {
       
   166     LOG(_L("[Mediator Server]\t CMediatorEventConsumerBody::SubscribeEvent\n"));
       
   167     
       
   168     __UHEAP_MARK;
       
   169         
       
   170     // Create a list to contain one event
       
   171     REventList eventList;
       
   172     TEvent event;
       
   173     event.iEventId = aEventId;
       
   174     event.iVersion = aVersion;
       
   175     TInt error = eventList.Append( event );
       
   176         
       
   177     if ( !error )
       
   178         {
       
   179         // Start to receive
       
   180         StartEventReceiving();
       
   181         // Subscribe eventlist
       
   182         error = iMediatorServer.SubscribeEventList( aDomain,
       
   183                                                     aCategory,
       
   184                                                     eventList );
       
   185         }
       
   186     else
       
   187         {
       
   188         ERROR_TRACE(Print(_L("[Mediator] CMediatorEventConsumerBody::SubscribeEvent: error=%d\n"), error ) );
       
   189         }
       
   190         
       
   191     // Clean up                                        
       
   192     eventList.Reset();
       
   193     
       
   194     __UHEAP_MARKEND;
       
   195     return error;
       
   196     }
       
   197     
       
   198 // -----------------------------------------------------------------------------
       
   199 // CMediatorEventConsumerBody::UnsubscribeEvent
       
   200 // Unsubscibe an event category.
       
   201 // (other items were commented in a header).
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 TInt CMediatorEventConsumerBody::UnsubscribeEvent( TUid aDomain,
       
   205                                                         TUid aCategory,
       
   206                                                         const REventList& aEvents )
       
   207     {
       
   208     LOG(_L("[Mediator Server]\t CMediatorEventConsumerBody::UnsubscribeEvent list\n"));
       
   209     return iMediatorServer.UnsubscribeEventList( aDomain, aCategory, aEvents );
       
   210     }
       
   211     
       
   212 // -----------------------------------------------------------------------------
       
   213 // CMediatorEventConsumerBody::UnsubscribeEvent
       
   214 // Unsubscibe an event.
       
   215 // (other items were commented in a header).
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TInt CMediatorEventConsumerBody::UnsubscribeEvent( TUid aDomain,
       
   219                                                         TUid aCategory, 
       
   220                                                         TInt aEventId )
       
   221     {
       
   222     LOG(_L("[Mediator Server]\t CMediatorEventConsumerBody::UnsubscribeEvent\n"));
       
   223     __UHEAP_MARK;
       
   224     
       
   225     // Create a list to contain one event
       
   226     REventList eventList;
       
   227     TEvent event;
       
   228     event.iEventId = aEventId;
       
   229     TInt error = eventList.Append( event );
       
   230         
       
   231     if ( !error )
       
   232         {
       
   233         // Unsubscribe eventlist
       
   234         error = iMediatorServer.UnsubscribeEventList( aDomain,
       
   235                                                       aCategory,
       
   236                                                       eventList );
       
   237         }
       
   238     else
       
   239         {
       
   240         ERROR_TRACE(Print(_L("[Mediator] CMediatorEventConsumerBody::UnsubscribeEvent: error=%d\n"), error ) );
       
   241         }        
       
   242         
       
   243     // Clean up                                        
       
   244     eventList.Reset();
       
   245     
       
   246     __UHEAP_MARKEND;
       
   247     return error;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CMediatorEventConsumerBody::StartEventReceiving
       
   252 // Starts to wait for events
       
   253 // (other items were commented in a header).
       
   254 // -----------------------------------------------------------------------------
       
   255 //    
       
   256 void CMediatorEventConsumerBody::StartEventReceiving()
       
   257     {
       
   258 	if ( !IsActive() )
       
   259 	    {
       
   260 	    iMediatorServer.ReceiveEvents( iStatus, 
       
   261 	                                   iCategoryBuffer, 
       
   262 	                                   iEventBuffer, 
       
   263 	                                   iEventDataPtr );
       
   264         SetActive();	                                   
       
   265 	    }
       
   266     }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CMediatorEventConsumerBody::ResetDataBufferL
       
   270 // Starts to receive commands asynchronously
       
   271 // (other items were commented in a header).
       
   272 // -----------------------------------------------------------------------------
       
   273 //    
       
   274 void CMediatorEventConsumerBody::ResetDataBufferL( TInt aSize )
       
   275     {
       
   276     if ( iEventData )
       
   277         {
       
   278         delete iEventData;
       
   279         iEventData = NULL;
       
   280         }
       
   281     iEventData = HBufC8::NewL( aSize );
       
   282     iEventDataPtr.Set( iEventData->Des() );
       
   283        
       
   284     }
       
   285 //  End of File