ximpfw/core/srcclient/ximpprocessstarter.cpp
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     1 /*
       
     2 * Copyright (c) 2006 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:  XIMP Framework process starter implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include "ximpprocessstarter.h"
       
    21 #include <e32std.h>
       
    22 #include <f32file.h>
       
    23 
       
    24 
       
    25 // CONSTANTS
       
    26 _LIT( KEka2ExeDir,"\\sys\\bin\\");
       
    27 _LIT( KEka2LaunchMutexExt, "[lMtx]" );
       
    28 const TInt KEka2SrvConnTries = 7;               // Try to start 7 times
       
    29                                                 // before giving up
       
    30 const TInt KEka2SrvConnInitialRetryWait = 500;  // MicroSeconds => 0.0005s
       
    31 
       
    32 
       
    33 
       
    34 // ==============================================================
       
    35 // ====================== HELPER CLASS ==========================
       
    36 // ==============================================================
       
    37 
       
    38 /**
       
    39  * RSessionBase accessor to give to the XIMPProcessStarter
       
    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 // --------------------------------------------------------------
       
    71 // XIMPProcessStarter::FullExePathForClienLocation()
       
    72 // --------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C void XIMPProcessStarter::FullExePathForClienLocation(
       
    75     const TDesC& aExeName,
       
    76     TFileName& aFullExePath )
       
    77     {
       
    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     }
       
    89 
       
    90 
       
    91 // --------------------------------------------------------------
       
    92 // XIMPProcessStarter::StartInstance()
       
    93 // --------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C TInt XIMPProcessStarter::StartInstance(
       
    96     const TDesC& aFullExePath,
       
    97     const TDesC& aCommand,
       
    98     const TArray< TXIMPProcessStartupParam >* aParams )
       
    99     {
       
   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             return error;
       
   123             }
       
   124         }
       
   125 
       
   126 
       
   127     launchMutex.Wait();
       
   128 
       
   129     //Serialized access
       
   130     error = XIMPProcessStarter::DoStartInstance( aFullExePath,
       
   131                                              aCommand,
       
   132                                              aParams );
       
   133 
       
   134     launchMutex.Signal();
       
   135     launchMutex.Close();
       
   136 
       
   137     return error;
       
   138     }
       
   139 
       
   140 
       
   141 // --------------------------------------------------------------
       
   142 // XIMPProcessStarter::ConnectToServer()
       
   143 // --------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C TInt XIMPProcessStarter::ConnectToServer(
       
   146     const TDesC& aFullExePath,
       
   147     const TDesC& aCommand,
       
   148     const TArray< TXIMPProcessStartupParam >* aParams,
       
   149     RSessionBase& aSessionToConnect,
       
   150     const TDesC& aServerName,
       
   151     const TVersion& aClientVersion,
       
   152     TInt aAsyncMessageSlots )
       
   153     {
       
   154     if( aSessionToConnect.Handle() != KNullHandle )
       
   155         {
       
   156         return KErrInUse;
       
   157         }
       
   158 
       
   159     TInt err = KErrNone;
       
   160     TInt startupWait = KEka2SrvConnInitialRetryWait;
       
   161 
       
   162     //Server connect and launch loop
       
   163     for( TInt trie = 0 ; trie < KEka2SrvConnTries ; trie++ )
       
   164         {
       
   165         REka2SessionBaseAccessor acc;
       
   166         err = acc.CreateSession( aServerName,
       
   167                                  aClientVersion,
       
   168                                  aAsyncMessageSlots );
       
   169 
       
   170         if( err == KErrNone )
       
   171             {
       
   172             //session ownership is now on client
       
   173             aSessionToConnect = acc;
       
   174             return KErrNone;
       
   175             }
       
   176 
       
   177         else if( ( err == KErrNotFound ) ||
       
   178                  ( err == KErrServerTerminated ) )
       
   179             {
       
   180             //Server missing or died when connecting
       
   181             //Start a new server
       
   182             err = XIMPProcessStarter::StartInstance( aFullExePath,
       
   183                                                      aCommand,
       
   184                                                      aParams );
       
   185 
       
   186             //If process exist already, then all is fine
       
   187             //(some other process started it between the origical connect and launch trie)
       
   188             if( err == KErrAlreadyExists )
       
   189                 {
       
   190                 err = KErrNone;
       
   191                 }
       
   192 
       
   193             //If server process start failed, bail out.
       
   194             if( err != KErrNone )
       
   195                 {
       
   196                 return err;
       
   197                 }
       
   198 
       
   199             //If this is 2nd or subsequent try,
       
   200             //give some time for server to startup
       
   201             if( trie > 0 )
       
   202                 {
       
   203                 User::After( startupWait ); // CSI: 92 #
       
   204                 startupWait = 2 * startupWait;
       
   205                 }
       
   206             }
       
   207 
       
   208         else
       
   209             {
       
   210             //Server process start failed. Bail out.
       
   211             return err;
       
   212             }
       
   213         }
       
   214 
       
   215     return err;
       
   216     }
       
   217 
       
   218 
       
   219 // --------------------------------------------------------------
       
   220 // XIMPProcessStarter::DoStartServerInstance()
       
   221 // --------------------------------------------------------------
       
   222 //
       
   223 TInt XIMPProcessStarter::DoStartInstance(
       
   224     const TDesC& aFullExePath,
       
   225     const TDesC& aCommand,
       
   226     const TArray< TXIMPProcessStartupParam >* aParams )
       
   227     {
       
   228     TInt error = KErrNone;
       
   229 
       
   230     //Create process
       
   231     RProcess process;
       
   232     error = process.Create( aFullExePath, aCommand );
       
   233 
       
   234     if( error == KErrNone )
       
   235         {
       
   236         //Set process startup parameters
       
   237         error = ApplyParameters( aParams, process );
       
   238 
       
   239         //And execute the process and wait it's startup
       
   240        if( error == KErrNone )
       
   241             {
       
   242             TRequestStatus rendezvousStatus;
       
   243             process.Rendezvous( rendezvousStatus );
       
   244 
       
   245             process.Resume();
       
   246             User::WaitForRequest( rendezvousStatus ); // CSI: 94 #
       
   247             error = rendezvousStatus.Int();
       
   248 
       
   249             if( process.ExitType() != EExitPending )
       
   250                 {
       
   251                 //Something failed in server startup
       
   252                 //Force the error code to be always something
       
   253                 //else than KErrNone
       
   254                 if( error == KErrNone )
       
   255                     {
       
   256                     error = KErrServerTerminated;
       
   257                     }
       
   258                 }
       
   259             }
       
   260         }
       
   261 
       
   262     process.Close();
       
   263 
       
   264     return error;
       
   265     }
       
   266 
       
   267 
       
   268 // --------------------------------------------------------------
       
   269 // XIMPProcessStarter::ApplyParameters()
       
   270 // --------------------------------------------------------------
       
   271 //
       
   272 TInt XIMPProcessStarter::ApplyParameters(
       
   273     const TArray< TXIMPProcessStartupParam >* aParams,
       
   274     RProcess& aProcess )
       
   275     {
       
   276     TInt error = KErrNone;
       
   277     if( aParams )
       
   278         {
       
   279         const TInt paramCount = aParams->Count();
       
   280         for( TInt ix = 0; ix < paramCount; ix++ )
       
   281             {
       
   282             error = (*aParams)[ ix ].ApplyParam( aProcess );
       
   283             if( error != KErrNone )
       
   284                 {
       
   285                 break;
       
   286                 }
       
   287             }
       
   288         }
       
   289 
       
   290     return error;
       
   291     }
       
   292 
       
   293 
       
   294 // ==============================================================
       
   295 // ================== TPROCESSSTARTUPPARAM ======================
       
   296 // ==============================================================
       
   297 
       
   298 // --------------------------------------------------------------
       
   299 // TXIMPProcessStartupParam::TXIMPProcessStartupParam()
       
   300 // --------------------------------------------------------------
       
   301 //
       
   302 EXPORT_C TXIMPProcessStartupParam::TXIMPProcessStartupParam()
       
   303     : iType( EUnknown ),
       
   304       iSlot( KErrNotFound )
       
   305     {
       
   306     }
       
   307 
       
   308 
       
   309 // --------------------------------------------------------------
       
   310 // TXIMPProcessStartupParam::Set()
       
   311 // --------------------------------------------------------------
       
   312 //
       
   313 void TXIMPProcessStartupParam::Set( TInt aSlot,
       
   314                                 const RHandleBase& aHandle )
       
   315     {
       
   316     iType = EHandle;
       
   317     iSlot = aSlot;
       
   318     iHandle = &aHandle;
       
   319     }
       
   320 
       
   321 
       
   322 // --------------------------------------------------------------
       
   323 // TXIMPProcessStartupParam::Set()
       
   324 // --------------------------------------------------------------
       
   325 //
       
   326 void TXIMPProcessStartupParam::Set( TInt aSlot,
       
   327                                 const RSubSessionBase& aSubSession )
       
   328     {
       
   329     iType = ESubSession;
       
   330     iSlot = aSlot;
       
   331     iSubSession = &aSubSession;
       
   332     }
       
   333 
       
   334 
       
   335 // --------------------------------------------------------------
       
   336 // TXIMPProcessStartupParam::Set()
       
   337 // --------------------------------------------------------------
       
   338 //
       
   339 void TXIMPProcessStartupParam::Set( TInt aSlot,
       
   340                                 const TDesC16& aDes )
       
   341     {
       
   342     iType = EDes16;
       
   343     iSlot = aSlot;
       
   344     iDes16.Set( aDes );
       
   345     }
       
   346 
       
   347 
       
   348 // --------------------------------------------------------------
       
   349 // TXIMPProcessStartupParam::Set()
       
   350 // --------------------------------------------------------------
       
   351 //
       
   352 void TXIMPProcessStartupParam::Set( TInt aSlot,
       
   353                                 const TDesC8& aDes )
       
   354     {
       
   355     iType = EDes8;
       
   356     iSlot = aSlot;
       
   357     iDes8.Set( aDes );
       
   358     }
       
   359 
       
   360 
       
   361 // --------------------------------------------------------------
       
   362 // TXIMPProcessStartupParam::Set()
       
   363 // --------------------------------------------------------------
       
   364 //
       
   365 EXPORT_C void TXIMPProcessStartupParam::Set( TInt aSlot,
       
   366                                 TInt aInt )
       
   367     {
       
   368     iType = EInt;
       
   369     iSlot = aSlot;
       
   370     iInt = aInt;
       
   371     }
       
   372 
       
   373 
       
   374 // --------------------------------------------------------------
       
   375 // TXIMPProcessStartupParam::ApplyParam()
       
   376 // --------------------------------------------------------------
       
   377 //
       
   378 TInt TXIMPProcessStartupParam::ApplyParam( RProcess& aProcess ) const
       
   379     {
       
   380     switch( iType )
       
   381         {
       
   382         case EHandle: return aProcess.SetParameter( iSlot, *iHandle );
       
   383         case ESubSession: return aProcess.SetParameter( iSlot, *iSubSession );
       
   384         case EDes16: return aProcess.SetParameter( iSlot, iDes16 );
       
   385         case EDes8: return aProcess.SetParameter( iSlot, iDes8 );
       
   386         case EInt: return aProcess.SetParameter( iSlot, iInt );
       
   387         }
       
   388 
       
   389     return KErrUnknown;
       
   390     }
       
   391 
       
   392 
       
   393 // END OF FILE
       
   394 
       
   395 
       
   396