forked from vrui-vr/arsandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteServer.cpp
More file actions
412 lines (351 loc) · 13 KB
/
Copy pathRemoteServer.cpp
File metadata and controls
412 lines (351 loc) · 13 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/***********************************************************************
RemoteServer - Class to connect remote bathymetry and water level
viewers to an Augmented Reality Sandbox.
Copyright (c) 2019-2025 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 <Misc/SizedTypes.h>
#include <Misc/MessageLogger.h>
#include <Comm/Pipe.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"
#include "IntraFrameCompressor.h"
#include "InterFrameCompressor.h"
/*************************************
Methods of class RemoteServer::Client:
*************************************/
RemoteServer::Client::Client(RemoteServer* sServer)
:server(sServer),
clientPipe(server->listenSocket),
state(START)
{
clientPipe.ref();
}
/*****************************
Methods of class RemoteServer:
*****************************/
void RemoteServer::quantizeGrid(GLsizei width,GLsizei height,const GLfloat* source,Pixel* dest)
{
Pixel* dEnd=dest+(height*width);
const GLfloat* sPtr=source;
for(Pixel* dPtr=dest;dPtr!=dEnd;++dPtr,++sPtr)
{
/* Scale the raw elevation value to the quantization range: */
GLfloat se=*sPtr*eScale+eOffset;
/* Clamp and quantize the scaled elevation: */
if(se<=0.0f)
*dPtr=Pixel(0);
else if(se>=65535.0f)
*dPtr=Pixel(65535U);
else
*dPtr=Pixel(se);
}
}
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::INTRA)
--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;
}
}
void RemoteServer::newConnectionCallback(Threads::EventDispatcher::IOEvent& event)
{
/* Get a pointer to the server object: */
RemoteServer* thisPtr=static_cast<RemoteServer*>(event.getUserData());
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;
}
}
void RemoteServer::clientMessageCallback(Threads::EventDispatcher::IOEvent& event)
{
/* Get a pointer to the client object: */
Client* client=static_cast<Client*>(event.getUserData());
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::INTRA;
++server->numClients;
break;
}
case Client::INTRA:
case Client::INTER:
{
/* 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: */
event.removeListener();
}
}
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::INTRA)
{
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 triplet: */
if(grids.lockNewValue())
{
/* Quantize the property grids: */
int newGrid=1-currentGrid;
quantizeGrid(gridSize[0]-1,gridSize[1]-1,grids.getLockedValue().bathymetry,bathymetry[newGrid]);
quantizeGrid(gridSize[0],gridSize[1],grids.getLockedValue().waterLevel,waterLevel[newGrid]);
quantizeGrid(gridSize[0],gridSize[1],grids.getLockedValue().snowHeight,snowHeight[newGrid]);
/* Send the quantized grid triplet to all connected clients in streaming state: */
std::vector<Client*> deadClients;
for(std::vector<Client*>::iterator cIt=clients.begin();cIt!=clients.end();++cIt)
{
try
{
if((*cIt)->state==Client::INTRA)
{
/* Send the new grid triplet to the client using intra-frame compression: */
IntraFrameCompressor compressor((*cIt)->clientPipe);
compressor.compressFrame(gridSize[0]-1,gridSize[1]-1,bathymetry[newGrid]);
compressor.compressFrame(gridSize[0],gridSize[1],waterLevel[newGrid]);
compressor.compressFrame(gridSize[0],gridSize[1],snowHeight[newGrid]);
/* Finish the message: */
(*cIt)->clientPipe.flush();
/* Send grid pairs using inter-frame compression from now on: */
(*cIt)->state=Client::INTER;
}
else if((*cIt)->state==Client::INTER)
{
/* Send the difference between the current and new grid pairs to the client using inter-frame compression: */
InterFrameCompressor compressor((*cIt)->clientPipe);
compressor.compressFrame(gridSize[0]-1,gridSize[1]-1,bathymetry[currentGrid],bathymetry[newGrid]);
compressor.compressFrame(gridSize[0],gridSize[1],waterLevel[currentGrid],waterLevel[newGrid]);
compressor.compressFrame(gridSize[0],gridSize[1],snowHeight[currentGrid],snowHeight[newGrid]);
/* Finish the message: */
(*cIt)->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);
currentGrid=newGrid;
}
}
return 0;
}
void RemoteServer::readBackCallback(GLfloat*,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: */
Comm::ignorePipeSignals();
/* 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];
GLfloat safety=(elevationRange[1]-elevationRange[0])*0.05f;
elevationRange[0]-=safety;
elevationRange[1]+=safety;
/* Calculate elevation quantization factors: */
eScale=65535.0f/(elevationRange[1]-elevationRange[0]);
eOffset=0.5f-elevationRange[0]*eScale;
/* Allocate the property grids: */
for(int i=0;i<3;++i)
grids.getBuffer(i).init(gridSize);
/* Create the grid quantization buffers: */
for(int i=0;i<2;++i)
{
bathymetry[i]=new Pixel[(gridSize[1]-1)*(gridSize[0]-1)];
waterLevel[i]=new Pixel[gridSize[1]*gridSize[0]];
snowHeight[i]=new Pixel[gridSize[1]*gridSize[0]];
}
currentGrid=1;
/* Start listening for incoming connections on the listening socket: */
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;
/* Release allocated resources: */
for(int i=0;i<2;++i)
{
delete[] bathymetry[i];
delete[] waterLevel[i];
delete[] snowHeight[i];
}
}
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,gb.snowHeight,&RemoteServer::readBackCallback,this))
{
/* Push the next request time forward: */
nextRequestTime=(Math::floor(applicationTime/requestInterval)+1.0)*requestInterval;
}
}
}
void RemoteServer::glRenderAction(const PTransform& projection,const OGTransform& modelview,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);
/* Set up the rendering matrices: */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadMatrix(projection);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrix(modelview);
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();
}
/* Restore original rendering matrices: */
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
}