1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Tcp/Ip link that manages execserver process.
22 *//*--------------------------------------------------------------------*/
24 #include "xeLocalTcpIpLink.hpp"
32 SERVER_START_TIMEOUT = 1000,
33 SERVER_START_IDLE_SLEEP = 50
39 LocalTcpIpLink::LocalTcpIpLink (void)
44 LocalTcpIpLink::~LocalTcpIpLink (void)
49 void LocalTcpIpLink::start (const char* execServerPath, const char* workDir, int port)
53 std::ostringstream cmdLine;
54 cmdLine << execServerPath << " --single --port=" << port;
56 m_process = deProcess_create();
59 if (deProcess_start(m_process, cmdLine.str().c_str(), workDir) != DE_TRUE)
61 std::string err = deProcess_getLastError(m_process);
62 deProcess_destroy(m_process);
65 XE_FAIL((std::string("Failed to start ExecServer '") + execServerPath + "' : " + err).c_str());
70 de::SocketAddress address;
71 address.setFamily (DE_SOCKETFAMILY_INET4);
72 address.setProtocol (DE_SOCKETPROTOCOL_TCP);
73 address.setHost ("127.0.0.1");
74 address.setPort (port);
76 // Wait until server has started - \todo [2012-07-19 pyry] This could be improved by having server to signal when it is ready.
77 deUint64 waitStart = deGetMicroseconds();
80 if (!deProcess_isRunning(m_process))
81 XE_FAIL("ExecServer died");
85 m_link.connect(address);
88 catch (const de::SocketError&)
90 if (deGetMicroseconds()-waitStart > SERVER_START_TIMEOUT*1000)
91 XE_FAIL("Server start timeout");
93 deSleep(SERVER_START_IDLE_SLEEP);
97 // Close stdout/stderr or otherwise process will hang once OS pipe buffers are full.
98 // \todo [2012-07-19 pyry] Read and store stdout/stderr from execserver.
99 XE_CHECK(deProcess_closeStdOut(m_process));
100 XE_CHECK(deProcess_closeStdErr(m_process));
102 catch (const std::exception&)
109 void LocalTcpIpLink::stop (void)
119 // Silently ignore since this is called in destructor.
122 // \note --single flag is used so execserver should kill itself once one connection is handled.
123 // This is here to make sure it dies even in case of hang.
124 deProcess_terminate (m_process);
125 deProcess_waitForFinish (m_process);
126 deProcess_destroy (m_process);
132 void LocalTcpIpLink::reset (void)
137 CommLinkState LocalTcpIpLink::getState (void) const
140 return COMMLINKSTATE_ERROR;
142 return m_link.getState();
145 CommLinkState LocalTcpIpLink::getState (std::string& error) const
149 error = "Not started";
150 return COMMLINKSTATE_ERROR;
153 return m_link.getState();
156 void LocalTcpIpLink::setCallbacks (StateChangedFunc stateChangedCallback, LogDataFunc testLogDataCallback, LogDataFunc infoLogDataCallback, void* userPtr)
158 m_link.setCallbacks(stateChangedCallback, testLogDataCallback, infoLogDataCallback, userPtr);
161 void LocalTcpIpLink::startTestProcess (const char* name, const char* params, const char* workingDir, const char* caseList)
164 m_link.startTestProcess(name, params, workingDir, caseList);
166 XE_FAIL("Not started");
169 void LocalTcpIpLink::stopTestProcess (void)
172 m_link.stopTestProcess();
174 XE_FAIL("Not started");