os/ossrv/genericservices/taskscheduler/Test/MinimalTaskHandler/minimaltaskhandler.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/taskscheduler/Test/MinimalTaskHandler/minimaltaskhandler.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,133 @@
     1.4 +// Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <schinfo.h>
    1.20 +#include <schinfointernal.h>
    1.21 +#include <schtask.h>
    1.22 +#include <s32file.h>
    1.23 +#include <e32math.h>
    1.24 +#include <e32cons.h>
    1.25 +
    1.26 +// Constants
    1.27 +_LIT(KMinimalTaskConsoleName, "MinimalTaskExecutor");
    1.28 +
    1.29 +static void SignalTestExe()
    1.30 +	{
    1.31 +	_LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
    1.32 +	RSemaphore sem;
    1.33 +	TInt ret = sem.OpenGlobal(KSchSemaphoreName);
    1.34 +	if (ret == KErrNone)
    1.35 +		{
    1.36 +		sem.Signal();
    1.37 +		sem.Close();
    1.38 +		}
    1.39 +	}
    1.40 +
    1.41 +//***********************************************************************************
    1.42 +LOCAL_D TInt GetRandomNumber(const TInt aLow, const TInt aHigh, TInt64& aSeed)
    1.43 +	{
    1.44 +	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
    1.45 +	TInt32 rand;
    1.46 +
    1.47 +	// Round to 0 decimal places, ie. the nearest whole numer
    1.48 +	Math::Round(initialRand, initialRand, 0);
    1.49 +	Math::Int(rand, initialRand);
    1.50 +
    1.51 +	return (aLow + rand);
    1.52 +	}
    1.53 +
    1.54 +//***********************************************************************************
    1.55 +LOCAL_D void ConstructConsoleL(RFile& aTaskFile)
    1.56 +	{
    1.57 +	CConsoleBase* console=Console::NewL(KMinimalTaskConsoleName, TSize(KConsFullScreen, KConsFullScreen));
    1.58 +	CleanupStack::PushL(console);
    1.59 +	console->Printf(_L(" contents of task file\n"));
    1.60 +	
    1.61 +	CFileStore* store;
    1.62 +	// Open the filestore
    1.63 +	store = CDirectFileStore::FromLC(aTaskFile);//pushes store
    1.64 +	RStoreReadStream instream;
    1.65 +	instream.OpenLC(*store,store->Root());//pushes instream
    1.66 +
    1.67 +	// Get task count
    1.68 +	TInt count = instream.ReadInt32L();
    1.69 +	for (TInt i=0;i<count;i++)
    1.70 +		{
    1.71 +		CScheduledTask* task = CScheduledTask::NewLC(instream);
    1.72 +		
    1.73 +		TBuf<150> buf;
    1.74 +		buf.Format(_L("Running task \"%S\""), &task->Info().iName);
    1.75 +		User::LeaveIfError(User::InfoPrint(buf));
    1.76 +		
    1.77 +		console->Printf(task->Info().iName);
    1.78 +		console->Printf(_L("\n"));
    1.79 +		HBufC* data = const_cast<HBufC*>(&(task->Data()));
    1.80 +		console->Printf(*data);
    1.81 +		console->Printf(_L("\n"));
    1.82 +		console->Printf(_L("%d \n"),task->Info().iTaskId);
    1.83 +		TTsTime tstime = task->ValidUntil();
    1.84 +		const TTime time = tstime.GetLocalTime();
    1.85 +		TBuf<30> dateString;
    1.86 +		time.FormatL(dateString,(_L("%H%:1%T%*E%*D%X%*N%Y %1 %2 %3")));
    1.87 +		console->Printf(_L(":%S\n"), &dateString);
    1.88 +		CleanupStack::PopAndDestroy(task);
    1.89 +		}
    1.90 +	console->Printf(_L("Pausing for a one second..."));
    1.91 +	User::After(1000000);
    1.92 +	CleanupStack::PopAndDestroy(3); //console, store, instream
    1.93 +	}
    1.94 +
    1.95 +
    1.96 +//***********************************************************************************
    1.97 +LOCAL_D TInt Execute()
    1.98 +	{
    1.99 +	TInt err = KErrNoMemory;
   1.100 +	CTrapCleanup* cleanup=CTrapCleanup::New();	//can fail
   1.101 +	if (cleanup)
   1.102 +		{
   1.103 +		RFile file;
   1.104 +		
   1.105 +		// Adopt the task file from the Task Scheduler
   1.106 +		err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
   1.107 +									TScheduledTaskFile::FileHandleIndex());
   1.108 +		if (err != KErrNone)
   1.109 +			return err;
   1.110 +		
   1.111 +		// The aParam is the name of a file where the relevant CTaskExCmdLine is
   1.112 +		// do the executing 
   1.113 +		TRAPD(err, ConstructConsoleL(file));
   1.114 +		if(err == KErrNone)
   1.115 +			{
   1.116 +			// Sometimes we want to return a bogus error value, 
   1.117 +			// sometimes we don't.
   1.118 +			TTime now;
   1.119 +			now.HomeTime();
   1.120 +			TInt64 seed = now.Int64();
   1.121 +			err = GetRandomNumber(-50, 200, seed); //20% chance of error being returned
   1.122 +			}
   1.123 +		
   1.124 +		file.Close();// Close the file		
   1.125 +		delete cleanup;
   1.126 +		}
   1.127 +	SignalTestExe();		
   1.128 +	return err;
   1.129 +	}
   1.130 +
   1.131 +
   1.132 +//***********************************************************************************
   1.133 +GLDEF_C TInt E32Main()
   1.134 +	{
   1.135 +	return Execute();
   1.136 +	}