source: xplra/src/plugin/src/xplra/ServerThread.h@ 45:72d5105fcb72

Last change on this file since 45:72d5105fcb72 was 45:72d5105fcb72, checked in by István Váradi <ivaradi@…>, 11 years ago

Implemented hotkey handling

File size: 8.1 KB
Line 
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/LocalAcceptor.h>
41
42#include <hu/varadiistvan/scpl/Mutex.h>
43
44#include <set>
45#include <map>
46
47//------------------------------------------------------------------------------
48
49namespace xplra {
50
51//------------------------------------------------------------------------------
52
53class ListenThread;
54
55class RequestQueue;
56
57class GetMultiDataRefRequest;
58class SetMultiDataRefRequest;
59
60//------------------------------------------------------------------------------
61
62/**
63 * A thread serving a client.
64 */
65class ServerThread : public hu::varadiistvan::scpl::Thread
66{
67private:
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
93public:
94 /**
95 * Call quit() an all threads.
96 */
97 static void quitAll();
98
99private:
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
150public:
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::LocalAcceptor& 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
173private:
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 /**
259 * Handle the COMMAND_SHOW_MESSAGE command
260 *
261 * @return true, if we can continue, false if the thread should quit
262 */
263 bool handleShowMessage();
264
265 /**
266 * Handle the COMMAND_REGISTER_HOTKEYS command.
267 *
268 * @return true if we can continue, false if the thread should
269 * quit.
270 */
271 bool handleRegisterHotkeys();
272
273 /**
274 * Handle the COMMAND_QUERY_HOTKEYS command.
275 *
276 * @return true if we can continue, false if the thread should
277 * quit.
278 */
279 bool handleQueryHotkeys();
280
281 /**
282 * Handle the COMMAND_UNREGISTER_HOTKEYS command.
283 *
284 * @return true if we can continue, false if the thread should
285 * quit.
286 */
287 bool handleUnregisterHotkeys();
288
289private:
290 /**
291 * Destroy the current set of hotkeys, if any.
292 *
293 * @return whether there were any hotkeys or not.
294 */
295 bool destroyHotkeys();
296};
297
298//------------------------------------------------------------------------------
299
300} /* namespace xplra */
301
302//------------------------------------------------------------------------------
303#endif // XPLRA_SERVERTHREAD_H
304
305// Local Variables:
306// mode: C++
307// c-basic-offset: 4
308// indent-tabs-mode: nil
309// End:
Note: See TracBrowser for help on using the repository browser.