utilityapps/loadgen/engine/src/loadgen_applications.cpp
changeset 55 2d9cac8919d3
parent 51 b048e15729d6
equal deleted inserted replaced
53:819e59dfc032 55:2d9cac8919d3
       
     1 /*
       
     2 * Copyright (c) 2010 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 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_applications.h"
       
    21 #include "loadgen_utils.h"
       
    22 #include "loadgen.hrh"
       
    23 #include <loadgen.rsg>
       
    24 
       
    25 #include <e32math.h>
       
    26 #include "loadgen_traces.h"
       
    27 
       
    28 _LIT(KThreadName, "Applications %d");
       
    29 
       
    30 const TInt KDefaultStartDelay = 500;
       
    31 const TInt KThreadNameLength = 64;
       
    32 const TInt KDescriptorBufSize = 256;
       
    33 const TInt KPriorityBufSize = 16;
       
    34 const TInt KScanCodeStart = 32;
       
    35     
       
    36 // ===================================== MEMBER FUNCTIONS =====================================
       
    37 
       
    38 CAppLauncher* CAppLauncher::NewL( TApplicationsAttributes& aAttributes, TInt aReferenceNumber )
       
    39     {
       
    40     CAppLauncher* self = new(ELeave) CAppLauncher( aAttributes, aReferenceNumber );
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;    
       
    45     }
       
    46 
       
    47 // --------------------------------------------------------------------------------------------
       
    48 
       
    49 CAppLauncher::~CAppLauncher()
       
    50     {
       
    51     Close();
       
    52     }
       
    53 
       
    54 // --------------------------------------------------------------------------------------------
       
    55 
       
    56 CAppLauncher::CAppLauncher(  
       
    57            TApplicationsAttributes& aAttributes, TInt aReferenceNumber ) : 
       
    58            iAttributes( aAttributes )
       
    59     {
       
    60     iAttributes.iId = aReferenceNumber;
       
    61     }
       
    62 
       
    63 // --------------------------------------------------------------------------------------------
       
    64 
       
    65 void CAppLauncher::ConstructL()
       
    66     {
       
    67     CLoadBase::ConstructL();
       
    68     
       
    69     iType = ELoadGenCmdNewLoadApplications;
       
    70     
       
    71     TBuf<KThreadNameLength> threadName;
       
    72     threadName.Format( KThreadName, iAttributes.iId );
       
    73     
       
    74     // create a thread
       
    75     User::LeaveIfError( iThread.Create( threadName, ThreadFunction, 
       
    76                                         KDefaultStackSize*2, KMinHeapSize, 
       
    77                                         KKilo * KMinHeapSize, (TAny*) &iAttributes ) );
       
    78     
       
    79     // set priority of the thread
       
    80     SetPriority();
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------------------------
       
    84 
       
    85 TInt CAppLauncher::ThreadFunction( TAny* aThreadArg )
       
    86     {
       
    87     CTrapCleanup* pC = CTrapCleanup::New();
       
    88     CActiveScheduler* pS = new CActiveScheduler;
       
    89     CActiveScheduler::Install( pS );
       
    90 
       
    91     // start generating load, pass pointer to arguments
       
    92     GenerateLoad( *( ( TApplicationsAttributes* ) aThreadArg ) );
       
    93 
       
    94     delete pS;
       
    95     delete pC;
       
    96     
       
    97     return KErrNone;
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------------------------
       
   101 
       
   102 void CAppLauncher::GenerateLoad( TApplicationsAttributes& aAttributes )
       
   103     {
       
   104     CAppLauncherManager* appLauncherManager = NULL;
       
   105     TRAPD( err, appLauncherManager = CAppLauncherManager::NewL( aAttributes ) );
       
   106     if ( err == KErrNone ) 
       
   107         {
       
   108         CActiveScheduler::Start();
       
   109         }
       
   110     delete appLauncherManager;
       
   111     }
       
   112 
       
   113 // --------------------------------------------------------------------------------------------
       
   114 
       
   115 void CAppLauncher::Resume()
       
   116     {
       
   117     CLoadBase::Resume();
       
   118     
       
   119     iThread.Resume();
       
   120     }
       
   121 
       
   122 // --------------------------------------------------------------------------------------------
       
   123 
       
   124 void CAppLauncher::Suspend()
       
   125     {
       
   126     CLoadBase::Suspend();
       
   127     
       
   128     iThread.Suspend();
       
   129     }
       
   130 
       
   131 // --------------------------------------------------------------------------------------------
       
   132 
       
   133 void CAppLauncher::SetPriority()
       
   134     {
       
   135     CLoadBase::SetPriority();
       
   136     
       
   137     iThread.SetPriority( CLoadGenUtils::SettingItemToThreadPriority( iAttributes.iPriority ) );
       
   138     }
       
   139     
       
   140 // --------------------------------------------------------------------------------------------
       
   141 
       
   142 void CAppLauncher::Close()
       
   143     {
       
   144     CLoadBase::Close();
       
   145     
       
   146     if ( iThread.ExitReason() == 0 ) // check if the thread is still alive
       
   147         {
       
   148         // signal the thread that it needs to close
       
   149         iThread.RequestComplete( iAttributes.iDeathStatus, KErrCancel );
       
   150 
       
   151         // wait the thread to die
       
   152         TRequestStatus waiter;
       
   153         iThread.Logon( waiter );
       
   154         User::WaitForRequest( waiter );
       
   155         iThread.Close();
       
   156         }
       
   157     }
       
   158     
       
   159 // --------------------------------------------------------------------------------------------
       
   160 
       
   161 TPtrC CAppLauncher::Description()
       
   162     {
       
   163     TBuf<KDescriptorBufSize> buf;
       
   164     TBuf<KPriorityBufSize> prioBuf;
       
   165     CLoadGenUtils::SettingItemToThreadDescription( iAttributes.iPriority, prioBuf );
       
   166     
       
   167     _LIT(KAppLauncherEntry, "[%d] Appls prio=%S max apps=%d heartbeat=%dms random=%d%%");
       
   168     buf.Format( KAppLauncherEntry, iAttributes.iId, &prioBuf, iAttributes.iMaxOpen, 
       
   169                 iAttributes.iHeartBeat, iAttributes.iRandomVariance );
       
   170    
       
   171     return TPtrC( buf );
       
   172     }               
       
   173 
       
   174 // --------------------------------------------------------------------------------------------
       
   175 // --------------------------------------------------------------------------------------------
       
   176 
       
   177 CAppLauncherManager* CAppLauncherManager::NewL( TApplicationsAttributes& aAttributes )
       
   178     {
       
   179     CAppLauncherManager* self = new(ELeave) CAppLauncherManager(aAttributes);
       
   180     CleanupStack::PushL( self );
       
   181     self->ConstructL();
       
   182     CleanupStack::Pop( self );
       
   183     return self;
       
   184     }
       
   185 
       
   186 // --------------------------------------------------------------------------------------------
       
   187 
       
   188 CAppLauncherManager::CAppLauncherManager( TApplicationsAttributes& aAttributes ) :
       
   189             CActive( EPriorityStandard ), iAttributes( aAttributes )
       
   190     {
       
   191     iAppEventType = EApplicationsLaunchApplication;
       
   192     }
       
   193 
       
   194 // --------------------------------------------------------------------------------------------
       
   195     
       
   196 CAppLauncherManager::~CAppLauncherManager()
       
   197     {
       
   198     if ( iPeriodicTimer )
       
   199         {
       
   200         iPeriodicTimer->Cancel();
       
   201         delete iPeriodicTimer;
       
   202         }
       
   203     delete iLauncherEngine;
       
   204     Cancel();
       
   205     
       
   206     iWsSession.Close();
       
   207     }
       
   208 
       
   209 // --------------------------------------------------------------------------------------------
       
   210  
       
   211 void CAppLauncherManager::ConstructL()
       
   212     {
       
   213     CActiveScheduler::Add( this );
       
   214     
       
   215     // create instance to application launcher
       
   216     iLauncherEngine = CLauncherEngine::NewL( iAttributes );
       
   217     
       
   218     // set the status as pending
       
   219     iStatus = KRequestPending;
       
   220     SetActive();
       
   221     
       
   222     // set the death status pointer point to the request status of this ao
       
   223     iAttributes.iDeathStatus = &iStatus;
       
   224     
       
   225     // init
       
   226     User::LeaveIfError( iWsSession.Connect() );
       
   227     
       
   228     // start timer    
       
   229     iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   230     iPeriodicTimer->Start( KDefaultStartDelay, 
       
   231                            CLoadGenUtils::MilliSecondsToMicroSeconds( 
       
   232                                iAttributes.iLaunchingInterval, iAttributes.iRandomVariance ),
       
   233                            TCallBack( PeriodicTimerCallBack, this ) );
       
   234     }
       
   235     
       
   236 // --------------------------------------------------------------------------------------------
       
   237  
       
   238 void CAppLauncherManager::RunL()
       
   239     {
       
   240     // request status has completed by the main thread meaning that we need to stop now
       
   241     CActiveScheduler::Stop();
       
   242     }
       
   243 
       
   244 // --------------------------------------------------------------------------------------------
       
   245  
       
   246 void CAppLauncherManager::DoCancel()
       
   247     {
       
   248     }
       
   249 
       
   250 // --------------------------------------------------------------------------------------------
       
   251 
       
   252 TInt CAppLauncherManager::PeriodicTimerCallBack( TAny* aAny )
       
   253     {
       
   254     CAppLauncherManager* self = static_cast<CAppLauncherManager*>( aAny );
       
   255 
       
   256     TRAPD( err, self->SimulateEventL() );
       
   257     if ( KErrNone != err )
       
   258         {
       
   259         return err;
       
   260         }
       
   261     return KErrNone;
       
   262     }
       
   263     
       
   264 // --------------------------------------------------------------------------------------------
       
   265  
       
   266 void CAppLauncherManager::SimulateEventL()
       
   267     {    
       
   268     // Randomly start new applications.
       
   269     // After maximum applications launched stop one application.
       
   270     // And after that start launching again.
       
   271     if ( iAppEventType == EApplicationsLaunchApplication && 
       
   272          iAttributes.iMaxOpen == iLauncherEngine->AppLaunchCounter() )
       
   273         {
       
   274         iAppEventType = EApplicationsCloseApplication;
       
   275         }
       
   276     if ( iAppEventType == EApplicationsCloseApplication && 
       
   277          iAttributes.iMaxOpen -1 == iLauncherEngine->AppLaunchCounter() )
       
   278         {
       
   279         iAppEventType = EApplicationsLaunchApplication;
       
   280         }
       
   281     
       
   282     // Launch or stop
       
   283     if ( iAppEventType == EApplicationsLaunchApplication )
       
   284         {
       
   285         iLauncherEngine->StartAppLaunchingL();
       
   286         }  
       
   287     else
       
   288         {
       
   289         iLauncherEngine->StopApplication( ETrue );
       
   290         }
       
   291     }
       
   292     
       
   293 // --------------------------------------------------------------------------------------------
       
   294 
       
   295 #include <avkon.hrh>
       
   296 #include <e32std.h>
       
   297 #include <w32std.h>
       
   298 #include <apgtask.h>
       
   299 #include <bautils.h>
       
   300 #include <s32file.h>
       
   301 #include <apgwgnam.h> 
       
   302 
       
   303 // ---------------------------------------------------------------------------
       
   304 
       
   305 CLauncherEngine* CLauncherEngine::NewL( TApplicationsAttributes& aAttributes )
       
   306     {
       
   307     CLauncherEngine* self = new(ELeave) CLauncherEngine( aAttributes );
       
   308     CleanupStack::PushL(self);
       
   309     self->ConstructL();
       
   310     CleanupStack::Pop( self );
       
   311     return self;
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 
       
   316 CLauncherEngine::CLauncherEngine( TApplicationsAttributes& aAttributes ) : 
       
   317                         iAttributes( aAttributes )
       
   318     {
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 
       
   323 void CLauncherEngine::ConstructL()
       
   324     {
       
   325     LOGSTRING("LoadGen: CLauncherEngine::ConstructL");
       
   326     iAppKeyPressManager = CAppKeyPressManager::NewL( iAttributes );
       
   327     iAppLaunchCounter = 0;
       
   328     User::LeaveIfError( iTimer.CreateLocal() );
       
   329 
       
   330     User::LeaveIfError( iLs.Connect() );
       
   331     User::LeaveIfError( iWs.Connect() );
       
   332 
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 
       
   337 CLauncherEngine::~CLauncherEngine()
       
   338     {
       
   339     LOGSTRING("LoadGen: CLauncherEngine::~CLauncherEngine");
       
   340     StopApplication( EFalse );
       
   341     iWs.Close();
       
   342     iLs.Close();
       
   343     iTimer.Close();
       
   344     delete iAppKeyPressManager;
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 
       
   349 void CLauncherEngine::StartAppLaunchingL()
       
   350     {
       
   351     LOGSTRING("LoadGen: CLauncherEngine::StartAppLaunchingL");
       
   352     if ( iAppLaunchCounter == iAttributes.iMaxOpen )
       
   353         {
       
   354         return;
       
   355         }
       
   356 
       
   357     // start launching
       
   358     TBool launched( EFalse );
       
   359     while ( !launched )
       
   360         {
       
   361         launched = LaunchApplicationL();
       
   362         }
       
   363     }
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 
       
   367 TBool CLauncherEngine::LaunchApplicationL()
       
   368     {
       
   369     LOGSTRING("LoadGen: CLauncherEngine::LaunchApplicationL");
       
   370     TBool result( EFalse );
       
   371     // get the uid of the current app
       
   372     iCurrentAppUid = KNullUid;
       
   373     TApaAppInfo appInfo;
       
   374     
       
   375     // get application list size
       
   376     TInt appsListSize = iAttributes.iAppsArray->MdcaCount();
       
   377     // get random application number in the list
       
   378     TInt appTolaunch = CLoadGenUtils::RandomNumber( 0,  appsListSize - 1 );
       
   379     // get file name of the application to be launched
       
   380     TFileName launchFileName = iAttributes.iAppsArray->MdcaPoint( appTolaunch );
       
   381     iLs.GetAllApps();
       
   382     while ( iLs.GetNextApp( appInfo ) == KErrNone )
       
   383         {
       
   384         // get file name in  current appInfo
       
   385         TFileName appFileName = appInfo.iFullName;
       
   386         // get AppUid
       
   387         if ( appFileName.CompareF( launchFileName ) == 0 )
       
   388             {
       
   389             iCurrentAppUid = appInfo.iUid;
       
   390             break;
       
   391             }
       
   392         }
       
   393 
       
   394     if ( iCurrentAppUid != KNullUid )
       
   395         {        
       
   396         // parse the filename
       
   397         TParse nameParser;
       
   398         nameParser.SetNoWild( iAttributes.iAppsArray->MdcaPoint( appTolaunch ), NULL, NULL );
       
   399         iCurrentAppNameAndExt.Copy( nameParser.Drive() );
       
   400         iCurrentAppNameAndExt.Append( nameParser.NameAndExt() );
       
   401            
       
   402         // do not try to launch these apps
       
   403         if ( iCurrentAppUid != KNullUid &&
       
   404               iAttributes.iAppsArray->MdcaPoint( appTolaunch).FindF(_L("\\loadgen.")) == KErrNotFound &&
       
   405               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\Phone." ) ) == KErrNotFound &&
       
   406               iAttributes.iAppsArray->MdcaPoint( appTolaunch).FindF( _L("\\Startup.") ) == KErrNotFound &&  
       
   407               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\SplashScreen.") ) == KErrNotFound &&  
       
   408               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\cameraapp.") ) == KErrNotFound &&
       
   409               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\eshell.") ) == KErrNotFound &&
       
   410               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\ConnTest.") ) == KErrNotFound &&
       
   411               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\Launcher.") ) == KErrNotFound &&
       
   412               
       
   413 #ifdef __WINS__
       
   414               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\DebugAgent.") ) == KErrNotFound &&
       
   415               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\redirect.") ) == KErrNotFound &&              
       
   416 #endif              
       
   417               iAttributes.iAppsArray->MdcaPoint( appTolaunch ).FindF( _L("\\VoiceRecorder.") ) == KErrNotFound )            
       
   418             {
       
   419         
       
   420             // check if the app is already running
       
   421             TApaTaskList taskList( iWs );
       
   422             TApaTask thisTask = taskList.FindApp( iCurrentAppUid );
       
   423             if ( !thisTask.Exists( ))
       
   424                 {
       
   425                 // check the program's capabilities
       
   426                 TApaAppCapabilityBuf buf;
       
   427                 iLs.GetAppCapability( buf, iCurrentAppUid );
       
   428                 TApaAppCapability cap = buf();
       
   429         
       
   430                 // if it's embeddable only, don't launch if setting is enabled
       
   431                 // if it's hidden, don't launch if setting is enabled
       
   432                 if ( cap.iEmbeddability != TApaAppCapability::EEmbeddableOnly &&
       
   433                      !cap.iAppIsHidden )
       
   434                     {
       
   435                     LOGSTRING2("launchFileName = %S", &launchFileName);
       
   436                     
       
   437                     DoLaunchApplicationL();
       
   438                     result = ETrue;
       
   439                     }            
       
   440                 } 
       
   441             }
       
   442         }
       
   443     return result;
       
   444     }
       
   445 
       
   446 
       
   447 // ---------------------------------------------------------------------------
       
   448 
       
   449 void CLauncherEngine::DoLaunchApplicationL()
       
   450     {
       
   451     LOGSTRING("LoadGen: CLauncherEngine::DoLaunchApplicationL");
       
   452 
       
   453     // Find the task with uid3
       
   454     TApaTaskList tasklist( iWs );
       
   455     TApaTask task=tasklist.FindApp( iCurrentAppUid );
       
   456 
       
   457     if ( !task.Exists() )
       
   458         // Task doesn't exist, launch a new instance of an application
       
   459         {
       
   460         TApaAppInfo appInfo;
       
   461         User::LeaveIfError( iLs.GetAppInfo( appInfo, iCurrentAppUid ) );
       
   462 
       
   463         CApaCommandLine* cmdLine=CApaCommandLine::NewLC();
       
   464         cmdLine->SetExecutableNameL( appInfo.iFullName );
       
   465 
       
   466         // app is launched in background
       
   467         cmdLine->SetCommandL( EApaCommandBackground );       
       
   468 
       
   469         // start the app
       
   470         User::LeaveIfError( iLs.StartApp( *cmdLine ) );
       
   471         iAppKeyPressManager->AddNewApplicationUidToKeyEventsL( iCurrentAppUid );
       
   472         iAppLaunchCounter++;
       
   473 
       
   474         CleanupStack::PopAndDestroy( cmdLine );
       
   475         }
       
   476     }
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 
       
   480 void CLauncherEngine::StopApplication( TBool aRandomApplication )
       
   481     {
       
   482     LOGSTRING("LoadGen: CLauncherEngine::StopApplication");
       
   483 
       
   484     // remove key pressing instance
       
   485     TUid appToDelete = iAppKeyPressManager->KillApplication( aRandomApplication );
       
   486     if ( appToDelete != KNullUid )
       
   487         {
       
   488         TApaTaskList taskList( iWs );
       
   489         TApaTask thisTask = taskList.FindApp( appToDelete );
       
   490     
       
   491         if ( thisTask.Exists() )
       
   492             {
       
   493             // since the application is still open, let's close it
       
   494             thisTask.EndTask();
       
   495             }
       
   496         iAppLaunchCounter--;
       
   497         }
       
   498     
       
   499     // return if only one application requested
       
   500     if ( aRandomApplication )
       
   501         {
       
   502         return;
       
   503         }
       
   504     
       
   505     // remove all launched applications because load is requested to be closed
       
   506     while ( appToDelete != KNullUid )
       
   507         {
       
   508         appToDelete = iAppKeyPressManager->KillApplication( aRandomApplication );
       
   509             
       
   510         if ( appToDelete != KNullUid )
       
   511             {
       
   512             TApaTaskList taskList( iWs );
       
   513             TApaTask thisTask = taskList.FindApp( appToDelete );
       
   514         
       
   515             if ( thisTask.Exists() )
       
   516                 {
       
   517                 // since the application is still open, let's close it
       
   518                 thisTask.EndTask();
       
   519                 }
       
   520             iAppLaunchCounter--;
       
   521             }
       
   522         }
       
   523     }
       
   524 
       
   525 // --------------------------------------------------------------------------------------------
       
   526 // --------------------------------------------------------------------------------------------
       
   527 
       
   528 CAppKeyPressManager* CAppKeyPressManager::NewL( TApplicationsAttributes& aAttributes )
       
   529     {
       
   530     LOGSTRING("LoadGen: CAppKeyPressManager::NewL");
       
   531     CAppKeyPressManager* self = new(ELeave) CAppKeyPressManager( aAttributes );
       
   532     CleanupStack::PushL( self );
       
   533     self->ConstructL();
       
   534     CleanupStack::Pop( self );
       
   535     return self;
       
   536     }
       
   537 
       
   538 // --------------------------------------------------------------------------------------------
       
   539 
       
   540 CAppKeyPressManager::CAppKeyPressManager( TApplicationsAttributes& aAttributes ) :
       
   541                                             iAttributes( aAttributes )
       
   542     {
       
   543     LOGSTRING("LoadGen: CAppKeyPressManager::CAppKeyPressManager");
       
   544     }
       
   545 
       
   546 // --------------------------------------------------------------------------------------------
       
   547     
       
   548 CAppKeyPressManager::~CAppKeyPressManager()
       
   549     {
       
   550     LOGSTRING("LoadGen: CAppKeyPressManager::~CAppKeyPressManager");
       
   551     if ( iKeyPresses.Count() != 0 )
       
   552         {
       
   553         iKeyPresses.ResetAndDestroy();
       
   554         }
       
   555 
       
   556     iKeyPresses.Close();
       
   557     }
       
   558 
       
   559 // --------------------------------------------------------------------------------------------
       
   560  
       
   561 void CAppKeyPressManager::ConstructL()
       
   562     {
       
   563     LOGSTRING("LoadGen: CAppKeyPressManager::ConstructL");
       
   564     }
       
   565     
       
   566    
       
   567 // --------------------------------------------------------------------------------------------
       
   568 void CAppKeyPressManager::AddNewApplicationUidToKeyEventsL( TUid aUid )
       
   569     {
       
   570     LOGSTRING("LoadGen: CAppKeyPressManager::AddNewApplicationUidToKeyEventsL");
       
   571     CApplicationKeyPresses* applicationKP = CApplicationKeyPresses::NewL( aUid,
       
   572                                                                     iAttributes );
       
   573     CleanupStack::PushL( applicationKP );
       
   574     User::LeaveIfError( iKeyPresses.Append( applicationKP ) );
       
   575     CleanupStack::Pop( applicationKP );
       
   576     }
       
   577 
       
   578 // --------------------------------------------------------------------------------------------
       
   579 TUid CAppKeyPressManager::KillApplication( TBool aRandomApplication )
       
   580     {
       
   581     TUid applicationUid = KNullUid;
       
   582     TInt appToDelete = iKeyPresses.Count() - 1 ;
       
   583     // remove the newest application if not random 
       
   584     if ( aRandomApplication )
       
   585         {
       
   586          appToDelete = CLoadGenUtils::RandomNumber( 0, iKeyPresses.Count() - 1 );
       
   587         }
       
   588     if ( iKeyPresses.Count() )
       
   589         {
       
   590         // get random application and delete it
       
   591         
       
   592         applicationUid = iKeyPresses[appToDelete]->ApplicationUid();
       
   593         delete iKeyPresses[appToDelete];
       
   594         iKeyPresses.Remove( appToDelete );
       
   595         }
       
   596     return applicationUid;
       
   597     }
       
   598 // --------------------------------------------------------------------------------------------
       
   599 // --------------------------------------------------------------------------------------------
       
   600 
       
   601 CApplicationKeyPresses* CApplicationKeyPresses::NewL( TUid aUid, TApplicationsAttributes&  
       
   602         aAttributes )
       
   603     {
       
   604     LOGSTRING("LoadGen: CApplicationKeyPresses::NewL");
       
   605     CApplicationKeyPresses* self = new(ELeave) CApplicationKeyPresses( aUid, aAttributes );
       
   606     CleanupStack::PushL( self );
       
   607     self->ConstructL();
       
   608     CleanupStack::Pop( self );
       
   609     return self;
       
   610     }
       
   611 
       
   612 // --------------------------------------------------------------------------------------------
       
   613 
       
   614 CApplicationKeyPresses::CApplicationKeyPresses( TUid aUid, 
       
   615                                                         TApplicationsAttributes& aAttributes ) :
       
   616                                                 iUid( aUid ), iAttributes( aAttributes ) 
       
   617     {
       
   618     LOGSTRING("LoadGen: CApplicationKeyPresses::CApplicationKeyPresses");
       
   619     }
       
   620 
       
   621 // --------------------------------------------------------------------------------------------
       
   622     
       
   623 CApplicationKeyPresses::~CApplicationKeyPresses()
       
   624     {
       
   625     LOGSTRING("LoadGen: CApplicationKeyPresses::~CApplicationKeyPresses");
       
   626     if ( iPeriodicTimer )
       
   627         {
       
   628         iPeriodicTimer->Cancel();
       
   629         delete iPeriodicTimer;
       
   630         }
       
   631     
       
   632     iWsSession.Close();
       
   633     }
       
   634 
       
   635 // --------------------------------------------------------------------------------------------
       
   636  
       
   637 void CApplicationKeyPresses::ConstructL()
       
   638     {
       
   639     LOGSTRING("LoadGen: CApplicationKeyPresses::ConstructL");
       
   640     // init
       
   641     User::LeaveIfError( iWsSession.Connect() );
       
   642     
       
   643     // start timer    
       
   644     iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   645     if ( iAttributes.iKeyPressType )
       
   646         {
       
   647         iPeriodicTimer->Start( KDefaultStartDelay,
       
   648                                CLoadGenUtils::MilliSecondsToMicroSeconds( 
       
   649                                        iAttributes.iHeartBeat,
       
   650                                        iAttributes.iRandomVariance ),
       
   651                                 TCallBack( PeriodicTimerCallBack, this ) );
       
   652         }
       
   653     }
       
   654     
       
   655 // --------------------------------------------------------------------------------------------
       
   656 
       
   657 TInt CApplicationKeyPresses::PeriodicTimerCallBack( TAny* aAny )
       
   658     {
       
   659     CApplicationKeyPresses* self = static_cast<CApplicationKeyPresses*>( aAny );
       
   660     self->SimulateKeyEvent();
       
   661     return KErrNone;
       
   662     }
       
   663     
       
   664 // --------------------------------------------------------------------------------------------
       
   665  
       
   666 void CApplicationKeyPresses::SimulateKeyEvent()
       
   667     {
       
   668     LOGSTRING("LoadGen: CApplicationKeyPresses::SimulateKeyEvent");
       
   669     // for arrow key events
       
   670     
       
   671     TInt wgId(0);
       
   672     CApaWindowGroupName::FindByAppUid( iUid, iWsSession, wgId );
       
   673     // generate a random arrow key event
       
   674     TWsEvent event;
       
   675     event.SetType( EEventKey );
       
   676     event.Key()->iCode = CLoadGenUtils::RandomNumber( EKeyLeftArrow, EKeyDownArrow );
       
   677     event.Key()->iScanCode = event.Key()->iCode - KScanCodeStart;
       
   678     event.Key()->iModifiers = 0;
       
   679     event.Key()->iRepeats = 0;
       
   680     iWsSession.SendEventToWindowGroup( wgId, event );
       
   681     }
       
   682 
       
   683 // End of File