commands/testexecute/testexecute.cpp
changeset 5 9c58252c6740
equal deleted inserted replaced
4:c061fa280d92 5:9c58252c6740
       
     1 // testexecute.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/memoryaccesscmd.h>
       
    14 #include <fshell/descriptorutils.h>
       
    15 #include "proxyserver.h"
       
    16 #include <test/testexecutelogger.h>
       
    17 
       
    18 using namespace IoUtils;
       
    19 
       
    20 class CCmdTestExecute : public CMemoryAccessCommandBase, public MMessageHandler
       
    21 	{
       
    22 public:
       
    23 	static CCommandBase* NewLC();
       
    24 	~CCmdTestExecute();
       
    25 private:
       
    26 	CCmdTestExecute();
       
    27 
       
    28 private: // From MMessageHandler
       
    29 	virtual TBool HandleMessageL(CProxySession* aSession, const RMessage2& aMessage);
       
    30 
       
    31 private: // From CCommandBase.
       
    32 	virtual const TDesC& Name() const;
       
    33 	virtual void DoRunL();
       
    34 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    35 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    36 
       
    37 private: // From CActive
       
    38 	void RunL();
       
    39 	void DoCancel();
       
    40 
       
    41 	class CLogonCompleter : public CActive
       
    42 		{
       
    43 	public:
       
    44 		CLogonCompleter(CCmdTestExecute* aCommand) : CActive(CActive::EPriorityStandard), iCommand(aCommand)
       
    45 			{
       
    46 			CActiveScheduler::Add(this);
       
    47 			iCommand->iTefProc.Logon(iStatus);
       
    48 			SetActive();
       
    49 			}
       
    50 		void RunL() { iCommand->Complete(iStatus.Int()); }
       
    51 		void DoCancel() { iCommand->iTefProc.LogonCancel(iStatus); }
       
    52 		~CLogonCompleter() { Cancel(); }
       
    53 
       
    54 	private:
       
    55 		CCmdTestExecute* iCommand;
       
    56 		};
       
    57 
       
    58 private:
       
    59 	CProxyServer* iProxy;
       
    60 	RTestExecuteLogServ iLogger;
       
    61 	RBuf8 iTempBuf;
       
    62 
       
    63 	TFileName2 iScriptFile;
       
    64 	TBool iIgnoredT, iIgnoredC;
       
    65 	HBufC* iIncludes;
       
    66 	HBufC* iExcludes;
       
    67 	TBool iDebug;
       
    68 	CLogonCompleter* iLogonCompleter;
       
    69 	RProcess iTefProc;
       
    70 	};
       
    71 
       
    72 
       
    73 EXE_BOILER_PLATE(CCmdTestExecute)
       
    74 
       
    75 CCommandBase* CCmdTestExecute::NewLC()
       
    76 	{
       
    77 	CCmdTestExecute* self = new(ELeave) CCmdTestExecute();
       
    78 	CleanupStack::PushL(self);
       
    79 	self->BaseConstructL();
       
    80 	return self;
       
    81 	}
       
    82 
       
    83 CCmdTestExecute::~CCmdTestExecute()
       
    84 	{
       
    85 	Cancel();
       
    86 	if (iProxy)
       
    87 		{
       
    88 		iProxy->Destroy();
       
    89 		}
       
    90 	iLogger.Close();
       
    91 	iTempBuf.Close();
       
    92 	delete iIncludes;
       
    93 	delete iExcludes;
       
    94 	delete iLogonCompleter;
       
    95 	iTefProc.Close();
       
    96 	}
       
    97 
       
    98 CCmdTestExecute::CCmdTestExecute()
       
    99 	: CMemoryAccessCommandBase(EManualComplete | ESharableIoSession)
       
   100 	{
       
   101 	iTefProc.SetHandle(0); // Don't default to the current process
       
   102 	}
       
   103 
       
   104 const TDesC& CCmdTestExecute::Name() const
       
   105 	{
       
   106 	_LIT(KName, "testexecute");	
       
   107 	return KName;
       
   108 	}
       
   109 
       
   110 void CCmdTestExecute::ArgumentsL(RCommandArgumentList& aArguments)
       
   111 	{
       
   112 	aArguments.AppendFileNameL(iScriptFile, _L("script-file"));
       
   113 	}
       
   114 
       
   115 void CCmdTestExecute::OptionsL(RCommandOptionList& aOptions)
       
   116 	{
       
   117 	aOptions.AppendBoolL(iDebug, _L("debug"));
       
   118 	aOptions.AppendBoolL(iIgnoredT, _L("ignore"));
       
   119 	aOptions.AppendBoolL(iIgnoredC, _L("ignore2"));
       
   120 	aOptions.AppendStringL(iIncludes, _L("include"));
       
   121 	aOptions.AppendStringL(iExcludes, _L("exclude"));
       
   122 	}
       
   123 
       
   124 #define CTRL(x) ((x)-'a'+1)
       
   125 
       
   126 void CCmdTestExecute::DoRunL()
       
   127 	{
       
   128 	LoadMemoryAccessL();
       
   129 	iTempBuf.CreateL(1024);
       
   130 	User::LeaveIfError(Stdin().CaptureKey(CTRL('c'), 0, 0)); // So we can do cleanup
       
   131 	Stdin().WaitForKey(iStatus);
       
   132 	SetActive();
       
   133 
       
   134 
       
   135 	User::LeaveIfError(iLogger.Connect()); // Keep it open
       
   136 	iLogger.CreateLog(_L("\\logs\\testexecute\\fshelllovesyou"), RTestExecuteLogServ::ELogModeOverWrite); // The server won't actually stay open unless you call CreateLog!
       
   137 
       
   138 	// Set up our proxy
       
   139 	iProxy = CProxyServer::NewInSeparateThreadL(_L("TestExecuteLogEngine"), this);
       
   140 
       
   141 	if (iScriptFile.Length())
       
   142 		{
       
   143 		LtkUtils::RLtkBuf cmdLine;
       
   144 		CleanupClosePushL(cmdLine);
       
   145 		cmdLine.AppendL(iScriptFile);
       
   146 		if (iDebug) cmdLine.AppendL(_L(" -d"));
       
   147 		if (iIncludes) cmdLine.AppendFormatL(_L(" -tci %S"), iIncludes);
       
   148 		if (iExcludes) cmdLine.AppendFormatL(_L(" -tcx %S"), iExcludes);
       
   149 
       
   150 		LeaveIfErr(iTefProc.Create(_L("testexecute.exe"), cmdLine), _L("Couldn't create testexecute.exe %S"), &cmdLine);
       
   151 		iLogonCompleter = new(ELeave) CLogonCompleter(this);
       
   152 		iTefProc.Resume();
       
   153 		CleanupStack::PopAndDestroy(&cmdLine);
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		// Just do some tests
       
   158 		RTestExecuteLogServ newlogger;
       
   159 		LeaveIfErr(newlogger.Connect(), _L("Couldn't connect to proxied RTestExecuteLogServ"));
       
   160 
       
   161 		newlogger.CreateLog(_L("\\logs\\testexecute\\scriptyscripty"), RTestExecuteLogServ::ELogModeOverWrite);
       
   162 		newlogger.Write(_L("I AM A LOG"));
       
   163 		newlogger.Close();
       
   164 		}
       
   165 	}
       
   166 
       
   167 TBool CCmdTestExecute::HandleMessageL(CProxySession* /*aSession*/, const RMessage2& aMessage)
       
   168 	{
       
   169 	switch (aMessage.Function())
       
   170 		{
       
   171 	case RTestExecuteLogServ::EWriteLog:
       
   172 		{
       
   173 		aMessage.ReadL(0, iTempBuf);
       
   174 		Write(iTempBuf.Expand());
       
   175 		break;
       
   176 		}
       
   177 	default:
       
   178 		break;
       
   179 		}
       
   180 
       
   181 	return EFalse; // We allow everything to go through to the real TEF as well
       
   182 	}
       
   183 
       
   184 void CCmdTestExecute::RunL()
       
   185 	{
       
   186 	if (Stdin().KeyCode() == CTRL('c'))
       
   187 		{
       
   188 		Printf(_L("CTRL-C detected, cleaning up proxy server...\r\n"));
       
   189 		iProxy->Destroy();
       
   190 		iProxy = NULL;
       
   191 		Printf(_L("Exiting...\r\n"));
       
   192 		SetErrorReported(ETrue); // Supress complaints about the cancel
       
   193 		Complete(KErrCancel);
       
   194 		}
       
   195 	else
       
   196 		{
       
   197 		Stdin().WaitForKey(iStatus);
       
   198 		SetActive();
       
   199 		}
       
   200 	}
       
   201 
       
   202 void CCmdTestExecute::DoCancel()
       
   203 	{
       
   204 	Stdin().WaitForKeyCancel();
       
   205 	}