xsExecutionServer.hpp revision 3c827367444ee418f129b2c238299f49d3264554
1#ifndef _XSEXECUTIONSERVER_HPP
2#define _XSEXECUTIONSERVER_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 Test Execution Server.
24 *//*--------------------------------------------------------------------*/
25
26#include "xsDefs.hpp"
27#include "xsTcpServer.hpp"
28#include "xsTestDriver.hpp"
29#include "xsProtocol.hpp"
30#include "xsTestProcess.hpp"
31
32#include <vector>
33
34namespace xs
35{
36
37class ExecutionServer : public TcpServer
38{
39public:
40	enum RunMode
41	{
42		RUNMODE_SINGLE_EXEC	= 0,
43		RUNMODE_FOREVER,
44
45		RUNMODE_LAST
46	};
47
48							ExecutionServer			(xs::TestProcess* testProcess, deSocketFamily family, int port, RunMode runMode);
49							~ExecutionServer		(void);
50
51	ConnectionHandler*		createHandler			(de::Socket* socket, const de::SocketAddress& clientAddress);
52
53	TestDriver*				acquireTestDriver		(void);
54	void					releaseTestDriver		(TestDriver* driver);
55
56	void					connectionDone			(ConnectionHandler* handler);
57
58private:
59	TestDriver				m_testDriver;
60	de::Mutex				m_testDriverLock;
61	RunMode					m_runMode;
62};
63
64class MessageBuilder
65{
66public:
67							MessageBuilder		(void) { clear(); }
68							~MessageBuilder		(void) {}
69
70	void					read				(ByteBuffer& buffer);
71	void					clear				(void);
72
73	bool					isComplete			(void) const;
74	MessageType				getMessageType		(void) const	{ return m_messageType;	}
75	int						getMessageSize		(void) const	{ return m_messageSize; }
76	const deUint8*			getMessageData		(void) const;
77	int						getMessageDataSize	(void) const;
78
79private:
80	std::vector<deUint8>	m_buffer;
81	MessageType				m_messageType;
82	int						m_messageSize;
83};
84
85class ExecutionRequestHandler : public ConnectionHandler
86{
87public:
88								ExecutionRequestHandler			(ExecutionServer* server, de::Socket* socket);
89								~ExecutionRequestHandler		(void);
90
91protected:
92	void						handle							(void);
93
94private:
95								ExecutionRequestHandler			(const ExecutionRequestHandler& handler);
96	ExecutionRequestHandler&	operator=						(const ExecutionRequestHandler& handler);
97
98	void						processSession					(void);
99	void						processMessage					(MessageType type, const deUint8* data, int dataSize);
100
101	inline TestDriver*			getTestDriver					(void) { if (!m_testDriver) acquireTestDriver(); return m_testDriver; }
102	void						acquireTestDriver				(void);
103
104	void						initKeepAlives					(void);
105	void						keepAliveReceived				(void);
106	void						pollKeepAlives					(void);
107
108	bool						receive							(void);
109	bool						send							(void);
110
111	ExecutionServer*			m_execServer;
112	TestDriver*					m_testDriver;
113
114	ByteBuffer					m_bufferIn;
115	ByteBuffer					m_bufferOut;
116
117	bool						m_run;
118	MessageBuilder				m_msgBuilder;
119
120	// \todo [2011-09-30 pyry] Move to some watchdog class instead.
121	deUint64					m_lastKeepAliveSent;
122	deUint64					m_lastKeepAliveReceived;
123
124	std::vector<deUint8>		m_sendRecvTmpBuf;
125};
126
127} // xs
128
129#endif // _XSEXECUTIONSERVER_HPP
130