forked from KeckCAVES/3DVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedVisualizationClient.cpp
530 lines (434 loc) · 16.9 KB
/
SharedVisualizationClient.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/***********************************************************************
SharedVisualizationClient - Client for collaborative data exploration in
spatially distributed VR environments, implemented as a plug-in of the
Vrui remote collaboration infrastructure.
Copyright (c) 2009-2012 Oliver Kreylos
This file is part of the 3D Data Visualizer (Visualizer).
The 3D Data Visualizer 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 3D Data Visualizer 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 3D Data Visualizer; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "SharedVisualizationClient.h"
#include <string>
#include <iostream>
#include <Comm/NetPipe.h>
#include <Cluster/MulticastPipe.h>
#include <Vrui/Vrui.h>
#include <Abstract/VariableManager.h>
#include <Abstract/Parameters.h>
#include <Abstract/BinaryParametersSize.h>
#include <Abstract/BinaryParametersSink.h>
#include <Abstract/BinaryParametersSource.h>
#include <Abstract/Algorithm.h>
#include <Abstract/Element.h>
#include <Abstract/Module.h>
#include "GLRenderState.h"
#include "Visualizer.h"
#include "ExtractorLocator.h"
#include "ElementList.h"
/*********************************************************
Methods of class SharedVisualizationClient::RemoteLocator:
*********************************************************/
SharedVisualizationClient::RemoteLocator::RemoteLocator(SharedVisualizationClient::Algorithm* sExtractor)
:Extractor(sExtractor)
{
}
void SharedVisualizationClient::RemoteLocator::readSeedRequest(Comm::NetPipe& pipe)
{
/* Read the seed request ID: */
unsigned int newSeedRequestID=pipe.read<Card>();
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Received seed request "<<newSeedRequestID<<std::endl;
#endif
/* Read extraction parameters from pipe: */
Visualization::Abstract::BinaryParametersSource source(extractor->getVariableManager(),pipe,false);
Parameters* newSeedParameters=extractor->cloneParameters();
pipe.read<Card>(); // Read and ignore parameter blob size
newSeedParameters->read(source);
/* Post a seed request: */
if(extractor->isMaster())
seedRequest(newSeedRequestID,newSeedParameters);
}
void SharedVisualizationClient::RemoteLocator::update(void)
{
/* Wake up the main application: */
Vrui::requestUpdate();
}
/*************************************************************
Methods of class SharedVisualizationClient::RemoteClientState:
*************************************************************/
SharedVisualizationClient::RemoteClientState::RemoteClientState(void)
:locators(17)
{
}
SharedVisualizationClient::RemoteClientState::~RemoteClientState(void)
{
/* Delete all remote locators: */
for(RemoteLocatorHash::Iterator lIt=locators.begin();!lIt.isFinished();++lIt)
delete lIt->getDest();
}
/******************************************
Methods of class SharedVisualizationClient:
******************************************/
void SharedVisualizationClient::receiveRemoteLocator(SharedVisualizationClient::RemoteClientState* rcs,Comm::NetPipe& pipe)
{
/* Receive the new locator's ID and algorithm name: */
unsigned int newLocatorID=pipe.read<Card>();
std::string algorithmName=read<std::string>(pipe);
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Creating "<<algorithmName<<" locator with ID "<<newLocatorID<<std::endl;
#endif
/* Create an extractor for the given algorithm name: */
Cluster::MulticastPipe* algorithmPipe=Vrui::openPipe();
Algorithm* algorithm=application->module->getAlgorithm(algorithmName.c_str(),application->variableManager,algorithmPipe);
if(algorithm!=0)
{
/* Create a new remote locator and add it to the client's hash table: */
RemoteLocator* newRemoteLocator=new RemoteLocator(algorithm);
{
Threads::Mutex::Lock locatorLock(rcs->locatorMutex);
rcs->locators.setEntry(RemoteLocatorHash::Entry(newLocatorID,newRemoteLocator));
}
}
else
{
/* Print a warning message, but carry on otherwise: */
std::cout<<"SharedVisualizationClient::receiveRemoteLocator: Remote client requested locator of unknown type "<<algorithmName<<std::endl;
delete algorithmPipe;
}
}
SharedVisualizationClient::RemoteLocator* SharedVisualizationClient::findRemoteLocator(SharedVisualizationClient::RemoteClientState* rcs,Comm::NetPipe& pipe)
{
/* Read the locator ID: */
unsigned int locatorID=pipe.read<Card>();
{
Threads::Mutex::Lock locatorLock(rcs->locatorMutex);
RemoteLocatorHash::Iterator lIt=rcs->locators.findEntry(locatorID);
if(lIt.isFinished())
return 0;
else
return lIt->getDest();
}
}
SharedVisualizationClient::SharedVisualizationClient(Visualizer* sApplication)
:application(sApplication),
nextLocatorID(0),
locators(17)
{
}
SharedVisualizationClient::~SharedVisualizationClient(void)
{
}
const char* SharedVisualizationClient::getName(void) const
{
return protocolName;
}
unsigned int SharedVisualizationClient::getNumMessages(void) const
{
return MESSAGES_END;
}
void SharedVisualizationClient::sendConnectRequest(Comm::NetPipe& pipe)
{
/* Send the length of the following message: */
pipe.write<Card>(sizeof(Card));
/* Send the client's protocol version: */
pipe.write<Card>(protocolVersion);
}
Collaboration::ProtocolClient::RemoteClientState* SharedVisualizationClient::receiveClientConnect(Comm::NetPipe& pipe)
{
/* Create a new remote client state object: */
RemoteClientState* newClient=new RemoteClientState;
/* Receive the number of locators on the remote client: */
unsigned int numLocators=pipe.read<Card>();
/* Create remote locators: */
for(unsigned int i=0;i<numLocators;++i)
receiveRemoteLocator(newClient,pipe);
return newClient;
}
bool SharedVisualizationClient::receiveServerUpdate(Comm::NetPipe& pipe)
{
/* Ignore a list of global server messages: */
MessageIdType message;
while((message=readMessage(pipe))!=UPDATE_END)
switch(message)
{
case CREATE_ELEMENT:
{
/* Skip an element creation message: */
unsigned int elementId=pipe.read<Card>();
std::string algorithmName=read<std::string>(pipe);
size_t parametersSize=pipe.read<Card>();
Byte* parameters=new Byte[parametersSize];
pipe.read(parameters,parametersSize);
pipe.read<Byte>(); // bool enabled=pipe.read<unsigned char>()!=0;
#ifdef VERBOSE
std::cout<<"Ignored creation of "<<algorithmName<<" element with ID "<<elementId<<std::endl;
#endif
}
}
return false;
}
bool SharedVisualizationClient::receiveServerUpdate(Collaboration::ProtocolClient::RemoteClientState* rcs,Comm::NetPipe& pipe)
{
RemoteClientState* myRcs=dynamic_cast<RemoteClientState*>(rcs);
if(myRcs==0)
Misc::throwStdErr("SharedVisualizationClient::receiveServerUpdate: Mismatching remote client state object type");
/* Receive a list of locator action messages from the server: */
MessageIdType message;
while((message=readMessage(pipe))!=UPDATE_END)
switch(message)
{
case CREATE_LOCATOR:
{
/* Receive and create a new remote locator: */
receiveRemoteLocator(myRcs,pipe);
break;
}
case SEED_REQUEST:
{
/* Find the remote locator: */
RemoteLocator* locator=findRemoteLocator(myRcs,pipe);
if(locator!=0)
{
/* Read and post the seed request: */
locator->readSeedRequest(pipe);
}
else
{
/* Read and ignore the seed request: */
pipe.skip<Card>(1);
unsigned int parametersSize=pipe.read<Card>();
pipe.skip<Byte>(parametersSize);
}
break;
}
case FINALIZATION_REQUEST:
{
/* Find the remote locator: */
RemoteLocator* locator=findRemoteLocator(myRcs,pipe);
/* Read the final seed request ID: */
unsigned int finalRequestID=pipe.read<Card>();
#ifdef VERBOSE
std::cout<<"SharedVisualizationServer: Received finalization request "<<finalRequestID<<std::endl;
#endif
if(locator!=0)
{
/* Post the finalization request: */
locator->finalize(finalRequestID);
}
break;
}
case DESTROY_LOCATOR:
{
/* Read the locator ID: */
unsigned int locatorID=pipe.read<Card>();
/* Find the remote locator and remove it from the hash table: */
RemoteLocator* locator=0;
{
Threads::Mutex::Lock locatorLock(myRcs->locatorMutex);
RemoteLocatorHash::Iterator lIt=myRcs->locators.findEntry(locatorID);
if(!lIt.isFinished())
{
locator=lIt->getDest();
myRcs->locators.removeEntry(lIt);
}
}
/* Destroy the locator: */
delete locator;
break;
}
default:
Misc::throwStdErr("SharedVisualizationClient::receiveServerUpdate: received unknown locator action message %u",message);
}
return false;
}
void SharedVisualizationClient::sendClientUpdate(Comm::NetPipe& pipe)
{
Threads::Mutex::Lock locatorLock(locatorMutex);
/* Send the locator action list to the server: */
for(LocatorActionList::iterator aIt=actions.begin();aIt!=actions.end();++aIt)
{
switch(aIt->action)
{
case CREATE_LOCATOR:
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Creating "<<aIt->locatorIt->getSource()->getExtractor()->getName()<<" locator with ID "<<aIt->locatorIt->getDest().locatorID<<std::endl;
#endif
/* Send a creation message: */
writeMessage(CREATE_LOCATOR,pipe);
/* Send the new locator's ID and algorithm name: */
pipe.write<Card>(aIt->locatorIt->getDest().locatorID);
write(std::string(aIt->locatorIt->getSource()->getExtractor()->getName()),pipe);
break;
case SEED_REQUEST:
/* Only send a message if the action's request ID matches what's still in the locator (i.e., if this was the most recent request): */
if(aIt->requestID==aIt->locatorIt->getDest().seedRequestID)
{
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Sending seed request "<<aIt->requestID<<" for locator "<<aIt->locatorIt->getDest().locatorID<<std::endl;
#endif
/* Send a seed request message: */
writeMessage(SEED_REQUEST,pipe);
/* Send the locator's ID and seed request ID: */
pipe.write<Card>(aIt->locatorIt->getDest().locatorID);
pipe.write<Card>(aIt->requestID);
/* Calculate and send the seed request's extraction parameters message size: */
Visualization::Abstract::BinaryParametersSize size(application->variableManager,false);
aIt->locatorIt->getDest().seedParameters->write(size);
pipe.write<Card>(size.getSize());
/* Send the seed request's extraction parameters: */
Visualization::Abstract::BinaryParametersSink sink(application->variableManager,pipe,false);
aIt->locatorIt->getDest().seedParameters->write(sink);
/* Delete the seed parameters: */
delete aIt->locatorIt->getDest().seedParameters;
aIt->locatorIt->getDest().seedParameters=0;
}
break;
case FINALIZATION_REQUEST:
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Sending finalization request "<<aIt->requestID<<" for locator "<<aIt->locatorIt->getDest().locatorID<<std::endl;
#endif
/* Send a finalization request message: */
writeMessage(FINALIZATION_REQUEST,pipe);
/* Send the locator's ID and final seed request ID: */
pipe.write<Card>(aIt->locatorIt->getDest().locatorID);
pipe.write<Card>(aIt->requestID);
break;
case DESTROY_LOCATOR:
#ifdef VERBOSE
std::cout<<"SharedVisualizationClient: Destroying locator "<<aIt->locatorIt->getDest().locatorID<<std::endl;
#endif
/* Send a destruction message: */
writeMessage(DESTROY_LOCATOR,pipe);
/* Send the locator's ID: */
pipe.write<Card>(aIt->locatorIt->getDest().locatorID);
/* Remove the locator's state from the hash table: */
locators.removeEntry(aIt->locatorIt);
break;
default:
; // Just to make g++ happy
}
}
/* Terminate the action list: */
writeMessage(UPDATE_END,pipe);
/* Clear the action list: */
actions.clear();
}
void SharedVisualizationClient::rejectedByServer(void)
{
/* Write a warning message: */
std::cout<<"SharedVisualizationClient: Server does not support shared Visualizer protocol. Bummer."<<std::endl;
}
void SharedVisualizationClient::connectClient(Collaboration::ProtocolClient::RemoteClientState* rcs)
{
RemoteClientState* myRcs=dynamic_cast<RemoteClientState*>(rcs);
if(myRcs==0)
Misc::throwStdErr("SharedVisualizationClient::connectClient: Mismatching remote client state object type");
/* Add the new remote client object to the list: */
Threads::Mutex::Lock clientStatesLock(clientStatesMutex);
clientStates.push_back(myRcs);
}
void SharedVisualizationClient::disconnectClient(Collaboration::ProtocolClient::RemoteClientState* rcs)
{
RemoteClientState* myRcs=dynamic_cast<RemoteClientState*>(rcs);
if(myRcs==0)
Misc::throwStdErr("SharedVisualizationClient::disconnectClient: Mismatching remote client state object type");
/* Remove the remote client object from the list: */
Threads::Mutex::Lock clientStatesLock(clientStatesMutex);
for(std::vector<RemoteClientState*>::iterator csIt=clientStates.begin();csIt!=clientStates.end();++csIt)
if(*csIt==rcs)
{
clientStates.erase(csIt);
break;
}
}
void SharedVisualizationClient::frame(Collaboration::ProtocolClient::RemoteClientState* rcs)
{
RemoteClientState* myRcs=dynamic_cast<RemoteClientState*>(rcs);
if(myRcs==0)
Misc::throwStdErr("SharedVisualizationClient::frame: Mismatching remote client state object type");
/* Check all the client's remote locators for updates: */
{
Threads::Mutex::Lock locatorLock(myRcs->locatorMutex);
for(RemoteLocatorHash::Iterator lIt=myRcs->locators.begin();!lIt.isFinished();++lIt)
{
Extractor::ElementPointer newElement=lIt->getDest()->checkUpdates();
if(newElement!=0)
application->elementList->addElement(newElement.getPointer(),lIt->getDest()->getExtractor()->getName());
}
}
}
void SharedVisualizationClient::createLocator(ExtractorLocator* locator)
{
{
Threads::Mutex::Lock locatorLock(locatorMutex);
/* Create a new locator state and store it in the locator hash table: */
LocatorState newLocator(nextLocatorID);
++nextLocatorID;
locators.setEntry(LocatorHash::Entry(locator,newLocator));
LocatorHash::Iterator locatorIt=locators.findEntry(locator);
/* Enqueue a locator list action: */
actions.push_back(LocatorAction(CREATE_LOCATOR,locatorIt,0));
}
}
void SharedVisualizationClient::postSeedRequest(ExtractorLocator* locator,unsigned int seedRequestID,SharedVisualizationClient::Parameters* seedParameters)
{
{
Threads::Mutex::Lock locatorLock(locatorMutex);
/* Find the locator's state in the hash table: */
LocatorHash::Iterator locatorIt=locators.findEntry(locator);
if(locatorIt.isFinished())
Misc::throwStdErr("SharedVisualizationClient::postSeedRequest: Locator not found");
/* Store the seed request: */
locatorIt->getDest().seedRequestID=seedRequestID;
delete locatorIt->getDest().seedParameters;
locatorIt->getDest().seedParameters=seedParameters;
/* Enqueue a locator list action: */
actions.push_back(LocatorAction(SEED_REQUEST,locatorIt,seedRequestID));
}
}
void SharedVisualizationClient::postFinalizationRequest(ExtractorLocator* locator,unsigned int finalSeedRequestID)
{
{
Threads::Mutex::Lock locatorLock(locatorMutex);
/* Find the locator's ID in the hash table: */
LocatorHash::Iterator locatorIt=locators.findEntry(locator);
if(locatorIt.isFinished())
Misc::throwStdErr("SharedVisualizationClient::postFinalizationRequest: Locator not found");
/* Enqueue a locator list action: */
actions.push_back(LocatorAction(FINALIZATION_REQUEST,locatorIt,finalSeedRequestID));
}
}
void SharedVisualizationClient::destroyLocator(ExtractorLocator* locator)
{
{
Threads::Mutex::Lock locatorLock(locatorMutex);
/* Find the locator's ID in the hash table: */
LocatorHash::Iterator locatorIt=locators.findEntry(locator);
if(locatorIt.isFinished())
Misc::throwStdErr("SharedVisualizationClient::destroyLocator: Locator not found");
/* Enqueue a locator list action: */
actions.push_back(LocatorAction(DESTROY_LOCATOR,locatorIt,0));
}
}
void SharedVisualizationClient::drawLocators(GLRenderState& renderState,bool transparent) const
{
Threads::Mutex::Lock clientStatesLock(clientStatesMutex);
for(std::vector<RemoteClientState*>::const_iterator csIt=clientStates.begin();csIt!=clientStates.end();++csIt)
{
/* Display the client's remote locators: */
{
Threads::Mutex::Lock locatorLock((*csIt)->locatorMutex);
for(RemoteLocatorHash::Iterator lIt=(*csIt)->locators.begin();!lIt.isFinished();++lIt)
lIt->getDest()->glRenderAction(renderState,transparent);
}
}
}