phoneclientserver/phoneserver/Src/Standard/CPhSrvSession.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Session.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "cphsrvserver.h" 
       
    22 #include "cphsrvsession.h" 
       
    23 #include "cphsrvsubsessionbase.h" 
       
    24 #include "phsrvsubsessionfactory.h" 
       
    25 #include "phcltclientserver.h" 
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CPhSrvSession::NewL
       
    35 // Two-phased constructor.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CPhSrvSession* CPhSrvSession::NewL( CPhSrvServer& aServer )   
       
    39     {
       
    40     CPhSrvSession* self = new ( ELeave ) CPhSrvSession();
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL( aServer );
       
    43     CleanupStack::Pop();
       
    44     return self;
       
    45     }
       
    46 
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CPhSrvSession::CPhSrvSession
       
    50 // C++ constructor can NOT contain any code, that might leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CPhSrvSession::CPhSrvSession() 
       
    54     {
       
    55     }
       
    56 
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CPhSrvSession::~CPhSrvSession
       
    60 // Destructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CPhSrvSession::~CPhSrvSession()
       
    64     {
       
    65     if ( iServer )
       
    66         {
       
    67         iServer->CancelCreateAll( *this );
       
    68         }
       
    69 
       
    70     delete iObjectIx;
       
    71    
       
    72    if ( iContainer )
       
    73         {
       
    74         iServer->RemoveContainer( iContainer );
       
    75         iContainer = NULL;
       
    76         }
       
    77     }
       
    78 
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CPhSrvSession::ServiceL
       
    82 // 
       
    83 // Calls DispatchMessageL under trap harness.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CPhSrvSession::ServiceL( const RMessage2& aMessage )  
       
    87     {
       
    88     TRAPD( err, ProcessRequestL( aMessage ) );
       
    89     if ( err != KErrNone )
       
    90         {
       
    91         aMessage.Complete( err );
       
    92         }
       
    93     }
       
    94 
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CPhSrvSession::ProcessRequestL
       
    98 // 
       
    99 // Checks the function specified by the client message
       
   100 // and depending on the type, performs an appropriate action.
       
   101 //
       
   102 // If the function is one of the "special" factory sub-session
       
   103 // creation op-codes, then this session will process it.
       
   104 //
       
   105 // If the function is a generic subsession function, then the
       
   106 // subsession which can handle this function is identified by
       
   107 // it's unique handle, and it is asked to process the request.
       
   108 //
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CPhSrvSession::ProcessRequestL( const RMessage2& aMessage )
       
   112     {
       
   113     if ( HandleCommandL( aMessage ) )
       
   114         {
       
   115         return;
       
   116         }
       
   117 
       
   118     const TInt function = aMessage.Function();
       
   119     const TUint32 sid = aMessage.SecureId().iId;
       
   120     
       
   121     // Is this a message that results in a new subsession?
       
   122     if  ( PhSrvSubSessionFactory::PhSrvSubSessionFactoryIsCreationFunction( 
       
   123           function ) )
       
   124         {
       
   125         if ( !PhSrvSubSessionFactory::PhSrvSubSessionFactoryIsCreationAllowed(
       
   126             function,
       
   127             sid ) )
       
   128             {
       
   129             aMessage.Complete( KErrPermissionDenied );
       
   130             }
       
   131         else
       
   132             {
       
   133             // Need to create a new subsession
       
   134             CPhSrvSubSessionBase* subSession = 
       
   135                 PhSrvSubSessionFactory::PhSrvSubSessionFactoryCreateLC( 
       
   136                     function, 
       
   137                     *this );
       
   138 
       
   139             // Add to container (takes ownership)
       
   140             iContainer->AddL( subSession );
       
   141             if (function != EPhoneServerImageHandlerSubSessionOpen )
       
   142                 {
       
   143                 CleanupStack::Pop( subSession );    
       
   144                 }
       
   145             // Get a handle for the object
       
   146             const TInt handle = iObjectIx->AddL( subSession );
       
   147 
       
   148             // Inform client of the handle its been allocated
       
   149             TPckg<TInt> handlePckg( handle );
       
   150             Write( 
       
   151                 aMessage,
       
   152                 3,
       
   153                 handlePckg );
       
   154 
       
   155             if ( !aMessage.IsNull() )
       
   156                 {
       
   157                 // Complete the message
       
   158                 aMessage.Complete( KErrNone );
       
   159                 }
       
   160              }    
       
   161          }
       
   162     else
       
   163         {
       
   164         // Find an appropriate object and pass the message to it
       
   165         // for processing...
       
   166         const TInt handle = aMessage.Int3();
       
   167 
       
   168         // Fetch the sub-session by its handle
       
   169         CObject* object = iObjectIx->At( handle );
       
   170         if ( !object )
       
   171             {
       
   172             PanicClient(
       
   173                 aMessage,
       
   174                 EPhCltServerInitiatedPanicInvalidHandle );
       
   175             }
       
   176 
       
   177         // We can cast the object to a subsession instance, since that's
       
   178         // all we store in the object container
       
   179         CPhSrvSubSessionBase* subSession = 
       
   180             static_cast< CPhSrvSubSessionBase* >( object );
       
   181         
       
   182         if ( subSession )
       
   183             {
       
   184             // Check that the subsession can handle this request
       
   185             if ( !subSession->PhSrvMessageDecoderCanProcessMessage( function ) )
       
   186                 {
       
   187                 PanicClient(
       
   188                     aMessage,
       
   189                     EPhCltServerInitiatedPanicInvalidHandle );
       
   190                 }
       
   191             else
       
   192                 {
       
   193                 // Get it to process this request
       
   194                 subSession->PhSrvMessageProcessorHandleMessageL( aMessage );
       
   195                 }
       
   196             }            
       
   197         }
       
   198     }
       
   199 
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CPhSrvSession::SubSessionCount
       
   203 // 
       
   204 // Return the number of subsessions
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 TInt CPhSrvSession::SubSessionCount() const
       
   208     {
       
   209     return iContainer->Count();
       
   210     }
       
   211 
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CPhSrvSession::SubSessionA
       
   215 // 
       
   216 // Return a subsession from an index
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 CPhSrvSubSessionBase& CPhSrvSession::SubSessionAt( TInt aIndex ) const
       
   220     {
       
   221     return *static_cast< CPhSrvSubSessionBase* >( ( *iContainer )[ aIndex ] );
       
   222     }
       
   223 
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CPhSrvSession::CloseSubsession
       
   227 // 
       
   228 // Remove object from object index
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CPhSrvSession::CloseSubSession( const RMessage2& aMessage )
       
   232     {
       
   233     TInt handle = aMessage.Int3();
       
   234 
       
   235     CObject* obj = iObjectIx->At( handle );  
       
   236     __ASSERT_ALWAYS( obj, 
       
   237         PanicClient(
       
   238             aMessage,
       
   239             EPhCltServerInitiatedPanicInvalidHandle ) );
       
   240     iObjectIx->Remove( handle );
       
   241 
       
   242     if ( !aMessage.IsNull() )
       
   243         {
       
   244         aMessage.Complete( KErrNone );
       
   245         }
       
   246     }
       
   247 
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CPhSrvSession::PanicClient
       
   251 // 
       
   252 // Panic the client's thread
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CPhSrvSession::PanicClient(
       
   256     const RMessage2& aMessage,
       
   257     TPhCltServerInitiatedPanic aPanic ) const    
       
   258     {
       
   259     if ( !aMessage.IsNull() )
       
   260         {
       
   261         _LIT( KPhServerPanicCategory, "PhSrvServer" );
       
   262         aMessage.Panic( KPhServerPanicCategory, aPanic );
       
   263         }
       
   264     }
       
   265 
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CPhSrvSession::ConstructL
       
   269 // 
       
   270 // Symbian OS 2nd phase constructor
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CPhSrvSession::ConstructL( CPhSrvServer& aServer )
       
   274     {
       
   275     iContainer = aServer.NewContainerL();
       
   276     iObjectIx = CObjectIx::NewL();   
       
   277     
       
   278     iServer = &aServer;
       
   279     }
       
   280 
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CPhSrvSession::SubSessionUniqueHandle
       
   284 // 
       
   285 // Return the unique handle for the specified subsession
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TInt CPhSrvSession::SubSessionUniqueHandle( 
       
   289     const CPhSrvSubSessionBase& aSubSession ) const
       
   290     {
       
   291     // Ensure that the handle really is unique.
       
   292     // return iObjectIx->At( &aSubSession ); is unique only in same session.
       
   293     return reinterpret_cast<TInt>( &aSubSession );
       
   294     }
       
   295 
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CPhSrvSession::PhoneServer
       
   299 // 
       
   300 // Returns the Phone Server
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 CPhSrvServer& CPhSrvSession::PhoneServer() const
       
   304     {
       
   305     return *static_cast< CPhSrvServer* >( const_cast< CServer2* >( Server() ) );
       
   306     }
       
   307 
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CPhSrvSession::Write
       
   311 // 
       
   312 // Write to the client address space. Panic client upon error
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CPhSrvSession::Write( 
       
   316     const RMessage2& aMessage,
       
   317     TInt aLocation,
       
   318     const TDesC8& aDes,
       
   319     TInt aOffset )
       
   320     {
       
   321     TInt ret = aMessage.Write( aLocation, aDes, aOffset );
       
   322     if ( ret != KErrNone )
       
   323         {
       
   324         PanicClient( 
       
   325             aMessage,
       
   326             EPhCltServerInitiatedPanicBadDescriptor );
       
   327         }
       
   328     }
       
   329 
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CPhSrvSession::Read
       
   333 // 
       
   334 // Read from the client address space. Panic client upon error
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 void CPhSrvSession::Read(
       
   338     const RMessage2& aMessage,
       
   339     TInt aLocation,
       
   340     TDes8& aDes,
       
   341     TInt aOffset )
       
   342     {
       
   343     TInt ret = aMessage.Read( aLocation, aDes, aOffset );
       
   344     if ( ret != KErrNone )
       
   345         {
       
   346         PanicClient( 
       
   347             aMessage,
       
   348             EPhCltServerInitiatedPanicBadDescriptor );
       
   349         }
       
   350     }
       
   351 
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CPhSrvSession::Read
       
   355 // 
       
   356 // Read from the client address space. Panic client upon error
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 void CPhSrvSession::Read(
       
   360     const RMessage2& aMessage,
       
   361     TInt aLocation,
       
   362     TDes& aDes,
       
   363     TInt aOffset )
       
   364     {
       
   365     TInt ret = aMessage.Read( aLocation, aDes, aOffset );
       
   366     if ( ret != KErrNone )
       
   367         {
       
   368         PanicClient( 
       
   369             aMessage,
       
   370             EPhCltServerInitiatedPanicBadDescriptor );
       
   371         }
       
   372     }
       
   373 
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CPhSrvSession::CompleteCreateAll
       
   377 // -----------------------------------------------------------------------------
       
   378 //
       
   379 void CPhSrvSession::CompleteCreateAll( TInt aError )
       
   380     {
       
   381     if ( iCreateAll )
       
   382         {
       
   383         iCreateAll = EFalse;
       
   384         iCreateAllMsg.Complete( aError );
       
   385         }
       
   386     }
       
   387 
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CPhSrvSession::HandleCommandL
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 TBool CPhSrvSession::HandleCommandL( const RMessage2& aMessage )
       
   394     {
       
   395     TBool result = EFalse;
       
   396 
       
   397     switch ( aMessage.Function() )
       
   398         {
       
   399         case EPhoneCreateAll:
       
   400             result = ETrue;
       
   401             
       
   402             iServer->CancelCreateAll( *this );
       
   403             iServer->CreateAllL( *this );
       
   404             
       
   405             iCreateAll = ETrue;
       
   406             iCreateAllMsg = aMessage;
       
   407             break;
       
   408         
       
   409         default:
       
   410             break;
       
   411         }
       
   412 
       
   413     return result;
       
   414     }
       
   415 
       
   416 
       
   417 //  End of File