source: xplra/src/plugin/src/xplra/ServerThread.h@ 38:128b9ced9779

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

Added basic support for showing a message

File size: 7.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 <hu/varadiistvan/scpl/Thread.h>
34
35#include <hu/varadiistvan/scpl/io/Waiter.h>
36#include <hu/varadiistvan/scpl/io/BufferedStream.h>
37#include <hu/varadiistvan/scpl/io/DataStream.h>
38#include <hu/varadiistvan/scpl/io/LocalAcceptor.h>
39
40#include <hu/varadiistvan/scpl/Mutex.h>
41
42#include <set>
43#include <map>
44
45//------------------------------------------------------------------------------
46
47namespace xplra {
48
49//------------------------------------------------------------------------------
50
51class ListenThread;
52
53class RequestQueue;
54
55class GetMultiDataRefRequest;
56class SetMultiDataRefRequest;
57
58//------------------------------------------------------------------------------
59
60/**
61 * A thread serving a client.
62 */
63class ServerThread : public hu::varadiistvan::scpl::Thread
64{
65private:
66 /**
67 * Type for the set of server thread instances.
68 */
69 typedef std::set<ServerThread*> instances_t;
70
71 /**
72 * Type for the registered multiple-data query requests.
73 */
74 typedef std::map<size_t, GetMultiDataRefRequest*> getMultiRequests_t;
75
76 /**
77 * Type for the registered multiple-data update requests.
78 */
79 typedef std::map<size_t, SetMultiDataRefRequest*> setMultiRequests_t;
80
81 /**
82 * A mutex to protect the collection of server threads.
83 */
84 static hu::varadiistvan::scpl::Mutex instancesMutex;
85
86 /**
87 * The instances of this class.
88 */
89 static instances_t instances;
90
91public:
92 /**
93 * Call quit() an all threads.
94 */
95 static void quitAll();
96
97private:
98 /**
99 * The listen thread this server was started by.
100 */
101 ListenThread& listenThread;
102
103 /**
104 * The request queue to use.
105 */
106 RequestQueue& requestQueue;
107
108 /**
109 * Our waiter.
110 */
111 hu::varadiistvan::scpl::io::Waiter waiter;
112
113 /**
114 * The buffered stream to use for communication at the low-level.
115 */
116 hu::varadiistvan::scpl::io::BufferedStream* bufferedStream;
117
118 /**
119 * The data stream being used for communication.
120 */
121 hu::varadiistvan::scpl::io::DataStream stream;
122
123 /**
124 * The ID of the next multiple-data query request.
125 */
126 size_t nextGetMultiRequestID;
127
128 /**
129 * The registered multiple-data query requests.
130 */
131 getMultiRequests_t getMultiRequests;
132
133 /**
134 * The ID of the next multiple-data update request.
135 */
136 size_t nextSetMultiRequestID;
137
138 /**
139 * The registered multiple-data query requests.
140 */
141 setMultiRequests_t setMultiRequests;
142
143public:
144 /**
145 * Construct the thread by using a LocalSocket returned by the
146 * given acceptor.
147 */
148 ServerThread(ListenThread& listenThread, RequestQueue& requestQueue,
149 hu::varadiistvan::scpl::io::LocalAcceptor& acceptor);
150
151 /**
152 * Destroy the thread.
153 */
154 virtual ~ServerThread();
155
156 /**
157 * Quit the thread. It interrupts the blocking stream.
158 */
159 void quit();
160
161 /**
162 * Perform the thread's operation.
163 */
164 virtual void run();
165
166private:
167 /**
168 * Handle the COMMAND_GET_SINGLE command
169 *
170 * @return true, if we can continue, false if the thread should quit
171 */
172 bool handleGetSingle();
173
174 /**
175 * Handle the COMMAND_SET_SINGLE command
176 *
177 * @return true, if we can continue, false if the thread should quit
178 */
179 bool handleSetSingle();
180
181 /**
182 * Handle the COMMAND_GET_MULTI command
183 *
184 * @return true, if we can continue, false if the thread should quit
185 */
186 bool handleGetMulti();
187
188 /**
189 * Handle the COMMAND_SET_MULTI command
190 *
191 * @return true, if we can continue, false if the thread should quit
192 */
193 bool handleSetMulti();
194
195 /**
196 * Handle the COMMAND_REGISTER_GET_MULTI command
197 *
198 * @return true, if we can continue, false if the thread should quit
199 */
200 bool handleRegisterGetMulti();
201
202 /**
203 * Handle the COMMAND_UNREGISTER_GET_MULTI command
204 *
205 * @return true, if we can continue, false if the thread should quit
206 */
207 bool handleUnregisterGetMulti();
208
209 /**
210 * Handle the COMMAND_EXECUTE_GET_MULTI command
211 *
212 * @return true, if we can continue, false if the thread should quit
213 */
214 bool handleExecuteGetMulti();
215
216 /**
217 * Handle the COMMAND_REGISTER_SET_MULTI command
218 *
219 * @return true, if we can continue, false if the thread should quit
220 */
221 bool handleRegisterSetMulti();
222
223 /**
224 * Handle the COMMAND_UNREGISTER_SET_MULTI command
225 *
226 * @return true, if we can continue, false if the thread should quit
227 */
228 bool handleUnregisterSetMulti();
229
230 /**
231 * Handle the COMMAND_EXECUTE_SET_MULTI command
232 *
233 * @return true, if we can continue, false if the thread should quit
234 */
235 bool handleExecuteSetMulti();
236
237 /**
238 * Handle the COMMAND_GET_VERSIONS command
239 *
240 * @return true, if we can continue, false if the thread should quit
241 */
242 bool handleGetVersions();
243
244 /**
245 * Handle the COMMAND_SHOW_MESSAGE command
246 *
247 * @return true, if we can continue, false if the thread should quit
248 */
249 bool handleShowMessage();
250};
251
252//------------------------------------------------------------------------------
253
254} /* namespace xplra */
255
256//------------------------------------------------------------------------------
257#endif // XPLRA_SERVERTHREAD_H
258
259// Local Variables:
260// mode: C++
261// c-basic-offset: 4
262// indent-tabs-mode: nil
263// End:
Note: See TracBrowser for help on using the repository browser.