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"
42 typedef std::auto_ptr<Message> ScopedMsgPtr;
44 class SocketError : public Error
47 SocketError (deSocketResult result, const char* message, const char* file, int line)
48 : Error (message, deGetSocketResultName(result), file, line)
53 deSocketResult getResult (void) const
59 deSocketResult m_result;
63 void sendMessage (de::Socket& socket, const Message& message)
71 while (pos < buf.size())
73 size_t numLeft = buf.size() - pos;
75 deSocketResult result = socket.send(&buf[pos], numLeft, &numSent);
77 if (result != DE_SOCKETRESULT_SUCCESS)
78 throw SocketError(result, "send() failed", __FILE__, __LINE__);
84 void readBytes (de::Socket& socket, vector<deUint8>& dst, size_t numBytes)
88 while (numRead < numBytes)
90 size_t numLeft = numBytes - numRead;
91 size_t curNumRead = 0;
92 deSocketResult result = socket.receive(&dst[numRead], numLeft, &curNumRead);
94 if (result != DE_SOCKETRESULT_SUCCESS)
95 throw SocketError(result, "receive() failed", __FILE__, __LINE__);
97 numRead += curNumRead;
101 Message* readMessage (de::Socket& socket)
104 vector<deUint8> header;
105 readBytes(socket, header, MESSAGE_HEADER_SIZE);
109 Message::parseHeader(&header[0], (int)header.size(), type, messageSize);
111 // Simple messages without any data.
114 case MESSAGETYPE_KEEPALIVE: return new KeepAliveMessage();
115 case MESSAGETYPE_PROCESS_STARTED: return new ProcessStartedMessage();
117 break; // Read message with data.
120 vector<deUint8> messageBuf;
121 readBytes(socket, messageBuf, messageSize-MESSAGE_HEADER_SIZE);
125 case MESSAGETYPE_HELLO: return new HelloMessage(&messageBuf[0], (int)messageBuf.size());
126 case MESSAGETYPE_TEST: return new TestMessage(&messageBuf[0], (int)messageBuf.size());
127 case MESSAGETYPE_PROCESS_LOG_DATA: return new ProcessLogDataMessage(&messageBuf[0], (int)messageBuf.size());
128 case MESSAGETYPE_INFO: return new InfoMessage(&messageBuf[0], (int)messageBuf.size());
129 case MESSAGETYPE_PROCESS_LAUNCH_FAILED: return new ProcessLaunchFailedMessage(&messageBuf[0], (int)messageBuf.size());
130 case MESSAGETYPE_PROCESS_FINISHED: return new ProcessFinishedMessage(&messageBuf[0], (int)messageBuf.size());
132 XS_FAIL("Unknown message");
139 de::SocketAddress address;
142 std::string workingDir;
143 std::string caseList;
144 std::string dstFileName;
150 Client (const CommandLine& cmdLine);
156 const CommandLine& m_cmdLine;
160 Client::Client (const CommandLine& cmdLine)
165 Client::~Client (void)
169 void Client::run (void)
171 // Connect to server.
172 m_socket.connect(m_cmdLine.address);
174 printf("Connected to %s:%d!\n", m_cmdLine.address.getHost(), m_cmdLine.address.getPort());
177 std::fstream out(m_cmdLine.dstFileName.c_str(), std::fstream::out|std::fstream::binary);
179 printf(" writing to %s\n", m_cmdLine.dstFileName.c_str());
181 // Send execution request.
183 ExecuteBinaryMessage msg;
185 msg.name = m_cmdLine.program;
186 msg.params = m_cmdLine.params;
187 msg.workDir = m_cmdLine.workingDir;
188 msg.caseList = m_cmdLine.caseList;
190 sendMessage(m_socket, msg);
191 printf(" execution request sent.\n");
195 bool isRunning = true;
198 ScopedMsgPtr msg(readMessage(m_socket));
202 case MESSAGETYPE_HELLO:
203 printf(" HelloMessage\n");
206 case MESSAGETYPE_KEEPALIVE:
208 printf(" KeepAliveMessage\n");
210 // Reply with keepalive.
211 sendMessage(m_socket, KeepAliveMessage());
215 case MESSAGETYPE_INFO:
216 printf(" InfoMessage: '%s'\n", static_cast<InfoMessage*>(msg.get())->info.c_str());
219 case MESSAGETYPE_PROCESS_STARTED:
220 printf(" ProcessStartedMessage\n");
223 case MESSAGETYPE_PROCESS_FINISHED:
224 printf(" ProcessFinished: exit code = %d\n", static_cast<ProcessFinishedMessage*>(msg.get())->exitCode);
228 case MESSAGETYPE_PROCESS_LAUNCH_FAILED:
229 printf(" ProcessLaunchFailed: '%s'\n", static_cast<ProcessLaunchFailedMessage*>(msg.get())->reason.c_str());
233 case MESSAGETYPE_PROCESS_LOG_DATA:
235 ProcessLogDataMessage* logDataMsg = static_cast<ProcessLogDataMessage*>(msg.get());
236 printf(" ProcessLogDataMessage: %d bytes\n", (int)logDataMsg->logData.length());
237 out << logDataMsg->logData;
242 XS_FAIL("Unknown message");
247 // Close output file.
257 string parseString (const char* str)
259 if (str[0] == '\'' || str[0] == '"')
263 std::ostringstream o;
265 while (*p != endChar && *p)
271 case 0: DE_ASSERT(DE_FALSE); break;
272 case 'n': o << '\n'; break;
273 case 't': o << '\t'; break;
274 default: o << p[1]; break;
289 void printHelp (const char* binName)
291 printf("%s:\n", binName);
292 printf(" --host=[host] Connect to host [host]\n");
293 printf(" --port=[name] Use port [port]\n");
294 printf(" --program=[program] Test program\n");
295 printf(" --params=[params] Test program params\n");
296 printf(" --workdir=[dir] Working directory\n");
297 printf(" --caselist=[caselist] Test case list\n");
298 printf(" --out=filename Test result file\n");
301 int runClient (int argc, const char* const* argv)
306 cmdLine.address.setHost("127.0.0.1");
307 cmdLine.address.setPort(50016);
308 cmdLine.dstFileName = "TestResults.qpa";
310 // Parse command line.
311 for (int argNdx = 1; argNdx < argc; argNdx++)
313 const char* arg = argv[argNdx];
315 if (deStringBeginsWith(arg, "--port="))
316 cmdLine.address.setPort(atoi(arg+7));
317 else if (deStringBeginsWith(arg, "--host="))
318 cmdLine.address.setHost(parseString(arg+7).c_str());
319 else if (deStringBeginsWith(arg, "--program="))
320 cmdLine.program = parseString(arg+10);
321 else if (deStringBeginsWith(arg, "--params="))
322 cmdLine.params = parseString(arg+9);
323 else if (deStringBeginsWith(arg, "--workdir="))
324 cmdLine.workingDir = parseString(arg+10);
325 else if (deStringBeginsWith(arg, "--caselist="))
326 cmdLine.caseList = parseString(arg+11);
327 else if (deStringBeginsWith(arg, "--out="))
328 cmdLine.dstFileName = parseString(arg+6);
339 Client client(cmdLine);
342 catch (const std::exception& e)
344 printf("%s\n", e.what());
353 int main (int argc, const char* const* argv)
355 return xs::runClient(argc, argv);