This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
forked from KeckCAVES/SARndbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteServer.cpp
352 lines (295 loc) · 13.9 KB
/
RemoteServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***********************************************************************
RemoteServer - Class to connect remote bathymetry and water level
viewers to an Augmented Reality Sandbox.
Copyright (c) 2019 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "RemoteServer.h"
#include <signal.h>
#include <Misc/SizedTypes.h>
#include <Misc/MessageLogger.h>
#include <Math/Math.h>
#include <GL/gl.h>
#include <GL/GLMaterialTemplates.h>
#include <GL/GLModels.h>
#include <GL/GLGeometryWrappers.h>
#include <GL/GLTransformationWrappers.h>
#include "WaterTable2.h"
#include "Sandbox.h"
/*************************************
Methods of class RemoteServer::Client:
*************************************/
RemoteServer::Client::Client(RemoteServer* sServer)
: server(sServer),
clientPipe(server->listenSocket),
state(START) {
}
/*****************************
Methods of class RemoteServer:
*****************************/
void RemoteServer::disconnectClient(Client* client, bool removeListener) {
/* Find the client in the client list: */
for (std::vector<Client*>::iterator cIt = clients.begin(); cIt != clients.end(); ++cIt)
if (*cIt == client) {
/* Reduce the number of streaming clients if the client was streaming: */
if (client->state == Client::STREAMING) {
--numClients;
}
if (removeListener) {
/* Remove the client's event listener: */
dispatcher.removeIOEventListener(client->listenerKey);
}
/* Remove the client from the list: */
*cIt = clients.back();
clients.pop_back();
/* Disconnect the client: */
delete client;
break;
}
}
bool RemoteServer::newConnectionCallback(Threads::EventDispatcher::ListenerKey eventKey,
int eventType, void* userData) {
/* Get a pointer to the server object: */
RemoteServer* thisPtr = static_cast<RemoteServer*>(userData);
Client* newClient = 0;
try {
/* Create a new client object: */
newClient = new Client(thisPtr);
/* Send an endianness token to the client: */
newClient->clientPipe.write<Misc::UInt32>(0x12345678U);
/* Send the water table's grid size and cell size to the client: */
for (int i = 0; i < 2; ++i) {
newClient->clientPipe.write<Misc::UInt32>(thisPtr->gridSize[i]);
newClient->clientPipe.write<Misc::Float32>(thisPtr->cellSize[i]);
}
/* Send the water table's elevation range: */
for (int i = 0; i < 2; ++i) {
newClient->clientPipe.write<Misc::Float32>(thisPtr->elevationRange[i]);
}
/* Finish the message: */
newClient->clientPipe.flush();
/* Add an event listener for incoming messages from the client: */
newClient->listenerKey = thisPtr->dispatcher.addIOEventListener(newClient->clientPipe.getFd(),
Threads::EventDispatcher::Read, thisPtr->clientMessageCallback, newClient);
/* Add the new client to the list: */
thisPtr->clients.push_back(newClient);
} catch (const std::runtime_error& err) {
/* Disconnect the new client: */
delete newClient;
}
return false;
}
bool RemoteServer::clientMessageCallback(Threads::EventDispatcher::ListenerKey eventKey,
int eventType, void* userData) {
/* Get a pointer to the client object: */
Client* client = static_cast<Client*>(userData);
RemoteServer* server = client->server;
try {
/* Handle incoming message based on the client's state: */
switch (client->state) {
case Client::START: {
/* Read an endianness token: */
Misc::UInt32 token = client->clientPipe.read<Misc::UInt32>();
if (token == 0x78563412U) {
client->clientPipe.setSwapOnRead(true);
} else if (token != 0x12345678U) {
throw std::runtime_error("Invalid endianness token");
}
/* Go to the next state: */
client->state = Client::STREAMING;
++server->numClients;
break;
}
case Client::STREAMING: {
/* Read the message token: */
unsigned int token = client->clientPipe.read<Misc::UInt16>();
switch (token) {
case 0: // Position update message
Misc::Float32 pos[3];
client->clientPipe.read(pos, 3);
client->position = Vrui::Point(pos);
Misc::Float32 dir[3];
client->clientPipe.read(dir, 3);
client->direction = Vrui::Vector(dir);
break;
default:
throw std::runtime_error("Invalid client message");
}
break;
}
}
} catch (const std::runtime_error& err) {
/* Disconnect the client or something: */
Misc::formattedConsoleWarning("RemoteServer: Disconnecting client due to exception %s",
err.what());
server->disconnectClient(client, false);
/* Stop listening on the client's socket: */
return true;
}
return false;
}
void* RemoteServer::communicationThreadMethod(void) {
/* Dispatch events on the communications socket(s) until stopped by the main thread: */
while (dispatcher.dispatchNextEvent()) {
/* Collect the current positions of all connected clients in streaming state: */
std::vector<Vrui::ONTransform>& positions = clientPositions.startNewValue();
positions.clear();
Vrui::Point gridOffset(Math::div2(gridSize[0]*cellSize[0]), Math::div2(gridSize[1]*cellSize[1]));
for (std::vector<Client*>::iterator cIt = clients.begin(); cIt != clients.end(); ++cIt)
if ((*cIt)->state == Client::STREAMING) {
Vrui::Vector t = (*cIt)->position - gridOffset;
Vrui::Rotation r = Vrui::Rotation::rotateFromTo(Vrui::Vector(0, 0, -1), (*cIt)->direction);
positions.push_back(Vrui::ONTransform(t, r));
}
clientPositions.postNewValue();
/* Check if there is a new grid pair: */
if (grids.lockNewValue()) {
/* Send the new grid pair to all connected clients in streaming state: */
std::vector<Client*> deadClients;
const GLfloat* bathymetry = grids.getLockedValue().bathymetry;
const GLfloat* waterLevel = grids.getLockedValue().waterLevel;
for (std::vector<Client*>::iterator cIt = clients.begin(); cIt != clients.end(); ++cIt)
if ((*cIt)->state == Client::STREAMING) {
/* Calculate elevation quantization factors: */
GLfloat eScale = 65535.0f / (elevationRange[1] - elevationRange[0]);
GLfloat eOffset = 0.5f - elevationRange[0] * eScale;
try {
Comm::TCPPipe& clientPipe = (*cIt)->clientPipe;
/* Send the bathymetry grid: */
const GLfloat* bPtr = bathymetry;
for (GLsizei y = 0; y < gridSize[1] - 1; ++y)
for (GLsizei x = 0; x < gridSize[0] - 1; ++x, ++bPtr) {
GLfloat se = *bPtr * eScale + eOffset;
if (se <= 0.0f) {
clientPipe.write<Misc::UInt16>(0U);
} else if (se >= 65535.0f) {
clientPipe.write<Misc::UInt16>(65535U);
} else {
clientPipe.write<Misc::UInt16>(Misc::UInt16(se));
}
}
/* Send the water level grid: */
const GLfloat* wlPtr = waterLevel;
for (GLsizei y = 0; y < gridSize[1]; ++y)
for (GLsizei x = 0; x < gridSize[0]; ++x, ++wlPtr) {
GLfloat se = *wlPtr * eScale + eOffset;
if (se <= 0.0f) {
clientPipe.write<Misc::UInt16>(0U);
} else if (se >= 65535.0f) {
clientPipe.write<Misc::UInt16>(65535U);
} else {
clientPipe.write<Misc::UInt16>(Misc::UInt16(se));
}
}
/* Finish the message: */
clientPipe.flush();
} catch (const std::runtime_error& err) {
/* Disconnect the client: */
Misc::formattedConsoleWarning("RemoteServer: Disconnecting client due to exception %s",
err.what());
deadClients.push_back(*cIt);
}
}
/* Disconnect all dead clients: */
for (std::vector<Client*>::iterator dcIt = deadClients.begin(); dcIt != deadClients.end();
++dcIt) {
disconnectClient(*dcIt, true);
}
}
}
return 0;
}
void RemoteServer::readBackCallback(GLfloat*, GLfloat*, void* userData) {
RemoteServer* thisPtr = static_cast<RemoteServer*>(userData);
/* Post the new grids to the grid triple buffer and wake up the communication thread: */
thisPtr->grids.postNewValue();
thisPtr->dispatcher.interrupt();
}
RemoteServer::RemoteServer(Sandbox* sSandbox, int listenPortId, double sRequestInterval)
: sandbox(sSandbox),
listenSocket(listenPortId, 0),
numClients(0),
requestInterval(sRequestInterval), nextRequestTime(0.0) {
/* Ignore SIGPIPE and leave handling of pipe errors to TCP sockets: */
struct sigaction sigPipeAction;
memset(&sigPipeAction, 0, sizeof(struct sigaction));
sigPipeAction.sa_handler = SIG_IGN;
sigemptyset(&sigPipeAction.sa_mask);
sigPipeAction.sa_flags = 0x0;
sigaction(SIGPIPE, &sigPipeAction, 0);
/* Retrieve the water table's grid and cell sizes: */
for (int i = 0; i < 2; ++i) {
gridSize[i] = sandbox->waterTable->getSize()[i];
cellSize[i] = sandbox->waterTable->getCellSize()[i];
}
/* Retrieve the water table's elevation range and add a safety margin: */
elevationRange[0] = sandbox->waterTable->getDomain().min[2];
elevationRange[1] = sandbox->waterTable->getDomain().max[2];
elevationRange[0] -= (elevationRange[1] - elevationRange[0]) * 0.05f;
elevationRange[1] += (elevationRange[1] - elevationRange[0]) * 0.05f;
/* Allocate the bathymetry and water level grids: */
for (int i = 0; i < 3; ++i) {
grids.getBuffer(i).init(gridSize);
}
/* Start listening for incoming connections on the listening sockets: */
dispatcher.addIOEventListener(listenSocket.getFd(), Threads::EventDispatcher::Read,
newConnectionCallback, this);
communicationThread.start(this, &RemoteServer::communicationThreadMethod);
}
RemoteServer::~RemoteServer(void) {
/* Shut down the communication thread: */
dispatcher.stop();
communicationThread.join();
/* Disconnect all clients: */
for (std::vector<Client*>::iterator cIt = clients.begin(); cIt != clients.end(); ++cIt) {
delete *cIt;
}
}
void RemoteServer::frame(double applicationTime) {
/* Lock the most recent list of client positions: */
clientPositions.lockNewValue();
/* Check if it's time to request a new set of grids: */
if (numClients > 0 && applicationTime >= nextRequestTime) {
/* Request new grids: */
GridBuffers& gb = grids.startNewValue();
if (sandbox->gridRequest.requestGrids(gb.bathymetry, gb.waterLevel,
&RemoteServer::readBackCallback, this)) {
/* Push the next request time forward: */
nextRequestTime = (Math::floor(applicationTime / requestInterval) + 1.0) * requestInterval;
}
}
}
void RemoteServer::glRenderAction(GLContextData& contextData) const {
/* Draw icons for all connected clients: */
const std::vector<Vrui::ONTransform>& positions = clientPositions.getLockedValue();
if (!positions.empty()) {
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(1.0f, 0.0f, 0.0f));
glMaterialSpecular(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(1.0f, 1.0f, 1.0f));
glMaterialShininess(GLMaterialEnums::FRONT, 32.0f);
glPushMatrix();
glMultMatrix(Geometry::invert(sandbox->boxTransform));
for (std::vector<Vrui::ONTransform>::const_iterator pIt = positions.begin();
pIt != positions.end(); ++pIt) {
glPushMatrix();
/* Draw the client's position: */
glMultMatrix(*pIt);
glDrawSphereIcosahedron(1.0f, 4);
/* Draw the client's viewing direction: */
glTranslate(0.0f, 0.0f, -1.25f);
glDrawCone(0.5f, 2.0f, 16);
glPopMatrix();
}
glPopMatrix();
}
}