1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*-------------------------------------------------------------------------
* drawElements Quality Program Test Executor
* ------------------------------------------
*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*//*!
* \file
* \brief Tcp/Ip link that manages execserver process.
*//*--------------------------------------------------------------------*/
#include "xeLocalTcpIpLink.hpp"
#include "deClock.h"
#include "deThread.h"
#include <sstream>
enum
{
SERVER_START_TIMEOUT = 1000,
SERVER_START_IDLE_SLEEP = 50
};
namespace xe
{
LocalTcpIpLink::LocalTcpIpLink (void)
: m_process(DE_NULL)
{
}
LocalTcpIpLink::~LocalTcpIpLink (void)
{
stop();
}
void LocalTcpIpLink::start (const char* execServerPath, const char* workDir, int port)
{
XE_CHECK(!m_process);
std::ostringstream cmdLine;
cmdLine << execServerPath << " --single --port=" << port;
m_process = deProcess_create();
XE_CHECK(m_process);
if (deProcess_start(m_process, cmdLine.str().c_str(), workDir) != DE_TRUE)
{
std::string err = deProcess_getLastError(m_process);
deProcess_destroy(m_process);
m_process = DE_NULL;
XE_FAIL((std::string("Failed to start ExecServer '") + execServerPath + "' : " + err).c_str());
}
try
{
de::SocketAddress address;
address.setFamily (DE_SOCKETFAMILY_INET4);
address.setProtocol (DE_SOCKETPROTOCOL_TCP);
address.setHost ("127.0.0.1");
address.setPort (port);
// Wait until server has started - \todo [2012-07-19 pyry] This could be improved by having server to signal when it is ready.
deUint64 waitStart = deGetMicroseconds();
for (;;)
{
if (!deProcess_isRunning(m_process))
XE_FAIL("ExecServer died");
try
{
m_link.connect(address);
break;
}
catch (const de::SocketError&)
{
if (deGetMicroseconds()-waitStart > SERVER_START_TIMEOUT*1000)
XE_FAIL("Server start timeout");
deSleep(SERVER_START_IDLE_SLEEP);
}
}
// Close stdout/stderr or otherwise process will hang once OS pipe buffers are full.
// \todo [2012-07-19 pyry] Read and store stdout/stderr from execserver.
XE_CHECK(deProcess_closeStdOut(m_process));
XE_CHECK(deProcess_closeStdErr(m_process));
}
catch (const std::exception&)
{
stop();
throw;
}
}
void LocalTcpIpLink::stop (void)
{
if (m_process)
{
try
{
m_link.disconnect();
}
catch (...)
{
// Silently ignore since this is called in destructor.
}
// \note --single flag is used so execserver should kill itself once one connection is handled.
// This is here to make sure it dies even in case of hang.
deProcess_terminate (m_process);
deProcess_waitForFinish (m_process);
deProcess_destroy (m_process);
m_process = DE_NULL;
}
}
void LocalTcpIpLink::reset (void)
{
m_link.reset();
}
CommLinkState LocalTcpIpLink::getState (void) const
{
if (!m_process)
return COMMLINKSTATE_ERROR;
else
return m_link.getState();
}
CommLinkState LocalTcpIpLink::getState (std::string& error) const
{
if (!m_process)
{
error = "Not started";
return COMMLINKSTATE_ERROR;
}
else
return m_link.getState();
}
void LocalTcpIpLink::setCallbacks (StateChangedFunc stateChangedCallback, LogDataFunc testLogDataCallback, LogDataFunc infoLogDataCallback, void* userPtr)
{
m_link.setCallbacks(stateChangedCallback, testLogDataCallback, infoLogDataCallback, userPtr);
}
void LocalTcpIpLink::startTestProcess (const char* name, const char* params, const char* workingDir, const char* caseList)
{
if (m_process)
m_link.startTestProcess(name, params, workingDir, caseList);
else
XE_FAIL("Not started");
}
void LocalTcpIpLink::stopTestProcess (void)
{
if (m_process)
m_link.stopTestProcess();
else
XE_FAIL("Not started");
}
} // xe
|