summaryrefslogtreecommitdiff
path: root/execserver/xsProtocol.cpp
blob: 53a1a35365da128d01a994bbcbd39493311b1db4 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*-------------------------------------------------------------------------
 * drawElements Quality Program Execution Server
 * ---------------------------------------------
 *
 * 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 Execution Server Protocol.
 *//*--------------------------------------------------------------------*/

#include "xsProtocol.hpp"

using std::string;
using std::vector;

namespace xs
{

inline deUint32 swapEndianess (deUint32 value)
{
	deUint32 b0 = (value >>  0) & 0xFF;
	deUint32 b1 = (value >>  8) & 0xFF;
	deUint32 b2 = (value >> 16) & 0xFF;
	deUint32 b3 = (value >> 24) & 0xFF;
	return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}

template <typename T> T networkToHost (T value);
template <typename T> T hostToNetwork (T value);

template <> int networkToHost (int value) { return (int)swapEndianess((deUint32)value); }
template <> int hostToNetwork (int value) { return (int)swapEndianess((deUint32)value); }

class MessageParser
{
public:
	MessageParser (const deUint8* data, size_t dataSize)
		: m_data	(data)
		, m_size	(dataSize)
		, m_pos		(0)
	{
	}

	template <typename T>
	T get (void)
	{
		XS_CHECK_MSG(m_pos + sizeof(T) <= m_size, "Invalid payload size");
		T netValue;
		deMemcpy(&netValue, &m_data[m_pos], sizeof(T));
		m_pos += sizeof(T);
		return networkToHost(netValue);
	}

	void getString (std::string& dst)
	{
		// \todo [2011-09-30 pyry] We should really send a size parameter instead.
		while (m_data[m_pos] != 0)
		{
			dst += (char)m_data[m_pos++];
			XS_CHECK_MSG(m_pos < m_size, "Unterminated string payload");
		}

		m_pos += 1;
	}

	void assumEnd (void)
	{
		if (m_pos != m_size)
			XS_FAIL("Invalid payload size");
	}

private:
	const deUint8*	m_data;
	size_t			m_size;
	size_t			m_pos;
};

class MessageWriter
{
public:
	MessageWriter (MessageType msgType, std::vector<deUint8>& buf)
		: m_buf(buf)
	{
		// Place for size.
		put<int>(0);

		// Write message type.
		put<int>(msgType);
	}

	~MessageWriter (void)
	{
		finalize();
	}

	void finalize (void)
	{
		DE_ASSERT(m_buf.size() >= MESSAGE_HEADER_SIZE);

		// Write actual size.
		int size = hostToNetwork((int)m_buf.size());
		deMemcpy(&m_buf[0], &size, sizeof(int));
	}

	template <typename T>
	void put (T value)
	{
		T netValue = hostToNetwork(value);
		size_t curPos = m_buf.size();
		m_buf.resize(curPos + sizeof(T));
		deMemcpy(&m_buf[curPos], &netValue, sizeof(T));
	}

private:
	std::vector<deUint8>& m_buf;
};

template <>
void MessageWriter::put<const char*> (const char* value)
{
	int curPos = (int)m_buf.size();
	int strLen = (int)strlen(value);

	m_buf.resize(curPos + strLen+1);
	deMemcpy(&m_buf[curPos], &value[0], strLen+1);
}

void Message::parseHeader (const deUint8* data, size_t dataSize, MessageType& type, size_t& size)
{
	XS_CHECK_MSG(dataSize >= MESSAGE_HEADER_SIZE, "Incomplete header");
	MessageParser parser(data, dataSize);
	size	= (size_t)(MessageType)parser.get<int>();
	type	= (MessageType)parser.get<int>();
}

void Message::writeHeader (MessageType type, size_t messageSize, deUint8* dst, size_t bufSize)
{
	XS_CHECK_MSG(bufSize >= MESSAGE_HEADER_SIZE, "Incomplete header");
	int netSize = hostToNetwork((int)messageSize);
	int netType = hostToNetwork((int)type);
	deMemcpy(dst+0, &netSize, sizeof(netSize));
	deMemcpy(dst+4, &netType, sizeof(netType));
}

void Message::writeNoData (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
}

HelloMessage::HelloMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_HELLO)
{
	MessageParser parser(data, dataSize);
	version = parser.get<int>();
	parser.assumEnd();
}

void HelloMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(version);
}

TestMessage::TestMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_TEST)
{
	MessageParser parser(data, dataSize);
	parser.getString(test);
	parser.assumEnd();
}

void TestMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(test.c_str());
}

ExecuteBinaryMessage::ExecuteBinaryMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_EXECUTE_BINARY)
{
	MessageParser parser(data, dataSize);
	parser.getString(name);
	parser.getString(params);
	parser.getString(workDir);
	parser.getString(caseList);
	parser.assumEnd();
}

void ExecuteBinaryMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(name.c_str());
	writer.put(params.c_str());
	writer.put(workDir.c_str());
	writer.put(caseList.c_str());
}

ProcessLogDataMessage::ProcessLogDataMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_PROCESS_LOG_DATA)
{
	MessageParser parser(data, dataSize);
	parser.getString(logData);
	parser.assumEnd();
}

void ProcessLogDataMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(logData.c_str());
}

ProcessLaunchFailedMessage::ProcessLaunchFailedMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_PROCESS_LAUNCH_FAILED)
{
	MessageParser parser(data, dataSize);
	parser.getString(reason);
	parser.assumEnd();
}

void ProcessLaunchFailedMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(reason.c_str());
}

ProcessFinishedMessage::ProcessFinishedMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_PROCESS_FINISHED)
{
	MessageParser parser(data, dataSize);
	exitCode = parser.get<int>();
	parser.assumEnd();
}

void ProcessFinishedMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(exitCode);
}

InfoMessage::InfoMessage (const deUint8* data, size_t dataSize)
	: Message(MESSAGETYPE_INFO)
{
	MessageParser parser(data, dataSize);
	parser.getString(info);
	parser.assumEnd();
}

void InfoMessage::write (vector<deUint8>& buf) const
{
	MessageWriter writer(type, buf);
	writer.put(info.c_str());
}

} // xs