stifui/avkon/uitestserverstarter/src/TestServerThreadStarter.cpp
branchRCL_3
changeset 18 48060abbbeaf
parent 17 d40e813b23c0
child 19 b3cee849fa46
equal deleted inserted replaced
17:d40e813b23c0 18:48060abbbeaf
     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: This module contains the implementation of 
       
    15 * CTestServerThreadStarter class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "TestServerThreadStarter.h"
       
    21 #include <stifinternal/TestServerClient.h>
       
    22 
       
    23 // EXTERNAL DATA STRUCTURES
       
    24 // None
       
    25 
       
    26 // EXTERNAL FUNCTION PROTOTYPES  
       
    27 // None
       
    28 
       
    29 // CONSTANTS
       
    30 // None
       
    31 
       
    32 // MACROS
       
    33 // None
       
    34 
       
    35 /**
       
    36  * CTestServerThreadStarter thread heap size.
       
    37  */
       
    38 const TUint KDefaultHeapSize = 0x10000;     // 64 K
       
    39 
       
    40 /**
       
    41  * CTestServerThreadStarter thread max heap size.
       
    42  */
       
    43 const TUint KMaxHeapSize     = 0x20000;     // 128 K 
       
    44 
       
    45 // LOCAL CONSTANTS AND MACROS
       
    46 // None
       
    47 
       
    48 // MODULE DATA STRUCTURES
       
    49 // None
       
    50 
       
    51 // LOCAL FUNCTION PROTOTYPES
       
    52 // None
       
    53 
       
    54 // FORWARD DECLARATIONS
       
    55 // None
       
    56 
       
    57 
       
    58 // ================= MEMBER FUNCTIONS =========================================
       
    59 
       
    60 /*
       
    61 -------------------------------------------------------------------------------
       
    62 
       
    63      Class: CTestServerThreadStarter
       
    64 
       
    65      Method: TestServerStarterThreadFunction
       
    66 
       
    67      Description: CTestServerThreadStarter thread function.
       
    68 
       
    69 	 CTestServerThreadStarter thread function in which testserver creation code
       
    70 	 is executed.
       
    71 
       
    72      Parameters: TAny* aParameters: in: Pointer to CTestServerThreadStarter object
       
    73      
       
    74      Return Values: None.
       
    75 
       
    76      Errors/Exceptions: None.
       
    77 
       
    78      Status: Draft
       
    79     
       
    80 -------------------------------------------------------------------------------
       
    81 */
       
    82 TInt CTestServerThreadStarter::TestServerStarterThreadFunction( TAny* aParameters )
       
    83 	{
       
    84 	RDebug::Printf( "UITESTING: CTestServerThreadStarter::TestServerStarterThreadFunction" );
       
    85     
       
    86     CTestServerThreadStarter* testServerStarter = (CTestServerThreadStarter*)aParameters;
       
    87     TThreadId mainThreadId = testServerStarter->iMainThreadId;
       
    88     CTestThreadContainerRunnerFactory* testThreadContainerRunnerFactory = 
       
    89     	testServerStarter->iTestThreadContainerRunnerFactory;
       
    90     
       
    91    	// Signal that all needed data is copied and main thread can continue its execution 
       
    92     testServerStarter->iServerThreadStartedSemaphore.Signal();
       
    93     	
       
    94     // Get module name from command line
       
    95 	const TInt length = User().CommandLineLength();
       
    96 
       
    97     HBufC* cmdLine = HBufC::New( length );
       
    98     
       
    99     if ( cmdLine == NULL )
       
   100         {
       
   101         return KErrNoMemory;
       
   102         }
       
   103 
       
   104     TPtr moduleName = cmdLine->Des();
       
   105 
       
   106 	User().CommandLine( moduleName );
       
   107 
       
   108     RDebug::Print(_L("CTestServerThreadStarter::TestServerStarterThreadFunction() Received data [%S]"), &moduleName);
       
   109 
       
   110     // Extract semaphore name passed in data
       
   111     TInt index = moduleName.Find(_L(" "));
       
   112     RDebug::Print(_L("CTestServerThreadStarter::TestServerStarterThreadFunction() Space separator found at position [%d]"), index);
       
   113     TPtrC semaphoreName = moduleName.Mid(index + 1);
       
   114     moduleName = moduleName.Left(index);
       
   115 
       
   116     RDebug::Print(_L("CTestServerThreadStarter::TestServerStarterThreadFunction() Extracted module name [%S] and sempahore name [%S]"), &moduleName, &semaphoreName);
       
   117 
       
   118     // Open start-up synchronization semaphore
       
   119     RSemaphore startup;
       
   120     RDebug::Print(_L(" Openingstart-up semaphore"));
       
   121     //TName semaphoreName = _L("startupSemaphore");
       
   122     //semaphoreName.Append( moduleName );
       
   123     
       
   124     TInt res = startup.OpenGlobal(semaphoreName);
       
   125     RDebug::Print(_L("Opening result %d"), res);    
       
   126 
       
   127 
       
   128     TFileName serverName;
       
   129     TInt r = StartNewServer ( moduleName, serverName, EFalse, startup, true, testThreadContainerRunnerFactory );
       
   130 
       
   131     if ( r ==   KErrAlreadyExists )
       
   132         {        
       
   133         // Ok, server was already started
       
   134         RDebug::Print(_L("UI TestServer already started, signaling semaphore and exiting"));
       
   135         startup.Signal();        
       
   136 
       
   137         delete cmdLine;
       
   138 
       
   139         return KErrNone;
       
   140         }
       
   141     else
       
   142         {       
       
   143         RDebug::Print(_L("UI TestServer is finished, code %d"), r);
       
   144         }
       
   145 
       
   146     delete cmdLine;
       
   147     
       
   148     //delete testThreadContainerRunnerFactory;
       
   149     testThreadContainerRunnerFactory = NULL;
       
   150     
       
   151     // Kill main thread to end UITestServerStarter application
       
   152     // when testserver is finished    
       
   153     RThread mainThread;
       
   154     TInt ret = mainThread.Open( mainThreadId );
       
   155 	if( ret != KErrNone )
       
   156 		{
       
   157 		User::Panic( _L("ThreadHandleOpenError"), ret );
       
   158 		}
       
   159     mainThread.Kill( KErrNone );
       
   160     
       
   161     return r;	
       
   162 	}
       
   163 
       
   164 /*
       
   165 -------------------------------------------------------------------------------
       
   166 
       
   167      Class: CTestServerThreadStarter
       
   168 
       
   169      Method: NewL
       
   170 
       
   171      Description: NewL is first phase of two-phased constructor.
       
   172 
       
   173 	 NewL is first phase of two-phased constructor.
       
   174 
       
   175      Parameters: None.
       
   176      
       
   177      Return Values: Pointer to new CTestServerThreadStarter object.
       
   178 
       
   179      Errors/Exceptions: Leaves if new or ConstructL leaves.
       
   180 
       
   181      Status: Draft
       
   182     
       
   183 -------------------------------------------------------------------------------
       
   184 */
       
   185 CTestServerThreadStarter* CTestServerThreadStarter::NewL( )
       
   186 	{
       
   187 	CTestServerThreadStarter* self = new(ELeave) CTestServerThreadStarter();
       
   188 	CleanupStack::PushL( self );
       
   189 	self->ConstructL();
       
   190 	CleanupStack::Pop( self );
       
   191 	return self;
       
   192 	}
       
   193 
       
   194 /*
       
   195 -------------------------------------------------------------------------------
       
   196 
       
   197      Class: CTestServerThreadStarter
       
   198 
       
   199      Method: CTestServerThreadStarter
       
   200 
       
   201      Description: Default constructor.
       
   202 
       
   203 	 Default constructor.
       
   204 
       
   205      Parameters: None.
       
   206      
       
   207      Return Values: None.
       
   208 
       
   209      Errors/Exceptions: None.
       
   210 
       
   211      Status: Draft
       
   212     
       
   213 -------------------------------------------------------------------------------
       
   214 */
       
   215 CTestServerThreadStarter::CTestServerThreadStarter()
       
   216 :CActive( EPriorityNormal )
       
   217 	{
       
   218 	}
       
   219 
       
   220 /*
       
   221 -------------------------------------------------------------------------------
       
   222 
       
   223      Class: CTestServerThreadStarter
       
   224 
       
   225      Method: NewL
       
   226 
       
   227      Description: Default destructor.
       
   228 
       
   229 	 Default destructor.
       
   230 
       
   231      Parameters: None.
       
   232      
       
   233      Return Values: None.
       
   234 
       
   235      Errors/Exceptions: None.
       
   236 
       
   237      Status: Draft
       
   238     
       
   239 -------------------------------------------------------------------------------
       
   240 */
       
   241 CTestServerThreadStarter::~CTestServerThreadStarter()
       
   242 	{
       
   243 	Cancel();
       
   244 	iTimer.Close();
       
   245 	}
       
   246 
       
   247 /*
       
   248 -------------------------------------------------------------------------------
       
   249 
       
   250      Class: CTestServerThreadStarter
       
   251 
       
   252      Method: ConstructL
       
   253 
       
   254      Description: Second phase of two-phased constructor.
       
   255 
       
   256 	 Second phase of two-phased constructor.
       
   257 
       
   258      Parameters: None.
       
   259      
       
   260      Return Values: None.
       
   261 
       
   262      Errors/Exceptions: None.
       
   263 
       
   264      Status: Draft
       
   265     
       
   266 -------------------------------------------------------------------------------
       
   267 */
       
   268 void CTestServerThreadStarter::ConstructL()
       
   269 	{
       
   270 	TInt ret = iTimer.CreateLocal();
       
   271 	if ( ret != KErrNone )
       
   272 		{
       
   273 		User::Leave( ret );
       
   274 		}
       
   275 
       
   276 	CActiveScheduler::Add( this );
       
   277 	}
       
   278 
       
   279 /*
       
   280 -------------------------------------------------------------------------------
       
   281 
       
   282      Class: CTestServerThreadStarter
       
   283 
       
   284      Method: RunTestServerThread
       
   285 
       
   286      Description: Starts-up testserver.
       
   287 
       
   288 	 Starts-up testserver.
       
   289 
       
   290      Parameters: None.
       
   291      
       
   292      Return Values: KErrNone when there was no error.
       
   293 
       
   294      Errors/Exceptions: None.
       
   295 
       
   296      Status: Draft
       
   297     
       
   298 -------------------------------------------------------------------------------
       
   299 */
       
   300 TInt CTestServerThreadStarter::RunTestServerThread( CTestThreadContainerRunnerFactory* aTestThreadContainerRunnerFactory )
       
   301 	{
       
   302 	if ( IsActive() )
       
   303 		{
       
   304 		User::Panic( _L("E32USER-CBase"), 42 );
       
   305 		}
       
   306 		
       
   307 	TInt ret = iServerThreadStartedSemaphore.CreateLocal( 0 );
       
   308 	if ( ret != KErrNone )
       
   309 		{
       
   310 		User::Leave( ret );
       
   311 		}
       
   312 	
       
   313 	iTestThreadContainerRunnerFactory = aTestThreadContainerRunnerFactory;
       
   314 	iMainThreadId = RThread().Id();
       
   315 	iReturnCode = KErrNone;
       
   316 	
       
   317 	iStatus = KRequestPending;
       
   318 
       
   319 	SetActive();
       
   320 	iTimer.After( iStatus, 0 );
       
   321 	
       
   322 	return KErrNone;
       
   323 	}
       
   324 
       
   325 /*
       
   326 -------------------------------------------------------------------------------
       
   327 
       
   328      Class: CTestServerThreadStarter
       
   329 
       
   330      Method: RunL
       
   331 
       
   332      Description: RunL derived from CActive handles the completed requests.
       
   333 
       
   334 	 RunL derived from CActive handles the completed requests.
       
   335 
       
   336      Parameters: None.
       
   337      
       
   338      Return Values: None.
       
   339 
       
   340      Errors/Exceptions: Leaves if one of the called method leavs.
       
   341 
       
   342      Status: Draft
       
   343     
       
   344 -------------------------------------------------------------------------------
       
   345 */
       
   346 void CTestServerThreadStarter::RunL()
       
   347 	{
       
   348 	RThread testServerStarterThread;	
       
   349 	TInt ret = testServerStarterThread.Create( _L("testserverstarterthread"), 
       
   350 			TestServerStarterThreadFunction, 10 * KDefaultStackSize, 10 * KDefaultHeapSize, 10 * KMaxHeapSize, this );
       
   351 	
       
   352 	User::LeaveIfError( ret );
       
   353 		
       
   354 	testServerStarterThread.Resume();	
       
   355 	testServerStarterThread.Close();
       
   356 	
       
   357 	iServerThreadStartedSemaphore.Wait();
       
   358 	iServerThreadStartedSemaphore.Close();
       
   359 	
       
   360 	// Delete CTestServerThreadStarter after testserver is started
       
   361 	delete this;
       
   362 	}
       
   363 
       
   364 /*
       
   365 -------------------------------------------------------------------------------
       
   366 
       
   367      Class: CTestServerThreadStarter
       
   368 
       
   369      Method: DoCancel
       
   370 
       
   371      Description: DoCancel derived from CActive handles the Cancel.
       
   372 
       
   373 	 DoCancel derived from CActive handles the Cancel.
       
   374 
       
   375      Parameters: None.
       
   376      
       
   377      Return Values: None.
       
   378 
       
   379      Errors/Exceptions: None.
       
   380 
       
   381      Status: Draft
       
   382     
       
   383 -------------------------------------------------------------------------------
       
   384 */
       
   385 void CTestServerThreadStarter::DoCancel()
       
   386 	{
       
   387 	if ( IsActive() )
       
   388 		{
       
   389 		iTimer.Cancel();
       
   390 		}
       
   391 	}
       
   392 
       
   393 // End of File