source: xplra/src/xplra/ServerThread.cc@ 6:8dd4ca9966d0

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

Made all the single query operation work for all types

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