imservices/instantmessagingcache/imcacheclient/src/imcacheprocessstarter.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 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:  start the server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "imcacheprocessstarter.h"
       
    20 // logs
       
    21 #include "imcachedebugtrace.h"
       
    22 // system includes
       
    23 #include <e32std.h>
       
    24 #include <f32file.h>
       
    25 
       
    26 
       
    27 // CONSTANTS
       
    28 _LIT( KEka2ExeDir,"\\sys\\bin\\");
       
    29 _LIT( KEka2LaunchMutexExt, "[lMtx]" );
       
    30 const TInt KEka2SrvConnTries = 7;
       
    31 const TInt KEka2SrvConnInitialRetryWait = 500; //MicroSeconds => 0.0005s
       
    32 
       
    33 
       
    34 // ==============================================================
       
    35 // ====================== HELPER CLASS ==========================
       
    36 // ==============================================================
       
    37 
       
    38 /**
       
    39  * RSessionBase accessor to give to the ProcessStarter
       
    40  * access to RSessionBase::CreateSession().
       
    41  */
       
    42 class REka2SessionBaseAccessor : public RSessionBase
       
    43     {
       
    44     public: // Constructor
       
    45         inline REka2SessionBaseAccessor()
       
    46             {
       
    47             }
       
    48 
       
    49     public: // New functions
       
    50 
       
    51         /**
       
    52          * Public access to RSessionBase::CreateSession().
       
    53          */
       
    54         inline TInt CreateSession( const TDesC& aServer,
       
    55                                    const TVersion& aVersion,
       
    56                                    TInt aAsyncMessageSlots )
       
    57             {
       
    58             return RSessionBase::CreateSession( aServer,
       
    59                                                 aVersion,
       
    60                                                 aAsyncMessageSlots );
       
    61             }
       
    62     };
       
    63 
       
    64 
       
    65 // ==============================================================
       
    66 // ====================== PROCESSSTARTER ========================
       
    67 // ==============================================================
       
    68 
       
    69 // --------------------------------------------------------------
       
    70 // IMCacheProcessStarter::FullExePathForClientLocation()
       
    71 // --------------------------------------------------------------
       
    72 //
       
    73 void IMCacheProcessStarter::FullExePathForClientLocation(
       
    74     const TDesC& aExeName,
       
    75     TFileName& aFullExePath )
       
    76     {
       
    77 	TRACE( T_LIT("IMCacheProcessStarter::FullExePathForClientLocation begin") );
       
    78     //Get drive (C:) where this client code is installed
       
    79         {
       
    80         TFileName tmp;
       
    81         Dll::FileName( tmp );
       
    82         aFullExePath.Copy( TParsePtrC( tmp ).Drive() );
       
    83         }
       
    84 
       
    85     //Build the rest from the exe path
       
    86     aFullExePath.Append( KEka2ExeDir );
       
    87     aFullExePath.Append( aExeName );
       
    88     TRACE( T_LIT("IMCacheProcessStarter::FullExePathForClientLocation end") );
       
    89     }
       
    90 
       
    91 // --------------------------------------------------------------
       
    92 // IMCacheProcessStarter::StartInstance()
       
    93 // --------------------------------------------------------------
       
    94 //
       
    95 TInt IMCacheProcessStarter::StartInstance(
       
    96     const TDesC& aFullExePath,
       
    97     const TDesC& aCommand )
       
    98     {
       
    99     TRACE( T_LIT("IMCacheProcessStarter::StartInstance begin") );
       
   100     RMutex launchMutex;
       
   101     TInt error = KErrNotFound;
       
   102 
       
   103         {
       
   104         // Dynamic mutex name used to allow code share
       
   105         TName launchMutexName( TParsePtrC( aFullExePath ).Name() );
       
   106         launchMutexName.Append( KEka2LaunchMutexExt );
       
   107 
       
   108         // Open or Create mutex to serialize to access to server startup code.
       
   109         // (race condition safe way)
       
   110         while( error == KErrNotFound )
       
   111             {
       
   112             error = launchMutex.CreateGlobal( launchMutexName );
       
   113             if( error != KErrAlreadyExists )
       
   114                 {
       
   115                 break;
       
   116                 }
       
   117             error = launchMutex.OpenGlobal( launchMutexName );
       
   118             }
       
   119 
       
   120         if( error != KErrNone )
       
   121             {
       
   122             TRACE( T_LIT("IMCacheProcessStarter::StartInstance end") );
       
   123             return error;
       
   124             }
       
   125         }
       
   126 
       
   127 
       
   128     launchMutex.Wait();
       
   129 
       
   130     //Serialized access
       
   131     error = IMCacheProcessStarter::DoStartInstance( aFullExePath,
       
   132                                              aCommand );
       
   133 
       
   134     launchMutex.Signal();
       
   135     launchMutex.Close();
       
   136 	TRACE( T_LIT("IMCacheProcessStarter::StartInstance end") );
       
   137     return error;
       
   138     }
       
   139 
       
   140 // --------------------------------------------------------------
       
   141 // IMCacheProcessStarter::ConnectToServer()
       
   142 // --------------------------------------------------------------
       
   143 //
       
   144 TInt IMCacheProcessStarter::ConnectToServer(
       
   145     const TDesC& aFullExePath,
       
   146     const TDesC& aCommand,
       
   147     RSessionBase& aSessionToConnect,
       
   148     const TDesC& aServerName,
       
   149     const TVersion& aClientVersion,
       
   150     TInt aAsyncMessageSlots )
       
   151     {
       
   152     TRACE( T_LIT("IMCacheProcessStarter::ConnectToServer begin") );
       
   153     if( aSessionToConnect.Handle() != KNullHandle )
       
   154         {
       
   155         return KErrInUse;
       
   156         }
       
   157 
       
   158     TInt err = KErrNone;
       
   159     TInt startupWait = KEka2SrvConnInitialRetryWait;
       
   160 
       
   161     //Server connect and launch loop
       
   162     for( TInt trie = 0 ; trie < KEka2SrvConnTries ; trie++ )
       
   163         {
       
   164         REka2SessionBaseAccessor acc;
       
   165         err = acc.CreateSession( aServerName,
       
   166                                  aClientVersion,
       
   167                                  aAsyncMessageSlots );
       
   168 
       
   169         if( err == KErrNone )
       
   170             {
       
   171             //session ownership is now on client
       
   172             aSessionToConnect = acc;
       
   173             return KErrNone;
       
   174             }
       
   175 
       
   176         else if( ( err == KErrNotFound ) ||
       
   177                  ( err == KErrServerTerminated ) )
       
   178             {
       
   179             //Server missing or died when connecting
       
   180             //Start a new server
       
   181             err = IMCacheProcessStarter::StartInstance( aFullExePath,
       
   182                                                  aCommand );
       
   183 
       
   184             //If process exist already, then all is fine
       
   185             //(some other process started it between the origical connect and launch trie)
       
   186             if( err == KErrAlreadyExists )
       
   187                 {
       
   188                 err = KErrNone;
       
   189                 }
       
   190 
       
   191             //If server process start failed, bail out.
       
   192             if( err != KErrNone )
       
   193                 {
       
   194                 return err;
       
   195                 }
       
   196 
       
   197             //If this is 2nd or subsequent try,
       
   198             //give some time for server to startup
       
   199             if( trie > 0 )
       
   200                 {
       
   201                 // Code scanner warning : Use of User::After (id:92)
       
   202                 // it is required to be used here
       
   203                 User::After( startupWait ); // CSI: 92 # See above
       
   204                 startupWait = 2 * startupWait;
       
   205                 }
       
   206             }
       
   207 
       
   208         else
       
   209             {
       
   210             //Server process start failed. Bail out.
       
   211             return err;
       
   212             }
       
   213         }
       
   214 	TRACE( T_LIT("IMCacheProcessStarter::ConnectToServer end") );
       
   215     return err;
       
   216     }
       
   217 
       
   218 // --------------------------------------------------------------
       
   219 // IMCacheProcessStarter::DoStartServerInstance()
       
   220 // --------------------------------------------------------------
       
   221 //
       
   222 TInt IMCacheProcessStarter::DoStartInstance(
       
   223     const TDesC& aFullExePath,
       
   224     const TDesC& aCommand )
       
   225     {
       
   226     TRACE( T_LIT("IMCacheProcessStarter::DoStartInstance begin") );
       
   227     TInt error = KErrNone;
       
   228 
       
   229     //Create process
       
   230     RProcess process;
       
   231     error = process.Create( aFullExePath, aCommand );
       
   232 
       
   233     if( error == KErrNone )
       
   234         {
       
   235        
       
   236             TRequestStatus rendezvousStatus;
       
   237             process.Rendezvous( rendezvousStatus );
       
   238 
       
   239             process.Resume();
       
   240             // Codescanner warning: user of User::WaitForRequest (Id:94)
       
   241             // it is required to use at server startup
       
   242             User::WaitForRequest( rendezvousStatus ); // CSI: 94 # See above
       
   243             error = rendezvousStatus.Int();
       
   244 
       
   245             if( process.ExitType() != EExitPending )
       
   246                 {
       
   247                 //Something failed in server startup
       
   248                 //Force the error code to be always something
       
   249                 //else than KErrNone
       
   250                 if( error == KErrNone )
       
   251                     {
       
   252                     error = KErrServerTerminated;
       
   253                     }
       
   254                 }
       
   255            
       
   256         }
       
   257 
       
   258     process.Close();
       
   259 	TRACE( T_LIT("IMCacheProcessStarter::DoStartInstance end") );
       
   260     return error;
       
   261     }
       
   262 
       
   263 // END OF FILE
       
   264 
       
   265 
       
   266