source: xplra/src/plugin/src/xplra/ServerThread.h@ 35:9451e75788ea

Last change on this file since 35:9451e75788ea was 13:42fd631176b7, checked in by István Váradi <ivaradi@…>, 11 years ago

Reorganized code to be able to handle the client library as a normal libtool library

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