IMPSengine/client/src/impsclient.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     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: 
       
    15 * class for imps server.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32base.h>
       
    22 #include    <e32std.h>
       
    23 #include    "impsservercommon.h"
       
    24 #include    "impshandler.h"
       
    25 #include    "impsclient.h"
       
    26 #include    "impserrors.h"
       
    27 #include    "impsutils.h"
       
    28 #include    "impsclientsrv.h"
       
    29 #include    "impsconst.h"
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt  KWaitForServerRetries = 3;
       
    33 
       
    34 // MACROS
       
    35 #ifndef _DEBUG
       
    36 #define _NO_IMPS_LOGGING_
       
    37 #endif
       
    38 
       
    39 // ================= MEMBER FUNCTIONS =======================
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // RImpsEng::RImpsEng
       
    43 // ----------------------------------------------------------------------------
       
    44 EXPORT_C RImpsEng::RImpsEng()
       
    45 : RSessionBase(),
       
    46   iSessionOk (EFalse )
       
    47     {
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // RImpsEng::Connect
       
    52 // ----------------------------------------------------------------------------
       
    53 EXPORT_C TInt RImpsEng::Connect( TInt aRate /*, TBool aReqular*/ )
       
    54     {
       
    55     // This has similar functionality than old version RImpsClient
       
    56     TInt err = KErrNone;
       
    57 
       
    58 #ifndef _NO_IMPS_LOGGING_
       
    59     CImpsClientLogger::Log(_L("Client: Connect rate=0x%x"), aRate);
       
    60 #endif
       
    61 
       
    62     for ( TInt tries = 0; tries < KWaitForServerRetries ; tries++ )
       
    63         { 
       
    64 #ifndef _NO_IMPS_LOGGING_
       
    65     CImpsClientLogger::Log(_L("Client: Calling CreateSession "));
       
    66 #endif
       
    67         err = CreateSession( 
       
    68                 KImpsServerName, Version(), KImpsDefaultMessageSlots );
       
    69 
       
    70         if ( err == KErrNone )
       
    71             {
       
    72             iSessionOk = ETrue;
       
    73             break; // connected to existing server - ok
       
    74             }
       
    75 
       
    76         // problems other than server not here - propagate error
       
    77         if ( err != KErrNotFound && err != KErrServerTerminated ) 
       
    78             {
       
    79             break;
       
    80             }
       
    81 
       
    82         err = StartServer( aRate, 0 );    // start a new server
       
    83         }
       
    84 #ifndef _NO_IMPS_LOGGING_
       
    85     const TSecureId currentSID( RProcess().SecureId() );
       
    86     CImpsClientLogger::Log(_L("Client Connect returns=0x%x currentSID=0x%x"), err, (TInt)currentSID);
       
    87 
       
    88     TBuf<256> name;
       
    89 	RProcess currentProcess;
       
    90     name.Append( RProcess().FullName() );
       
    91     CImpsClientLogger::Log( _L("Client CONNECT Process name: %S"), &name );
       
    92 #endif
       
    93     return err;
       
    94     }
       
    95 
       
    96 // ----------------------------------------------------------------------------
       
    97 // RImpsEng::Close
       
    98 // ----------------------------------------------------------------------------
       
    99 EXPORT_C void RImpsEng::Close()
       
   100     {  
       
   101 #ifndef _NO_IMPS_LOGGING_
       
   102     const TSecureId currentSID( RProcess().SecureId() );
       
   103     CImpsClientLogger::Log(_L("Client Close currentSID=0x%x"), (TInt)currentSID);
       
   104     
       
   105     TBuf<256> name;
       
   106 	RProcess currentProcess;
       
   107     name.Append( RProcess().FullName() );
       
   108     CImpsClientLogger::Log( _L("Client CLOSE Process name: %S"), &name );
       
   109 #endif
       
   110 
       
   111     iSessionOk = EFalse;
       
   112     RSessionBase::Close();   // deletes session object
       
   113     }
       
   114 
       
   115 // ----------------------------------------------------------------------------
       
   116 // RImpsEng::StartServer
       
   117 // ----------------------------------------------------------------------------
       
   118 TInt RImpsEng::StartServer( TInt aRate, TInt /*aRound*/ )
       
   119     {
       
   120 
       
   121 #ifndef _NO_IMPS_LOGGING_
       
   122     CImpsClientLogger::Log(_L("Client: StartServer rate=0x%x"), aRate);
       
   123 #endif
       
   124 
       
   125     TInt ret( KErrNone );
       
   126     TRequestStatus status  = KRequestPending;
       
   127     TImpsSignal signal( aRate );  
       
   128 
       
   129     if ( !IsServerStarted() )
       
   130         {
       
   131         // launch server process
       
   132         
       
   133 
       
   134         RProcess server;
       
   135 #ifndef _NO_IMPS_LOGGING_
       
   136         CImpsClientLogger::Log(_L("Client: PROCESS TO START"));
       
   137 #endif
       
   138 
       
   139         ret = server.Create( 
       
   140             KImpsServerExe,  
       
   141             signal.Get() );
       
   142             
       
   143         if ( ret )
       
   144             {
       
   145             // we are lost
       
   146 #ifndef _NO_IMPS_LOGGING_
       
   147             CImpsClientLogger::Log(_L("Client: process START FAILED"));
       
   148 #endif
       
   149             return ret;
       
   150             }
       
   151             
       
   152         
       
   153               
       
   154         server.Rendezvous( status );    	
       
   155         if ( status != KRequestPending )
       
   156             {            
       
   157             server.Kill(0);		// abort startup
       
   158             }
       
   159         else
       
   160             {          
       
   161             server.Resume();	// logon OK - start the server
       
   162             }
       
   163 #ifndef _NO_IMPS_LOGGING_
       
   164         CImpsClientLogger::Log(_L("Client: WaitForAnyRequest"));
       
   165 #endif		
       
   166         // wait for start or death 
       
   167         User::WaitForRequest( status );	
       
   168         
       
   169 #ifndef _NO_IMPS_LOGGING_
       
   170         CImpsClientLogger::Log(_L("WaitForRequest return=%d"), status.Int() );
       
   171 #endif        
       
   172         // we can't use the 'exit reason' if the server panicked as this
       
   173         // is the panic 'reason' and may be '0' which cannot be distinguished
       
   174         // from KErrNone
       
   175         ret = (server.ExitType() == EExitPanic) ? KErrGeneral : status.Int();
       
   176         server.Close();	
       
   177     	
       
   178         } // if !IsStarted
       
   179     
       
   180 #ifndef _NO_IMPS_LOGGING_
       
   181     CImpsClientLogger::Log(_L("StartServer return=0x%x"), ret);
       
   182 #endif
       
   183     return ret;
       
   184     }
       
   185 
       
   186 // ----------------------------------------------------------------------------
       
   187 // RImpsEng::IsServerStarted
       
   188 // ----------------------------------------------------------------------------
       
   189 TBool RImpsEng::IsServerStarted()
       
   190     {
       
   191     TFindServer findServer( KImpsServerName );
       
   192     TFullName name;
       
   193     return ( findServer.Next( name ) == KErrNone );
       
   194     }
       
   195 
       
   196 // ----------------------------------------------------------------------------
       
   197 // RImpsEng::Version()
       
   198 // Returns the client side version number.
       
   199 // ----------------------------------------------------------------------------
       
   200 TVersion RImpsEng::Version() const
       
   201     {
       
   202     return TVersion( KImpsServMajorVersionNumber, KImpsServMinorVersionNumber,
       
   203                      KImpsServBuildVersionNumber );
       
   204     }
       
   205 
       
   206 // ----------------------------------------------------------------------------
       
   207 // RImpsEng::CspIdentifier()
       
   208 // ----------------------------------------------------------------------------
       
   209 TImpsCspIdentifier* RImpsEng::CspIdentifier()
       
   210     {
       
   211     return &iCspId;
       
   212     }
       
   213     
       
   214 // ----------------------------------------------------------------------------
       
   215 // RImpsEng::SetCspIdentifier()
       
   216 // ----------------------------------------------------------------------------
       
   217 void RImpsEng::SetCspIdentifier( TImpsCspIdentifier aCspId )
       
   218     {
       
   219     TInt err ( KErrNone );
       
   220     TRAP ( err, iCspId.SetSapL( aCspId.Sap() ) );
       
   221     TRAP ( err, iCspId.SetUserIdL( aCspId.UserId() ) );
       
   222     }
       
   223     
       
   224 // ---------------------------------------------------------
       
   225 // RImpsClient2::RImpsClient2
       
   226 // ---------------------------------------------------------
       
   227 //
       
   228 EXPORT_C RImpsClient2::RImpsClient2() :
       
   229         iHandler( NULL ), 
       
   230         iActiveCommand( NULL ),
       
   231         iRunning( EFalse ),
       
   232         iOpId( 0 ),
       
   233         iLimitUpperValue( 0 ),
       
   234         iLimitLowerValue( 0 ),
       
   235         iEngine( NULL )
       
   236     {}
       
   237   
       
   238 // ---------------------------------------------------------
       
   239 // RImpsClient2::DoAssign
       
   240 // ---------------------------------------------------------
       
   241 //
       
   242 TInt RImpsClient2::DoAssign(
       
   243    const TDesC& aClientId )
       
   244    {
       
   245    TInt err = KErrNone;
       
   246    TPtrC myId;
       
   247    myId.Set( aClientId );
       
   248    err = SendReceive( EImpsServAssign, TIpcArgs( &myId ) );
       
   249    return err;
       
   250    }   
       
   251 
       
   252 // ---------------------------------------------------------
       
   253 // RImpsClient2::DoUnregister
       
   254 // SubSession Close
       
   255 // ---------------------------------------------------------
       
   256 //
       
   257 void RImpsClient2::DoUnregister()
       
   258     {
       
   259 
       
   260 #ifndef _NO_IMPS_LOGGING_
       
   261     CImpsClientLogger::Log(_L("Client: DoUnregister cli=%d"), (TInt)this);
       
   262 #endif
       
   263     if ( iRunning )
       
   264        {
       
   265        // Send cancel message to the server thread
       
   266        TInt srvRet = SendReceive( EImpsServCloseSub, TIpcArgs() );
       
   267 
       
   268        // Server completes pending next event request message for this subsession
       
   269        __ASSERT_DEBUG( iHandler, 
       
   270                        User::Panic( KImpsPanicCategory,
       
   271                                     EImpsNotifyHandlerMissing ) );
       
   272        if ( !srvRet && iHandler->IsActive() )
       
   273            {
       
   274 #ifndef _NO_IMPS_LOGGING_
       
   275         CImpsClientLogger::Log(_L("Client2: WaitForRequest START %d"), srvRet);
       
   276 #endif
       
   277            User::WaitForRequest( iHandler->iStatus );  
       
   278 
       
   279 #ifndef _NO_IMPS_LOGGING_
       
   280         CImpsClientLogger::Log(_L("Client2: WaitForRequest END"));
       
   281 #endif
       
   282            }
       
   283 
       
   284        iRunning = EFalse;
       
   285        RSubSessionBase::CloseSubSession( EImpsServDeleteSub );
       
   286        }
       
   287 
       
   288    // Cancel active objects before deleting them. 
       
   289    // Server is informed about cancel above so that it will not send data.
       
   290    if ( iHandler )
       
   291        {
       
   292        iHandler->Cancel();
       
   293        }
       
   294    if ( iActiveCommand )
       
   295        {
       
   296        iActiveCommand->Cancel();
       
   297        }
       
   298 
       
   299    // Now this is safe, i.e. no requests outstanding for the handler
       
   300    delete iHandler; 
       
   301    iHandler = NULL;
       
   302 
       
   303    delete iActiveCommand;
       
   304    iActiveCommand = NULL;
       
   305    }
       
   306 
       
   307 
       
   308 // ----------------------------------------------------------------------------
       
   309 // RImpsClient2::RegisterStatusObserverL
       
   310 // ----------------------------------------------------------------------------
       
   311 EXPORT_C void RImpsClient2::RegisterStatusObserverL( 
       
   312     MImpsStatusHandler2* aObserver )
       
   313     {
       
   314     if ( !iHandler )
       
   315         {
       
   316         // Register must have been done in derived client API!
       
   317         User::Leave( KImpsErrorNotRegistered );
       
   318         }
       
   319 
       
   320     if ( iHandler->StatusHandler() == NULL )
       
   321         {
       
   322         iHandler->SetStatusHandlerL( aObserver );
       
   323         }
       
   324     }
       
   325 
       
   326 // ----------------------------------------------------------------------------
       
   327 // RImpsClient2::UnregisterStatusObserverL
       
   328 // ----------------------------------------------------------------------------
       
   329 EXPORT_C void RImpsClient2::UnregisterStatusObserverL( )
       
   330     {
       
   331     // Check if null if somebody calls this after Unregister anyway ...
       
   332     if ( iHandler )
       
   333         {
       
   334         if ( iHandler->StatusHandler() != NULL )
       
   335             {
       
   336             iHandler->SetStatusHandlerL( NULL );
       
   337             }
       
   338         }
       
   339     }
       
   340 
       
   341 // ----------------------------------------------------------------------------
       
   342 // RImpsClient2::RegisterErrorObserverL
       
   343 // ----------------------------------------------------------------------------
       
   344 EXPORT_C void RImpsClient2::RegisterErrorObserverL( 
       
   345     MImpsErrorHandler2& aObserver )
       
   346     {
       
   347 
       
   348     if ( !iHandler )
       
   349         {
       
   350         // Register must have been done in derived client API!
       
   351         User::Leave( KImpsErrorNotRegistered );
       
   352         }
       
   353 
       
   354     if ( iHandler->ErrorHandler() == NULL )
       
   355         {
       
   356         iHandler->SetErrorHandlerL( &aObserver );
       
   357         }
       
   358     }
       
   359 
       
   360 // ----------------------------------------------------------------------------
       
   361 // RImpsClient2::UnregisterErrorObserverL
       
   362 // ----------------------------------------------------------------------------
       
   363 EXPORT_C void RImpsClient2::UnregisterErrorObserverL( )
       
   364     {
       
   365     // Check if null if somebody calls this after Unregister anyway ...
       
   366     if ( iHandler )
       
   367         {
       
   368         if ( iHandler->ErrorHandler() != NULL )
       
   369             {
       
   370             iHandler->SetErrorHandlerL( NULL );
       
   371             }
       
   372         }
       
   373     }
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // RImpsClient2::MaxTransactionContentLengthL
       
   377 // ----------------------------------------------------------------------------
       
   378 EXPORT_C TInt RImpsClient2::MaxTransactionContentLengthL( )
       
   379     {
       
   380     TInt ret =  SendReceive( EImpsServBuffSizeReq, TIpcArgs() );
       
   381     if ( ret < 0 )
       
   382         {
       
   383         User::Leave( ret );
       
   384         } 
       
   385     return ret;
       
   386     }
       
   387 
       
   388 // ----------------------------------------------------------------------------
       
   389 // RImpsClient2::ErrorHandlerL
       
   390 // ----------------------------------------------------------------------------
       
   391 MImpsErrorHandler2* RImpsClient2::ErrorHandler()
       
   392     {
       
   393     if ( iHandler )
       
   394         {
       
   395         return ( iHandler->ErrorHandler() );
       
   396         }
       
   397     return (MImpsErrorHandler2*)NULL;
       
   398     }
       
   399 
       
   400 // ----------------------------------------------------------------------------
       
   401 // RImpsClient2::GetServicesL
       
   402 // ----------------------------------------------------------------------------
       
   403 EXPORT_C void RImpsClient2::GetServicesL( TImpsServices& aServices )
       
   404     {
       
   405 #ifndef _NO_IMPS_LOGGING_
       
   406     CImpsClientLogger::Log(_L("Client2: GetServicesL starts"));
       
   407 #endif
       
   408 
       
   409     // result buffer
       
   410     TBuf<sizeof( TImpsServices ) + 4> myBuf( sizeof( TImpsServices ) + 4 );
       
   411 
       
   412     // Server will write the data to myservices
       
   413     TInt ret = SendReceive( EImpsServServices, TIpcArgs( &myBuf ) );
       
   414 #ifndef _NO_IMPS_LOGGING_
       
   415     CImpsClientLogger::Log(_L("Client2: GetServicesL SendReceive ret=%d"), ret );
       
   416 #endif
       
   417     User::LeaveIfError( ret );
       
   418 
       
   419     // convert descriptor to TImpsServices
       
   420     TInt32 tempSize = 0;
       
   421     const TUint8* textPtr = (const TUint8*)myBuf.Ptr();
       
   422     Mem::Copy( &tempSize, textPtr, sizeof( tempSize) );
       
   423 #ifndef _NO_IMPS_LOGGING_
       
   424     CImpsClientLogger::Log(_L("Client2: GetServicesL tempSize=%d"), tempSize);
       
   425 #endif
       
   426     textPtr = textPtr + sizeof( tempSize );
       
   427     // copy result to the call parameter
       
   428     Mem::Copy( (void*)&aServices, textPtr, tempSize );
       
   429 #ifndef _NO_IMPS_LOGGING_
       
   430     CImpsClientLogger::Log(_L("Client2: GetServicesL ends Ok"));
       
   431 #endif
       
   432 
       
   433     }
       
   434 
       
   435 // ----------------------------------------------------------------------------
       
   436 // RImpsClient2::WVVersionL
       
   437 // ----------------------------------------------------------------------------
       
   438 
       
   439 EXPORT_C TPtrC RImpsClient2::WVVersionL()
       
   440     {
       
   441     TInt ret =  SendReceive( EImpsServCspVersion, TIpcArgs() );
       
   442     if ( ret == EImpsCspVersion12 )
       
   443         {
       
   444         return TPtrC( KImpsVersionStr );
       
   445         } 
       
   446     return TPtrC( KImpsVersionStr12 );
       
   447     } 
       
   448 
       
   449 // ----------------------------------------------------------------------------
       
   450 // RImpsClient2::CspIdentifierL( )
       
   451 // ----------------------------------------------------------------------------
       
   452 EXPORT_C TImpsCspIdentifier* RImpsClient2::CspIdentifierL( )
       
   453     {
       
   454     TInt ret =  SendReceive( EImpsServIsLogged, TIpcArgs() );
       
   455     if ( ret < 0 )
       
   456         {
       
   457         User::Leave( KErrNotFound );
       
   458         } 
       
   459     return iEngine->CspIdentifier();
       
   460     }
       
   461 
       
   462 // ----------------------------------------------------------------------------
       
   463 // RImpsClient2::CancelTransaction
       
   464 // ----------------------------------------------------------------------------
       
   465 EXPORT_C void RImpsClient2::CancelTransaction( TInt aOperationId )
       
   466     {
       
   467     TInt ret = SendReceive( EImpsServCancelTrans, TIpcArgs( aOperationId ) );
       
   468 #ifndef _NO_IMPS_LOGGING_
       
   469     CImpsClientLogger::Log(_L("RImpsClient2::CancelTransaction opId=%d ret=%d"), aOperationId, ret );
       
   470 #endif
       
   471     }    
       
   472 
       
   473 // ----------------------------------------------------------------------------
       
   474 // RImpsClient2::SetExpiryTime
       
   475 // ----------------------------------------------------------------------------
       
   476 EXPORT_C void RImpsClient2::SetExpiryTime( TInt aSeconds )
       
   477     {
       
   478     TInt ret = SendReceive( EImpsServSetExpiry, TIpcArgs( aSeconds ) );
       
   479 #ifndef _NO_IMPS_LOGGING_
       
   480     CImpsClientLogger::Log(_L("RImpsClient2::SetExpiryTime time=%d ret=%d"), aSeconds, ret );
       
   481 #endif
       
   482     }
       
   483     
       
   484 // ----------------------------------------------------------------------------
       
   485 // RImpsClient2::IncreaseOpId
       
   486 // ----------------------------------------------------------------------------
       
   487 void RImpsClient2::IncreaseOpId( )
       
   488     {
       
   489     iOpId++;
       
   490     if( iOpId >= iLimitUpperValue )
       
   491         {
       
   492         iOpId = iLimitLowerValue;
       
   493         }
       
   494     }
       
   495 
       
   496 // ----------------------------------------------------------------------------
       
   497 // RImpsClient2::SetOpIdRange
       
   498 // ----------------------------------------------------------------------------
       
   499 void RImpsClient2::SetOpIdRange( )
       
   500     {
       
   501     iLimitLowerValue = REINTERPRET_CAST(TInt, Dll::Tls() );
       
   502     iLimitUpperValue = iLimitLowerValue + 100;
       
   503     // Check that counter does not go around
       
   504         if ( iLimitUpperValue & KImpsOOMReply )
       
   505         {
       
   506         // start from 50, if this ever happens
       
   507         iLimitLowerValue = 50;
       
   508         iLimitUpperValue = iLimitLowerValue + 100;
       
   509         }
       
   510     Dll::SetTls( REINTERPRET_CAST(TAny*, iLimitUpperValue ) );
       
   511     iOpId = iLimitLowerValue;
       
   512     }
       
   513 
       
   514 // ----------------------------------------------------------------------------
       
   515 // RImpsClient2::DoRegister2
       
   516 // ----------------------------------------------------------------------------
       
   517 TInt RImpsClient2::DoRegister2( 
       
   518     RImpsEng& aEngine, CImpsHandler2* aHandler, TInt aReq )
       
   519     {
       
   520 #ifndef _NO_IMPS_LOGGING_
       
   521     CImpsClientLogger::Log(_L("Client2: DoRegister2 cli=%d"), (TInt)this);
       
   522 #endif
       
   523 
       
   524     iEngine = &aEngine;
       
   525     // add bit mask for extra info (handle-new and any-content)
       
   526     TInt srvReg = aReq;
       
   527     TInt bits = 0;
       
   528     bits = ( bits & ~KImpsReqHandleNew ) 
       
   529             | ( iHandleNew ? KImpsReqHandleNew : 0x0000 );
       
   530     bits = ( bits & ~KImpsReqAnyContent ) 
       
   531             | ( iAnyContent ? KImpsReqAnyContent : 0x0000 );
       
   532     bits = bits << 16;
       
   533     srvReg = srvReg | bits;
       
   534 
       
   535     TImpsServRequest req = (TImpsServRequest)srvReg;
       
   536     TInt err = CreateSubSession( aEngine, req );
       
   537     if ( !err )
       
   538         {
       
   539         iRunning = ETrue;
       
   540         aHandler->EventHandled();       
       
   541         }
       
   542     return err;
       
   543     }
       
   544 
       
   545 // ----------------------------------------------------------------------------
       
   546 // RImpsClient2::CspIdentifier( )
       
   547 // ----------------------------------------------------------------------------
       
   548 TImpsCspIdentifier* RImpsClient2::CspIdentifier( )
       
   549     {
       
   550     return iEngine->CspIdentifier();
       
   551     }    
       
   552 
       
   553 
       
   554 //  End of File  
       
   555