loadgen/src/loadgen_applications.cpp
branchRCL_3
changeset 22 fad26422216a
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     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 "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_model.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( CLoadGenModel::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     CLoadGenModel::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                            CLoadGenModel::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 = CLoadGenModel::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         TApaAppCapabilityBuf capBuf;
       
   463         User::LeaveIfError( iLs.GetAppCapability( capBuf, iCurrentAppUid ) );
       
   464         TApaAppCapability& caps = capBuf();
       
   465 
       
   466         CApaCommandLine* cmdLine=CApaCommandLine::NewLC();
       
   467         cmdLine->SetExecutableNameL( appInfo.iFullName );
       
   468 
       
   469         // app is launched in background
       
   470         cmdLine->SetCommandL( EApaCommandBackground );       
       
   471 
       
   472         // start the app
       
   473         User::LeaveIfError( iLs.StartApp( *cmdLine ) );
       
   474         iAppKeyPressManager->AddNewApplicationUidToKeyEventsL( iCurrentAppUid );
       
   475         iAppLaunchCounter++;
       
   476 
       
   477         CleanupStack::PopAndDestroy( cmdLine );
       
   478         }
       
   479     }
       
   480 
       
   481 // ---------------------------------------------------------------------------
       
   482 
       
   483 void CLauncherEngine::StopApplication( TBool aRandomApplication )
       
   484     {
       
   485     LOGSTRING("LoadGen: CLauncherEngine::StopApplication");
       
   486 
       
   487     // remove key pressing instance
       
   488     TUid appToDelete = iAppKeyPressManager->KillApplication( aRandomApplication );
       
   489     if ( appToDelete != KNullUid )
       
   490         {
       
   491         TApaTaskList taskList( iWs );
       
   492         TApaTask thisTask = taskList.FindApp( appToDelete );
       
   493     
       
   494         if ( thisTask.Exists() )
       
   495             {
       
   496             // since the application is still open, let's close it
       
   497             thisTask.EndTask();
       
   498             }
       
   499         iAppLaunchCounter--;
       
   500         }
       
   501     
       
   502     // return if only one application requested
       
   503     if ( aRandomApplication )
       
   504         {
       
   505         return;
       
   506         }
       
   507     
       
   508     // remove all launched applications because load is requested to be closed
       
   509     while ( appToDelete != KNullUid )
       
   510         {
       
   511         appToDelete = iAppKeyPressManager->KillApplication( aRandomApplication );
       
   512             
       
   513         if ( appToDelete != KNullUid )
       
   514             {
       
   515             TApaTaskList taskList( iWs );
       
   516             TApaTask thisTask = taskList.FindApp( appToDelete );
       
   517         
       
   518             if ( thisTask.Exists() )
       
   519                 {
       
   520                 // since the application is still open, let's close it
       
   521                 thisTask.EndTask();
       
   522                 }
       
   523             iAppLaunchCounter--;
       
   524             }
       
   525         }
       
   526     }
       
   527 
       
   528 // --------------------------------------------------------------------------------------------
       
   529 // --------------------------------------------------------------------------------------------
       
   530 
       
   531 CAppKeyPressManager* CAppKeyPressManager::NewL( TApplicationsAttributes& aAttributes )
       
   532     {
       
   533     LOGSTRING("LoadGen: CAppKeyPressManager::NewL");
       
   534     CAppKeyPressManager* self = new(ELeave) CAppKeyPressManager( aAttributes );
       
   535     CleanupStack::PushL( self );
       
   536     self->ConstructL();
       
   537     CleanupStack::Pop( self );
       
   538     return self;
       
   539     }
       
   540 
       
   541 // --------------------------------------------------------------------------------------------
       
   542 
       
   543 CAppKeyPressManager::CAppKeyPressManager( TApplicationsAttributes& aAttributes ) :
       
   544                                             iAttributes( aAttributes )
       
   545     {
       
   546     LOGSTRING("LoadGen: CAppKeyPressManager::CAppKeyPressManager");
       
   547     }
       
   548 
       
   549 // --------------------------------------------------------------------------------------------
       
   550     
       
   551 CAppKeyPressManager::~CAppKeyPressManager()
       
   552     {
       
   553     LOGSTRING("LoadGen: CAppKeyPressManager::~CAppKeyPressManager");
       
   554     if ( iKeyPresses.Count() != 0 )
       
   555         {
       
   556         iKeyPresses.ResetAndDestroy();
       
   557         }
       
   558 
       
   559     iKeyPresses.Close();
       
   560     }
       
   561 
       
   562 // --------------------------------------------------------------------------------------------
       
   563  
       
   564 void CAppKeyPressManager::ConstructL()
       
   565     {
       
   566     LOGSTRING("LoadGen: CAppKeyPressManager::ConstructL");
       
   567     }
       
   568     
       
   569    
       
   570 // --------------------------------------------------------------------------------------------
       
   571 void CAppKeyPressManager::AddNewApplicationUidToKeyEventsL( TUid aUid )
       
   572     {
       
   573     LOGSTRING("LoadGen: CAppKeyPressManager::AddNewApplicationUidToKeyEventsL");
       
   574     CApplicationKeyPresses* applicationKP = CApplicationKeyPresses::NewL( aUid,
       
   575                                                                     iAttributes );
       
   576     CleanupStack::PushL( applicationKP );
       
   577     User::LeaveIfError( iKeyPresses.Append( applicationKP ) );
       
   578     CleanupStack::Pop( applicationKP );
       
   579     }
       
   580 
       
   581 // --------------------------------------------------------------------------------------------
       
   582 TUid CAppKeyPressManager::KillApplication( TBool aRandomApplication )
       
   583     {
       
   584     TUid applicationUid = KNullUid;
       
   585     TInt appToDelete = iKeyPresses.Count() - 1 ;
       
   586     // remove the newest application if not random 
       
   587     if ( aRandomApplication )
       
   588         {
       
   589          appToDelete = CLoadGenModel::RandomNumber( 0, iKeyPresses.Count() - 1 );
       
   590         }
       
   591     if ( iKeyPresses.Count() )
       
   592         {
       
   593         // get random application and delete it
       
   594         
       
   595         applicationUid = iKeyPresses[appToDelete]->ApplicationUid();
       
   596         delete iKeyPresses[appToDelete];
       
   597         iKeyPresses.Remove( appToDelete );
       
   598         }
       
   599     return applicationUid;
       
   600     }
       
   601 // --------------------------------------------------------------------------------------------
       
   602 // --------------------------------------------------------------------------------------------
       
   603 
       
   604 CApplicationKeyPresses* CApplicationKeyPresses::NewL( TUid aUid, TApplicationsAttributes&  
       
   605         aAttributes )
       
   606     {
       
   607     LOGSTRING("LoadGen: CApplicationKeyPresses::NewL");
       
   608     CApplicationKeyPresses* self = new(ELeave) CApplicationKeyPresses( aUid, aAttributes );
       
   609     CleanupStack::PushL( self );
       
   610     self->ConstructL();
       
   611     CleanupStack::Pop( self );
       
   612     return self;
       
   613     }
       
   614 
       
   615 // --------------------------------------------------------------------------------------------
       
   616 
       
   617 CApplicationKeyPresses::CApplicationKeyPresses( TUid aUid, 
       
   618                                                         TApplicationsAttributes& aAttributes ) :
       
   619                                                 iUid( aUid ), iAttributes( aAttributes ) 
       
   620     {
       
   621     LOGSTRING("LoadGen: CApplicationKeyPresses::CApplicationKeyPresses");
       
   622     }
       
   623 
       
   624 // --------------------------------------------------------------------------------------------
       
   625     
       
   626 CApplicationKeyPresses::~CApplicationKeyPresses()
       
   627     {
       
   628     LOGSTRING("LoadGen: CApplicationKeyPresses::~CApplicationKeyPresses");
       
   629     if ( iPeriodicTimer )
       
   630         {
       
   631         iPeriodicTimer->Cancel();
       
   632         delete iPeriodicTimer;
       
   633         }
       
   634     
       
   635     iWsSession.Close();
       
   636     }
       
   637 
       
   638 // --------------------------------------------------------------------------------------------
       
   639  
       
   640 void CApplicationKeyPresses::ConstructL()
       
   641     {
       
   642     LOGSTRING("LoadGen: CApplicationKeyPresses::ConstructL");
       
   643     // init
       
   644     User::LeaveIfError( iWsSession.Connect() );
       
   645     
       
   646     // start timer    
       
   647     iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   648     if ( iAttributes.iKeyPressType )
       
   649         {
       
   650         iPeriodicTimer->Start( KDefaultStartDelay,
       
   651                                CLoadGenModel::MilliSecondsToMicroSeconds( 
       
   652                                        iAttributes.iHeartBeat,
       
   653                                        iAttributes.iRandomVariance ),
       
   654                                 TCallBack( PeriodicTimerCallBack, this ) );
       
   655         }
       
   656     }
       
   657     
       
   658 // --------------------------------------------------------------------------------------------
       
   659 
       
   660 TInt CApplicationKeyPresses::PeriodicTimerCallBack( TAny* aAny )
       
   661     {
       
   662     CApplicationKeyPresses* self = static_cast<CApplicationKeyPresses*>( aAny );
       
   663     self->SimulateKeyEvent();
       
   664     return KErrNone;
       
   665     }
       
   666     
       
   667 // --------------------------------------------------------------------------------------------
       
   668  
       
   669 void CApplicationKeyPresses::SimulateKeyEvent()
       
   670     {
       
   671     LOGSTRING("LoadGen: CApplicationKeyPresses::SimulateKeyEvent");
       
   672     // for arrow key events
       
   673     
       
   674     TInt wgId(0);
       
   675     CApaWindowGroupName::FindByAppUid( iUid, iWsSession, wgId );
       
   676     // generate a random arrow key event
       
   677     TWsEvent event;
       
   678     event.SetType( EEventKey );
       
   679     event.Key()->iCode = CLoadGenModel::RandomNumber( EKeyLeftArrow, EKeyDownArrow );
       
   680     event.Key()->iScanCode = event.Key()->iCode - KScanCodeStart;
       
   681     event.Key()->iModifiers = 0;
       
   682     event.Key()->iRepeats = 0;
       
   683     iWsSession.SendEventToWindowGroup( wgId, event );
       
   684     }
       
   685 
       
   686 // End of File