[2] | 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 |
|
---|
[11] | 33 | #include <hu/varadiistvan/scpl/Thread.h>
|
---|
[2] | 34 |
|
---|
[11] | 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>
|
---|
[2] | 41 |
|
---|
| 42 | #include <set>
|
---|
[8] | 43 | #include <map>
|
---|
[2] | 44 |
|
---|
| 45 | //------------------------------------------------------------------------------
|
---|
| 46 |
|
---|
| 47 | namespace xplra {
|
---|
| 48 |
|
---|
| 49 | //------------------------------------------------------------------------------
|
---|
| 50 |
|
---|
[36] | 51 | class ListenThread;
|
---|
| 52 |
|
---|
[3] | 53 | class RequestQueue;
|
---|
| 54 |
|
---|
[8] | 55 | class GetMultiDataRefRequest;
|
---|
[9] | 56 | class SetMultiDataRefRequest;
|
---|
[8] | 57 |
|
---|
[3] | 58 | //------------------------------------------------------------------------------
|
---|
| 59 |
|
---|
[2] | 60 | /**
|
---|
| 61 | * A thread serving a client.
|
---|
| 62 | */
|
---|
[11] | 63 | class ServerThread : public hu::varadiistvan::scpl::Thread
|
---|
[2] | 64 | {
|
---|
| 65 | private:
|
---|
| 66 | /**
|
---|
| 67 | * Type for the set of server thread instances.
|
---|
| 68 | */
|
---|
| 69 | typedef std::set<ServerThread*> instances_t;
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[9] | 72 | * Type for the registered multiple-data query requests.
|
---|
[8] | 73 | */
|
---|
| 74 | typedef std::map<size_t, GetMultiDataRefRequest*> getMultiRequests_t;
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
[9] | 77 | * Type for the registered multiple-data update requests.
|
---|
| 78 | */
|
---|
| 79 | typedef std::map<size_t, SetMultiDataRefRequest*> setMultiRequests_t;
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
[2] | 82 | * A mutex to protect the collection of server threads.
|
---|
| 83 | */
|
---|
[11] | 84 | static hu::varadiistvan::scpl::Mutex instancesMutex;
|
---|
[2] | 85 |
|
---|
| 86 | /**
|
---|
| 87 | * The instances of this class.
|
---|
| 88 | */
|
---|
| 89 | static instances_t instances;
|
---|
| 90 |
|
---|
| 91 | public:
|
---|
| 92 | /**
|
---|
| 93 | * Call quit() an all threads.
|
---|
| 94 | */
|
---|
| 95 | static void quitAll();
|
---|
| 96 |
|
---|
| 97 | private:
|
---|
| 98 | /**
|
---|
[36] | 99 | * The listen thread this server was started by.
|
---|
| 100 | */
|
---|
| 101 | ListenThread& listenThread;
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
[3] | 104 | * The request queue to use.
|
---|
| 105 | */
|
---|
| 106 | RequestQueue& requestQueue;
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
[2] | 109 | * Our waiter.
|
---|
| 110 | */
|
---|
[11] | 111 | hu::varadiistvan::scpl::io::Waiter waiter;
|
---|
[2] | 112 |
|
---|
| 113 | /**
|
---|
| 114 | * The buffered stream to use for communication at the low-level.
|
---|
| 115 | */
|
---|
[11] | 116 | hu::varadiistvan::scpl::io::BufferedStream* bufferedStream;
|
---|
[2] | 117 |
|
---|
| 118 | /**
|
---|
[3] | 119 | * The data stream being used for communication.
|
---|
[2] | 120 | */
|
---|
[11] | 121 | hu::varadiistvan::scpl::io::DataStream stream;
|
---|
[2] | 122 |
|
---|
[8] | 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 |
|
---|
[9] | 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 |
|
---|
[2] | 143 | public:
|
---|
| 144 | /**
|
---|
| 145 | * Construct the thread by using a LocalSocket returned by the
|
---|
| 146 | * given acceptor.
|
---|
| 147 | */
|
---|
[36] | 148 | ServerThread(ListenThread& listenThread, RequestQueue& requestQueue,
|
---|
[11] | 149 | hu::varadiistvan::scpl::io::LocalAcceptor& acceptor);
|
---|
[2] | 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();
|
---|
[6] | 165 |
|
---|
| 166 | private:
|
---|
| 167 | /**
|
---|
| 168 | * Handle the COMMAND_GET_SINGLE command
|
---|
| 169 | *
|
---|
| 170 | * @return true, if we can continue, false if the thread should quit
|
---|
| 171 | */
|
---|
[7] | 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();
|
---|
[8] | 180 |
|
---|
| 181 | /**
|
---|
[10] | 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 | /**
|
---|
[8] | 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();
|
---|
[9] | 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();
|
---|
[36] | 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();
|
---|
[38] | 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();
|
---|
[2] | 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:
|
---|