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 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | #include "ServerThread.h"
|
---|
32 |
|
---|
33 | #include "RequestQueue.h"
|
---|
34 | #include "Protocol.h"
|
---|
35 | #include "GetDataRefTask.h"
|
---|
36 | #include "SetDataRefTask.h"
|
---|
37 | #include "TaskRequest.h"
|
---|
38 |
|
---|
39 | #include <xplcommon/Util.h>
|
---|
40 |
|
---|
41 | #include <cstdio>
|
---|
42 |
|
---|
43 | //------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | using xplra::ServerThread;
|
---|
46 |
|
---|
47 | using xplcommon::LocalAcceptor;
|
---|
48 | using xplcommon::Mutex;
|
---|
49 | using xplcommon::Util;
|
---|
50 |
|
---|
51 | using std::string;
|
---|
52 |
|
---|
53 | //------------------------------------------------------------------------------
|
---|
54 |
|
---|
55 | Mutex ServerThread::instancesMutex;
|
---|
56 |
|
---|
57 | ServerThread::instances_t ServerThread::instances;
|
---|
58 |
|
---|
59 | //------------------------------------------------------------------------------
|
---|
60 |
|
---|
61 | void ServerThread::quitAll()
|
---|
62 | {
|
---|
63 | instancesMutex.lock();
|
---|
64 | for(instances_t::iterator i = instances.begin(); i!=instances.end(); ++i) {
|
---|
65 | (*i)->quit();
|
---|
66 | }
|
---|
67 | instancesMutex.unlock();
|
---|
68 | }
|
---|
69 |
|
---|
70 | //------------------------------------------------------------------------------
|
---|
71 |
|
---|
72 | ServerThread::ServerThread(RequestQueue& requestQueue, LocalAcceptor& acceptor) :
|
---|
73 | Thread(true),
|
---|
74 | requestQueue(requestQueue),
|
---|
75 | bufferedStream(acceptor.getSocket(&waiter)),
|
---|
76 | stream(*bufferedStream)
|
---|
77 | {
|
---|
78 | instancesMutex.lock();
|
---|
79 | instances.insert(this);
|
---|
80 | instancesMutex.unlock();
|
---|
81 | }
|
---|
82 |
|
---|
83 | //------------------------------------------------------------------------------
|
---|
84 |
|
---|
85 | ServerThread::~ServerThread()
|
---|
86 | {
|
---|
87 | delete bufferedStream;
|
---|
88 |
|
---|
89 | instancesMutex.lock();
|
---|
90 | instances.erase(this);
|
---|
91 | instancesMutex.unlock();
|
---|
92 | }
|
---|
93 |
|
---|
94 | //------------------------------------------------------------------------------
|
---|
95 |
|
---|
96 | void ServerThread::quit()
|
---|
97 | {
|
---|
98 | stream.interrupt();
|
---|
99 | }
|
---|
100 |
|
---|
101 | //------------------------------------------------------------------------------
|
---|
102 |
|
---|
103 | void ServerThread::run()
|
---|
104 | {
|
---|
105 | Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run\n", this);
|
---|
106 | while(stream) {
|
---|
107 | uint8_t command = stream.readU8();
|
---|
108 | if (!stream) continue;
|
---|
109 |
|
---|
110 | // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: command=0x%02x\n",
|
---|
111 | // this, command);
|
---|
112 |
|
---|
113 | if (command==Protocol::COMMAND_GET_SINGLE) {
|
---|
114 | if (!handleGetSingle()) break;
|
---|
115 | } else if (command==Protocol::COMMAND_SET_SINGLE) {
|
---|
116 | if (!handleSetSingle()) break;
|
---|
117 | } else {
|
---|
118 | stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
|
---|
119 | }
|
---|
120 | stream.flush();
|
---|
121 | }
|
---|
122 |
|
---|
123 | Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: quitting\n", this);
|
---|
124 | }
|
---|
125 |
|
---|
126 | //------------------------------------------------------------------------------
|
---|
127 |
|
---|
128 | bool ServerThread::handleGetSingle()
|
---|
129 | {
|
---|
130 | uint8_t result = Protocol::RESULT_OK;
|
---|
131 | GetDataRefTask* task = GetDataRefTask::create(result, stream);
|
---|
132 |
|
---|
133 | if (!stream) return false;
|
---|
134 | else if (task==0) {
|
---|
135 | stream.writeU8(result);
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | TaskRequest request(task);
|
---|
140 | if (!requestQueue.execute(&request)) return false;
|
---|
141 |
|
---|
142 | if (task->isValid()) {
|
---|
143 | stream.writeU8(Protocol::RESULT_OK);
|
---|
144 | task->writeValue(stream);
|
---|
145 | } else {
|
---|
146 | stream.writeU8(Protocol::RESULT_UNKNOWN_DATAREF);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | //------------------------------------------------------------------------------
|
---|
153 |
|
---|
154 | bool ServerThread::handleSetSingle()
|
---|
155 | {
|
---|
156 | uint8_t result = Protocol::RESULT_OK;
|
---|
157 | SetDataRefTask* task = SetDataRefTask::create(result, stream);
|
---|
158 |
|
---|
159 | if (!stream) return false;
|
---|
160 | else if (task==0) {
|
---|
161 | stream.writeU8(result);
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 | TaskRequest request(task);
|
---|
166 | if (!requestQueue.execute(&request)) return false;
|
---|
167 |
|
---|
168 | stream.writeU8(task->isValid() ? Protocol::RESULT_OK :
|
---|
169 | Protocol::RESULT_UNKNOWN_DATAREF);
|
---|
170 | return true;
|
---|
171 | }
|
---|
172 |
|
---|
173 | //------------------------------------------------------------------------------
|
---|
174 |
|
---|
175 | // Local Variables:
|
---|
176 | // mode: C++
|
---|
177 | // c-basic-offset: 4
|
---|
178 | // indent-tabs-mode: nil
|
---|
179 | // End:
|
---|