13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Tester Core
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Android ExecServer.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuAndroidExecService.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deFile.h"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deClock.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if 0
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#	define DBG_PRINT(ARGS) print ARGS
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#	define DBG_PRINT(ARGS)
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace tcu
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace Android
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const char* LOG_FILE_NAME = "/sdcard/dEQP-log.qpa";
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyryenum
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	PROCESS_START_TIMEOUT	= 5000*1000,	//!< Timeout in usec.
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	PROCESS_QUERY_INTERVAL	= 1000*1000		//!< Running query interval limit in usec.
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void checkJniException (JNIEnv* env, const char* file, int line)
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (env->ExceptionOccurred())
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->ExceptionDescribe();
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->ExceptionClear();
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw InternalError("JNI Exception", DE_NULL, file, line);
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define JNI_CHECK(EXPR) do { checkJniException(env, __FILE__, __LINE__); TCU_CHECK_INTERNAL(EXPR); } while (deGetFalse())
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// TestProcess
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTestProcess::TestProcess (JavaVM* vm, jobject context)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_vm					(vm)
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_remoteCls			(0)
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_remote				(0)
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_start				(0)
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_kill				(0)
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_isRunning			(0)
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_launchTime			(0)
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_lastQueryTime		(0)
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_lastRunningStatus	(false)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_logReader			(xs::LOG_BUFFER_BLOCK_SIZE, xs::LOG_BUFFER_NUM_BLOCKS)
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::TestProcess(%p, %p)", vm, context));
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv* env			= getCurrentThreadEnv();
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jobject	remote		= 0;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jstring	logFileName	= 0;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		jclass		remoteCls	= 0;
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		jmethodID	ctorId		= 0;
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		remoteCls = env->FindClass("com/drawelements/deqp/testercore/RemoteAPI");
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(remoteCls);
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Acquire global reference to RemoteAPI class.
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_remoteCls = reinterpret_cast<jclass>(env->NewGlobalRef(remoteCls));
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(m_remoteCls);
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->DeleteLocalRef(remoteCls);
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		remoteCls = 0;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ctorId = env->GetMethodID(m_remoteCls, "<init>", "(Landroid/content/Context;Ljava/lang/String;)V");
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(ctorId);
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		logFileName = env->NewStringUTF(LOG_FILE_NAME);
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(logFileName);
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Create RemoteAPI instance.
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		remote = env->NewObject(m_remoteCls, ctorId, context, logFileName);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(remote);
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->DeleteLocalRef(logFileName);
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		logFileName = 0;
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Acquire global reference to remote.
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_remote = env->NewGlobalRef(remote);
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(m_remote);
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->DeleteLocalRef(remote);
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		remote = 0;
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_start	= env->GetMethodID(m_remoteCls, "start", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z");
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(m_start);
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_kill = env->GetMethodID(m_remoteCls, "kill", "()Z");
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(m_kill);
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_isRunning = env->GetMethodID(m_remoteCls, "isRunning", "()Z");
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(m_isRunning);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (...)
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (logFileName)
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteLocalRef(logFileName);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (remote)
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteLocalRef(remote);
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_remoteCls)
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteGlobalRef(reinterpret_cast<jobject>(m_remoteCls));
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_remote)
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteGlobalRef(m_remote);
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw;
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1353c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTestProcess::~TestProcess (void)
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::~TestProcess()"));
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNIEnv* env = getCurrentThreadEnv();
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->DeleteGlobalRef(m_remote);
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->DeleteGlobalRef(m_remoteCls);
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (...)
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid TestProcess::start (const char* name, const char* params, const char* workingDir, const char* caseList)
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::start(%s, %s, %s, ...)", name, params, workingDir));
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv* env			= getCurrentThreadEnv();
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jstring	nameStr		= 0;
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jstring	paramsStr	= 0;
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jstring caseListStr	= 0;
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_UNREF(workingDir);
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Remove old log file if such exists.
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (deFileExists(LOG_FILE_NAME))
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (!deDeleteFile(LOG_FILE_NAME) || deFileExists(LOG_FILE_NAME))
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			throw xs::TestProcessException(std::string("Failed to remove '") + LOG_FILE_NAME + "'");
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		nameStr = env->NewStringUTF(name);
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(nameStr);
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		paramsStr = env->NewStringUTF(params);
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(paramsStr);
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		caseListStr = env->NewStringUTF(caseList);
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		JNI_CHECK(caseListStr);
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		jboolean res = env->CallBooleanMethod(m_remote, m_start, nameStr, paramsStr, caseListStr);
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		checkJniException(env, __FILE__, __LINE__);
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (res == JNI_FALSE)
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			throw xs::TestProcessException("Failed to launch activity");
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_launchTime		= deGetMicroseconds();
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_lastQueryTime		= m_launchTime;
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_lastRunningStatus	= true;
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (...)
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (nameStr)
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteLocalRef(nameStr);
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (paramsStr)
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteLocalRef(paramsStr);
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (caseListStr)
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			env->DeleteLocalRef(caseListStr);
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw;
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	env->DeleteLocalRef(nameStr);
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	env->DeleteLocalRef(paramsStr);
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	env->DeleteLocalRef(caseListStr);
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid TestProcess::terminate (void)
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::terminate()"));
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv*		env		= getCurrentThreadEnv();
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jboolean	res		= env->CallBooleanMethod(m_remote, m_kill);
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	checkJniException(env, __FILE__, __LINE__);
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_UNREF(res); // Failure to kill process is ignored.
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid TestProcess::cleanup (void)
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::cleanup()"));
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	terminate();
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_logReader.stop();
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool TestProcess::isRunning (void)
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint64 curTime = deGetMicroseconds();
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// On Android process launch is asynchronous so we don't want to poll for process until after some time.
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (curTime-m_launchTime < PROCESS_START_TIMEOUT ||
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		curTime-m_lastQueryTime < PROCESS_QUERY_INTERVAL)
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return m_lastRunningStatus;
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv*		env		= getCurrentThreadEnv();
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jboolean	res		= env->CallBooleanMethod(m_remote, m_isRunning);
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	checkJniException(env, __FILE__, __LINE__);
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DBG_PRINT(("TestProcess::isRunning(): %s", res == JNI_TRUE ? "true" : "false"));
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_lastQueryTime		= curTime;
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_lastRunningStatus	= res == JNI_TRUE;
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return m_lastRunningStatus;
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2433c827367444ee418f129b2c238299f49d3264554Jarkko PoyryJNIEnv* TestProcess::getCurrentThreadEnv (void)
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv* env = DE_NULL;
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jint	ret	= m_vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (ret == JNI_OK)
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return env;
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw InternalError("GetEnv() failed");
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint TestProcess::readTestLog (deUint8* dst, int numBytes)
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!m_logReader.isRunning())
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (deGetMicroseconds() - m_launchTime > xs::LOG_FILE_TIMEOUT*1000)
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Timeout, kill process.
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			terminate();
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DBG_PRINT(("TestProcess:readTestLog(): Log file timeout occurred!"));
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return 0; // \todo [2013-08-13 pyry] Throw exception?
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (!deFileExists(LOG_FILE_NAME))
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return 0;
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Start reader.
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_logReader.start(LOG_FILE_NAME);
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(m_logReader.isRunning());
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return m_logReader.read(dst, numBytes);
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint	TestProcess::getExitCode (void) const
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint TestProcess::readInfoLog (deUint8* dst, int numBytes)
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2012-11-12 pyry] Read device log.
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_UNREF(dst && numBytes);
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ExecutionServer
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2913c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecutionServer::ExecutionServer (JavaVM* vm, xs::TestProcess* testProcess, deSocketFamily family, int port, RunMode runMode)
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: xs::ExecutionServer	(testProcess, family, port, runMode)
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_vm					(vm)
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryxs::ConnectionHandler* ExecutionServer::createHandler (de::Socket* socket, const de::SocketAddress& clientAddress)
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_UNREF(clientAddress);
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return new ConnectionHandler(m_vm, this, socket);
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ConnectionHandler
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3053c827367444ee418f129b2c238299f49d3264554Jarkko PoyryConnectionHandler::ConnectionHandler (JavaVM* vm, xs::ExecutionServer* server, de::Socket* socket)
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: xs::ExecutionRequestHandler	(server, socket)
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_vm							(vm)
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ConnectionHandler::run (void)
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JNIEnv* env = DE_NULL;
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_vm->AttachCurrentThread(&env, DE_NULL) != 0)
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		print("AttachCurrentThread() failed");
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return;
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xs::ExecutionRequestHandler::run();
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_vm->DetachCurrentThread() != 0)
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		print("DetachCurrentThread() failed");
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ServerThread
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3283c827367444ee418f129b2c238299f49d3264554Jarkko PoyryServerThread::ServerThread (JavaVM* vm, xs::TestProcess* process, deSocketFamily family, int port)
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_server(vm, process, family, port, xs::ExecutionServer::RUNMODE_FOREVER)
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ServerThread::run (void)
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_server.runServer();
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (const std::exception& e)
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		die("ServerThread::run(): %s", e.what());
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ServerThread::stop (void)
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_server.stopServer();
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	join();
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ExecService
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353330f6e62b24a80eb92f08a60eb6b769480572be9Pyry HaulosExecService::ExecService (JavaVM* vm, jobject context, int port, deSocketFamily family)
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_process		(vm, context)
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_thread		(vm, &m_process, family, port)
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3593c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecService::~ExecService (void)
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ExecService::start (void)
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_thread.start();
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ExecService::stop (void)
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_thread.stop();
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // Android
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // tcu
375