brandingserver/BSClient/bsprocessstarter.cpp
changeset 31 9dbc70490d9a
equal deleted inserted replaced
30:1fa9b890f29c 31:9dbc70490d9a
       
     1 /*
       
     2 * Copyright (c) 2009 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 the License "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:  bsprocessstarter.cpp
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDE FILES
       
    19 #include "bsprocessstarter.h"
       
    20 #include <e32std.h>
       
    21 #include <f32file.h>
       
    22 
       
    23 
       
    24 // CONSTANTS
       
    25 _LIT( KEka2ExeDir,"\\sys\\bin\\");
       
    26 _LIT( KEka2LaunchMutexExt, "[lMtx]" );
       
    27 const TInt KEka2SrvConnTries = 7;
       
    28 const TInt KEka2SrvConnInitialRetryWait = 500; //MicroSeconds => 0.0005s
       
    29 
       
    30 
       
    31 
       
    32 // ==============================================================
       
    33 // ====================== HELPER CLASS ==========================
       
    34 // ==============================================================
       
    35 
       
    36 /**
       
    37  * RSessionBase accessor to give to the ProcessStarter
       
    38  * access to RSessionBase::CreateSession().
       
    39  */
       
    40 class REka2SessionBaseAccessor : public RSessionBase
       
    41     {
       
    42     public: // Constructor
       
    43         inline REka2SessionBaseAccessor()
       
    44             {
       
    45             }
       
    46 
       
    47     public: // New functions
       
    48 
       
    49         /**
       
    50          * Public access to RSessionBase::CreateSession().
       
    51          */
       
    52         inline TInt CreateSession( const TDesC& aServer,
       
    53                                    const TVersion& aVersion,
       
    54                                    TInt aAsyncMessageSlots )
       
    55             {
       
    56             return RSessionBase::CreateSession( aServer,
       
    57                                                 aVersion,
       
    58                                                 aAsyncMessageSlots );
       
    59             }
       
    60     };
       
    61 
       
    62 
       
    63 
       
    64 
       
    65 
       
    66 // ==============================================================
       
    67 // ====================== PROCESSSTARTER ========================
       
    68 // ==============================================================
       
    69 
       
    70 
       
    71 // --------------------------------------------------------------
       
    72 // BSProcessStarter::FullExePathForClienLocation()
       
    73 // --------------------------------------------------------------
       
    74 //
       
    75 void BSProcessStarter::FullExePathForClienLocation(
       
    76     const TDesC& aExeName,
       
    77     TFileName& aFullExePath )
       
    78     {
       
    79     //Get drive (C:) where this client code is installed
       
    80         {
       
    81         TFileName tmp;
       
    82         Dll::FileName( tmp );
       
    83         aFullExePath.Copy( TParsePtrC( tmp ).Drive() );
       
    84         }
       
    85 
       
    86     //Build the rest from the exe path
       
    87     aFullExePath.Append( KEka2ExeDir );
       
    88     aFullExePath.Append( aExeName );
       
    89     }
       
    90 
       
    91 
       
    92 
       
    93 // --------------------------------------------------------------
       
    94 // BSProcessStarter::StartInstance()
       
    95 // --------------------------------------------------------------
       
    96 //
       
    97 TInt BSProcessStarter::StartInstance(
       
    98     const TDesC& aFullExePath,
       
    99     const TDesC& aCommand,
       
   100     const TArray< TProcessStartupParam >* aParams )
       
   101     {
       
   102     RMutex launchMutex;
       
   103     TInt error = KErrNotFound;
       
   104 
       
   105         {
       
   106         // Dynamic mutex name used to allow code share
       
   107         TName launchMutexName( TParsePtrC( aFullExePath ).Name() );
       
   108         launchMutexName.Append( KEka2LaunchMutexExt );
       
   109 
       
   110         // Open or Create mutex to serialize to access to server startup code.
       
   111         // (race condition safe way)
       
   112         while( error == KErrNotFound )
       
   113             {
       
   114             error = launchMutex.CreateGlobal( launchMutexName );
       
   115             if( error != KErrAlreadyExists )
       
   116                 {
       
   117                 break;
       
   118                 }
       
   119             error = launchMutex.OpenGlobal( launchMutexName );
       
   120             }
       
   121 
       
   122         if( error != KErrNone )
       
   123             {
       
   124             return error;
       
   125             }
       
   126         }
       
   127 
       
   128 
       
   129     launchMutex.Wait();
       
   130 
       
   131     //Serialized access
       
   132     error = BSProcessStarter::DoStartInstance( aFullExePath,
       
   133                                              aCommand,
       
   134                                              aParams );
       
   135 
       
   136     launchMutex.Signal();
       
   137     launchMutex.Close();
       
   138 
       
   139     return error;
       
   140     }
       
   141 
       
   142 
       
   143 
       
   144 // --------------------------------------------------------------
       
   145 // BSProcessStarter::ConnectToServer()
       
   146 // --------------------------------------------------------------
       
   147 //
       
   148 TInt BSProcessStarter::ConnectToServer(
       
   149     const TDesC& aFullExePath,
       
   150     const TDesC& aCommand,
       
   151     const TArray< TProcessStartupParam >* aParams,
       
   152     RSessionBase& aSessionToConnect,
       
   153     const TDesC& aServerName,
       
   154     const TVersion& aClientVersion,
       
   155     TInt aAsyncMessageSlots )
       
   156     {
       
   157     if( aSessionToConnect.Handle() != KNullHandle )
       
   158         {
       
   159         return KErrInUse;
       
   160         }
       
   161 
       
   162     TInt err = KErrNone;
       
   163     TInt startupWait = KEka2SrvConnInitialRetryWait;
       
   164 
       
   165     //Server connect and launch loop
       
   166     for( TInt trie = 0 ; trie < KEka2SrvConnTries ; trie++ )
       
   167         {
       
   168         REka2SessionBaseAccessor acc;
       
   169         err = acc.CreateSession( aServerName,
       
   170                                  aClientVersion,
       
   171                                  aAsyncMessageSlots );
       
   172 
       
   173         if( err == KErrNone )
       
   174             {
       
   175             //session ownership is now on client
       
   176             aSessionToConnect = acc;
       
   177             return KErrNone;
       
   178             }
       
   179 
       
   180         else if( ( err == KErrNotFound ) ||
       
   181                  ( err == KErrServerTerminated ) )
       
   182             {
       
   183             //Server missing or died when connecting
       
   184             //Start a new server
       
   185             err = BSProcessStarter::StartInstance( aFullExePath,
       
   186                                                  aCommand,
       
   187                                                  aParams );
       
   188 
       
   189             //If process exist already, then all is fine
       
   190             //(some other process started it between the origical connect and launch trie)
       
   191             if( err == KErrAlreadyExists )
       
   192                 {
       
   193                 err = KErrNone;
       
   194                 }
       
   195 
       
   196             //If server process start failed, bail out.
       
   197             if( err != KErrNone )
       
   198                 {
       
   199                 return err;
       
   200                 }
       
   201 
       
   202             //If this is 2nd or subsequent try,
       
   203             //give some time for server to startup
       
   204             if( trie > 0 )
       
   205                 {
       
   206                 // Code scanner warning : Use of User::After (id:92)
       
   207                 // it is required to be used here
       
   208                 User::After( startupWait ); // CSI: 92 # See above
       
   209                 startupWait = 2 * startupWait;
       
   210                 }
       
   211             }
       
   212 
       
   213         else
       
   214             {
       
   215             //Server process start failed. Bail out.
       
   216             return err;
       
   217             }
       
   218         }
       
   219 
       
   220     return err;
       
   221     }
       
   222 
       
   223 
       
   224 
       
   225 // --------------------------------------------------------------
       
   226 // BSProcessStarter::DoStartServerInstance()
       
   227 // --------------------------------------------------------------
       
   228 //
       
   229 TInt BSProcessStarter::DoStartInstance(
       
   230     const TDesC& aFullExePath,
       
   231     const TDesC& aCommand,
       
   232     const TArray< TProcessStartupParam >* aParams )
       
   233     {
       
   234     TInt error = KErrNone;
       
   235 
       
   236     //Create process
       
   237     RProcess process;
       
   238     error = process.Create( aFullExePath, aCommand );
       
   239 
       
   240     if( error == KErrNone )
       
   241         {
       
   242         //Set process startup parameters
       
   243         error = ApplyParameters( aParams, process );
       
   244 
       
   245         //And execute the process and wait it's startup
       
   246        if( error == KErrNone )
       
   247             {
       
   248             TRequestStatus rendezvousStatus;
       
   249             process.Rendezvous( rendezvousStatus );
       
   250 
       
   251             process.Resume();
       
   252             // Codescanner warning: user of User::WaitForRequest (Id:94)
       
   253             // it is required to use at server startup
       
   254             User::WaitForRequest( rendezvousStatus ); // CSI: 94 # See above
       
   255             error = rendezvousStatus.Int();
       
   256 
       
   257             if( process.ExitType() != EExitPending )
       
   258                 {
       
   259                 //Something failed in server startup
       
   260                 //Force the error code to be always something
       
   261                 //else than KErrNone
       
   262                 if( error == KErrNone )
       
   263                     {
       
   264                     error = KErrServerTerminated;
       
   265                     }
       
   266                 }
       
   267             }
       
   268         }
       
   269 
       
   270     process.Close();
       
   271 
       
   272     return error;
       
   273     }
       
   274 
       
   275 
       
   276 
       
   277 // --------------------------------------------------------------
       
   278 // BSProcessStarter::ApplyParameters()
       
   279 // --------------------------------------------------------------
       
   280 //
       
   281 TInt BSProcessStarter::ApplyParameters(
       
   282     const TArray< TProcessStartupParam >* aParams,
       
   283     RProcess& aProcess )
       
   284     {
       
   285     TInt error = KErrNone;
       
   286     if( aParams )
       
   287         {
       
   288         const TInt paramCount = aParams->Count();
       
   289         for( TInt ix = 0; ix < paramCount; ix++ )
       
   290             {
       
   291             error = (*aParams)[ ix ].ApplyParam( aProcess );
       
   292             if( error != KErrNone )
       
   293                 {
       
   294                 break;
       
   295                 }
       
   296             }
       
   297         }
       
   298 
       
   299     return error;
       
   300     }
       
   301 
       
   302 
       
   303 
       
   304 // ==============================================================
       
   305 // ================== TPROCESSSTARTUPPARAM ======================
       
   306 // ==============================================================
       
   307 
       
   308 // --------------------------------------------------------------
       
   309 // TProcessStartupParam::TProcessStartupParam()
       
   310 // --------------------------------------------------------------
       
   311 //
       
   312 TProcessStartupParam::TProcessStartupParam()
       
   313     : iType( EUnknown ),
       
   314       iSlot( KErrNotFound )
       
   315     {
       
   316     }
       
   317 
       
   318 
       
   319 // --------------------------------------------------------------
       
   320 // TProcessStartupParam::Set()
       
   321 // --------------------------------------------------------------
       
   322 //
       
   323 void TProcessStartupParam::Set( TInt aSlot,
       
   324                                 const RHandleBase& aHandle )
       
   325     {
       
   326     iType = EHandle;
       
   327     iSlot = aSlot;
       
   328     iHandle = &aHandle;
       
   329     }
       
   330 
       
   331 
       
   332 // --------------------------------------------------------------
       
   333 // TProcessStartupParam::Set()
       
   334 // --------------------------------------------------------------
       
   335 //
       
   336 void TProcessStartupParam::Set( TInt aSlot,
       
   337                                 const RSubSessionBase& aSubSession )
       
   338     {
       
   339     iType = ESubSession;
       
   340     iSlot = aSlot;
       
   341     iSubSession = &aSubSession;
       
   342     }
       
   343 
       
   344 
       
   345 // --------------------------------------------------------------
       
   346 // TProcessStartupParam::Set()
       
   347 // --------------------------------------------------------------
       
   348 //
       
   349 void TProcessStartupParam::Set( TInt aSlot,
       
   350                                 const TDesC16& aDes )
       
   351     {
       
   352     iType = EDes16;
       
   353     iSlot = aSlot;
       
   354     iDes16.Set( aDes );
       
   355     }
       
   356 
       
   357 
       
   358 // --------------------------------------------------------------
       
   359 // TProcessStartupParam::Set()
       
   360 // --------------------------------------------------------------
       
   361 //
       
   362 void TProcessStartupParam::Set( TInt aSlot,
       
   363                                 const TDesC8& aDes )
       
   364     {
       
   365     iType = EDes8;
       
   366     iSlot = aSlot;
       
   367     iDes8.Set( aDes );
       
   368     }
       
   369 
       
   370 
       
   371 // --------------------------------------------------------------
       
   372 // TProcessStartupParam::Set()
       
   373 // --------------------------------------------------------------
       
   374 //
       
   375 void TProcessStartupParam::Set( TInt aSlot,
       
   376                                 TInt aInt )
       
   377     {
       
   378     iType = EInt;
       
   379     iSlot = aSlot;
       
   380     iInt = aInt;
       
   381     }
       
   382 
       
   383 
       
   384 // --------------------------------------------------------------
       
   385 // TProcessStartupParam::ApplyParam()
       
   386 // --------------------------------------------------------------
       
   387 //
       
   388 TInt TProcessStartupParam::ApplyParam( RProcess& aProcess ) const
       
   389     {
       
   390     switch( iType )
       
   391         {
       
   392         case EHandle: return aProcess.SetParameter( iSlot, *iHandle );
       
   393         case ESubSession: return aProcess.SetParameter( iSlot, *iSubSession );
       
   394         case EDes16: return aProcess.SetParameter( iSlot, iDes16 );
       
   395         case EDes8: return aProcess.SetParameter( iSlot, iDes8 );
       
   396         case EInt: return aProcess.SetParameter( iSlot, iInt );
       
   397         }
       
   398 
       
   399     return KErrUnknown;
       
   400     }
       
   401 
       
   402 
       
   403 // END OF FILE
       
   404 
       
   405 
       
   406