1 #ifndef _XSPROTOCOL_HPP
2 #define _XSPROTOCOL_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Execution Server
5  * ---------------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Execution Server Protocol.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "xsDefs.hpp"
27 #include "deMemory.h"
28 
29 #include <string>
30 #include <vector>
31 
32 namespace xs
33 {
34 
35 enum
36 {
37 	PROTOCOL_VERSION			= 18,
38 	MESSAGE_HEADER_SIZE			= 8,
39 
40 	// Times are in milliseconds.
41 	KEEPALIVE_SEND_INTERVAL		= 5000,
42 	KEEPALIVE_TIMEOUT			= 30000,
43 };
44 
45 enum MessageType
46 {
47 	MESSAGETYPE_NONE					= 0,	//!< Not valid.
48 
49 	// Commands (from Client to ExecServer).
50 	MESSAGETYPE_HELLO					= 100,	//!< First message from client, specifies the protocol version
51 	MESSAGETYPE_TEST					= 101,	//!< Debug only
52 	MESSAGETYPE_EXECUTE_BINARY			= 111,	//!< Request execution of a test package binary.
53 	MESSAGETYPE_STOP_EXECUTION			= 112,	//!< Request cancellation of the currently executing binary.
54 
55 	// Responses (from ExecServer to Client)
56 	MESSAGETYPE_PROCESS_STARTED			= 200,	//!< Requested process has started.
57 	MESSAGETYPE_PROCESS_LAUNCH_FAILED	= 201,	//!< Requested process failed to launch.
58 	MESSAGETYPE_PROCESS_FINISHED		= 202,	//!< Requested process has finished (for any reason).
59 	MESSAGETYPE_PROCESS_LOG_DATA		= 203,	//!< Unprocessed log data from TestResults.qpa.
60 	MESSAGETYPE_INFO					= 204,	//!< Generic info message from ExecServer (for debugging purposes).
61 
62 	MESSAGETYPE_KEEPALIVE				= 102	//!< Keep-alive packet
63 };
64 
65 class MessageWriter;
66 
67 class Message
68 {
69 public:
70 	MessageType		type;
71 
Message(MessageType type_)72 					Message			(MessageType type_) : type(type_) {}
~Message(void)73 	virtual			 ~Message		(void) {}
74 
75 	virtual void	write			(std::vector<deUint8>& buf) const = DE_NULL;
76 
77 	static void		parseHeader		(const deUint8* data, size_t dataSize, MessageType& type, size_t& messageSize);
78 	static void		writeHeader		(MessageType type, size_t messageSize, deUint8* dst, size_t bufSize);
79 
80 protected:
81 	void			writeNoData		(std::vector<deUint8>& buf) const;
82 
83 					Message			(const Message& other);
84 	Message&		operator=		(const Message& other);
85 };
86 
87 // Simple messages without any data.
88 template <int MsgType>
89 class SimpleMessage : public Message
90 {
91 public:
SimpleMessage(const deUint8 * data,size_t dataSize)92 					SimpleMessage	(const deUint8* data, size_t dataSize) : Message((MessageType)MsgType) { DE_UNREF(data); XS_CHECK_MSG(dataSize == 0, "No payload expected"); }
SimpleMessage(void)93 					SimpleMessage	(void) : Message((MessageType)MsgType) {}
~SimpleMessage(void)94 					~SimpleMessage	(void) {}
95 
write(std::vector<deUint8> & buf) const96 	void			write			(std::vector<deUint8>& buf) const { writeNoData(buf); }
97 };
98 
99 typedef SimpleMessage<MESSAGETYPE_STOP_EXECUTION>			StopExecutionMessage;
100 typedef SimpleMessage<MESSAGETYPE_PROCESS_STARTED>			ProcessStartedMessage;
101 typedef SimpleMessage<MESSAGETYPE_KEEPALIVE>				KeepAliveMessage;
102 
103 class HelloMessage : public Message
104 {
105 public:
106 	int				version;
107 
108 					HelloMessage	(const deUint8* data, size_t dataSize);
HelloMessage(void)109 					HelloMessage	(void) : Message(MESSAGETYPE_HELLO), version(PROTOCOL_VERSION) {}
~HelloMessage(void)110 					~HelloMessage	(void) {}
111 
112 	void			write			(std::vector<deUint8>& buf) const;
113 };
114 
115 class ExecuteBinaryMessage : public Message
116 {
117 public:
118 	std::string		name;
119 	std::string		params;
120 	std::string		workDir;
121 	std::string		caseList;
122 
123 					ExecuteBinaryMessage	(const deUint8* data, size_t dataSize);
ExecuteBinaryMessage(void)124 					ExecuteBinaryMessage	(void) : Message(MESSAGETYPE_EXECUTE_BINARY) {}
~ExecuteBinaryMessage(void)125 					~ExecuteBinaryMessage	(void) {};
126 
127 	void			write			(std::vector<deUint8>& buf) const;
128 };
129 
130 class ProcessLogDataMessage : public Message
131 {
132 public:
133 	std::string		logData;
134 
135 					ProcessLogDataMessage		(const deUint8* data, size_t dataSize);
~ProcessLogDataMessage(void)136 					~ProcessLogDataMessage		(void) {}
137 
138 	void			write						(std::vector<deUint8>& buf) const;
139 };
140 
141 class ProcessLaunchFailedMessage : public Message
142 {
143 public:
144 	std::string		reason;
145 
146 					ProcessLaunchFailedMessage			(const deUint8* data, size_t dataSize);
ProcessLaunchFailedMessage(const char * reason_)147 					ProcessLaunchFailedMessage			(const char* reason_) : Message(MESSAGETYPE_PROCESS_LAUNCH_FAILED), reason(reason_) {}
~ProcessLaunchFailedMessage(void)148 					~ProcessLaunchFailedMessage			(void) {}
149 
150 	void			write								(std::vector<deUint8>& buf) const;
151 };
152 
153 class ProcessFinishedMessage : public Message
154 {
155 public:
156 	int				exitCode;
157 
158 					ProcessFinishedMessage			(const deUint8* data, size_t dataSize);
ProcessFinishedMessage(int exitCode_)159 					ProcessFinishedMessage			(int exitCode_) : Message(MESSAGETYPE_PROCESS_FINISHED), exitCode(exitCode_) {}
~ProcessFinishedMessage(void)160 					~ProcessFinishedMessage			(void) {}
161 
162 	void			write							(std::vector<deUint8>& buf) const;
163 };
164 
165 class InfoMessage : public Message
166 {
167 public:
168 	std::string		info;
169 
170 					InfoMessage			(const deUint8* data, size_t dataSize);
~InfoMessage(void)171 					~InfoMessage		(void) {}
172 
173 	void			write				(std::vector<deUint8>& buf) const;
174 };
175 
176 // For debug purposes only.
177 class TestMessage : public Message
178 {
179 public:
180 	std::string		test;
181 
182 					TestMessage		(const deUint8* data, size_t dataSize);
~TestMessage(void)183 					~TestMessage	(void) {}
184 
185 	void			write			(std::vector<deUint8>& buf) const;
186 };
187 
188 } // xs
189 
190 #endif // _XSPROTOCOL_HPP
191