mediator/src/Server/MediatorServerSession.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2005-2008 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:  Session implementation class for Mediator Server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 #include    <s32mem.h>
       
    22 
       
    23 #include    "MediatorServerSession.h"
       
    24 #include    "MediatorCommon.h"
       
    25 #include    "MediatorServer.h"
       
    26 #include    "MediatorServiceDefs.h"
       
    27 #include    "mediatorqueitem.h"
       
    28 #include    "MediatorDebug.h"
       
    29 #include    "Debug.h"
       
    30 using namespace MediatorService;
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CMediatorServerSession::CMediatorServerSession
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CMediatorServerSession::CMediatorServerSession() :iNotificationsRegistered( EFalse ),
       
    39                                                   iNotificationQueue( _FOFF(CMediatorQueItem,iSlink) )
       
    40 	{
       
    41 	}
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CMediatorServerSession::NewL
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CMediatorServerSession* CMediatorServerSession::NewL()
       
    48 	{
       
    49 	LOG(_L("[Mediator Server]\t CMediatorServerSession::NewL"));
       
    50 	return new( ELeave ) CMediatorServerSession();
       
    51 	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CMediatorServerSession::~CMediatorServerSession
       
    55 // -----------------------------------------------------------------------------
       
    56 //    
       
    57 CMediatorServerSession::~CMediatorServerSession()
       
    58 	{
       
    59 	LOG(_L("[Mediator Server]\t CMediatorServerSession::~CMediatorServerSession"));
       
    60 
       
    61     // cancel all asynchronous waiting procedures
       
    62     CancelAll( KErrCancel );
       
    63 
       
    64     // cancel pending commands
       
    65     Server().CommandHandler().CancelCommands( this, this );
       
    66 
       
    67 	// Unregister all subscriptions
       
    68 	// Get handler
       
    69     CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
    70     objectHandler.ClearRegistrations( this, this, this );
       
    71     
       
    72     iEventServiceList.ResetAndDestroy();
       
    73 	iCommandServiceList.ResetAndDestroy();
       
    74 	
       
    75 	if ( iDataBuffer )
       
    76 	    {
       
    77 	    delete iDataBuffer;
       
    78 	    }
       
    79 	
       
    80 	CleanNotificationQueue();
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CMediatorServerSession::CleanNotificationQueue
       
    85 // -----------------------------------------------------------------------------
       
    86 // 
       
    87 void CMediatorServerSession::CleanNotificationQueue()
       
    88     {
       
    89     LOG(_L("[Mediator Server]\t CMediatorServerSession::CleanNotificationQueue"));
       
    90     
       
    91     // release memory allocated to notification queue items
       
    92     TSglQueIter<CMediatorQueItem> iter( iNotificationQueue );
       
    93     CMediatorQueItem* item;
       
    94     
       
    95     while ( ( item = iter++ ) != NULL)
       
    96         {
       
    97         iNotificationQueue.Remove( *item );
       
    98         delete item;
       
    99         }
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CMediatorServerSession::Server
       
   104 // Returns a reference to server
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CMediatorServer& CMediatorServerSession::Server()
       
   108     {
       
   109     return *static_cast<CMediatorServer*>(const_cast<CServer2*>(CSession2::Server()));
       
   110     }
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CMediatorServerSession::ServiceL
       
   114 // Implements CSession2 -derived ServiceL -method.
       
   115 // (other items were commented in a header).
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CMediatorServerSession::ServiceL(const RMessage2& aMessage)
       
   119 	{
       
   120 	LOG(_L("[Mediator Server]\t CMediatorServerSession::ServiceL"));
       
   121 	
       
   122 	// If ServiceL leaves, default implementation in server framework
       
   123 	// completes the RMessage2 with the leave code.
       
   124 	DispatchMessageL( aMessage );
       
   125 	}
       
   126 	
       
   127 // -----------------------------------------------------------------------------
       
   128 // CMediatorServerSession::ServiceError
       
   129 // Implements CSession2 -derived ServiceError -method.
       
   130 // (other items were commented in a header).
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CMediatorServerSession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   134     {
       
   135     ERROR_TRACE(Print(_L("[Mediator] CMediatorServerSession::ServiceError: opcode=%d, aError=%d\n"), aMessage.Function(), aError ));
       
   136     
       
   137     RMediatorDebug::LogError(aMessage, aError);
       
   138     
       
   139     CSession2::ServiceError(aMessage, aError);
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CMediatorServerSession::ReadCategoryL
       
   144 // 
       
   145 // (other items were commented in a header).
       
   146 // -----------------------------------------------------------------------------
       
   147 //   
       
   148 TMediatorCategory CMediatorServerSession::ReadCategoryL( 
       
   149                                         const RMessage2& aMessage,
       
   150                                         const TInt aMessageSlot )
       
   151     {
       
   152     TMediatorCategoryBuffer categoryBuffer;
       
   153     aMessage.ReadL( aMessageSlot, categoryBuffer );
       
   154     TMediatorCategory category = categoryBuffer();
       
   155     return category;
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CMediatorServerSession::WriteCategoryL
       
   160 // 
       
   161 // (other items were commented in a header).
       
   162 // -----------------------------------------------------------------------------
       
   163 //   
       
   164 void CMediatorServerSession::WriteCategoryL( TUid aDomain,
       
   165                                              TUid aCategory,
       
   166                                              const RMessage2& aMessage,
       
   167                                              const TInt aMessageSlot  )
       
   168     {
       
   169     TMediatorCategory category;
       
   170     category.iDomain = aDomain;
       
   171     category.iCategory = aCategory;
       
   172     TMediatorCategoryRetBuffer categoryBuffer( category );
       
   173     aMessage.WriteL( aMessageSlot, categoryBuffer );
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CMediatorServerSession::WriteEventL
       
   178 // 
       
   179 // (other items were commented in a header).
       
   180 // -----------------------------------------------------------------------------
       
   181 //       
       
   182 void CMediatorServerSession::WriteEventL( TInt aEventId,
       
   183                                           const RMessage2& aMessage,
       
   184                                           const TInt aMessageSlot )
       
   185     {
       
   186     MediatorService::TEvent event;
       
   187     event.iEventId = aEventId;
       
   188     TEventRetBuffer buffer( event );
       
   189     aMessage.WriteL( aMessageSlot, buffer );
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CMediatorServerSession::WriteCommandL
       
   194 // 
       
   195 // (other items were commented in a header).
       
   196 // -----------------------------------------------------------------------------
       
   197 //       
       
   198 void CMediatorServerSession::WriteCommandL( TInt aCommandId,
       
   199                                             TVersion aVersion,
       
   200                                             const RMessage2& aMessage,
       
   201                                             const TInt aMessageSlot )
       
   202     {
       
   203     MediatorService::TCommand command;
       
   204     command.iCommandId  = aCommandId;
       
   205     command.iVersion    = aVersion;
       
   206     TCommandRetBuffer buffer( command );
       
   207     aMessage.WriteL( aMessageSlot, buffer );
       
   208     }
       
   209     
       
   210    
       
   211 // -----------------------------------------------------------------------------
       
   212 // CMediatorServerSession::ReadEventListL
       
   213 // 
       
   214 // (other items were commented in a header).
       
   215 // -----------------------------------------------------------------------------
       
   216 // 
       
   217 REventList CMediatorServerSession::ReadEventListL( const RMessage2& aMessage,
       
   218                                                    const TInt aMessageSlot )
       
   219     {
       
   220      // Read event list information from Message
       
   221     REventList eventList;
       
   222     CleanupClosePushL( eventList );
       
   223     
       
   224     // Create input pointer for data
       
   225     TInt size = aMessage.GetDesMaxLength( aMessageSlot );
       
   226     TInt count = size / ( sizeof(MediatorService::TEvent) );
       
   227     
       
   228     if ( count <= 0 )
       
   229         {
       
   230         User::Leave( KErrArgument );
       
   231         }
       
   232     
       
   233     for ( TInt index = 0; index < count; index ++ )
       
   234         {
       
   235         TEvent event;
       
   236         eventList.AppendL( event );
       
   237         }
       
   238     TPtr8 arrayPtr( (TUint8*)&eventList[0], size );
       
   239     
       
   240     // Read data from message
       
   241     aMessage.ReadL( aMessageSlot, arrayPtr );
       
   242     
       
   243     CleanupStack::Pop( &eventList );
       
   244     return eventList;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CMediatorServerSession::EventListFitsToClientMemory
       
   249 // -----------------------------------------------------------------------------
       
   250 // 
       
   251 TBool CMediatorServerSession::EventListFitsToClientMemory( const REventList& aEvents,
       
   252                                                            const RMessage2& aMessage,
       
   253                                                            const TInt aMessageSlot )
       
   254     {
       
   255     TBool retval( ETrue );
       
   256     
       
   257     if ( aEvents.Count() > 0 )
       
   258         {
       
   259         TInt arraySize( ( sizeof(MediatorService::TEvent) ) * aEvents.Count() );
       
   260     
       
   261         // Check that there's enough space before writing the answer
       
   262         TInt space( aMessage.GetDesMaxLength( aMessageSlot ) ); 
       
   263         
       
   264         retval = ( arraySize <= space );
       
   265         }
       
   266     
       
   267     return retval;
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CMediatorServerSession::CommandListFitsToClientMemory
       
   272 // -----------------------------------------------------------------------------
       
   273 // 
       
   274 TBool CMediatorServerSession::CommandListFitsToClientMemory( const RCommandList& aCommands,
       
   275                                                              const RMessage2& aMessage,
       
   276                                                              const TInt aMessageSlot )
       
   277     {
       
   278     TBool retval( ETrue );
       
   279     
       
   280     if ( aCommands.Count() > 0 )
       
   281         {
       
   282         TInt arraySize( ( sizeof(MediatorService::TCommand) ) * aCommands.Count() );
       
   283         
       
   284         // Check that there's enough space before writing the answer
       
   285         TInt space( aMessage.GetDesMaxLength( aMessageSlot ) ); 
       
   286                 
       
   287         retval = ( arraySize <= space );
       
   288         }
       
   289     
       
   290     return retval;
       
   291     }
       
   292 
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CMediatorServerSession::WriteEventListL
       
   296 // 
       
   297 // (other items were commented in a header).
       
   298 // -----------------------------------------------------------------------------
       
   299 //   
       
   300 void CMediatorServerSession::WriteEventListL( const REventList& aEvents,
       
   301                                               const RMessage2& aMessage,
       
   302                                               const TInt aMessageSlot )
       
   303     {
       
   304     if ( aEvents.Count() > 0 )
       
   305         {
       
   306         TInt arraySize = ( sizeof(MediatorService::TEvent) ) * aEvents.Count();
       
   307         TInt space( aMessage.GetDesMaxLength( aMessageSlot ) );
       
   308         
       
   309         // the method assumes that there's enought space in client memory to accommadate the events
       
   310         __ASSERT_DEBUG( space >= arraySize, User::Invariant() );
       
   311         
       
   312         if ( arraySize > space )
       
   313             {
       
   314             arraySize = space; // ensure that we don't overflow client buffer in any circumstances
       
   315             }
       
   316 
       
   317         TPtr8 arrayPtr( (TUint8*)&aEvents[0], arraySize, arraySize );
       
   318         aMessage.WriteL( aMessageSlot, arrayPtr );
       
   319         }
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CMediatorServerSession::ReadCommandListL
       
   324 // 
       
   325 // (other items were commented in a header).
       
   326 // -----------------------------------------------------------------------------
       
   327 // 
       
   328 RCommandList CMediatorServerSession::ReadCommandListL( const RMessage2& aMessage,
       
   329                                                        const TInt aMessageSlot )
       
   330     {
       
   331      // Read command list information from Message
       
   332     RCommandList commandList;
       
   333     CleanupClosePushL( commandList );
       
   334     
       
   335     // Create input pointer for data
       
   336     TInt size = aMessage.GetDesMaxLength( aMessageSlot );
       
   337     TInt count = size / ( sizeof(MediatorService::TCommand) );
       
   338     for ( TInt index = 0; index < count; index ++ )
       
   339         {
       
   340         TCommand command;
       
   341         commandList.AppendL( command );
       
   342         }
       
   343     TPtr8 arrayPtr( (TUint8*)&commandList[0], size );
       
   344     
       
   345     // Read data from message
       
   346     aMessage.ReadL( aMessageSlot, arrayPtr );
       
   347     
       
   348     CleanupStack::Pop( &commandList );
       
   349     return commandList;
       
   350     }
       
   351    
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CMediatorServerSession::WriteCommandListL
       
   355 // 
       
   356 // (other items were commented in a header).
       
   357 // -----------------------------------------------------------------------------
       
   358 //   
       
   359 void CMediatorServerSession::WriteCommandListL( const RCommandList& aCommands,
       
   360                                                 const RMessage2& aMessage,
       
   361                                                 const TInt aMessageSlot )
       
   362     {
       
   363     // Write the command list back to client
       
   364     if ( ( aCommands.Count() > 0 ) )
       
   365         {
       
   366         TInt arraySize( ( sizeof(MediatorService::TCommand) ) * aCommands.Count() );
       
   367 
       
   368         // Check that there's enough space before writing the answer
       
   369         TInt space( aMessage.GetDesMaxLength( aMessageSlot ) ); 
       
   370         
       
   371         // this method assumes that there's enough space in client side
       
   372         __ASSERT_DEBUG( arraySize <= space, User::Invariant() );
       
   373         
       
   374         if ( arraySize > space ) // overflow prevention
       
   375             {
       
   376             arraySize = space;
       
   377             }
       
   378         
       
   379         TPtr8 arrayPtr( (TUint8*)&aCommands[0], arraySize, arraySize );    
       
   380         aMessage.WriteL( aMessageSlot, arrayPtr );
       
   381         }
       
   382     }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CMediatorServerSession::ReadDataL
       
   386 // 
       
   387 // (other items were commented in a header).
       
   388 // -----------------------------------------------------------------------------
       
   389 //     
       
   390 HBufC8* CMediatorServerSession::ReadDataLC( const RMessage2& aMessage,
       
   391                                             const TInt aMessageSlot )
       
   392     {
       
   393      // Then read parameter data from slot 2
       
   394     TInt dataSize = aMessage.GetDesLength( aMessageSlot );
       
   395 
       
   396     if ( dataSize < 0 )
       
   397         {
       
   398         User::Leave( KErrArgument );
       
   399         }
       
   400 
       
   401     HBufC8* data = HBufC8::NewLC( dataSize );
       
   402     TPtr8 ptr( data->Des() );
       
   403 	aMessage.ReadL( aMessageSlot, ptr );
       
   404 	return data;
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CMediatorServerSession::WriteDataL
       
   409 // 
       
   410 // (other items were commented in a header).
       
   411 // -----------------------------------------------------------------------------
       
   412 //   
       
   413 void CMediatorServerSession::WriteDataL( const TDesC8& aData,
       
   414                                          const RMessage2& aMessage,
       
   415                                          const TInt aMessageSlot )
       
   416     {
       
   417     // Read max data length
       
   418     TInt space = aMessage.GetDesMaxLength( aMessageSlot );
       
   419     if ( space >= aData.Length() )
       
   420         {
       
   421         aMessage.WriteL( aMessageSlot, aData );
       
   422         }
       
   423     else    // Not enough space to write
       
   424         {
       
   425         // Store the data to temporary buffer --> it is fetched from client side
       
   426         // synchronously
       
   427         if ( iDataBuffer )
       
   428             {
       
   429             delete iDataBuffer;
       
   430             iDataBuffer = NULL;
       
   431             }
       
   432         iDataBuffer = aData.AllocL();
       
   433         }
       
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // CMediatorServerSession::ReadEventL
       
   438 // 
       
   439 // (other items were commented in a header).
       
   440 // -----------------------------------------------------------------------------
       
   441 //       
       
   442 MediatorService::TEvent CMediatorServerSession::ReadEventL( 
       
   443                                     const RMessage2& aMessage,
       
   444                                     const TInt aMessageSlot )
       
   445     {
       
   446     TEventBuffer eventBuffer;
       
   447     aMessage.ReadL( aMessageSlot, eventBuffer );
       
   448     return eventBuffer();
       
   449     }
       
   450     
       
   451 // -----------------------------------------------------------------------------
       
   452 // CMediatorServerSession::ReadCommandL
       
   453 // 
       
   454 // (other items were commented in a header).
       
   455 // -----------------------------------------------------------------------------
       
   456 //       
       
   457 MediatorService::TCommand CMediatorServerSession::ReadCommandL( 
       
   458                                     const RMessage2& aMessage,
       
   459                                     const TInt aMessageSlot )
       
   460     {
       
   461     TCommandBuffer commandBuffer;
       
   462     aMessage.ReadL( aMessageSlot, commandBuffer );
       
   463     return commandBuffer();
       
   464     }
       
   465     
       
   466 // -----------------------------------------------------------------------------
       
   467 // CMediatorServerSession::DispatchMessageL
       
   468 // Dispatches message according to its opcode
       
   469 // (other items were commented in a header).
       
   470 // -----------------------------------------------------------------------------
       
   471 //
       
   472 void CMediatorServerSession::DispatchMessageL( const RMessage2 &aMessage )
       
   473     {
       
   474 	LOG(_L("[Mediator Server]\t CMediatorServerSession::DispatchMessageL"));
       
   475 	
       
   476 	RMediatorDebug::Log(aMessage, *this);
       
   477 	
       
   478     switch ( aMessage.Function() )
       
   479         {
       
   480         case ERegisterEventList:
       
   481             {
       
   482             LOG(_L("[Mediator Server]\t DispatchMessageL: ERegisterEventList"));
       
   483             RegisterEventListL( aMessage );
       
   484             break;
       
   485             }    
       
   486         case ERegisterCommandList:
       
   487             {
       
   488             LOG(_L("[Mediator Server]\t DispatchMessageL: ERegisterCommand"));
       
   489             RegisterCommandListL( aMessage );
       
   490             break;
       
   491             }
       
   492         case EUnregisterEventList:
       
   493             {
       
   494             LOG(_L("[Mediator Server]\t DispatchMessageL: EUnregisterEventList"));
       
   495             UnregisterEventListL( aMessage );
       
   496             break;
       
   497             }
       
   498          case EUnregisterCommandList:
       
   499             {
       
   500             LOG(_L("[Mediator Server]\t DispatchMessageL: EUnregisterCommandList"));
       
   501             UnregisterCommandListL( aMessage );
       
   502             break;
       
   503             }
       
   504         case ESubscribeEventList:
       
   505             {
       
   506             LOG(_L("[Mediator Server]\t DispatchMessageL: ESubscribeEvent"));
       
   507             SubscribeEventListL( aMessage );
       
   508             break;
       
   509             }
       
   510         case EUnsubscribeEventList:
       
   511             {
       
   512             LOG(_L("[Mediator Server]\t DispatchMessageL: EUnsubscribeEvent"));
       
   513             UnsubscribeEventListL( aMessage );
       
   514             break;
       
   515             }
       
   516         case ERaiseEvent:
       
   517             {
       
   518             LOG(_L("[Mediator Server]\t DispatchMessageL: ERaiseEvent"));
       
   519             RaiseEventL( aMessage );
       
   520             break;
       
   521             }
       
   522         case EIssueCommand:
       
   523             {
       
   524             LOG(_L("[Mediator Server]\t DispatchMessageL: IssueCommandL"));
       
   525             IssueCommandL( aMessage );
       
   526             break;
       
   527             }  
       
   528          case ECancelCommand:
       
   529             {
       
   530             LOG(_L("[Mediator Server]\t DispatchMessageL: CancelCommandL"));
       
   531             CancelCommandL( aMessage );
       
   532             break;
       
   533             }
       
   534          case EIssueResponse:
       
   535             {
       
   536             LOG(_L("[Mediator Server]\t DispatchMessageL: EIssueResponse"));
       
   537             IssueResponseL( aMessage );
       
   538             break;
       
   539             }
       
   540         case EGetDomains:
       
   541             {
       
   542             LOG(_L("[Mediator Server]\t DispatchMessageL: EGetDomains"));
       
   543             GetDomainsL( aMessage );
       
   544             break;
       
   545             }
       
   546         case EGetCategories:
       
   547             {
       
   548             LOG(_L("[Mediator Server]\t DispatchMessageL: EGetCategories"));
       
   549             GetCategoriesL( aMessage );
       
   550             break;
       
   551             }
       
   552         case EGetCommands:
       
   553             {
       
   554             LOG(_L("[Mediator Server]\t DispatchMessageL: EGetCommands"));
       
   555             GetCommandsL( aMessage );
       
   556             break;
       
   557             }
       
   558         case EGetEvents:
       
   559             {
       
   560             LOG(_L("[Mediator Server]\t DispatchMessageL: EGetEvents"));
       
   561             GetEventsL( aMessage );
       
   562             break;
       
   563             }
       
   564         case EWaitForEvent:
       
   565             {
       
   566             LOG(_L("[Mediator Server]\t DispatchMessageL: EWaitForEvent"));
       
   567             WaitForEventsL( aMessage );
       
   568             break;
       
   569             }
       
   570         case EWaitForCommand:
       
   571             {
       
   572             LOG(_L("[Mediator Server]\t DispatchMessageL: EWaitForCommand"));
       
   573             WaitForCommandsL( aMessage );
       
   574             break;
       
   575             }
       
   576         case EWaitForNotifications:
       
   577             {
       
   578             LOG(_L("[Mediator Server]\t DispatchMessageL: EWaitForNotifications"));
       
   579             WaitForNotificationsL( aMessage );
       
   580             break;
       
   581             }
       
   582         case ECancelNotifications:
       
   583             {
       
   584             LOG(_L("[Mediator Server]\t DispatchMessageL: ECancelNotifications"));
       
   585             CancelNotificationsL( aMessage );
       
   586             break;
       
   587             }
       
   588         case EWaitForCommandResponse:
       
   589             {
       
   590             LOG(_L("[Mediator Server]\t DispatchMessageL: EWaitForCommandResponse"));
       
   591             WaitForCommandResponseL( aMessage );
       
   592             break;
       
   593             }
       
   594         case EFetchParameterData:
       
   595             {
       
   596             LOG(_L("[Mediator Server]\t DispatchMessageL: EFetchParameterData"));
       
   597             FetchParameterDataL( aMessage );
       
   598             break;
       
   599             }
       
   600         case ECancelAll:
       
   601             {
       
   602             LOG(_L("[Mediator Server]\t DispatchMessageL: ECancelAll"));
       
   603             CancelAll( aMessage );
       
   604             break;
       
   605             }
       
   606             
       
   607         case EFetchNotificationEventList:
       
   608             {
       
   609             LOG(_L("[Mediator Server]\t DispatchMessageL: EFetchNotificationEventList"));
       
   610             FetchNotificationEventListL( aMessage );
       
   611             break;    
       
   612             }
       
   613         
       
   614         case EFetchNotificationCommandList:
       
   615             {
       
   616             LOG(_L("[Mediator Server]\t DispatchMessageL: EFetchNotificationCommandList"));
       
   617             FetchNotificationCommandListL( aMessage );
       
   618             break;
       
   619             }
       
   620         default:
       
   621             {
       
   622             // Unknown opcode --> panic client
       
   623             ERROR_TRACE(Print(_L("[Mediator] CMediatorServerSession::DispatchMessageL: Unknown opcode=%d\n"), aMessage.Function()));
       
   624             CMediatorServer::PanicClient( aMessage, EMediatorClientBadRequest );
       
   625             break;
       
   626             }
       
   627         }
       
   628     }
       
   629 
       
   630 
       
   631 // -----------------------------------------------------------------------------
       
   632 // CMediatorServerSession::MediatorCommandL
       
   633 // Callback from command handler when command is issued
       
   634 // (other items were commented in a header).
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CMediatorServerSession::MediatorCommandL( TUid aDomain,
       
   638                                                TUid aCategory, 
       
   639                                                TInt aCommandId,
       
   640                                                TVersion aVersion, 
       
   641                                                const TDesC8& aData )
       
   642     {
       
   643     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCommandL"));
       
   644     
       
   645     if ( !iCommandInitMessage.IsNull() )
       
   646         {
       
   647         // Write domain & category information
       
   648         WriteCategoryL( aDomain, aCategory, iCommandInitMessage, 0 );
       
   649         
       
   650         // Write event to slot 1
       
   651         WriteCommandL( aCommandId, aVersion, iCommandInitMessage, 1 );
       
   652         
       
   653         // Write data to slot 2
       
   654         WriteDataL( aData, iCommandInitMessage, 2 );
       
   655         
       
   656         // Complete message with data size
       
   657         iCommandInitMessage.Complete( aData.Length() );
       
   658         }
       
   659     else    // Append command to service list
       
   660         {
       
   661         // Create a new command object
       
   662         CCommand* newCommand = CCommand::NewL( aCommandId );
       
   663         CleanupStack::PushL( newCommand );
       
   664         newCommand->SetParameterDataL( aData );
       
   665         newCommand->SetDomain( aDomain );
       
   666         newCommand->SetCategory( aCategory );
       
   667         newCommand->SetVersion( aVersion );
       
   668         iCommandServiceList.AppendL( newCommand );  // Last to queue
       
   669         CleanupStack::Pop( newCommand );  // ownership is in the array
       
   670         TRACE(Print(_L("[Mediator Server]\t Session %d - command buffer %d\n"), this, iCommandServiceList.Count() ));
       
   671         }
       
   672     
       
   673     }
       
   674 
       
   675 // -----------------------------------------------------------------------------
       
   676 // CMediatorServerSession::CancelMediatorCommandL
       
   677 // Callback from command handler when command processing is cancelled
       
   678 // (other items were commented in a header).
       
   679 // -----------------------------------------------------------------------------
       
   680 //
       
   681 void CMediatorServerSession::CancelMediatorCommandL( TUid aDomain,
       
   682                                                      TUid aCategory, 
       
   683                                                      TInt aCommandId )
       
   684     {
       
   685     LOG(_L("[Mediator Server]\t CMediatorServerSession::CancelMediatorCommand"));
       
   686     
       
   687     // Check the service list and remove the command from there if exists
       
   688     // At this point it has not been delivered to responder yet
       
   689     TBool found = EFalse;
       
   690     for ( TInt index = 0; index < iCommandServiceList.Count() && !found; index++ )
       
   691         {
       
   692         CCommand* commandPtr = iCommandServiceList[index];
       
   693         if ( commandPtr )
       
   694             {
       
   695             if ( commandPtr->Domain() == aDomain
       
   696                  && commandPtr->Category() == aCategory
       
   697                  && commandPtr->Id() == aCommandId 
       
   698                  && commandPtr->Status() == ECommandPending )
       
   699                 {
       
   700                 iCommandServiceList.Remove( index );
       
   701                 delete commandPtr;
       
   702                 commandPtr = NULL;
       
   703                 found = ETrue;
       
   704                 }
       
   705             }
       
   706         }
       
   707     
       
   708     // Write domain & category information to slot 0
       
   709     if ( iCommandInitMessage.Handle() && !found )
       
   710         {
       
   711         WriteCategoryL( aDomain, aCategory, iCommandInitMessage, 0 );
       
   712     
       
   713         // Write command id and version to slot 1
       
   714         TVersion version( 0, 0, 0 );
       
   715         WriteCommandL( aCommandId, version, iCommandInitMessage, 1 );
       
   716         
       
   717         // Status to slot 2
       
   718         TPckg<TInt> statusBuffer( KErrCancel );
       
   719         iCommandInitMessage.WriteL( 2, statusBuffer );
       
   720         
       
   721         // Write data to slot 3
       
   722         WriteDataL( KNullDesC8, iCommandInitMessage, 2 );
       
   723         
       
   724         // Complete message
       
   725         iCommandInitMessage.Complete( KErrCancel );
       
   726         }
       
   727     // The command hs been delivered, but the responder is busy or otherwise is not
       
   728     // yet ready to handle the command cancellation, add to the list with cancelled status
       
   729     else if ( !iCommandInitMessage.Handle() && !found )
       
   730         {
       
   731         // Create a new command object
       
   732         CCommand* newCommand = CCommand::NewL( aCommandId );
       
   733         CleanupStack::PushL( newCommand );
       
   734         newCommand->SetDomain( aDomain );
       
   735         newCommand->SetCategory( aCategory );
       
   736         newCommand->SetStatus( ECommandCanceled );
       
   737         iCommandServiceList.AppendL( newCommand );  // Last to queue
       
   738         CleanupStack::Pop( newCommand );  // ownership is in the array
       
   739         }
       
   740             
       
   741 
       
   742     }
       
   743 
       
   744 // -----------------------------------------------------------------------------
       
   745 // CMediatorServerSession::CancelMediatorCommandL
       
   746 // Callback from command handler when command processing is cancelled
       
   747 // (other items were commented in a header).
       
   748 // -----------------------------------------------------------------------------
       
   749 //
       
   750 void CMediatorServerSession::MediatorCommandTimeoutL( TUid aDomain,
       
   751                                                       TUid aCategory, 
       
   752                                                       TInt aCommandId )
       
   753     {
       
   754     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCommandTimeoutL"));
       
   755     
       
   756     // Check the service list and remove the command from there if exists
       
   757     // At this point it has not been delivered to responder yet
       
   758     TBool found = EFalse;
       
   759     for ( TInt index = 0; index < iCommandServiceList.Count() && !found; index++ )
       
   760         {
       
   761         CCommand* commandPtr = iCommandServiceList[index];
       
   762         if ( commandPtr )
       
   763             {
       
   764             if ( commandPtr->Domain() == aDomain
       
   765                  && commandPtr->Category() == aCategory
       
   766                  && commandPtr->Id() == aCommandId
       
   767                  && commandPtr->Status() == ECommandPending ) // only pending commands are completed with timeout
       
   768                 {
       
   769                 iCommandServiceList.Remove( index );
       
   770                 delete commandPtr;
       
   771                 commandPtr = NULL;
       
   772                 found = ETrue;
       
   773                 }
       
   774             }
       
   775         }
       
   776     }
       
   777 
       
   778 // -----------------------------------------------------------------------------
       
   779 // CMediatorServerSession::CommandResponseL
       
   780 // Callback from command handler when command processing is cancelled
       
   781 // (other items were commented in a header).
       
   782 // -----------------------------------------------------------------------------
       
   783 //
       
   784 void CMediatorServerSession::CommandResponseL( TUid aDomain,
       
   785                                                TUid aCategory, 
       
   786                                                TInt aCommandId,
       
   787                                                TInt aStatus, 
       
   788                                                const TDesC8& aData )
       
   789     {
       
   790     LOG(_L("[Mediator Server]\t CMediatorServerSession::CommandResponseL"));
       
   791     
       
   792     if ( !iCommandResponseMessage.IsNull() )
       
   793         {
       
   794         // Write domain & category information
       
   795         WriteCategoryL( aDomain, aCategory, iCommandResponseMessage, 0 );
       
   796         
       
   797         // Write command to slot 1
       
   798         TVersion version( 0, 0, 0 );
       
   799         WriteCommandL( aCommandId, version, iCommandResponseMessage, 1 );
       
   800         
       
   801         // Write data to slot 2
       
   802         WriteDataL( aData, iCommandResponseMessage, 2 );
       
   803         
       
   804         // Status to slot 3
       
   805         TPckg<TInt> statusBuffer( aStatus );
       
   806         iCommandResponseMessage.WriteL( 3, statusBuffer );
       
   807         
       
   808         // Complete message with data size
       
   809         iCommandResponseMessage.Complete( aData.Length() );
       
   810         }
       
   811     }
       
   812     
       
   813 // -----------------------------------------------------------------------------
       
   814 // CMediatorServerSession::MediatorEventL
       
   815 // Callback from event handler when event occurs
       
   816 // (other items were commented in a header).
       
   817 // -----------------------------------------------------------------------------
       
   818 //
       
   819 void CMediatorServerSession::MediatorEventL( TUid aDomain,
       
   820                                              TUid aCategory, 
       
   821                                              TInt aEventId, 
       
   822                                              const TDesC8& aData )
       
   823     {
       
   824     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorEventL"));   
       
   825     if ( !iEventNotificationMessage.IsNull() )
       
   826         {
       
   827         // Write domain & category information
       
   828         WriteCategoryL( aDomain, aCategory, iEventNotificationMessage, 0 );
       
   829         
       
   830         // Write event to slot 1
       
   831         WriteEventL( aEventId, iEventNotificationMessage, 1 );
       
   832         
       
   833         // Write data to slot 2
       
   834         WriteDataL( aData, iEventNotificationMessage, 2 );
       
   835         
       
   836         // Complete message with data size
       
   837         iEventNotificationMessage.Complete( aData.Length() );
       
   838         }
       
   839     else    // Append event to service list
       
   840         {
       
   841         // Create a new event object
       
   842         CEvent* newEvent = CEvent::NewL( aEventId );
       
   843         CleanupStack::PushL( newEvent );
       
   844         newEvent->SetParameterDataL( aData );
       
   845         newEvent->SetDomain( aDomain );
       
   846         newEvent->SetCategory( aCategory );
       
   847         iEventServiceList.AppendL( newEvent );  // Last to queue
       
   848         CleanupStack::Pop( newEvent );  // ownership is in the array
       
   849         TRACE(Print(_L("[Mediator Server]\t Session %d - event buffer %d\n"),this ,iEventServiceList.Count() ));
       
   850         }
       
   851     
       
   852     }
       
   853     
       
   854 // -----------------------------------------------------------------------------
       
   855 // CMediatorServerSession::MediatorEventsAddedL
       
   856 // Callback from event handler when an event is added
       
   857 // (other items were commented in a header).
       
   858 // -----------------------------------------------------------------------------
       
   859 //
       
   860 void CMediatorServerSession::MediatorEventsAddedL( TUid aDomain, 
       
   861                                                    TUid aCategory, 
       
   862                                                    const REventList& aEvents )
       
   863     {
       
   864     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorEventsAddedL"));
       
   865     
       
   866     HandleMediatorEventsChangedL( EMediatorEventsRegistered, aDomain, aCategory, aEvents );
       
   867     }
       
   868 
       
   869 // -----------------------------------------------------------------------------
       
   870 // CMediatorServerSession::HandleMediatorEventsChangedL
       
   871 // -----------------------------------------------------------------------------
       
   872 //
       
   873 void CMediatorServerSession::HandleMediatorEventsChangedL( TMediatorNotificationType aNotificationType,
       
   874                                                          const TUid& aDomain, 
       
   875                                                          const TUid& aCategory, 
       
   876                                                          const REventList& aEvents )
       
   877     {
       
   878     LOG(_L("[Mediator Server]\t CMediatorServerSession::HandleMediatorEventsChangedL"));
       
   879     
       
   880     if ( !iNotificationMessage.IsNull() )
       
   881         {
       
   882         TBool eventsPassed( ETrue );
       
   883         
       
   884         DoMediatorEventsChangedL( aNotificationType, aDomain, aCategory, aEvents, eventsPassed );
       
   885         
       
   886         if ( !eventsPassed )
       
   887             {
       
   888             // store events for next EFetchNotificationEventList
       
   889             CMediatorQueItem* eventsBuffer = CEventListQueItem::NewL( aNotificationType, aDomain, aCategory, aEvents );
       
   890             iNotificationQueue.AddFirst( *eventsBuffer );
       
   891             }
       
   892         }
       
   893     else if ( iNotificationsRegistered )
       
   894         {
       
   895         LOG(_L("[Mediator Server]\t CMediatorServerSession::HandleMediatorEventsChangedL: received events buffered"));
       
   896         
       
   897         // store events for next EWaitForNotifications
       
   898         CMediatorQueItem* eventsBuffer = CEventListQueItem::NewL( aNotificationType, aDomain, aCategory, aEvents );
       
   899         iNotificationQueue.AddLast( *eventsBuffer );
       
   900         }
       
   901     }
       
   902 
       
   903 
       
   904 // -----------------------------------------------------------------------------
       
   905 // CMediatorServerSession::DoMediatorEventsChangedL
       
   906 // -----------------------------------------------------------------------------
       
   907 //
       
   908 void CMediatorServerSession::DoMediatorEventsChangedL( TMediatorNotificationType aNotificationType,
       
   909                                                      const TUid& aDomain, 
       
   910                                                      const TUid& aCategory, 
       
   911                                                      const REventList& aEvents,
       
   912                                                      TBool& aEventsPassedToClient )
       
   913     {
       
   914     TRACE( Print( _L("[Mediator Server]\t CMediatorServerSession::DoMediatorEventsChangedL: D=0x%x C=0x%x, count=%d"),
       
   915                     aDomain.iUid, aCategory.iUid, aEvents.Count() ) );
       
   916     
       
   917     // Write category information to message slot 0
       
   918     WriteCategoryL( aDomain, aCategory, iNotificationMessage, 0 );
       
   919     
       
   920     // Write notification type to slot 1
       
   921     TNotificationTypeRetBuffer typeBuffer( aNotificationType );
       
   922     iNotificationMessage.WriteL( 1, typeBuffer );
       
   923     
       
   924     // Write event list
       
   925     if ( EventListFitsToClientMemory( aEvents, iNotificationMessage, 2 ) )
       
   926         {
       
   927         WriteEventListL( aEvents, iNotificationMessage, 2 );
       
   928         aEventsPassedToClient = ETrue;
       
   929         }
       
   930     else
       
   931         {
       
   932         aEventsPassedToClient = EFalse;
       
   933         }
       
   934     
       
   935     // Complete message
       
   936     iNotificationMessage.Complete( aEvents.Count() );
       
   937     }
       
   938 
       
   939 // -----------------------------------------------------------------------------
       
   940 // CMediatorServerSession::PurgeNextBufferedNotificationL
       
   941 // -----------------------------------------------------------------------------
       
   942 //
       
   943 void CMediatorServerSession::PurgeNextBufferedNotificationL()
       
   944     {
       
   945     if ( !iNotificationQueue.IsEmpty() )
       
   946          {
       
   947          TBool completed( ETrue );
       
   948          CMediatorQueItem* notification = iNotificationQueue.First();
       
   949          
       
   950          TRACE( Print( _L("[Mediator Server]\t CMediatorServerSession::PurgeNextBufferedNotificationL: type=%d"),
       
   951                        notification->NofiticationType() ) );
       
   952          // remove now, notification will be pushed back to queue if cannot be delivered with the current client request
       
   953          iNotificationQueue.Remove( *notification ); 
       
   954          CleanupStack::PushL( notification );
       
   955          
       
   956          switch ( notification->NofiticationType() )
       
   957              {
       
   958              case EMediatorEventsRegistered:
       
   959              case EMediatorEventsUnregistered:    
       
   960                  DoMediatorEventsChangedL( notification->NofiticationType(),
       
   961                                            notification->Domain(),
       
   962                                            notification->Category(),
       
   963                                            *(notification->Events()),
       
   964                                            completed ); 
       
   965                  break;
       
   966 
       
   967              case EMediatorCommandsRegistered:
       
   968              case EMediatorCommandsUnregistered:
       
   969                  DoMediatorCommandsChangedL( notification->NofiticationType(),
       
   970                                              notification->Domain(),
       
   971                                              notification->Category(),
       
   972                                              *(notification->Commands()),
       
   973                                              completed );
       
   974                  break;
       
   975                  
       
   976              case EMediatorCategoryUnregistered:
       
   977                  DoMediatorCategoryRemovedL( notification->Domain(), 
       
   978                                              notification->Category() );    
       
   979                  break;
       
   980              
       
   981              default:
       
   982                  __ASSERT_DEBUG( EFalse, User::Invariant() );
       
   983              }
       
   984          
       
   985          if ( completed )
       
   986              {
       
   987              // no longer needed, cleanup
       
   988              CleanupStack::PopAndDestroy( notification );
       
   989              }
       
   990          else // could not be completed without further client request
       
   991              {
       
   992              iNotificationQueue.AddFirst( *notification );
       
   993              CleanupStack::Pop( notification );
       
   994              }
       
   995 
       
   996          }
       
   997     }
       
   998 
       
   999 
       
  1000 // -----------------------------------------------------------------------------
       
  1001 // CMediatorServerSession::MediatorCommandsAddedL
       
  1002 // 
       
  1003 // (other items were commented in a header).
       
  1004 // -----------------------------------------------------------------------------
       
  1005 //
       
  1006 void CMediatorServerSession::MediatorCommandsAddedL( TUid aDomain, 
       
  1007                                                      TUid aCategory,
       
  1008                                                      const RCommandList& aCommands )
       
  1009     {
       
  1010     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCommandsAddedL"));
       
  1011     
       
  1012     HandleMediatorCommandsChangedL( EMediatorCommandsRegistered, aDomain, aCategory, aCommands);
       
  1013     }
       
  1014 
       
  1015 // -----------------------------------------------------------------------------
       
  1016 // CMediatorServerSession::HandleMediatorCommandsChangedL
       
  1017 // -----------------------------------------------------------------------------
       
  1018 //
       
  1019 void CMediatorServerSession::HandleMediatorCommandsChangedL( TMediatorNotificationType aNotificationType,
       
  1020                                                              const TUid& aDomain, 
       
  1021                                                              const TUid& aCategory, 
       
  1022                                                              const RCommandList& aCommands )
       
  1023     {
       
  1024     LOG(_L("[Mediator Server]\t CMediatorServerSession::HandleMediatorCommandsChangedL"));
       
  1025     if ( !iNotificationMessage.IsNull() )
       
  1026         {
       
  1027         TBool commandListPassed( ETrue );
       
  1028                
       
  1029         DoMediatorCommandsChangedL( aNotificationType, aDomain, aCategory, aCommands, commandListPassed );
       
  1030        
       
  1031         if ( !commandListPassed )
       
  1032             {
       
  1033             // store commands for next EFetchNotificationCommandList
       
  1034             CMediatorQueItem* commandsItem = CCommandListQueItem::NewL( aNotificationType, aDomain, aCategory, aCommands );
       
  1035             iNotificationQueue.AddFirst( *commandsItem );
       
  1036             }
       
  1037         }
       
  1038     else if ( iNotificationsRegistered )
       
  1039         {
       
  1040         LOG(_L("[Mediator Server]\t CMediatorServerSession::HandleMediatorCommandsChangedL: received commands buffered"));
       
  1041        
       
  1042         // store commands for next EWaitForNotifications
       
  1043         CMediatorQueItem* commandsItem = CCommandListQueItem::NewL( aNotificationType, aDomain, aCategory, aCommands );
       
  1044         iNotificationQueue.AddLast( *commandsItem );
       
  1045        }
       
  1046     }
       
  1047 
       
  1048 // -----------------------------------------------------------------------------
       
  1049 // CMediatorServerSession::DoMediatorCommandsChangedL
       
  1050 // -----------------------------------------------------------------------------
       
  1051 //
       
  1052 void CMediatorServerSession::DoMediatorCommandsChangedL( TMediatorNotificationType aNotificationType, 
       
  1053                                                          const TUid& aDomain, 
       
  1054                                                          const TUid& aCategory, 
       
  1055                                                          const RCommandList& aCommands,
       
  1056                                                          TBool& aCommandsPassedToClient )
       
  1057     {
       
  1058     TRACE( Print( _L("[Mediator Server]\t CMediatorServerSession::DoMediatorCommandsChangedL: D=0x%x, C=0x%x, count=%d" ),
       
  1059                   aDomain.iUid, aCategory.iUid, aCommands.Count() ) );
       
  1060     
       
  1061     // Write category information to message slot 0
       
  1062     WriteCategoryL( aDomain, aCategory, iNotificationMessage, 0 );
       
  1063     
       
  1064     // Write notification type to slot 1
       
  1065     TMediatorNotificationType notificationType = aNotificationType;
       
  1066     TNotificationTypeRetBuffer typeBuffer( notificationType );
       
  1067     iNotificationMessage.WriteL( 1, typeBuffer );
       
  1068     
       
  1069     // Write command list to slot 3
       
  1070     if ( CommandListFitsToClientMemory( aCommands, iNotificationMessage, 3 ) )
       
  1071         {
       
  1072         WriteCommandListL( aCommands, iNotificationMessage, 3 );
       
  1073         aCommandsPassedToClient = ETrue;
       
  1074         }
       
  1075     else
       
  1076         {
       
  1077         aCommandsPassedToClient = EFalse;
       
  1078         }
       
  1079     
       
  1080     // Complete message
       
  1081     iNotificationMessage.Complete( aCommands.Count() );
       
  1082     }
       
  1083 
       
  1084 // -----------------------------------------------------------------------------
       
  1085 // CMediatorServerSession::DoMediatorCategoryRemovedL
       
  1086 // -----------------------------------------------------------------------------
       
  1087 //
       
  1088 void CMediatorServerSession::DoMediatorCategoryRemovedL( const TUid& aDomain, 
       
  1089                                                          const TUid& aCategory )
       
  1090     {
       
  1091     TRACE( Print( _L("[Mediator Server]\t CMediatorServerSession::DoMediatorCategoryRemovedL: D=0x%x, C=0x%x"),
       
  1092            aDomain.iUid, aCategory.iUid ) );
       
  1093     
       
  1094     // Write category information to message slot 0
       
  1095     WriteCategoryL( aDomain, aCategory, iNotificationMessage, 0 );
       
  1096         
       
  1097     // Write notification type to slot 1
       
  1098     TMediatorNotificationType notificationType = EMediatorCategoryUnregistered;
       
  1099     TNotificationTypeRetBuffer typeBuffer( notificationType );
       
  1100     iNotificationMessage.WriteL( 1, typeBuffer );
       
  1101         
       
  1102     // Complete message
       
  1103     iNotificationMessage.Complete( KErrNone );
       
  1104     }
       
  1105 
       
  1106 // -----------------------------------------------------------------------------
       
  1107 // CMediatorServerSession::MediatorEventL
       
  1108 // Callback from event handler when event occurs
       
  1109 // (other items were commented in a header).
       
  1110 // -----------------------------------------------------------------------------
       
  1111 //
       
  1112 void CMediatorServerSession::MediatorCategoryRemovedL( TUid aDomain, TUid aCategory )
       
  1113     {
       
  1114     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCategoryRemovedL"));
       
  1115     
       
  1116     if ( !iNotificationMessage.IsNull() )
       
  1117         {
       
  1118         DoMediatorCategoryRemovedL( aDomain, aCategory );
       
  1119         }
       
  1120     else if ( iNotificationsRegistered )
       
  1121         {
       
  1122         LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCategoryRemovedL: buffering"));
       
  1123             
       
  1124         // store category removal for next EWaitForNotifications
       
  1125         CMediatorQueItem* notification = CCategoryQueItem::NewL( aDomain, aCategory );
       
  1126         iNotificationQueue.AddLast( *notification );
       
  1127         }
       
  1128     }
       
  1129 
       
  1130 // -----------------------------------------------------------------------------
       
  1131 // CMediatorServerSession::MediatorEventL
       
  1132 // Callback from event handler when event occurs
       
  1133 // (other items were commented in a header).
       
  1134 // -----------------------------------------------------------------------------
       
  1135 //
       
  1136 void CMediatorServerSession::MediatorEventsRemovedL( TUid aDomain, 
       
  1137                                                      TUid aCategory,
       
  1138                                                      const REventList& aEvents )
       
  1139     {
       
  1140     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorEventsRemovedL"));
       
  1141     
       
  1142     HandleMediatorEventsChangedL( EMediatorEventsUnregistered, aDomain, aCategory, aEvents );
       
  1143     }
       
  1144                                      
       
  1145 // -----------------------------------------------------------------------------
       
  1146 // CMediatorServerSession::MediatorCommandsRemovedL
       
  1147 // Callback from event handler when event occurs
       
  1148 // (other items were commented in a header).
       
  1149 // -----------------------------------------------------------------------------
       
  1150 //
       
  1151 void CMediatorServerSession::MediatorCommandsRemovedL( TUid aDomain, 
       
  1152                                                        TUid aCategory,
       
  1153                                                        const RCommandList& aCommands )
       
  1154     {
       
  1155     LOG(_L("[Mediator Server]\t CMediatorServerSession::MediatorCommandsRemovedL"));
       
  1156     
       
  1157     HandleMediatorCommandsChangedL( EMediatorCommandsUnregistered, aDomain, aCategory, aCommands );
       
  1158     }
       
  1159 
       
  1160 // -----------------------------------------------------------------------------
       
  1161 // CMediatorServerSession::RegisterEventListL
       
  1162 // Helper method to register event list. Uses Event handler.
       
  1163 // (other items were commented in a header).
       
  1164 // -----------------------------------------------------------------------------
       
  1165 //
       
  1166 void CMediatorServerSession::RegisterEventListL( const RMessage2 &aMessage )
       
  1167     {
       
  1168     LOG(_L("[Mediator Server]\t CMediatorServerSession::RegisterEventListL"));
       
  1169     
       
  1170     // Read domain & category
       
  1171     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1172     
       
  1173     // Read event list
       
  1174     REventList eventList = ReadEventListL( aMessage, 1 );
       
  1175     CleanupClosePushL( eventList );
       
  1176     
       
  1177     RMediatorDebug::LogData(aMessage, *this, eventList);
       
  1178     
       
  1179     // Get handler
       
  1180     CMediatorServerEventHandler& eventHandler = Server().EventHandler();
       
  1181     
       
  1182     // Register events
       
  1183     eventHandler.RegisterEventListL( category, eventList, aMessage.SecureId() );
       
  1184     
       
  1185     // Cleanup
       
  1186     CleanupStack::PopAndDestroy( &eventList );
       
  1187     
       
  1188     // Complete message in case of no errors
       
  1189     aMessage.Complete( KErrNone );
       
  1190     
       
  1191     RMediatorDebug::LogStatus();
       
  1192     }
       
  1193 
       
  1194     
       
  1195 // -----------------------------------------------------------------------------
       
  1196 // CMediatorServerSession::RegisterCommandListL
       
  1197 // Helper function to register command. Uses Command Handler
       
  1198 // (other items were commented in a header).
       
  1199 // -----------------------------------------------------------------------------
       
  1200 //
       
  1201 void CMediatorServerSession::RegisterCommandListL( const RMessage2 &aMessage )
       
  1202     {
       
  1203     LOG(_L("[Mediator Server]\t CMediatorServerSession::RegisterCommandListL"));
       
  1204 
       
  1205     // Read domain & category
       
  1206     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1207     
       
  1208      // Read command list information from Message
       
  1209     RCommandList commandList = ReadCommandListL( aMessage, 1 );
       
  1210     CleanupClosePushL( commandList );
       
  1211     
       
  1212     RMediatorDebug::LogData(aMessage, *this, commandList);
       
  1213     
       
  1214     // Get handler
       
  1215     CMediatorServerCommandHandler& commandHandler = Server().CommandHandler();
       
  1216     
       
  1217     // Register commands
       
  1218     commandHandler.RegisterCommandListL( category, 
       
  1219                                          commandList, 
       
  1220                                          aMessage.SecureId(),
       
  1221                                          this );
       
  1222     
       
  1223     // cleanup
       
  1224     CleanupStack::PopAndDestroy( &commandList );
       
  1225     
       
  1226     // Complete message in case of no errors
       
  1227     aMessage.Complete( KErrNone );
       
  1228 
       
  1229     RMediatorDebug::LogStatus();
       
  1230     }
       
  1231     
       
  1232 // -----------------------------------------------------------------------------
       
  1233 // CMediatorServerSession::UnregisterEventListL
       
  1234 // Helper method to unregister event(s). Uses Event handler.
       
  1235 // (other items were commented in a header).
       
  1236 // -----------------------------------------------------------------------------
       
  1237 //
       
  1238 void CMediatorServerSession::UnregisterEventListL( const RMessage2 &aMessage )
       
  1239     {
       
  1240     LOG(_L("[Mediator Server]\t CMediatorServerSession::UnregisterEventListL"));
       
  1241     
       
  1242     // Read domain & category
       
  1243     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1244     
       
  1245      // Read event list information from Message
       
  1246     REventList eventList = ReadEventListL( aMessage, 1 );
       
  1247     CleanupClosePushL( eventList );
       
  1248     
       
  1249     RMediatorDebug::LogData(aMessage, *this, eventList);
       
  1250     
       
  1251     // Get handler
       
  1252     CMediatorServerEventHandler& eventHandler = Server().EventHandler();
       
  1253     
       
  1254     // Register events
       
  1255     eventHandler.UnregisterEventListL( category, eventList, aMessage.SecureId() );
       
  1256     
       
  1257     // cleanup
       
  1258     CleanupStack::PopAndDestroy( &eventList );
       
  1259 
       
  1260     // Complete message in case of no errors
       
  1261     aMessage.Complete( KErrNone );
       
  1262 
       
  1263     RMediatorDebug::LogStatus();
       
  1264     }
       
  1265 
       
  1266 
       
  1267 // -----------------------------------------------------------------------------
       
  1268 // CMediatorServerSession::UnregisterCommandListL
       
  1269 // Helper method to unregister command(s). Uses Command handler.
       
  1270 // (other items were commented in a header).
       
  1271 // -----------------------------------------------------------------------------
       
  1272 //
       
  1273 void CMediatorServerSession::UnregisterCommandListL( const RMessage2 &aMessage )
       
  1274     {
       
  1275     LOG(_L("[Mediator Server]\t CMediatorServerSession::UnregisterCommandListL"));
       
  1276     
       
  1277     // Read domain & category
       
  1278     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1279     
       
  1280     // Read command list information from Message
       
  1281     RCommandList commandList = ReadCommandListL( aMessage, 1 );
       
  1282     CleanupClosePushL( commandList );
       
  1283     
       
  1284     RMediatorDebug::LogData(aMessage, *this, commandList);
       
  1285     
       
  1286     // Get handler
       
  1287     CMediatorServerCommandHandler& commandHandler = Server().CommandHandler();
       
  1288     
       
  1289     // Unregister commands
       
  1290     commandHandler.UnregisterCommandListL( category, commandList, aMessage.SecureId() );
       
  1291     
       
  1292     // cleanup
       
  1293     CleanupStack::PopAndDestroy( &commandList );
       
  1294 
       
  1295     // Complete message in case of no errors
       
  1296     aMessage.Complete( KErrNone );
       
  1297     
       
  1298     RMediatorDebug::LogStatus();    
       
  1299     }
       
  1300 
       
  1301 
       
  1302 
       
  1303 // -----------------------------------------------------------------------------
       
  1304 // CMediatorServerSession::SubscribeEventL
       
  1305 // Helper method to subscribes to event(s). Uses Event handler.
       
  1306 // (other items were commented in a header).
       
  1307 // -----------------------------------------------------------------------------
       
  1308 //
       
  1309 void CMediatorServerSession::SubscribeEventListL( const RMessage2 &aMessage )
       
  1310     {
       
  1311     LOG(_L("[Mediator Server]\t CMediatorServerSession::SubscribeEventListL"));
       
  1312   
       
  1313     // Read domain & category information
       
  1314     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1315     
       
  1316      // Read event list information from Message
       
  1317     REventList eventList = ReadEventListL( aMessage, 1 );
       
  1318     CleanupClosePushL( eventList );
       
  1319     
       
  1320     RMediatorDebug::LogData(aMessage, *this, eventList);
       
  1321     
       
  1322     // Get handler
       
  1323     CMediatorServerEventHandler& eventHandler = Server().EventHandler();
       
  1324     
       
  1325     // Subscribe to events
       
  1326     TSecurityInfo messageCaps( aMessage );
       
  1327     eventHandler.SubscribeEventListL( category, eventList, messageCaps.iCaps, this );
       
  1328     
       
  1329     // Cleanup 
       
  1330     CleanupStack::PopAndDestroy( &eventList );
       
  1331     
       
  1332     // Complete message in case of no errors
       
  1333     aMessage.Complete( KErrNone ); 
       
  1334     
       
  1335     RMediatorDebug::LogStatus();
       
  1336     }
       
  1337 
       
  1338 // -----------------------------------------------------------------------------
       
  1339 // CMediatorServerSession::UnsubscribeEventL
       
  1340 // Helper method to subscribes to event(s). Uses Event handler.
       
  1341 // (other items were commented in a header).
       
  1342 // -----------------------------------------------------------------------------
       
  1343 //
       
  1344 void CMediatorServerSession::UnsubscribeEventListL( const RMessage2 &aMessage )
       
  1345     {
       
  1346     LOG(_L("[Mediator Server]\t CMediatorServerSession::UnsubscribeEventListL"));
       
  1347     
       
  1348     // Read domain & category
       
  1349     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1350     
       
  1351     // Read event list from slot 1
       
  1352     REventList eventList = ReadEventListL( aMessage, 1 );
       
  1353     CleanupClosePushL( eventList );
       
  1354     
       
  1355     RMediatorDebug::LogData(aMessage, *this, eventList);
       
  1356     
       
  1357     // Get handler
       
  1358     CMediatorServerEventHandler& eventHandler = Server().EventHandler();
       
  1359     
       
  1360     // Unsubscribe to events
       
  1361     eventHandler.UnsubscribeEventListL( category, eventList, this );
       
  1362     
       
  1363     // Cleanup
       
  1364     CleanupStack::PopAndDestroy( &eventList );
       
  1365     // Complete message in case of no errors
       
  1366     aMessage.Complete( KErrNone );
       
  1367 
       
  1368     RMediatorDebug::LogStatus();
       
  1369     }
       
  1370 
       
  1371 // -----------------------------------------------------------------------------
       
  1372 // CMediatorServerSession::GetDomainsL
       
  1373 // Helper method return registered events. Uses Object handler.
       
  1374 // (other items were commented in a header).
       
  1375 // -----------------------------------------------------------------------------
       
  1376 //
       
  1377 void CMediatorServerSession::GetDomainsL( const RMessage2 &aMessage )
       
  1378     {
       
  1379     LOG(_L("[Mediator Server]\t CMediatorServerSession::GetDomainsL"));
       
  1380     
       
  1381     // Construct array to hold registered domains
       
  1382     RDomainList domainList;
       
  1383     CleanupClosePushL( domainList );
       
  1384     
       
  1385     // Get handler
       
  1386     CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1387     
       
  1388     // Get the list of events
       
  1389     objectHandler.GetDomainsL( domainList );
       
  1390 
       
  1391     RMediatorDebug::LogData(aMessage, *this, domainList);
       
  1392     
       
  1393     // Write the information back to client
       
  1394     // 2 is for key information
       
  1395     TInt arraySize = ( sizeof(TUid) ) * domainList.Count();
       
  1396     
       
  1397     // Check that there's enough space before writing the answer
       
  1398     TInt space = aMessage.GetDesMaxLength(0);   // Slot 0
       
  1399     if ( ( domainList.Count() > 0 ) && ( arraySize <= space  ) )
       
  1400         {
       
  1401         TPtr8 arrayPtr( (TUint8*)&domainList[0], arraySize, arraySize );
       
  1402         aMessage.WriteL( 0, arrayPtr );
       
  1403         }
       
  1404     
       
  1405     // Complete message in case of no errors with domain count
       
  1406     aMessage.Complete( domainList.Count() );
       
  1407     
       
  1408     // Clean up
       
  1409     CleanupStack::PopAndDestroy( &domainList );
       
  1410 
       
  1411     }
       
  1412 
       
  1413 
       
  1414 // -----------------------------------------------------------------------------
       
  1415 // CMediatorServerSession::GetCategoriesL
       
  1416 // Helper method return registered events. Uses Object handler.
       
  1417 // (other items were commented in a header).
       
  1418 // -----------------------------------------------------------------------------
       
  1419 //
       
  1420 void CMediatorServerSession::GetCategoriesL( const RMessage2 &aMessage )
       
  1421     {
       
  1422     LOG(_L("[Mediator Server]\t CMediatorServerSession::GetCategoriesL"));
       
  1423     
       
  1424     // Read domain information
       
  1425     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1426     
       
  1427     // Construct array to hold registered categories
       
  1428     RCategoryList categoryList;
       
  1429     CleanupClosePushL( categoryList );
       
  1430     
       
  1431     // Get handler
       
  1432     CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1433     
       
  1434     // Get the list of events
       
  1435     objectHandler.GetCategoriesL( category, categoryList );
       
  1436     
       
  1437     RMediatorDebug::LogData(aMessage, *this, categoryList);
       
  1438     
       
  1439     // Write the information back to client
       
  1440     // 2 is for key information
       
  1441     TInt arraySize = ( sizeof(TUid) ) * categoryList.Count();
       
  1442     
       
  1443     // Check that there's enough space before writing the answer
       
  1444     TInt space = aMessage.GetDesMaxLength(1);   // Slot 1
       
  1445     if ( ( categoryList.Count() > 0 ) && ( arraySize <= space  ) )
       
  1446         {
       
  1447         TPtr8 arrayPtr( (TUint8*)&categoryList[0], arraySize, arraySize );
       
  1448         aMessage.WriteL( 1, arrayPtr );
       
  1449         }
       
  1450     
       
  1451     // Complete message in case of no errors with category count
       
  1452     aMessage.Complete( categoryList.Count() );
       
  1453     
       
  1454     // Clean up
       
  1455     CleanupStack::PopAndDestroy( &categoryList );
       
  1456     }
       
  1457 
       
  1458 
       
  1459 // -----------------------------------------------------------------------------
       
  1460 // CMediatorServerSession::GetCommandsL
       
  1461 // Helper method return registered commands. Uses Object handler.
       
  1462 // (other items were commented in a header).
       
  1463 // -----------------------------------------------------------------------------
       
  1464 //
       
  1465 void CMediatorServerSession::GetCommandsL( const RMessage2 &aMessage )
       
  1466     {
       
  1467     LOG(_L("[Mediator Server]\t CMediatorServerSession::GetCommandsL"));
       
  1468     
       
  1469     // Read domain & category information
       
  1470     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1471     
       
  1472     // Construct array to hold registered events
       
  1473     RCommandList commandList;
       
  1474     CleanupClosePushL( commandList );
       
  1475     
       
  1476     // Get command list using Object handler
       
  1477     CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1478     objectHandler.GetCommandsL( category, commandList );
       
  1479    
       
  1480     RMediatorDebug::LogData(aMessage, *this, commandList);
       
  1481     
       
  1482     // Write command list to slot 1
       
  1483     if ( CommandListFitsToClientMemory( commandList, aMessage, 1 ) )
       
  1484         {
       
  1485         WriteCommandListL( commandList, aMessage, 1 );
       
  1486         }
       
  1487     // if the list does not fit to memory, the client library makes a new synchronous request
       
  1488     //  with a large enough write buffer
       
  1489     // Complete message in case of no errors with command count
       
  1490     aMessage.Complete( commandList.Count() );
       
  1491        
       
  1492      // Clean up
       
  1493     CleanupStack::PopAndDestroy( &commandList );
       
  1494     }
       
  1495 
       
  1496 
       
  1497 // -----------------------------------------------------------------------------
       
  1498 // CMediatorServerSession::GetEventsL
       
  1499 // Helper method return registered events. Uses Object Handler
       
  1500 // (other items were commented in a header).
       
  1501 // -----------------------------------------------------------------------------
       
  1502 //
       
  1503 void CMediatorServerSession::GetEventsL( const RMessage2 &aMessage )
       
  1504     {
       
  1505     LOG(_L("[Mediator Server]\t CMediatorServerSession::GetEventsL"));
       
  1506     
       
  1507     // Read domain & category information
       
  1508     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1509     
       
  1510     // Construct array to hold registered events
       
  1511     REventList eventList;
       
  1512     CleanupClosePushL( eventList );
       
  1513     
       
  1514     // Get events using Event handler
       
  1515     CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1516     objectHandler.GetEventsL( category, eventList );
       
  1517     
       
  1518     RMediatorDebug::LogData(aMessage, *this, eventList);
       
  1519     
       
  1520     // Write event list to slot 1
       
  1521     if ( EventListFitsToClientMemory( eventList, aMessage, 1 ) )
       
  1522         {
       
  1523         WriteEventListL( eventList, aMessage, 1 );
       
  1524         } 
       
  1525     // if the list does not fit to memory, the client library makes a new synchronous request
       
  1526     //  with a large enough write buffer
       
  1527     
       
  1528     // Complete message in case of no errors with command count
       
  1529     aMessage.Complete( eventList.Count() );
       
  1530        
       
  1531      // Clean up
       
  1532     CleanupStack::PopAndDestroy( &eventList );
       
  1533     }
       
  1534 
       
  1535 // -----------------------------------------------------------------------------
       
  1536 // CMediatorServerSession::RaiseEventL
       
  1537 // Helper method to raise events. Uses Event Handler
       
  1538 // (other items were commented in a header).
       
  1539 // -----------------------------------------------------------------------------
       
  1540 //
       
  1541 void CMediatorServerSession::RaiseEventL( const RMessage2& aMessage )
       
  1542     {
       
  1543     LOG(_L("[Mediator Server]\t CMediatorServerSession::RaiseEventL"));
       
  1544     
       
  1545     // Read domain & category information
       
  1546     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1547     
       
  1548     // Read event information
       
  1549     TEvent raiseEvent = ReadEventL( aMessage, 1 );
       
  1550     
       
  1551     // Then read parameter data from slot 2
       
  1552     HBufC8* eventData = ReadDataLC( aMessage, 2 );
       
  1553 
       
  1554     RMediatorDebug::LogData(aMessage, *this, raiseEvent);
       
  1555     RMediatorDebug::LogData(aMessage, *this, *eventData);
       
  1556     
       
  1557     // Get handler
       
  1558     CMediatorServerEventHandler& eventHandler = Server().EventHandler();
       
  1559     
       
  1560     // Raise event
       
  1561     eventHandler.RaiseEventL( category, raiseEvent, *eventData );
       
  1562     
       
  1563     // Cleanup
       
  1564     CleanupStack::PopAndDestroy( eventData );
       
  1565 
       
  1566     // Complete message in case of no errors
       
  1567     aMessage.Complete( KErrNone );
       
  1568     
       
  1569     }
       
  1570 
       
  1571 // -----------------------------------------------------------------------------
       
  1572 // CMediatorServerSession::IssueCommandL
       
  1573 // Helper function to handle command request. Uses Command Handler
       
  1574 // (other items were commented in a header).
       
  1575 // -----------------------------------------------------------------------------
       
  1576 //
       
  1577 void CMediatorServerSession::IssueCommandL( const RMessage2 &aMessage )
       
  1578     {
       
  1579     LOG(_L("[Mediator Server]\t CMediatorServerSession::IssueCommandL"));
       
  1580 
       
  1581     // Read domain & category information
       
  1582     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1583      
       
  1584     // Read command information
       
  1585     TCommand issueCommand = ReadCommandL( aMessage, 1 ); 
       
  1586     
       
  1587     // Then read parameter data from slot 2
       
  1588     HBufC8* commandData = ReadDataLC( aMessage, 2 );
       
  1589     
       
  1590     RMediatorDebug::LogData(aMessage, *this, issueCommand);
       
  1591     RMediatorDebug::LogData(aMessage, *this, *commandData);
       
  1592     
       
  1593     // Get handler & issue command
       
  1594     TSecurityInfo messageCaps( aMessage );
       
  1595     CMediatorServerCommandHandler& commandHandler = Server().CommandHandler();
       
  1596     commandHandler.IssueCommandL( category,
       
  1597                                   issueCommand,
       
  1598                                   *commandData,
       
  1599                                   messageCaps.iCaps,
       
  1600                                   this );
       
  1601     
       
  1602     
       
  1603     // cleanup
       
  1604     CleanupStack::PopAndDestroy( commandData );
       
  1605 
       
  1606     // Complete message in case of no errors
       
  1607     aMessage.Complete( KErrNone );
       
  1608     }
       
  1609 
       
  1610 // -----------------------------------------------------------------------------
       
  1611 // CMediatorServerSession::CancelCommandL
       
  1612 // Cancels ongoing command processing. Uses Command Handler
       
  1613 // (other items were commented in a header).
       
  1614 // -----------------------------------------------------------------------------
       
  1615 //
       
  1616 void CMediatorServerSession::CancelCommandL( const RMessage2 &aMessage )
       
  1617     {
       
  1618     LOG(_L("[Mediator Server]\t CMediatorServerSession::CancelCommandL"));
       
  1619 
       
  1620     // Read domain & category information
       
  1621     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1622      
       
  1623     // Read command information
       
  1624     TCommand cancelCommand = ReadCommandL( aMessage, 1 ); 
       
  1625     
       
  1626     RMediatorDebug::LogData(aMessage, *this, cancelCommand);
       
  1627     
       
  1628     // Get handler & cancel command
       
  1629     CMediatorServerCommandHandler& commandHandler = Server().CommandHandler();
       
  1630     commandHandler.CancelCommand( category, cancelCommand );
       
  1631                                    
       
  1632     // Complete initiators async request with KErrCancel
       
  1633     if ( iCommandResponseMessage.Handle() )
       
  1634         {
       
  1635         
       
  1636         // Write domain & category information to slot 0
       
  1637         WriteCategoryL( category.iDomain, 
       
  1638                         category.iCategory, 
       
  1639                         iCommandResponseMessage, 
       
  1640                         0 );
       
  1641         
       
  1642         // Write event to slot 1
       
  1643         TVersion version( 0, 0, 0 );
       
  1644         WriteCommandL( cancelCommand.iCommandId, 
       
  1645                        version, 
       
  1646                        iCommandResponseMessage, 
       
  1647                        1 );
       
  1648         
       
  1649         // Status to slot 2
       
  1650         TPckg<TInt> statusBuffer( KErrCancel );
       
  1651         iCommandResponseMessage.WriteL( 2, statusBuffer );
       
  1652         
       
  1653         // Write data to slot 3
       
  1654         WriteDataL( KNullDesC8, iCommandResponseMessage, 2 );
       
  1655         
       
  1656         iCommandResponseMessage.Complete( KErrCancel );
       
  1657         }
       
  1658 
       
  1659     // Complete message in case of no errors
       
  1660     aMessage.Complete( KErrNone );
       
  1661     }
       
  1662 
       
  1663 
       
  1664 // -----------------------------------------------------------------------------
       
  1665 // CMediatorServerSession::IssueResponseL
       
  1666 // Helper function to handle command response. Uses Command Handler
       
  1667 // (other items were commented in a header).
       
  1668 // -----------------------------------------------------------------------------
       
  1669 //
       
  1670 void CMediatorServerSession::IssueResponseL( const RMessage2 &aMessage )
       
  1671     {
       
  1672     LOG(_L("[Mediator Server]\t CMediatorServerSession::IssueResponseL"));
       
  1673 
       
  1674     // Read domain & category information
       
  1675     TMediatorCategory category = ReadCategoryL( aMessage, 0 );
       
  1676      
       
  1677     // Read command information
       
  1678     TCommand responseCommand = ReadCommandL( aMessage, 1 ); 
       
  1679     
       
  1680     // Then read status data from slot 2
       
  1681     TPckgBuf<TInt> statusBuffer;
       
  1682     aMessage.ReadL( 2, statusBuffer );
       
  1683     TInt status = statusBuffer();
       
  1684     
       
  1685     // And result data from slot 3
       
  1686     HBufC8* responseData = ReadDataLC( aMessage, 3 );
       
  1687     
       
  1688     RMediatorDebug::LogData(aMessage, *this, responseCommand);
       
  1689     RMediatorDebug::LogData(aMessage, *this, *responseData);
       
  1690     
       
  1691     // Get handler & respond to command
       
  1692     TSecurityInfo messageCaps( aMessage );
       
  1693     CMediatorServerCommandHandler& commandHandler = Server().CommandHandler();
       
  1694     
       
  1695     TInt err = commandHandler.IssueResponse( category,
       
  1696                                              responseCommand,
       
  1697                                              *responseData,
       
  1698                                              status );
       
  1699                                              
       
  1700     if ( err != KErrNone )
       
  1701         {
       
  1702         // If command is not found, it means that the command initiator has cancelled the command
       
  1703         
       
  1704         // Check the command service list and remove the cancelled command if found, because responder 
       
  1705         // can interpret the return value as a cancellation and further notifications are thus not needed
       
  1706         TBool found = EFalse;
       
  1707         for ( TInt index = 0; index < iCommandServiceList.Count() && !found; index++ )
       
  1708             {
       
  1709             CCommand* commandPtr = iCommandServiceList[index];
       
  1710             if ( commandPtr )
       
  1711                 {
       
  1712                 if ( commandPtr->Domain() == category.iDomain
       
  1713                     && commandPtr->Category() == category.iCategory
       
  1714                     && commandPtr->Id() == responseCommand.iCommandId
       
  1715                     && commandPtr->Status() == ECommandCanceled )
       
  1716                     {
       
  1717                     iCommandServiceList.Remove( index );
       
  1718                     delete commandPtr;
       
  1719                     commandPtr = NULL;
       
  1720                     found = ETrue;
       
  1721                     }
       
  1722                 }
       
  1723             }
       
  1724         
       
  1725         User::Leave( err );
       
  1726         }
       
  1727     
       
  1728     // cleanup
       
  1729     CleanupStack::PopAndDestroy( responseData );
       
  1730 
       
  1731     // Complete message in case of no errors
       
  1732     aMessage.Complete( KErrNone );
       
  1733     }
       
  1734 
       
  1735 // -----------------------------------------------------------------------------
       
  1736 // CMediatorServerSession::WaitForEventsL
       
  1737 // Helper method to receive events
       
  1738 // (other items were commented in a header).
       
  1739 // -----------------------------------------------------------------------------
       
  1740 //
       
  1741 void CMediatorServerSession::WaitForEventsL( const RMessage2& aMessage )
       
  1742     {
       
  1743     // Check if we have already events to be notified
       
  1744     if ( iEventServiceList.Count() > 0 )
       
  1745         {
       
  1746         CEvent* eventPtr = iEventServiceList[0];    // Take the first event
       
  1747         CleanupStack::PushL( eventPtr );
       
  1748         iEventServiceList.Remove(0);
       
  1749         
       
  1750         // Write domain & category to slot 0
       
  1751         WriteCategoryL( eventPtr->Domain(), eventPtr->Category(), aMessage, 0 );
       
  1752         
       
  1753         // Write event to slot 1
       
  1754         WriteEventL( eventPtr->Id(), aMessage, 1 );
       
  1755         
       
  1756         // Write data to slot 2
       
  1757         WriteDataL( eventPtr->ParameterData(), aMessage, 2 );
       
  1758        
       
  1759         // Complete message with data amount
       
  1760         aMessage.Complete( eventPtr->ParameterData().Length() );
       
  1761         
       
  1762         //Cleanup
       
  1763         CleanupStack::PopAndDestroy( eventPtr );
       
  1764         
       
  1765         TRACE(Print(_L("[Mediator Server]\t Session %d - event buffer %d\n"), this, iEventServiceList.Count() ));
       
  1766         }
       
  1767     else    // Start async waiting
       
  1768         {
       
  1769         iEventNotificationMessage = aMessage;
       
  1770         }
       
  1771     }
       
  1772 
       
  1773 // -----------------------------------------------------------------------------
       
  1774 // CMediatorServerSession::WaitForCommandsL
       
  1775 // Helper method to receive commands.
       
  1776 // (other items were commented in a header).
       
  1777 // -----------------------------------------------------------------------------
       
  1778 //
       
  1779 void CMediatorServerSession::WaitForCommandsL( const RMessage2& aMessage )
       
  1780     {
       
  1781     // Check if we have already commands to be delivered
       
  1782     if ( iCommandServiceList.Count() > 0 )
       
  1783         {
       
  1784         CCommand* commandPtr = iCommandServiceList[0];    // Take the first command
       
  1785         CleanupStack::PushL( commandPtr );
       
  1786         iCommandServiceList.Remove(0);
       
  1787         
       
  1788         // If it's a pending command it can now be delivered
       
  1789         if ( commandPtr->Status() == ECommandPending )
       
  1790             {
       
  1791             // Write domain & category to slot 0
       
  1792             WriteCategoryL( commandPtr->Domain(), commandPtr->Category(), aMessage, 0 );
       
  1793         
       
  1794             // Write event to slot 1
       
  1795             WriteCommandL( commandPtr->Id(), commandPtr->Version(), aMessage, 1 );
       
  1796         
       
  1797             // Write data to slot 2
       
  1798             WriteDataL( commandPtr->ParameterData(), aMessage, 2 );
       
  1799        
       
  1800             // Complete message with data amount
       
  1801             aMessage.Complete( commandPtr->ParameterData().Length() );
       
  1802     
       
  1803             }
       
  1804         // If it's a cancelled command then the responder will be notified about cancellation that
       
  1805         // it was not able to handle before   
       
  1806         else if ( commandPtr->Status() == ECommandCanceled )
       
  1807             {
       
  1808             // Write domain & category to slot 0
       
  1809             WriteCategoryL( commandPtr->Domain(), commandPtr->Category(), aMessage, 0 );
       
  1810             
       
  1811             // Write command id and version to slot 1
       
  1812             WriteCommandL( commandPtr->Id(), commandPtr->Version(), aMessage, 1 );
       
  1813             
       
  1814             // Status to slot 2
       
  1815             TPckg<TInt> statusBuffer( KErrCancel );
       
  1816             aMessage.WriteL( 2, statusBuffer );
       
  1817         
       
  1818             // Write data to slot 3
       
  1819             WriteDataL( KNullDesC8, aMessage, 2 );
       
  1820         
       
  1821             // Complete message
       
  1822             aMessage.Complete( KErrCancel );
       
  1823             }
       
  1824             
       
  1825         //Cleanup
       
  1826         CleanupStack::PopAndDestroy( commandPtr );            
       
  1827                 
       
  1828         TRACE(Print(_L("[Mediator Server]\t Session %d - command buffer %d\n"), this, iCommandServiceList.Count() ));
       
  1829         }
       
  1830     else    // Start async waiting
       
  1831         {
       
  1832         iCommandInitMessage = aMessage;
       
  1833         }
       
  1834     }
       
  1835 
       
  1836 // -----------------------------------------------------------------------------
       
  1837 // CMediatorServerSession::WaitForNotificationsL
       
  1838 // Starts async notification waiting. Uses Object Handler
       
  1839 // (other items were commented in a header).
       
  1840 // -----------------------------------------------------------------------------
       
  1841 //
       
  1842 void CMediatorServerSession::WaitForNotificationsL( const RMessage2& aMessage )
       
  1843     {
       
  1844     if ( !iNotificationMessage.Handle() )
       
  1845         {
       
  1846         if ( !iNotificationsRegistered )
       
  1847             {
       
  1848             // Get handler
       
  1849             CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1850             objectHandler.AddObserverL( this );
       
  1851             iNotificationsRegistered = ETrue;
       
  1852             }
       
  1853         iNotificationMessage = aMessage;
       
  1854         
       
  1855         if ( !iNotificationQueue.IsEmpty() )
       
  1856             {
       
  1857             PurgeNextBufferedNotificationL();
       
  1858             }
       
  1859         }
       
  1860     else
       
  1861         {
       
  1862         ERROR_LOG(_L("[Mediator] CMediatorServerSession::WaitForNotificationsL: KErrAlreadyExists\n"));
       
  1863         User::Leave( KErrAlreadyExists );
       
  1864         }
       
  1865     }
       
  1866 
       
  1867 // -----------------------------------------------------------------------------
       
  1868 // CMediatorServerSession::CancelNotificationsL
       
  1869 // Stops async notification waiting.
       
  1870 // (other items were commented in a header).
       
  1871 // -----------------------------------------------------------------------------
       
  1872 //
       
  1873 void CMediatorServerSession::CancelNotificationsL( const RMessage2& aMessage )
       
  1874     {
       
  1875     CleanNotificationQueue();
       
  1876     
       
  1877     if ( iNotificationMessage.Handle() )
       
  1878         {
       
  1879         iNotificationMessage.Complete( KErrCancel );
       
  1880         }   
       
  1881     if ( iNotificationsRegistered )
       
  1882         {
       
  1883         // Get handler
       
  1884         CMediatorServerObjectHandler& objectHandler = Server().ObjectHandler();
       
  1885         objectHandler.RemoveObserverL( this );
       
  1886         iNotificationsRegistered = EFalse;
       
  1887         }
       
  1888     else
       
  1889         {
       
  1890         ERROR_LOG(_L("[Mediator] CMediatorServerSession::CancelNotificationsL: KErrNotFound\n"));
       
  1891         User::Leave( KErrNotFound );
       
  1892         }
       
  1893     aMessage.Complete( KErrNone );
       
  1894     }
       
  1895 
       
  1896 // -----------------------------------------------------------------------------
       
  1897 // CMediatorServerSession::WaitForCommandResponseL
       
  1898 // Async waiting for command responses
       
  1899 // (other items were commented in a header).
       
  1900 // -----------------------------------------------------------------------------
       
  1901 //
       
  1902 void CMediatorServerSession::WaitForCommandResponseL( const RMessage2& aMessage )
       
  1903     {
       
  1904     // Just take message to member variable to future use
       
  1905     if ( !iCommandResponseMessage.Handle() )
       
  1906         {
       
  1907         iCommandResponseMessage = aMessage;
       
  1908         }
       
  1909     }
       
  1910 
       
  1911 // -----------------------------------------------------------------------------
       
  1912 // CMediatorServerSession::FetchParameterDataL
       
  1913 // Async waiting for command responses
       
  1914 // (other items were commented in a header).
       
  1915 // -----------------------------------------------------------------------------
       
  1916 //
       
  1917 void CMediatorServerSession::FetchParameterDataL( const RMessage2& aMessage )
       
  1918     {
       
  1919     // Write data buffer to message slot 0 and complete with size of the buffer
       
  1920     if ( iDataBuffer )
       
  1921         {
       
  1922         WriteDataL( *iDataBuffer, aMessage, 0 );
       
  1923         aMessage.Complete( iDataBuffer->Length() );
       
  1924         delete iDataBuffer;
       
  1925         iDataBuffer = NULL;
       
  1926         }
       
  1927     else
       
  1928         {
       
  1929         aMessage.Complete( 0 ); // Just complete with zero sized data
       
  1930         }
       
  1931     }
       
  1932 
       
  1933 // -----------------------------------------------------------------------------
       
  1934 // CMediatorServerSession::FetchNotificationEventListL
       
  1935 // 
       
  1936 // (other items were commented in a header).
       
  1937 // -----------------------------------------------------------------------------
       
  1938 //
       
  1939 void CMediatorServerSession::FetchNotificationEventListL( const RMessage2& aMessage )
       
  1940     {
       
  1941     // Write event list to message slot 0 and complete with count of events in the list
       
  1942     CMediatorQueItem* eventList = iNotificationQueue.First();
       
  1943     iNotificationQueue.Remove( *eventList );
       
  1944     CleanupStack::PushL( eventList );
       
  1945     const REventList* events( eventList->Events() );
       
  1946     
       
  1947     if ( !events )
       
  1948         {
       
  1949         // should not ever end up leaving here, as this indicates a
       
  1950         // problem with queue management i.e. a programming error
       
  1951         ERROR_LOG( _L("[Mediator] CMediatorServerSession::FetchNotificationEventListL: notif queue corrupted\n") );
       
  1952         User::Leave( KErrCorrupt );
       
  1953         }
       
  1954     
       
  1955     WriteEventListL( *events, aMessage, 0 );
       
  1956     aMessage.Complete( events->Count() );
       
  1957     CleanupStack::PopAndDestroy( eventList );
       
  1958     }
       
  1959     
       
  1960 // -----------------------------------------------------------------------------
       
  1961 // CMediatorServerSession::FetchNotificationCommandListL
       
  1962 // 
       
  1963 // (other items were commented in a header).
       
  1964 // -----------------------------------------------------------------------------
       
  1965 //
       
  1966 void CMediatorServerSession::FetchNotificationCommandListL( const RMessage2& aMessage )
       
  1967     {
       
  1968     // Write command list buffer to message slot 0 and complete count of commands in the list
       
  1969     CMediatorQueItem* commandList = iNotificationQueue.First();
       
  1970     iNotificationQueue.Remove( *commandList );
       
  1971     CleanupStack::PushL( commandList );
       
  1972     const RCommandList* commands( commandList->Commands() );
       
  1973     
       
  1974     if ( !commands )
       
  1975         {
       
  1976         // should not ever end up leaving here, as this indicates a
       
  1977         // problem with queue management i.e. a programming error
       
  1978         ERROR_LOG( _L("[Mediator] CMediatorServerSession::FetchNotificationCommandListL: notif queue corrupted\n") );
       
  1979         User::Leave( KErrCorrupt );
       
  1980         }
       
  1981     
       
  1982     WriteCommandListL( *commands, aMessage, 0 );
       
  1983     aMessage.Complete( commands->Count() );
       
  1984     CleanupStack::PopAndDestroy( commandList );
       
  1985     }
       
  1986 
       
  1987 
       
  1988 // -----------------------------------------------------------------------------
       
  1989 // CMediatorServerSession::CancelAll
       
  1990 // Async waiting for command responses
       
  1991 // (other items were commented in a header).
       
  1992 // -----------------------------------------------------------------------------
       
  1993 //
       
  1994 void CMediatorServerSession::CancelAll( const RMessage2& aMessage )
       
  1995     {
       
  1996     // Cancel all possible async wait operations
       
  1997     CancelAll( KErrCancel );
       
  1998     // Finally complete own message
       
  1999     aMessage.Complete( KErrNone );
       
  2000     }
       
  2001 
       
  2002 // -----------------------------------------------------------------------------
       
  2003 // CMediatorServerSession::CancelAll
       
  2004 // Async waiting for command responses
       
  2005 // (other items were commented in a header).
       
  2006 // -----------------------------------------------------------------------------
       
  2007 //
       
  2008 void CMediatorServerSession::CancelAll( TInt aError )
       
  2009     {
       
  2010     // Cancel all possible async wait operations
       
  2011     if ( iCommandResponseMessage.Handle() )
       
  2012         {
       
  2013         iCommandResponseMessage.Complete( aError );
       
  2014         }
       
  2015     if ( iCommandInitMessage.Handle() )
       
  2016         {
       
  2017         iCommandInitMessage.Complete( aError );
       
  2018         }
       
  2019     if ( iNotificationMessage.Handle() )
       
  2020         {
       
  2021         CleanNotificationQueue();
       
  2022         iNotificationMessage.Complete( aError );
       
  2023         }
       
  2024     if ( iEventNotificationMessage.Handle() )
       
  2025         {
       
  2026         iEventNotificationMessage.Complete( aError );
       
  2027         }
       
  2028     }
       
  2029 
       
  2030 // End of File