1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Execution Server
3e5c31af7Sopenharmony_ci * ---------------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief ExecServer main().
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "xsExecutionServer.hpp"
25e5c31af7Sopenharmony_ci#include "deCommandLine.hpp"
26e5c31af7Sopenharmony_ci#include "deString.h"
27e5c31af7Sopenharmony_ci
28e5c31af7Sopenharmony_ci#if (DE_OS == DE_OS_WIN32)
29e5c31af7Sopenharmony_ci#	include "xsWin32TestProcess.hpp"
30e5c31af7Sopenharmony_ci#else
31e5c31af7Sopenharmony_ci#	include "xsPosixTestProcess.hpp"
32e5c31af7Sopenharmony_ci#endif
33e5c31af7Sopenharmony_ci
34e5c31af7Sopenharmony_ci#include <iostream>
35e5c31af7Sopenharmony_ci
36e5c31af7Sopenharmony_cinamespace opt
37e5c31af7Sopenharmony_ci{
38e5c31af7Sopenharmony_ci
39e5c31af7Sopenharmony_ciDE_DECLARE_COMMAND_LINE_OPT(Port,		int);
40e5c31af7Sopenharmony_ciDE_DECLARE_COMMAND_LINE_OPT(SingleExec,	bool);
41e5c31af7Sopenharmony_ci
42e5c31af7Sopenharmony_civoid registerOptions (de::cmdline::Parser& parser)
43e5c31af7Sopenharmony_ci{
44e5c31af7Sopenharmony_ci	using de::cmdline::Option;
45e5c31af7Sopenharmony_ci	using de::cmdline::NamedValue;
46e5c31af7Sopenharmony_ci
47e5c31af7Sopenharmony_ci	parser << Option<Port>		("p", "port",	"Port", "50016")
48e5c31af7Sopenharmony_ci		   << Option<SingleExec>("s", "single",	"Kill execserver after first session");
49e5c31af7Sopenharmony_ci}
50e5c31af7Sopenharmony_ci
51e5c31af7Sopenharmony_ci}
52e5c31af7Sopenharmony_ci
53e5c31af7Sopenharmony_ciint main (int argc, const char* const* argv)
54e5c31af7Sopenharmony_ci{
55e5c31af7Sopenharmony_ci	de::cmdline::CommandLine	cmdLine;
56e5c31af7Sopenharmony_ci
57e5c31af7Sopenharmony_ci#if (DE_OS == DE_OS_WIN32)
58e5c31af7Sopenharmony_ci	xs::Win32TestProcess		testProcess;
59e5c31af7Sopenharmony_ci#else
60e5c31af7Sopenharmony_ci	xs::PosixTestProcess		testProcess;
61e5c31af7Sopenharmony_ci
62e5c31af7Sopenharmony_ci	// Set line buffered mode to stdout so executor gets any log messages in a timely manner.
63e5c31af7Sopenharmony_ci	setvbuf(stdout, DE_NULL, _IOLBF, 4*1024);
64e5c31af7Sopenharmony_ci#endif
65e5c31af7Sopenharmony_ci
66e5c31af7Sopenharmony_ci	// Parse command line.
67e5c31af7Sopenharmony_ci	{
68e5c31af7Sopenharmony_ci		de::cmdline::Parser	parser;
69e5c31af7Sopenharmony_ci		opt::registerOptions(parser);
70e5c31af7Sopenharmony_ci
71e5c31af7Sopenharmony_ci		if (!parser.parse(argc, argv, &cmdLine, std::cerr))
72e5c31af7Sopenharmony_ci		{
73e5c31af7Sopenharmony_ci			parser.help(std::cout);
74e5c31af7Sopenharmony_ci			return -1;
75e5c31af7Sopenharmony_ci		}
76e5c31af7Sopenharmony_ci	}
77e5c31af7Sopenharmony_ci
78e5c31af7Sopenharmony_ci	try
79e5c31af7Sopenharmony_ci	{
80e5c31af7Sopenharmony_ci		const xs::ExecutionServer::RunMode	runMode		= cmdLine.getOption<opt::SingleExec>()
81e5c31af7Sopenharmony_ci														? xs::ExecutionServer::RUNMODE_SINGLE_EXEC
82e5c31af7Sopenharmony_ci														: xs::ExecutionServer::RUNMODE_FOREVER;
83e5c31af7Sopenharmony_ci		const int							port		= cmdLine.getOption<opt::Port>();
84e5c31af7Sopenharmony_ci		xs::ExecutionServer					server		(&testProcess, DE_SOCKETFAMILY_INET4, port, runMode);
85e5c31af7Sopenharmony_ci
86e5c31af7Sopenharmony_ci		std::cout << "Listening on port " << port << ".\n";
87e5c31af7Sopenharmony_ci		server.runServer();
88e5c31af7Sopenharmony_ci	}
89e5c31af7Sopenharmony_ci	catch (const std::exception& e)
90e5c31af7Sopenharmony_ci	{
91e5c31af7Sopenharmony_ci		std::cerr << e.what() << "\n";
92e5c31af7Sopenharmony_ci		return -1;
93e5c31af7Sopenharmony_ci	}
94e5c31af7Sopenharmony_ci
95e5c31af7Sopenharmony_ci	return 0;
96e5c31af7Sopenharmony_ci}
97