1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef XPLRA_SERVERTHREAD_H
|
---|
30 | #define XPLRA_SERVERTHREAD_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "HotkeyHandler.h"
|
---|
34 |
|
---|
35 | #include <hu/varadiistvan/scpl/Thread.h>
|
---|
36 |
|
---|
37 | #include <hu/varadiistvan/scpl/io/Waiter.h>
|
---|
38 | #include <hu/varadiistvan/scpl/io/BufferedStream.h>
|
---|
39 | #include <hu/varadiistvan/scpl/io/DataStream.h>
|
---|
40 | #include <hu/varadiistvan/scpl/io/BufferedStreamAcceptor.h>
|
---|
41 |
|
---|
42 | #include <hu/varadiistvan/scpl/Mutex.h>
|
---|
43 |
|
---|
44 | #include <set>
|
---|
45 | #include <map>
|
---|
46 |
|
---|
47 | //------------------------------------------------------------------------------
|
---|
48 |
|
---|
49 | namespace xplra {
|
---|
50 |
|
---|
51 | //------------------------------------------------------------------------------
|
---|
52 |
|
---|
53 | class ListenThread;
|
---|
54 |
|
---|
55 | class RequestQueue;
|
---|
56 |
|
---|
57 | class GetMultiDataRefRequest;
|
---|
58 | class SetMultiDataRefRequest;
|
---|
59 |
|
---|
60 | //------------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * A thread serving a client.
|
---|
64 | */
|
---|
65 | class ServerThread : public hu::varadiistvan::scpl::Thread
|
---|
66 | {
|
---|
67 | private:
|
---|
68 | /**
|
---|
69 | * Type for the set of server thread instances.
|
---|
70 | */
|
---|
71 | typedef std::set<ServerThread*> instances_t;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Type for the registered multiple-data query requests.
|
---|
75 | */
|
---|
76 | typedef std::map<size_t, GetMultiDataRefRequest*> getMultiRequests_t;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Type for the registered multiple-data update requests.
|
---|
80 | */
|
---|
81 | typedef std::map<size_t, SetMultiDataRefRequest*> setMultiRequests_t;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * A mutex to protect the collection of server threads.
|
---|
85 | */
|
---|
86 | static hu::varadiistvan::scpl::Mutex instancesMutex;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * The instances of this class.
|
---|
90 | */
|
---|
91 | static instances_t instances;
|
---|
92 |
|
---|
93 | public:
|
---|
94 | /**
|
---|
95 | * Call quit() an all threads.
|
---|
96 | */
|
---|
97 | static void quitAll();
|
---|
98 |
|
---|
99 | private:
|
---|
100 | /**
|
---|
101 | * The listen thread this server was started by.
|
---|
102 | */
|
---|
103 | ListenThread& listenThread;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The request queue to use.
|
---|
107 | */
|
---|
108 | RequestQueue& requestQueue;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Our waiter.
|
---|
112 | */
|
---|
113 | hu::varadiistvan::scpl::io::Waiter waiter;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * The buffered stream to use for communication at the low-level.
|
---|
117 | */
|
---|
118 | hu::varadiistvan::scpl::io::BufferedStream* bufferedStream;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The data stream being used for communication.
|
---|
122 | */
|
---|
123 | hu::varadiistvan::scpl::io::DataStream stream;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The ID of the next multiple-data query request.
|
---|
127 | */
|
---|
128 | size_t nextGetMultiRequestID;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * The registered multiple-data query requests.
|
---|
132 | */
|
---|
133 | getMultiRequests_t getMultiRequests;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * The ID of the next multiple-data update request.
|
---|
137 | */
|
---|
138 | size_t nextSetMultiRequestID;
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * The registered multiple-data query requests.
|
---|
142 | */
|
---|
143 | setMultiRequests_t setMultiRequests;
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * The set of hotkeys currently being handled.
|
---|
147 | */
|
---|
148 | HotkeyHandler::Hotkeys* hotkeys;
|
---|
149 |
|
---|
150 | public:
|
---|
151 | /**
|
---|
152 | * Construct the thread by using a LocalSocket returned by the
|
---|
153 | * given acceptor.
|
---|
154 | */
|
---|
155 | ServerThread(ListenThread& listenThread, RequestQueue& requestQueue,
|
---|
156 | hu::varadiistvan::scpl::io::BufferedStreamAcceptor& acceptor);
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Destroy the thread.
|
---|
160 | */
|
---|
161 | virtual ~ServerThread();
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Quit the thread. It interrupts the blocking stream.
|
---|
165 | */
|
---|
166 | void quit();
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Perform the thread's operation.
|
---|
170 | */
|
---|
171 | virtual void run();
|
---|
172 |
|
---|
173 | private:
|
---|
174 | /**
|
---|
175 | * Handle the COMMAND_GET_SINGLE command
|
---|
176 | *
|
---|
177 | * @return true, if we can continue, false if the thread should quit
|
---|
178 | */
|
---|
179 | bool handleGetSingle();
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Handle the COMMAND_SET_SINGLE command
|
---|
183 | *
|
---|
184 | * @return true, if we can continue, false if the thread should quit
|
---|
185 | */
|
---|
186 | bool handleSetSingle();
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Handle the COMMAND_GET_MULTI command
|
---|
190 | *
|
---|
191 | * @return true, if we can continue, false if the thread should quit
|
---|
192 | */
|
---|
193 | bool handleGetMulti();
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Handle the COMMAND_SET_MULTI command
|
---|
197 | *
|
---|
198 | * @return true, if we can continue, false if the thread should quit
|
---|
199 | */
|
---|
200 | bool handleSetMulti();
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Handle the COMMAND_REGISTER_GET_MULTI command
|
---|
204 | *
|
---|
205 | * @return true, if we can continue, false if the thread should quit
|
---|
206 | */
|
---|
207 | bool handleRegisterGetMulti();
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Handle the COMMAND_UNREGISTER_GET_MULTI command
|
---|
211 | *
|
---|
212 | * @return true, if we can continue, false if the thread should quit
|
---|
213 | */
|
---|
214 | bool handleUnregisterGetMulti();
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Handle the COMMAND_EXECUTE_GET_MULTI command
|
---|
218 | *
|
---|
219 | * @return true, if we can continue, false if the thread should quit
|
---|
220 | */
|
---|
221 | bool handleExecuteGetMulti();
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Handle the COMMAND_REGISTER_SET_MULTI command
|
---|
225 | *
|
---|
226 | * @return true, if we can continue, false if the thread should quit
|
---|
227 | */
|
---|
228 | bool handleRegisterSetMulti();
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Handle the COMMAND_UNREGISTER_SET_MULTI command
|
---|
232 | *
|
---|
233 | * @return true, if we can continue, false if the thread should quit
|
---|
234 | */
|
---|
235 | bool handleUnregisterSetMulti();
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Handle the COMMAND_EXECUTE_SET_MULTI command
|
---|
239 | *
|
---|
240 | * @return true, if we can continue, false if the thread should quit
|
---|
241 | */
|
---|
242 | bool handleExecuteSetMulti();
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Handle the COMMAND_GET_VERSIONS command
|
---|
246 | *
|
---|
247 | * @return true, if we can continue, false if the thread should quit
|
---|
248 | */
|
---|
249 | bool handleGetVersions();
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Handle the COMMAND_RELOAD_PLUGINS command.
|
---|
253 | *
|
---|
254 | * @return true, if we can continue, false if the thread should quit
|
---|
255 | */
|
---|
256 | bool handleReloadPlugins();
|
---|
257 |
|
---|
258 | #ifdef XPLM200
|
---|
259 | /**
|
---|
260 | * Handle the COMMAND_SAVE_SITUATION command.
|
---|
261 | *
|
---|
262 | * @return true, if we can continue, false if the thread should quit
|
---|
263 | */
|
---|
264 | bool handleSaveSituation();
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Handle the COMMAND_SHOW_MESSAGE command
|
---|
269 | *
|
---|
270 | * @return true, if we can continue, false if the thread should quit
|
---|
271 | */
|
---|
272 | bool handleShowMessage();
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Handle the COMMAND_REGISTER_HOTKEYS command.
|
---|
276 | *
|
---|
277 | * @return true if we can continue, false if the thread should
|
---|
278 | * quit.
|
---|
279 | */
|
---|
280 | bool handleRegisterHotkeys();
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Handle the COMMAND_QUERY_HOTKEYS command.
|
---|
284 | *
|
---|
285 | * @return true if we can continue, false if the thread should
|
---|
286 | * quit.
|
---|
287 | */
|
---|
288 | bool handleQueryHotkeys();
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Handle the COMMAND_UNREGISTER_HOTKEYS command.
|
---|
292 | *
|
---|
293 | * @return true if we can continue, false if the thread should
|
---|
294 | * quit.
|
---|
295 | */
|
---|
296 | bool handleUnregisterHotkeys();
|
---|
297 |
|
---|
298 | private:
|
---|
299 | /**
|
---|
300 | * Destroy the current set of hotkeys, if any.
|
---|
301 | *
|
---|
302 | * @return whether there were any hotkeys or not.
|
---|
303 | */
|
---|
304 | bool destroyHotkeys();
|
---|
305 | };
|
---|
306 |
|
---|
307 | //------------------------------------------------------------------------------
|
---|
308 |
|
---|
309 | } /* namespace xplra */
|
---|
310 |
|
---|
311 | //------------------------------------------------------------------------------
|
---|
312 | #endif // XPLRA_SERVERTHREAD_H
|
---|
313 |
|
---|
314 | // Local Variables:
|
---|
315 | // mode: C++
|
---|
316 | // c-basic-offset: 4
|
---|
317 | // indent-tabs-mode: nil
|
---|
318 | // End:
|
---|