-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSharedVisualizationClient.cpp
More file actions
420 lines (343 loc) · 17.3 KB
/
SharedVisualizationClient.cpp
File metadata and controls
420 lines (343 loc) · 17.3 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
413
414
415
416
417
418
419
420
/***********************************************************************
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-2023 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 <stdexcept>
#include <Misc/SizedTypes.h>
#include <Threads/FunctionCalls.h>
#include <Vrui/Vrui.h>
#include <Collaboration2/MessageWriter.h>
#include <Collaboration2/DataType.icpp>
#include <Collaboration2/MessageContinuation.h>
#include <Collaboration2/NonBlockSocket.h>
#include <Collaboration2/Client.h>
#include <Abstract/VariableManager.h>
#include <Abstract/Parameters.h>
#include <Abstract/Algorithm.h>
#include <Abstract/Element.h>
#include "ElementList.h"
// DEBUGGING
#include <iostream>
namespace Collab {
namespace Plugins {
/******************************************
Methods of class SharedVisualizationClient:
******************************************/
/*********************************************************
Methods of class SharedVisualizationClient::SharedElement:
*********************************************************/
SharedVisualizationClient::SharedElement::~SharedElement(void)
{
/* Destroy the algorithm used to create the element: */
delete algorithm;
/* Destroy the shared parameters structure: */
if(parameters!=0)
elementTypes.destroyObject(parametersType,parameters);
}
MessageContinuation* SharedVisualizationClient::connectRejectCallback(unsigned int messageId,MessageContinuation* continuation)
{
/* Signal a bad connection: */
{
Threads::MutexCond::Lock connectionEstablishedLock(connectionEstablishedCond);
receivedReply=true;
connected=false;
connectionEstablishedCond.signal();
}
/* Done with message: */
return 0;
}
MessageContinuation* SharedVisualizationClient::connectReplyCallback(unsigned int messageId,MessageContinuation* continuation)
{
/* Check if this is the start of a new message: */
if(continuation==0)
{
/* Prepare to read the connect reply message: */
continuation=protocolTypes.prepareReading(serverMessageTypes[ConnectReply],new ConnectReplyMsg);
}
/* Continue reading the connect reply message and check whether it's complete: */
if(protocolTypes.continueReading(client->getSocket(),continuation))
{
/* Extract the connect reply message: */
ConnectReplyMsg* msg=protocolTypes.getReadObject<ConnectReplyMsg>(continuation);
/* Update the variable manager with the color maps received from the server: */
for(Misc::Vector<ConnectReplyMsg::ColorMapListEntry>::iterator cmIt=msg->colorMaps.begin();cmIt!=msg->colorMaps.end();++cmIt)
{
// ...
}
/* Signal a good connection: */
{
Threads::MutexCond::Lock connectionEstablishedLock(connectionEstablishedCond);
receivedReply=true;
connected=true;
connectionEstablishedCond.signal();
}
/* Delete the connect reply message and the continuation object: */
delete msg;
delete continuation;
continuation=0;
}
return continuation;
}
MessageContinuation* SharedVisualizationClient::colorMapUpdatedNotificationCallback(unsigned int messageId,MessageContinuation* continuation)
{
/* Check if this is the start of a new message: */
if(continuation==0)
{
/* Prepare to read the color map updated notification message: */
continuation=protocolTypes.prepareReading(serverMessageTypes[ConnectReply],new ColorMapUpdatedNotificationMsg);
}
/* Continue reading the color map updated notification message and check whether it's complete: */
if(protocolTypes.continueReading(client->getSocket(),continuation))
{
/* Extract the color map updated notification message: */
ColorMapUpdatedNotificationMsg* msg=protocolTypes.getReadObject<ColorMapUpdatedNotificationMsg>(continuation);
/* Update the variable manager with the new color map: */
// ...
/* Delete the connect reply message and the continuation object: */
delete msg;
delete continuation;
continuation=0;
}
return continuation;
}
void* SharedVisualizationClient::createElementFunction(KoinoniaClient* client,KoinoniaProtocol::NamespaceID namespaceId,DataType::TypeID type,void* userData)
{
SharedVisualizationClient* thisPtr=static_cast<SharedVisualizationClient*>(userData);
/* Return a new shared element structure: */
return new SharedElement(thisPtr->elementTypeDictionary);
}
void SharedVisualizationClient::extractElementJobComplete(Threads::WorkerPool::JobFunction* job,SharedVisualizationClient::SharedElement* sharedElement)
{
/* Check whether the element hasn't been destroyed already: */
if(!sharedElement->destroyed)
{
/* Add the shared element to the secondary map: */
sharedElementsByElement.setEntry(SharedElementByElementMap::Entry(sharedElement->element,sharedElement));
/* Add the extracted element to the element list and set its visibility: */
elementList->addElement(sharedElement->algorithm,sharedElement->element,true);
elementList->setElementVisible(sharedElement->element,sharedElement->visible,true);
/* Register a parameters updated callback with the new element: */
sharedElement->element->getParametersUpdatedCallbacks().add(this,&SharedVisualizationClient::elementParametersUpdatedCallback);
}
else
{
/* Remove the shared element from the primary map and delete it: */
sharedElementsById.removeEntry(sharedElement->objectId);
delete sharedElement;
}
}
void SharedVisualizationClient::extractElementJob(int,SharedVisualizationClient::SharedElement* sharedElement)
{
/* Get an algorithm to create the new element: */
if(sharedElement->algorithmIndex<numScalarAlgorithms)
sharedElement->algorithm=module->getScalarAlgorithm(sharedElement->algorithmIndex,variableManager,Vrui::openPipe());
else
sharedElement->algorithm=module->getVectorAlgorithm(sharedElement->algorithmIndex-numScalarAlgorithms,variableManager,Vrui::openPipe());
/* Extract the element using the received extraction parameters: */
Visualization::Abstract::Parameters* parameters=sharedElement->algorithm->cloneParameters();
parameters->read(sharedElement->parameters,variableManager);
sharedElement->element=sharedElement->algorithm->createElement(parameters);
}
void SharedVisualizationClient::elementCreatedCallback(KoinoniaClient* client,KoinoniaProtocol::NamespaceID namespaceId,KoinoniaProtocol::ObjectID objectId,void* object,void* userData)
{
SharedVisualizationClient* thisPtr=static_cast<SharedVisualizationClient*>(userData);
/* Add the new shared element to the main map: */
SharedElement* sharedElement=static_cast<SharedElement*>(object);
sharedElement->objectId=objectId;
sharedElement->parametersType=thisPtr->algorithmParameterTypes[sharedElement->algorithmIndex];
thisPtr->sharedElementsById.setEntry(SharedElementByIDMap::Entry(sharedElement->objectId,sharedElement));
/* Submit a background job to extract the new element: */
Vrui::submitJob(*Threads::createFunctionCall(thisPtr,&SharedVisualizationClient::extractElementJob,sharedElement),*Threads::createFunctionCall(thisPtr,&SharedVisualizationClient::extractElementJobComplete,sharedElement));
}
void SharedVisualizationClient::elementReplacedCallback(KoinoniaClient* client,KoinoniaProtocol::NamespaceID namespaceId,KoinoniaProtocol::ObjectID objectId,KoinoniaProtocol::VersionNumber newVersion,void* object,void* userData)
{
SharedVisualizationClient* thisPtr=static_cast<SharedVisualizationClient*>(userData);
/* Access the shared element: */
SharedElement* sharedElement=thisPtr->sharedElementsById.getEntry(objectId).getDest();
/* Update the shared element's parameters: */
sharedElement->element->getParameters()->read(sharedElement->parameters,thisPtr->variableManager);
/* Check if the element has already been added to the element list: */
if(sharedElement->element!=0)
{
/* Update the element's visibility in the element list: */
thisPtr->elementList->setElementVisible(sharedElement->element,sharedElement->visible,true);
}
}
void SharedVisualizationClient::elementDestroyedCallback(KoinoniaClient* client,KoinoniaProtocol::NamespaceID namespaceId,KoinoniaProtocol::ObjectID objectId,void* object,void* userData)
{
SharedVisualizationClient* thisPtr=static_cast<SharedVisualizationClient*>(userData);
/* Access the destroyed shared element: */
SharedElement* sharedElement=thisPtr->sharedElementsById.getEntry(objectId).getDest();
/* Check if the element has already been added to the element list: */
if(sharedElement->element!=0)
{
/* Tell the element list that the shared element is being destroyed: */
thisPtr->elementList->deleteElement(sharedElement->element,true);
/* Remove the shared element from both hash tables and delete it: */
thisPtr->sharedElementsById.removeEntry(sharedElement->objectId);
thisPtr->sharedElementsByElement.removeEntry(sharedElement->element);
delete sharedElement;
}
else
{
/* Mark the element for deletion once it's done extracting: */
sharedElement->destroyed=true;
}
}
void SharedVisualizationClient::elementParametersUpdatedCallback(Visualization::Abstract::Element::ParametersUpdatedCallbackData* cbData)
{
/* Find the shared element associated with the given visualization element: */
SharedElement* sharedElement=sharedElementsByElement.getEntry(cbData->element).getDest();
/* Update the shared element: */
sharedElement->element->getParameters()->write(sharedElement->parameters);
koinonia->replaceNsObject(elementNamespaceId,sharedElement->objectId);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
SharedVisualizationClient::SharedVisualizationClient(Client* sClient,Visualization::Abstract::VariableManager* sVariableManager,Visualization::Abstract::Module* sModule,ElementList* sElementList)
:PluginClient(sClient),
variableManager(sVariableManager),module(sModule),algorithmIndices(17),elementList(sElementList),
koinonia(KoinoniaClient::requestClient(client)),
receivedReply(false),connected(false),
numScalarAlgorithms(module->getNumScalarAlgorithms()),numVectorAlgorithms(variableManager->getNumVectorVariables()>0?module->getNumVectorAlgorithms():0),
algorithmParameterTypes(new DataType::TypeID[numScalarAlgorithms+numVectorAlgorithms]),elementTypes(new DataType::TypeID[numScalarAlgorithms+numVectorAlgorithms]),
sharedElementsById(17),sharedElementsByElement(17)
{
/* Register all scalar algorithms: */
for(unsigned int i=0;i<numScalarAlgorithms;++i)
{
algorithmIndices.setEntry(AlgorithmNameMap::Entry(module->getScalarAlgorithmName(i),i));
algorithmParameterTypes[i]=module->createScalarAlgorithmParametersType(i,elementTypeDictionary);
}
/* Register all vector algorithms: */
for(unsigned int i=0;i<numVectorAlgorithms;++i)
{
algorithmIndices.setEntry(AlgorithmNameMap::Entry(module->getVectorAlgorithmName(i),numScalarAlgorithms+i));
algorithmParameterTypes[numScalarAlgorithms+i]=module->createVectorAlgorithmParametersType(i,elementTypeDictionary);
}
/* Register element types for all algorithms: */
for(unsigned int i=0;i<numScalarAlgorithms+numVectorAlgorithms;++i)
{
DataType::TypeID pointerType=elementTypeDictionary.createPointer(algorithmParameterTypes[i]);
DataType::StructureElement sharedElementElements[]=
{
{DataType::getAtomicType<Misc::UInt8>(),offsetof(SharedElement,algorithmIndex)},
{pointerType,offsetof(SharedElement,parameters)},
{DataType::getAtomicType<bool>(),offsetof(SharedElement,visible)},
};
elementTypes[i]=elementTypeDictionary.createStructure(3,sharedElementElements,sizeof(SharedElement));
}
/* Create a Koinonia namespace for extracted visualization elements: */
elementNamespaceId=koinonia->shareNamespace("SharedVisualization::Elements",protocolVersion,elementTypeDictionary,
createElementFunction,this,
elementCreatedCallback,this,
elementReplacedCallback,this,
elementDestroyedCallback,this);
}
#pragma GCC diagnostic pop
SharedVisualizationClient::~SharedVisualizationClient(void)
{
/* Destroy all shared elements: */
for(SharedElementByIDMap::Iterator seIt=sharedElementsById.begin();!seIt.isFinished();++seIt)
delete seIt->getDest();
delete[] algorithmParameterTypes;
delete[] elementTypes;
}
const char* SharedVisualizationClient::getName(void) const
{
return protocolName;
}
unsigned int SharedVisualizationClient::getVersion(void) const
{
return protocolVersion;
}
unsigned int SharedVisualizationClient::getNumClientMessages(void) const
{
return NumClientMessages;
}
unsigned int SharedVisualizationClient::getNumServerMessages(void) const
{
return NumServerMessages;
}
void SharedVisualizationClient::setMessageBases(unsigned int newClientMessageBase,unsigned int newServerMessageBase)
{
/* Call the base class method: */
PluginClient::setMessageBases(newClientMessageBase,newServerMessageBase);
/* Register message handlers: */
client->setTCPMessageHandler(serverMessageBase+ConnectReject,Client::wrapMethod<SharedVisualizationClient,&SharedVisualizationClient::connectRejectCallback>,this,0);
client->setTCPMessageHandler(serverMessageBase+ConnectReply,Client::wrapMethod<SharedVisualizationClient,&SharedVisualizationClient::connectReplyCallback>,this,getServerMsgSize(ConnectReply));
client->setTCPMessageHandler(serverMessageBase+ColorMapUpdatedNotification,Client::wrapMethod<SharedVisualizationClient,&SharedVisualizationClient::colorMapUpdatedNotificationCallback>,this,getServerMsgSize(ColorMapUpdatedNotification));
}
void SharedVisualizationClient::start(void)
{
/* Send a connect request message to the server: */
ConnectRequestMsg connectRequest;
connectRequest.numScalarVariables=VariableIndex(variableManager->getNumScalarVariables());
connectRequest.numVectorVariables=VariableIndex(variableManager->getNumVectorVariables());
sendServerMessage(ConnectRequest,&connectRequest,true);
}
bool SharedVisualizationClient::waitForConnection(void)
{
Threads::MutexCond::Lock connectionEstablishedLock(connectionEstablishedCond);
/* Wait until the connect reject or reply message is received: */
while(!receivedReply)
connectionEstablishedCond.wait(connectionEstablishedLock);
/* Return the connection status: */
return connected;
}
void SharedVisualizationClient::addElement(Visualization::Abstract::Algorithm* algorithm,Visualization::Abstract::Element* newElement)
{
/* Create a new shared element: */
SharedElement* sharedElement=new SharedElement(elementTypeDictionary);
sharedElement->algorithmIndex=algorithmIndices.getEntry(algorithm->getName()).getDest();
sharedElement->parametersType=algorithmParameterTypes[sharedElement->algorithmIndex];
sharedElement->parameters=elementTypeDictionary.createObject(sharedElement->parametersType);
newElement->getParameters()->write(sharedElement->parameters);
sharedElement->element=newElement;
/* Create a new object in the visualization element namespace: */
sharedElement->objectId=koinonia->createNsObject(elementNamespaceId,elementTypes[sharedElement->algorithmIndex],sharedElement);
/* Add the new shared element to both maps: */
sharedElementsById.setEntry(SharedElementByIDMap::Entry(sharedElement->objectId,sharedElement));
sharedElementsByElement.setEntry(SharedElementByElementMap::Entry(sharedElement->element,sharedElement));
/* Register a parameters updated callback with the new element: */
sharedElement->element->getParametersUpdatedCallbacks().add(this,&SharedVisualizationClient::elementParametersUpdatedCallback);
}
void SharedVisualizationClient::setElementVisible(Visualization::Abstract::Element* element,bool newVisible)
{
/* Find the shared element associated with the given visualization element: */
SharedElement* sharedElement=sharedElementsByElement.getEntry(element).getDest();
/* Update the shared element: */
if(sharedElement->visible!=newVisible)
{
sharedElement->visible=newVisible;
koinonia->replaceNsObject(elementNamespaceId,sharedElement->objectId);
}
}
void SharedVisualizationClient::deleteElement(Visualization::Abstract::Element* element)
{
/* Find the shared element associated with the given visualization element: */
SharedElement* sharedElement=sharedElementsByElement.getEntry(element).getDest();
/* Delete the shared element in the visualization element namespace: */
koinonia->destroyNsObject(elementNamespaceId,sharedElement->objectId);
/* Remove the shared element from both hash tables and delete it: */
sharedElementsById.removeEntry(sharedElement->objectId);
sharedElementsByElement.removeEntry(sharedElement->element);
delete sharedElement;
}
}
}