mediator/src/Client/MediatorNotificationsBody.cpp
changeset 0 4e1aa6a622a0
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 for receiving command/event notifications and info.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 #include    "MediatorNotificationsBody.h"
       
    22 #include    "MediatorServerClient.h"
       
    23 #include    "Debug.h"
       
    24 
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KMediatorDefaultCommandCount = 10;
       
    28 const TInt KMediatorDefaultEventCount = 10;
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 CMediatorNotificationsBody::CMediatorNotificationsBody()
       
    33     : CActive( EPriorityNormal ), 
       
    34       iCommandArrayPtr(NULL, 0), 
       
    35       iEventArrayPtr(NULL, 0),
       
    36       iCategoryBuffer( iCategory ),
       
    37       iNotificationTypeBuffer( iNotificationType ),
       
    38       iDestroyed(NULL)
       
    39      
       
    40     {
       
    41     }
       
    42 
       
    43 void CMediatorNotificationsBody::ConstructL()
       
    44     {
       
    45     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::ConstructL\n"));
       
    46     CActiveScheduler::Add( this );
       
    47     User::LeaveIfError( iMediatorServer.Connect() );
       
    48     }
       
    49 
       
    50 CMediatorNotificationsBody* CMediatorNotificationsBody::NewL()
       
    51     {
       
    52     CMediatorNotificationsBody* self = new( ELeave ) CMediatorNotificationsBody;
       
    53     
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop( self );
       
    57 
       
    58     return self;
       
    59     }
       
    60     
       
    61 CMediatorNotificationsBody::~CMediatorNotificationsBody()
       
    62     {
       
    63     Cancel();
       
    64     iMediatorServer.Close();
       
    65     iEventList.Close();
       
    66     iCommandList.Close();
       
    67     
       
    68     if ( iDestroyed ) // RunL is being executed
       
    69         {
       
    70         *iDestroyed = ETrue;
       
    71         }
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CMediatorNotificationsBody::RunL
       
    76 //  
       
    77 // (other items were commented in a header).
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CMediatorNotificationsBody::RunL()
       
    81     {
       
    82     TRACE(Print(_L("[Mediator Server]\t CMediatorNotificationsBody::RunL status %d\n"), iStatus.Int() ));
       
    83     
       
    84     if ( iStatus < 0 )  // We have an error
       
    85         {
       
    86         ERROR_TRACE(Print(_L("[Mediator] CMediatorNotificationsBody::RunL: iStatus=%d\n"), iStatus.Int() ) );
       
    87         
       
    88         // Cleanup and return
       
    89         iCommandList.Reset();
       
    90         iEventList.Reset();
       
    91         return;
       
    92         }
       
    93     
       
    94     // Set the flag to the member variable that is updated by the destructor 
       
    95     // in case this instance is destroyed by observer callback.
       
    96     // Otherwise an attempt to manipulate member data after destruction will cause a panic.
       
    97     TBool destroyed = EFalse;
       
    98     // iDestroyed is set individually in each case in order to prevent erros if a leave occurs
       
    99     
       
   100     TInt err = KErrNone;
       
   101     
       
   102     // Act according to notification type. NOTE! All the observer callback-calls are trapped because
       
   103     // leave would cause a possible access violation in destructor (iDestoryed pointing to an non-existing variable)
       
   104     switch( iNotificationType )
       
   105         {
       
   106         case EMediatorEventsRegistered:
       
   107             {
       
   108             LOG(_L("[Mediator Server]\t Events registered\n"));
       
   109             if ( iObserver )
       
   110                 {
       
   111                 TBool fetchEvents = iStatus.Int() > iEventList.Count() ? ETrue : EFalse;
       
   112                 
       
   113                 ResizeEventListL( iStatus.Int() );
       
   114                 
       
   115                 if ( fetchEvents )
       
   116                     {
       
   117                     FetchEventListL();
       
   118                     }
       
   119                 
       
   120                 iDestroyed = &destroyed;
       
   121                 TRAP( err, iObserver->MediatorEventsAddedL( iCategory.iDomain,
       
   122                                                             iCategory.iCategory,
       
   123                                                             iEventList ) );
       
   124                 }
       
   125             break;
       
   126             }
       
   127         case EMediatorCommandsRegistered:
       
   128             {
       
   129             LOG(_L("[Mediator Server]\t Commands registered\n"));
       
   130             if ( iObserver )
       
   131                 {
       
   132                 TBool fetchCommands = iStatus.Int() > iCommandList.Count() ? ETrue : EFalse;
       
   133                 
       
   134                 ResizeCommandListL( iStatus.Int() );
       
   135                 
       
   136                 if ( fetchCommands )
       
   137                     {
       
   138                     FetchCommandListL();
       
   139                     }
       
   140                 
       
   141                 iDestroyed = &destroyed;    
       
   142                 TRAP( err, iObserver->MediatorCommandsAddedL( iCategory.iDomain,
       
   143                                                               iCategory.iCategory,
       
   144                                                               iCommandList ) );
       
   145                 }
       
   146             break;
       
   147             }
       
   148         case EMediatorEventsUnregistered:
       
   149             {
       
   150             LOG(_L("[Mediator Server]\t Events unregistered\n"));
       
   151             if ( iObserver )
       
   152                 {
       
   153                 TBool fetchEvents = iStatus.Int() > iEventList.Count() ? ETrue : EFalse;
       
   154                 
       
   155                 ResizeEventListL( iStatus.Int() );
       
   156                 
       
   157                 if ( fetchEvents )
       
   158                     {
       
   159                     FetchEventListL();
       
   160                     }
       
   161                 
       
   162                 iDestroyed = &destroyed;    
       
   163                 TRAP( err, iObserver->MediatorEventsRemovedL( iCategory.iDomain,
       
   164                                                               iCategory.iCategory,
       
   165                                                               iEventList ) );
       
   166                 }
       
   167             break;
       
   168             }
       
   169         case EMediatorCommandsUnregistered:
       
   170             {
       
   171             LOG(_L("[Mediator Server]\t Commands unregistered\n"));
       
   172             if ( iObserver )
       
   173                 {
       
   174                 TBool fetchCommands = iStatus.Int() > iCommandList.Count() ? ETrue : EFalse;
       
   175                 
       
   176                 ResizeCommandListL( iStatus.Int() );
       
   177                 
       
   178                 if ( fetchCommands )
       
   179                     {
       
   180                     FetchCommandListL();
       
   181                     }
       
   182                 
       
   183                 iDestroyed = &destroyed;    
       
   184                 TRAP( err, iObserver->MediatorCommandsRemovedL( iCategory.iDomain,
       
   185                                                                 iCategory.iCategory,
       
   186                                                                 iCommandList ) );
       
   187                 }
       
   188             break;
       
   189             }
       
   190         case EMediatorCategoryUnregistered:
       
   191             {
       
   192             
       
   193             LOG(_L("[Mediator Server]\t Category unregistered\n"));
       
   194             if ( iObserver )
       
   195                 {
       
   196                 iDestroyed = &destroyed;
       
   197                 TRAP( err, iObserver->MediatorCategoryRemovedL( iCategory.iDomain,
       
   198                                                                 iCategory.iCategory ) );
       
   199                 }
       
   200             break;
       
   201             }
       
   202         default:
       
   203             {
       
   204             LOG(_L("[Mediator Server]\t Unknown notification\n"));
       
   205             break;
       
   206             }
       
   207         }
       
   208     
       
   209     
       
   210     if ( err != KErrNone )
       
   211         {
       
   212         ERROR_TRACE(Print(_L("[Mediator] CMediatorNotificationsBody::RunL: err=%d, iNotificationType=%d\n"), err,
       
   213                                                                                                              iNotificationType ) );
       
   214         if ( !destroyed )
       
   215             {
       
   216             iDestroyed = NULL;
       
   217             }
       
   218             
       
   219         User::Leave( err ); // leave now, if an observer callback caused an error
       
   220         }
       
   221     
       
   222     if ( !destroyed ) // client may delete instance in observer callback
       
   223         {
       
   224         iDestroyed = NULL; // set to NULL, because local variable goes out of scope soon
       
   225         // Continue receiving notifications
       
   226         ReceiveNotificationsL();    
       
   227         }
       
   228     
       
   229     }
       
   230         
       
   231 // -----------------------------------------------------------------------------
       
   232 // CMediatorNotificationsBody::DoCancel
       
   233 //  
       
   234 // (other items were commented in a header).
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CMediatorNotificationsBody::DoCancel()
       
   238     {
       
   239     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::DoCancel\n"));  
       
   240     iMediatorServer.Cancel(); 
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CMediatorNotificationsBody::RegisterNotificationObserver
       
   245 //  
       
   246 // (other items were commented in a header).
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TInt CMediatorNotificationsBody::RegisterNotificationObserver( 
       
   250             MMediatorNotifications* aObserver )
       
   251     {
       
   252     iObserver = aObserver;
       
   253     //return ReceiveNotifications();
       
   254     
       
   255     TRAPD ( err, ReceiveNotificationsL() );
       
   256     
       
   257     return err;
       
   258     }
       
   259             
       
   260 // -----------------------------------------------------------------------------
       
   261 // CMediatorNotificationsBody::UnregisterNotificationObserver
       
   262 //  
       
   263 // (other items were commented in a header).
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 TInt CMediatorNotificationsBody::UnregisterNotificationObserver()
       
   267     {
       
   268     iObserver = NULL;
       
   269     // Empty existing arrays
       
   270     if ( iCommandList.Count() > 0 )
       
   271         {
       
   272         iCommandList.Reset();
       
   273         }
       
   274     if ( iEventList.Count() > 0 )
       
   275         {
       
   276         iEventList.Reset();
       
   277         }
       
   278     // Cancel the show
       
   279     return iMediatorServer.CancelNotifications();
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CMediatorNotificationsBody::GetDomains
       
   284 // Gets a list of registered domains
       
   285 // (other items were commented in a header).
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TInt CMediatorNotificationsBody::GetDomains( RDomainList& aDomains )
       
   289     {
       
   290     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::GetDomains\n"));
       
   291     return iMediatorServer.GetDomains( aDomains );
       
   292     }
       
   293     
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CMediatorNotificationsBody::GetCategories
       
   297 // Gets a list of registered domains
       
   298 // (other items were commented in a header).
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 TInt CMediatorNotificationsBody::GetCategories( TUid aDomain, 
       
   302                                                 RCategoryList& aCategories )
       
   303     {
       
   304     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::GetCategories\n"));
       
   305     return iMediatorServer.GetCategories( aDomain, aCategories );
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // CMediatorNotificationsBody::GetEvents
       
   310 // Gets a list of registered events withing the domain/category
       
   311 // (other items were commented in a header).
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 TInt CMediatorNotificationsBody::GetEvents( TUid aDomain, 
       
   315                                             TUid aCategory,
       
   316                                             REventList& aEvents )
       
   317     {
       
   318     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::GetEvents\n"));
       
   319     return iMediatorServer.GetEvents( aDomain, aCategory, aEvents );
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CMediatorNotificationsBody::GetCommands
       
   324 // Gets a list of registered commands withing the domain/category
       
   325 // (other items were commented in a header).
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 TInt CMediatorNotificationsBody::GetCommands( TUid aDomain, 
       
   329                                               TUid aCategory,
       
   330                                               RCommandList& aCommands )
       
   331     {
       
   332     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::GetCommands\n"));
       
   333     return iMediatorServer.GetCommands( aDomain, aCategory, aCommands );
       
   334     }
       
   335 
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CMediatorNotificationsBody::ReceiveNotificationsL
       
   339 // 
       
   340 // (other items were commented in a header).
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 
       
   344 void CMediatorNotificationsBody::ReceiveNotificationsL()
       
   345     {
       
   346     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::ReceiveNotificationsL\n"));
       
   347     if ( IsActive() )
       
   348         {
       
   349         ERROR_LOG(_L("[Mediator] CMediatorNotificationsBody::ReceiveNotificationsL: User::Leave( KErrAlreadyExists )\n") );
       
   350         User::Leave( KErrAlreadyExists );
       
   351         }
       
   352         
       
   353     ResizeEventListL( KMediatorDefaultEventCount );
       
   354     
       
   355     ResizeCommandListL ( KMediatorDefaultCommandCount );
       
   356     
       
   357     // Define data return pointers
       
   358     TInt commandArraySize = ( sizeof(MediatorService::TCommand) ) * KMediatorDefaultCommandCount; 
       
   359     TPtr8 commandArrayPtr( (TUint8*)&iCommandList[0], commandArraySize );
       
   360     iCommandArrayPtr.Set( commandArrayPtr );
       
   361     
       
   362     TInt eventArraySize = ( sizeof(MediatorService::TEvent) ) * KMediatorDefaultEventCount; 
       
   363     TPtr8 eventArrayPtr( (TUint8*)&iEventList[0], eventArraySize );
       
   364     iEventArrayPtr.Set( eventArrayPtr );
       
   365     
       
   366     SetActive();
       
   367     
       
   368     iMediatorServer.ReceiveNotifications( iStatus,
       
   369                                           iCategoryBuffer, 
       
   370                                           iNotificationTypeBuffer,
       
   371                                           iEventArrayPtr,
       
   372                                           iCommandArrayPtr );
       
   373     }
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CMediatorNotificationsBody::ResizeEventListL
       
   377 // 
       
   378 // (other items were commented in a header).
       
   379 // -----------------------------------------------------------------------------
       
   380 //
       
   381 void CMediatorNotificationsBody::ResizeEventListL( TInt aEventCount )
       
   382     {
       
   383     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::ResizeEventListL\n"));
       
   384     if ( iEventList.Count() > aEventCount ) // less events has been received than space allocated
       
   385         {
       
   386         // suppress list
       
   387         for ( TInt i = iEventList.Count() - 1; i >= aEventCount; i-- )
       
   388             {
       
   389             iEventList.Remove( i );
       
   390             }
       
   391         }
       
   392     else if ( iEventList.Count() < aEventCount )  // more events than initially/previously allocated
       
   393         {
       
   394         // reallocate event list
       
   395         iEventList.Reset();
       
   396         
       
   397         User::LeaveIfError( iEventList.Reserve( aEventCount ) );
       
   398         
       
   399         for ( TInt i = 0; i < aEventCount; i++ )
       
   400             {
       
   401             TEvent emptyEvent;
       
   402             iEventList.Append( emptyEvent );
       
   403             }    
       
   404         }
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CMediatorNotificationsBody::ResizeCommandListL
       
   409 // 
       
   410 // (other items were commented in a header).
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CMediatorNotificationsBody::ResizeCommandListL( TInt aCommandCount )
       
   414     {
       
   415     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::ResizeCommandListL\n"));
       
   416     if ( iCommandList.Count() > aCommandCount ) // less commands has been received than space allocated
       
   417         {
       
   418         // suppress list
       
   419         for ( TInt i = iCommandList.Count() - 1; i >= aCommandCount; i-- )
       
   420             {
       
   421             iCommandList.Remove( i );
       
   422             }
       
   423         }
       
   424     else if ( iCommandList.Count() < aCommandCount )  // more commands than initially/previously allocated
       
   425         {
       
   426         // reallocate command list
       
   427         iCommandList.Reset();
       
   428         
       
   429         User::LeaveIfError( iCommandList.Reserve( aCommandCount) );
       
   430         
       
   431         for ( TInt i = 0; i < aCommandCount; i++ )
       
   432             {
       
   433             TCommand emptyCommand;
       
   434             iCommandList.Append( emptyCommand );
       
   435             }    
       
   436         }
       
   437     }
       
   438 
       
   439 // -----------------------------------------------------------------------------
       
   440 // CMediatorNotificationsBody::FetchEventListL
       
   441 // 
       
   442 // (other items were commented in a header).
       
   443 // -----------------------------------------------------------------------------
       
   444 //
       
   445 void CMediatorNotificationsBody::FetchEventListL()
       
   446     {
       
   447     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::FetchEventListL\n"));
       
   448     TInt eventArraySize = ( sizeof(MediatorService::TEvent) ) * iEventList.Count(); 
       
   449     TPtr8 eventArrayPtr( (TUint8*)&iEventList[0], eventArraySize );
       
   450     iEventArrayPtr.Set( eventArrayPtr );
       
   451     User::LeaveIfError( iMediatorServer.FetchNotificationEventList( iEventArrayPtr ) );
       
   452     }
       
   453     
       
   454 // -----------------------------------------------------------------------------
       
   455 // CMediatorNotificationsBody::FetchCommandListL
       
   456 // 
       
   457 // (other items were commented in a header).
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 void CMediatorNotificationsBody::FetchCommandListL()
       
   461     {
       
   462     LOG(_L("[Mediator Server]\t CMediatorNotificationsBody::FetchCommandListL\n"));
       
   463     TInt commandArraySize = ( sizeof(MediatorService::TCommand) ) * iCommandList.Count(); 
       
   464     TPtr8 commandArrayPtr( (TUint8*)&iCommandList[0], commandArraySize );
       
   465     iCommandArrayPtr.Set( commandArrayPtr );
       
   466     User::LeaveIfError( iMediatorServer.FetchNotificationCommandList( commandArrayPtr ) );
       
   467     }
       
   468 
       
   469 // ----------------------------------------------------------------------------
       
   470 // CStartupMediatorPluginSubscriber::RunError()
       
   471 // ----------------------------------------------------------------------------
       
   472 #ifdef _DEBUG
       
   473 TInt CMediatorNotificationsBody::RunError( TInt aError )
       
   474 #else
       
   475 TInt CMediatorNotificationsBody::RunError( TInt /*aError*/ )
       
   476 #endif //_DEBUG
       
   477     {
       
   478     ERROR_TRACE(Print(_L("[Mediator] CMediatorNotificationsBody::RunError: err=%d\n"), aError));
       
   479 
       
   480     // Return KErrNone to avoid panic
       
   481     return KErrNone;
       
   482     }
       
   483 
       
   484 //  End of File