source: xplra/test/emulator.cc@ 112:58892a32a039

Last change on this file since 112:58892a32a039 was 99:bb320ead601d, checked in by István Váradi <ivaradi@…>, 5 years ago

The beginnings of a simple plugin emulator

File size: 7.8 KB
Line 
1// Copyright (c) 2019 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#include "xplra/Protocol.h"
31
32#include <hu/varadiistvan/scpl/io/Waiter.h>
33#include <hu/varadiistvan/scpl/io/LocalServerSocket.h>
34#include <hu/varadiistvan/scpl/io/LocalAcceptor.h>
35#include <hu/varadiistvan/scpl/io/BufferedStream.h>
36#include <hu/varadiistvan/scpl/io/DataStream.h>
37
38#include <hu/varadiistvan/scpl/Thread.h>
39
40//------------------------------------------------------------------------------
41
42using xplra::Protocol;
43
44using hu::varadiistvan::scpl::io::Waiter;
45using hu::varadiistvan::scpl::io::LocalServerSocket;
46using hu::varadiistvan::scpl::io::LocalAcceptor;
47
48using std::string;
49
50//------------------------------------------------------------------------------
51
52class ServerThread : public hu::varadiistvan::scpl::Thread
53{
54private:
55 /**
56 * Our waiter.
57 */
58 hu::varadiistvan::scpl::io::Waiter waiter;
59
60 /**
61 * The buffered stream to use for communication at the low-level.
62 */
63 hu::varadiistvan::scpl::io::BufferedStream* bufferedStream;
64
65 /**
66 * The data stream being used for communication.
67 */
68 hu::varadiistvan::scpl::io::DataStream stream;
69
70public:
71 /**
72 * Construct the thread by using a LocalSocket returned by the
73 * given acceptor.
74 */
75 ServerThread(hu::varadiistvan::scpl::io::LocalAcceptor& acceptor);
76
77 /**
78 * Destroy the thread.
79 */
80 virtual ~ServerThread();
81
82 /**
83 * Perform the thread's operation.
84 */
85 virtual void run();
86};
87
88//------------------------------------------------------------------------------
89
90ServerThread::ServerThread(LocalAcceptor& acceptor) :
91 bufferedStream(acceptor.getSocket(&waiter)),
92 stream(*bufferedStream)
93{
94}
95
96//------------------------------------------------------------------------------
97
98ServerThread::~ServerThread()
99{
100 delete bufferedStream;
101}
102
103//------------------------------------------------------------------------------
104
105void ServerThread::run()
106{
107 printf("hu.varadiistvan.xplra.ServerThread[%p]::run\n", this);
108 while(stream) {
109 uint8_t command = stream.readU8();
110 if (!stream) continue;
111
112 printf("hu.varadiistvan.xplra.ServerThread[%p]::run: command=0x%02x\n",
113 this, command);
114
115 if (command==Protocol::COMMAND_GET_VERSIONS) {
116 printf("Get versions\n");
117 int xplaneVersion = 0x110000;
118 int xplmVersion = 0x31000;
119
120
121 stream.writeU8(Protocol::RESULT_OK);
122 stream.writeS32(xplaneVersion);
123 stream.writeS32(xplmVersion);
124 stream.writeS32(Protocol::version);
125 } else if (command==Protocol::COMMAND_GET_MULTI) {
126 printf("Get multi\n");
127 uint32_t numTasks = stream.readU32();
128 if (!stream) break;
129
130 for(size_t i = 0; i<numTasks; ++i) {
131 string name = stream.readString();
132 uint8_t type = stream.readU8();
133 if (!stream) break;
134
135 printf(" name='%s', type=%u\n",
136 name.c_str(), type);
137
138 if (type==Protocol::TYPE_FLOAT_ARRAY ||
139 type==Protocol::TYPE_INT_ARRAY ||
140 type==Protocol::TYPE_BYTE_ARRAY)
141 {
142 int length = stream.readS32();
143 int offset = stream.readS32();
144 if (!stream) break;
145 }
146
147 }
148
149 stream.writeU8(Protocol::RESULT_UNKNOWN_DATAREF);
150 stream.writeU32(0);
151 }
152// if (command==Protocol::COMMAND_GET_SINGLE) {
153// if (!handleGetSingle()) break;
154// } else if (command==Protocol::COMMAND_SET_SINGLE) {
155// if (!handleSetSingle()) break;
156// } else if (command==Protocol::COMMAND_GET_MULTI) {
157// if (!handleGetMulti()) break;
158// } else if (command==Protocol::COMMAND_SET_MULTI) {
159// if (!handleSetMulti()) break;
160// } else if (command==Protocol::COMMAND_REGISTER_GET_MULTI) {
161// if (!handleRegisterGetMulti()) break;
162// } else if (command==Protocol::COMMAND_UNREGISTER_GET_MULTI) {
163// if (!handleUnregisterGetMulti()) break;
164// } else if (command==Protocol::COMMAND_EXECUTE_GET_MULTI) {
165// if (!handleExecuteGetMulti()) break;
166// } else if (command==Protocol::COMMAND_REGISTER_SET_MULTI) {
167// if (!handleRegisterSetMulti()) break;
168// } else if (command==Protocol::COMMAND_UNREGISTER_SET_MULTI) {
169// if (!handleUnregisterSetMulti()) break;
170// } else if (command==Protocol::COMMAND_EXECUTE_SET_MULTI) {
171// if (!handleExecuteSetMulti()) break;
172// } else if (command==Protocol::COMMAND_GET_VERSIONS) {
173// if (!handleGetVersions()) break;
174// } else if (command==Protocol::COMMAND_RELOAD_PLUGINS) {
175// if (!handleReloadPlugins()) break;
176// #ifdef XPLM200
177// } else if (command==Protocol::COMMAND_SAVE_SITUATION) {
178// if (!handleSaveSituation()) break;
179// #endif
180// } else if (command==Protocol::COMMAND_SHOW_MESSAGE) {
181// if (!handleShowMessage()) break;
182// } else if (command==Protocol::COMMAND_REGISTER_HOTKEYS) {
183// if (!handleRegisterHotkeys()) break;
184// } else if (command==Protocol::COMMAND_QUERY_HOTKEYS) {
185// if (!handleQueryHotkeys()) break;
186// } else if (command==Protocol::COMMAND_UNREGISTER_HOTKEYS) {
187// if (!handleUnregisterHotkeys()) break;
188// } else {
189// stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
190// }
191 stream.flush();
192 }
193
194 // destroyHotkeys();
195
196 printf("hu.varadiistvan.xplra.ServerThread[%p]::run: quitting\n", this);
197}
198
199//------------------------------------------------------------------------------
200//------------------------------------------------------------------------------
201
202int main()
203{
204 Waiter waiter;
205
206 LocalServerSocket socket("xplra", &waiter);
207 LocalAcceptor& acceptor = socket.getAcceptor();
208 while(!acceptor.failed()) {
209 while (acceptor.accept()) {
210 ServerThread* serverThread = new ServerThread(acceptor);
211 serverThread->start();
212 }
213 waiter.wait();
214 }
215}
Note: See TracBrowser for help on using the repository browser.