1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Execution Server
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 ExecServer Client.
22 *//*--------------------------------------------------------------------*/
25 #include "xsProtocol.hpp"
26 #include "deSocket.hpp"
27 #include "deUniquePtr.hpp"
43 typedef de::UniquePtr<Message> ScopedMsgPtr;
45 class SocketError : public Error
48 SocketError (deSocketResult result, const char* message, const char* file, int line)
49 : Error (message, deGetSocketResultName(result), file, line)
54 deSocketResult getResult (void) const
60 deSocketResult m_result;
64 void sendMessage (de::Socket& socket, const Message& message)
72 while (pos < buf.size())
74 size_t numLeft = buf.size() - pos;
76 deSocketResult result = socket.send(&buf[pos], numLeft, &numSent);
78 if (result != DE_SOCKETRESULT_SUCCESS)
79 throw SocketError(result, "send() failed", __FILE__, __LINE__);
85 void readBytes (de::Socket& socket, vector<deUint8>& dst, size_t numBytes)
89 while (numRead < numBytes)
91 size_t numLeft = numBytes - numRead;
92 size_t curNumRead = 0;
93 deSocketResult result = socket.receive(&dst[numRead], numLeft, &curNumRead);
95 if (result != DE_SOCKETRESULT_SUCCESS)
96 throw SocketError(result, "receive() failed", __FILE__, __LINE__);
98 numRead += curNumRead;
102 Message* readMessage (de::Socket& socket)
105 vector<deUint8> header;
106 readBytes(socket, header, MESSAGE_HEADER_SIZE);
110 Message::parseHeader(&header[0], (int)header.size(), type, messageSize);
112 // Simple messages without any data.
115 case MESSAGETYPE_KEEPALIVE: return new KeepAliveMessage();
116 case MESSAGETYPE_PROCESS_STARTED: return new ProcessStartedMessage();
118 break; // Read message with data.
121 vector<deUint8> messageBuf;
122 readBytes(socket, messageBuf, messageSize-MESSAGE_HEADER_SIZE);
126 case MESSAGETYPE_HELLO: return new HelloMessage(&messageBuf[0], (int)messageBuf.size());
127 case MESSAGETYPE_TEST: return new TestMessage(&messageBuf[0], (int)messageBuf.size());
128 case MESSAGETYPE_PROCESS_LOG_DATA: return new ProcessLogDataMessage(&messageBuf[0], (int)messageBuf.size());
129 case MESSAGETYPE_INFO: return new InfoMessage(&messageBuf[0], (int)messageBuf.size());
130 case MESSAGETYPE_PROCESS_LAUNCH_FAILED: return new ProcessLaunchFailedMessage(&messageBuf[0], (int)messageBuf.size());
131 case MESSAGETYPE_PROCESS_FINISHED: return new ProcessFinishedMessage(&messageBuf[0], (int)messageBuf.size());
133 XS_FAIL("Unknown message");
140 de::SocketAddress address;
143 std::string workingDir;
144 std::string caseList;
145 std::string dstFileName;
151 Client (const CommandLine& cmdLine);
157 const CommandLine& m_cmdLine;
161 Client::Client (const CommandLine& cmdLine)
166 Client::~Client (void)
170 void Client::run (void)
172 // Connect to server.
173 m_socket.connect(m_cmdLine.address);
175 printf("Connected to %s:%d!\n", m_cmdLine.address.getHost(), m_cmdLine.address.getPort());
178 std::fstream out(m_cmdLine.dstFileName.c_str(), std::fstream::out|std::fstream::binary);
180 printf(" writing to %s\n", m_cmdLine.dstFileName.c_str());
182 // Send execution request.
184 ExecuteBinaryMessage msg;
186 msg.name = m_cmdLine.program;
187 msg.params = m_cmdLine.params;
188 msg.workDir = m_cmdLine.workingDir;
189 msg.caseList = m_cmdLine.caseList;
191 sendMessage(m_socket, msg);
192 printf(" execution request sent.\n");
196 bool isRunning = true;
199 ScopedMsgPtr msg(readMessage(m_socket));
203 case MESSAGETYPE_HELLO:
204 printf(" HelloMessage\n");
207 case MESSAGETYPE_KEEPALIVE:
209 printf(" KeepAliveMessage\n");
211 // Reply with keepalive.
212 sendMessage(m_socket, KeepAliveMessage());
216 case MESSAGETYPE_INFO:
217 printf(" InfoMessage: '%s'\n", static_cast<InfoMessage*>(msg.get())->info.c_str());
220 case MESSAGETYPE_PROCESS_STARTED:
221 printf(" ProcessStartedMessage\n");
224 case MESSAGETYPE_PROCESS_FINISHED:
225 printf(" ProcessFinished: exit code = %d\n", static_cast<ProcessFinishedMessage*>(msg.get())->exitCode);
229 case MESSAGETYPE_PROCESS_LAUNCH_FAILED:
230 printf(" ProcessLaunchFailed: '%s'\n", static_cast<ProcessLaunchFailedMessage*>(msg.get())->reason.c_str());
234 case MESSAGETYPE_PROCESS_LOG_DATA:
236 ProcessLogDataMessage* logDataMsg = static_cast<ProcessLogDataMessage*>(msg.get());
237 printf(" ProcessLogDataMessage: %d bytes\n", (int)logDataMsg->logData.length());
238 out << logDataMsg->logData;
243 XS_FAIL("Unknown message");
248 // Close output file.
258 string parseString (const char* str)
260 if (str[0] == '\'' || str[0] == '"')
264 std::ostringstream o;
266 while (*p != endChar && *p)
272 case 0: DE_ASSERT(DE_FALSE); break;
273 case 'n': o << '\n'; break;
274 case 't': o << '\t'; break;
275 default: o << p[1]; break;
290 void printHelp (const char* binName)
292 printf("%s:\n", binName);
293 printf(" --host=[host] Connect to host [host]\n");
294 printf(" --port=[name] Use port [port]\n");
295 printf(" --program=[program] Test program\n");
296 printf(" --params=[params] Test program params\n");
297 printf(" --workdir=[dir] Working directory\n");
298 printf(" --caselist=[caselist] Test case list\n");
299 printf(" --out=filename Test result file\n");
302 int runClient (int argc, const char* const* argv)
307 cmdLine.address.setHost("127.0.0.1");
308 cmdLine.address.setPort(50016);
309 cmdLine.dstFileName = "TestResults.qpa";
311 // Parse command line.
312 for (int argNdx = 1; argNdx < argc; argNdx++)
314 const char* arg = argv[argNdx];
316 if (deStringBeginsWith(arg, "--port="))
317 cmdLine.address.setPort(atoi(arg+7));
318 else if (deStringBeginsWith(arg, "--host="))
319 cmdLine.address.setHost(parseString(arg+7).c_str());
320 else if (deStringBeginsWith(arg, "--program="))
321 cmdLine.program = parseString(arg+10);
322 else if (deStringBeginsWith(arg, "--params="))
323 cmdLine.params = parseString(arg+9);
324 else if (deStringBeginsWith(arg, "--workdir="))
325 cmdLine.workingDir = parseString(arg+10);
326 else if (deStringBeginsWith(arg, "--caselist="))
327 cmdLine.caseList = parseString(arg+11);
328 else if (deStringBeginsWith(arg, "--out="))
329 cmdLine.dstFileName = parseString(arg+6);
340 Client client(cmdLine);
343 catch (const std::exception& e)
345 printf("%s\n", e.what());
354 int main (int argc, const char* const* argv)
356 return xs::runClient(argc, argv);