1 #ifndef _XETCPIPLINK_HPP
2 #define _XETCPIPLINK_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Test Executor
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 Tcp/Ip communication link.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "xeDefs.hpp"
27 #include "xeCommLink.hpp"
28 #include "deSocket.hpp"
29 #include "deRingBuffer.hpp"
30 #include "deBlockBuffer.hpp"
31 #include "xsProtocol.hpp"
32 #include "deThread.hpp"
33 #include "deTimer.h"
34 
35 #include <vector>
36 
37 namespace xe
38 {
39 
40 class TcpIpLinkState
41 {
42 public:
43 								TcpIpLinkState				(CommLinkState initialState, const char* initialErr);
44 								~TcpIpLinkState				(void);
45 
46 	CommLinkState				getState					(void) const;
47 	CommLinkState				getState					(std::string& error) const;
48 
49 	void						setCallbacks				(CommLink::StateChangedFunc stateChangedCallback, CommLink::LogDataFunc testLogDataCallback, CommLink::LogDataFunc infoLogDataCallback, void* userPtr);
50 
51 	void						setState					(CommLinkState state, const char* error = "");
52 	void						onTestLogData				(const deUint8* bytes, size_t numBytes) const;
53 	void						onInfoLogData				(const deUint8* bytes, size_t numBytes) const;
54 
55 	void						onKeepaliveReceived			(void);
56 	deUint64					getLastKeepaliveRecevied	(void) const;
57 
58 private:
59 	mutable de::Mutex					m_lock;
60 	volatile CommLinkState				m_state;
61 	std::string							m_error;
62 
63 	volatile deUint64					m_lastKeepaliveReceived;
64 
65 	volatile CommLink::StateChangedFunc	m_stateChangedCallback;
66 	volatile CommLink::LogDataFunc		m_testLogDataCallback;
67 	volatile CommLink::LogDataFunc		m_infoLogDataCallback;
68 	void* volatile						m_userPtr;
69 };
70 
71 class TcpIpSendThread : public de::Thread
72 {
73 public:
74 								TcpIpSendThread			(de::Socket& socket, TcpIpLinkState& state);
75 								~TcpIpSendThread		(void);
76 
77 	void						start					(void);
78 	void						run						(void);
79 	void						stop					(void);
80 
isRunning(void) const81 	bool						isRunning				(void) const { return m_isRunning; }
82 
getBuffer(void)83 	de::BlockBuffer<deUint8>&	getBuffer				(void) { return m_buffer; }
84 
85 private:
86 	de::Socket&					m_socket;
87 	TcpIpLinkState&				m_state;
88 
89 	de::BlockBuffer<deUint8>	m_buffer;
90 
91 	bool						m_isRunning;
92 };
93 
94 class TcpIpRecvThread : public de::Thread
95 {
96 public:
97 								TcpIpRecvThread			(de::Socket& socket, TcpIpLinkState& state);
98 								~TcpIpRecvThread		(void);
99 
100 	void						start					(void);
101 	void						run						(void);
102 	void						stop					(void);
103 
isRunning(void) const104 	bool						isRunning				(void) const { return m_isRunning; }
105 
106 private:
107 	void						handleMessage			(xs::MessageType messageType, const deUint8* data, size_t dataSize);
108 
109 	de::Socket&					m_socket;
110 	TcpIpLinkState&				m_state;
111 
112 	std::vector<deUint8>		m_curMsgBuf;
113 	size_t						m_curMsgPos;
114 
115 	bool						m_isRunning;
116 };
117 
118 class TcpIpLink : public CommLink
119 {
120 public:
121 								TcpIpLink				(void);
122 								~TcpIpLink				(void);
123 
124 	// TcpIpLink -specific API
125 	void						connect					(const de::SocketAddress& address);
126 	void						disconnect				(void);
127 
128 	// CommLink API
129 	void						reset					(void);
130 
131 	CommLinkState				getState				(void) const;
132 	CommLinkState				getState				(std::string& error) const;
133 
134 	void						setCallbacks			(StateChangedFunc stateChangedCallback, LogDataFunc testLogDataCallback, LogDataFunc infoLogDataCallback, void* userPtr);
135 
136 	void						startTestProcess		(const char* name, const char* params, const char* workingDir, const char* caseList);
137 	void						stopTestProcess			(void);
138 
139 private:
140 	void						closeConnection			(void);
141 
142 	static void					keepaliveTimerCallback	(void* ptr);
143 
144 	de::Socket					m_socket;
145 	TcpIpLinkState				m_state;
146 
147 	TcpIpSendThread				m_sendThread;
148 	TcpIpRecvThread				m_recvThread;
149 
150 	deTimer*					m_keepaliveTimer;
151 };
152 
153 } // xe
154 
155 #endif // _XETCPIPLINK_HPP
156