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 Execution Server Protocol.
22 *//*--------------------------------------------------------------------*/
24 #include "xsProtocol.hpp"
32 inline deUint32 swapEndianess (deUint32 value)
34 deUint32 b0 = (value >> 0) & 0xFF;
35 deUint32 b1 = (value >> 8) & 0xFF;
36 deUint32 b2 = (value >> 16) & 0xFF;
37 deUint32 b3 = (value >> 24) & 0xFF;
38 return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
41 template <typename T> T networkToHost (T value);
42 template <typename T> T hostToNetwork (T value);
44 template <> int networkToHost (int value) { return (int)swapEndianess((deUint32)value); }
45 template <> int hostToNetwork (int value) { return (int)swapEndianess((deUint32)value); }
50 MessageParser (const deUint8* data, size_t dataSize)
60 XS_CHECK_MSG(m_pos + sizeof(T) <= m_size, "Invalid payload size");
62 deMemcpy(&netValue, &m_data[m_pos], sizeof(T));
64 return networkToHost(netValue);
67 void getString (std::string& dst)
69 // \todo [2011-09-30 pyry] We should really send a size parameter instead.
70 while (m_data[m_pos] != 0)
72 dst += (char)m_data[m_pos++];
73 XS_CHECK_MSG(m_pos < m_size, "Unterminated string payload");
82 XS_FAIL("Invalid payload size");
86 const deUint8* m_data;
94 MessageWriter (MessageType msgType, std::vector<deUint8>& buf)
100 // Write message type.
104 ~MessageWriter (void)
111 DE_ASSERT(m_buf.size() >= MESSAGE_HEADER_SIZE);
113 // Write actual size.
114 int size = hostToNetwork((int)m_buf.size());
115 deMemcpy(&m_buf[0], &size, sizeof(int));
118 template <typename T>
121 T netValue = hostToNetwork(value);
122 size_t curPos = m_buf.size();
123 m_buf.resize(curPos + sizeof(T));
124 deMemcpy(&m_buf[curPos], &netValue, sizeof(T));
128 std::vector<deUint8>& m_buf;
132 void MessageWriter::put<const char*> (const char* value)
134 int curPos = (int)m_buf.size();
135 int strLen = (int)strlen(value);
137 m_buf.resize(curPos + strLen+1);
138 deMemcpy(&m_buf[curPos], &value[0], strLen+1);
141 void Message::parseHeader (const deUint8* data, size_t dataSize, MessageType& type, size_t& size)
143 XS_CHECK_MSG(dataSize >= MESSAGE_HEADER_SIZE, "Incomplete header");
144 MessageParser parser(data, dataSize);
145 size = (size_t)(MessageType)parser.get<int>();
146 type = (MessageType)parser.get<int>();
149 void Message::writeHeader (MessageType type, size_t messageSize, deUint8* dst, size_t bufSize)
151 XS_CHECK_MSG(bufSize >= MESSAGE_HEADER_SIZE, "Incomplete header");
152 int netSize = hostToNetwork((int)messageSize);
153 int netType = hostToNetwork((int)type);
154 deMemcpy(dst+0, &netSize, sizeof(netSize));
155 deMemcpy(dst+4, &netType, sizeof(netType));
158 void Message::writeNoData (vector<deUint8>& buf) const
160 MessageWriter writer(type, buf);
163 HelloMessage::HelloMessage (const deUint8* data, size_t dataSize)
164 : Message(MESSAGETYPE_HELLO)
166 MessageParser parser(data, dataSize);
167 version = parser.get<int>();
171 void HelloMessage::write (vector<deUint8>& buf) const
173 MessageWriter writer(type, buf);
177 TestMessage::TestMessage (const deUint8* data, size_t dataSize)
178 : Message(MESSAGETYPE_TEST)
180 MessageParser parser(data, dataSize);
181 parser.getString(test);
185 void TestMessage::write (vector<deUint8>& buf) const
187 MessageWriter writer(type, buf);
188 writer.put(test.c_str());
191 ExecuteBinaryMessage::ExecuteBinaryMessage (const deUint8* data, size_t dataSize)
192 : Message(MESSAGETYPE_EXECUTE_BINARY)
194 MessageParser parser(data, dataSize);
195 parser.getString(name);
196 parser.getString(params);
197 parser.getString(workDir);
198 parser.getString(caseList);
202 void ExecuteBinaryMessage::write (vector<deUint8>& buf) const
204 MessageWriter writer(type, buf);
205 writer.put(name.c_str());
206 writer.put(params.c_str());
207 writer.put(workDir.c_str());
208 writer.put(caseList.c_str());
211 ProcessLogDataMessage::ProcessLogDataMessage (const deUint8* data, size_t dataSize)
212 : Message(MESSAGETYPE_PROCESS_LOG_DATA)
214 MessageParser parser(data, dataSize);
215 parser.getString(logData);
219 void ProcessLogDataMessage::write (vector<deUint8>& buf) const
221 MessageWriter writer(type, buf);
222 writer.put(logData.c_str());
225 ProcessLaunchFailedMessage::ProcessLaunchFailedMessage (const deUint8* data, size_t dataSize)
226 : Message(MESSAGETYPE_PROCESS_LAUNCH_FAILED)
228 MessageParser parser(data, dataSize);
229 parser.getString(reason);
233 void ProcessLaunchFailedMessage::write (vector<deUint8>& buf) const
235 MessageWriter writer(type, buf);
236 writer.put(reason.c_str());
239 ProcessFinishedMessage::ProcessFinishedMessage (const deUint8* data, size_t dataSize)
240 : Message(MESSAGETYPE_PROCESS_FINISHED)
242 MessageParser parser(data, dataSize);
243 exitCode = parser.get<int>();
247 void ProcessFinishedMessage::write (vector<deUint8>& buf) const
249 MessageWriter writer(type, buf);
250 writer.put(exitCode);
253 InfoMessage::InfoMessage (const deUint8* data, size_t dataSize)
254 : Message(MESSAGETYPE_INFO)
256 MessageParser parser(data, dataSize);
257 parser.getString(info);
261 void InfoMessage::write (vector<deUint8>& buf) const
263 MessageWriter writer(type, buf);
264 writer.put(info.c_str());