File: vtkSMCollaborationManager.cxx

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (435 lines) | stat: -rw-r--r-- 13,776 bytes parent folder | download | duplicates (2)
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
/*=========================================================================

  Program:   ParaView
  Module:    vtkSMCollaborationManager.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkSMCollaborationManager.h"

#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkPVServerInformation.h"
#include "vtkSMMessage.h"
#include "vtkSMSession.h"
#include "vtkSMSessionClient.h"
#include "vtkReservedRemoteObjectIds.h"
#include "vtkPVMultiClientsInformation.h"

#include "vtkSMProxyLocator.h"
#include "vtkSMProxy.h"

#include <map>
#include <vector>
#include <string>

//****************************************************************************
//                              Internal class
//****************************************************************************
class vtkSMCollaborationManager::vtkInternal
{
public:
  vtkInternal(vtkSMCollaborationManager* m)
    {
    this->Manager = m;
    this->ObserverTag = 0;
    this->Me = 0;
    this->UserToFollow = 0;
    this->Clear();
    }

  ~vtkInternal()
    {
    this->Clear();
    if(this->Manager && this->Manager->GetSession() && this->ObserverTag != 0)
      {
      this->Manager->GetSession()->RemoveObserver(this->ObserverTag);
      this->ObserverTag = 0;
      }
    }

  void Init()
    {
    // Check our current user Id and store it
    this->Me = this->Manager->GetSession()->GetServerInformation()->GetClientId();
    this->ObserverTag =
      this->Manager->GetSession()->AddObserver(
        vtkPVSessionBase::ProcessingRemoteEnd,
        this,
        &vtkSMCollaborationManager::vtkInternal::StopProcessingRemoteNotificationCallback );
    }

  const char* GetUserName(int userId)
    {
    return this->UserNames[userId].c_str();
    }

  bool UpdateMaster(int newMaster)
    {
    if(this->Master != newMaster)
      {
      this->Master = newMaster;

      // If no user to follow yet, then we follow master...
      this->UpdateState( (this->UserToFollow == 0) ?
                           newMaster : this->UserToFollow);
      this->Manager->InvokeEvent(
          (unsigned long)vtkSMCollaborationManager::UpdateMasterUser,
          (void*) &newMaster);
      return true;
      }
    return false;
    }

  bool UpdateUserName(int userId, const char* userName)
    {
    if(userName)
      {
      if(this->UserNames[userId] != userName)
        {
        this->UserNames[userId] = userName;
        this->UpdateState(this->UserToFollow);
        this->Manager->InvokeEvent(
            (unsigned long)vtkSMCollaborationManager::UpdateUserName,
            (void*) &userId);
        return true;
        }
      }
    else
      {
      this->UserNames.erase(userId);
      this->UpdateState(this->UserToFollow);
      }
    return false;
    }

  void Clear()
    {
    this->UserNames.clear();
    this->Users.clear();
    this->Master = 0;
    this->State.Clear();
    this->PendingCameraUpdate.Clear();
    this->LocalCameraStateCache.clear();
    }

  void UpdateState(int followCamUserId)
    {
    this->UserToFollow = followCamUserId;
    this->State.ClearExtension(ClientsInformation::user);
    size_t size = this->Users.size();
    for(size_t i=0; i < size; ++i)
      {
      ClientsInformation_ClientInfo* user =
          this->State.AddExtension(ClientsInformation::user);
      user->set_user(this->Users[i]);
      user->set_name(this->GetUserName(this->Users[i]));
      if(this->Users[i] == this->Master)
        {
        user->set_is_master(true);
        }
      if(this->Users[i] == followCamUserId)
        {
        user->set_follow_cam(true);
        }
      }
    }

  bool LoadState(const vtkSMMessage* msg)
    {
    int size = msg->ExtensionSize(ClientsInformation::user);
    bool foundChanges = (size != static_cast<int>(this->Users.size()));
    // Update User list first
    this->Users.clear();
    for(int i=0; i < size; ++i)
      {
      const ClientsInformation_ClientInfo* user =
          &msg->GetExtension(ClientsInformation::user, i);
      int id = user->user();
      this->Users.push_back(id);
      }

    // Update user name/master info
    int newFollow = 0;
    for(int i=0; i < size; ++i)
      {
      const ClientsInformation_ClientInfo* user =
          &msg->GetExtension(ClientsInformation::user, i);
      int id = user->user();
      foundChanges = this->UpdateUserName(id, user->name().c_str()) || foundChanges;
      if(user->is_master())
        {
        foundChanges = this->UpdateMaster(id) || foundChanges;
        }

      if(user->follow_cam())
        {
        // Invoke event...
        newFollow = id;
        this->Manager->InvokeEvent(
            (unsigned long)vtkSMCollaborationManager::FollowUserCamera,
            (void*) &id);
        }
      }
    if(newFollow)
      {
      this->UserToFollow = newFollow;
      }

    return foundChanges || newFollow;
    }

  // Return the camera update message user origin otherwise
  // if not a camera update message we return -1;
  int StoreCameraByUser(const vtkSMMessage* msg)
  {
  if(msg->HasExtension(DefinitionHeader::client_class) &&
     msg->GetExtension(DefinitionHeader::client_class) == "vtkSMCameraProxy")
    {
    int currentUserId = static_cast<int>(msg->client_id());
    this->LocalCameraStateCache[currentUserId].CopyFrom(*msg);
    return currentUserId;
    }
  return -1;
  }

  void UpdateCamera(const vtkSMMessage* msg)
    {
    vtkTypeUInt32 cameraId = msg->global_id();
    vtkSMProxyLocator* locator = this->Manager->GetSession()->GetProxyLocator();
    vtkSMProxy* proxy = locator->LocateProxy(cameraId);

    // As camera do not synch its properties while IsProcessingRemoteNotification
    // there is no point of updating it when we are in that case.
    // So we just push back that request to later...
    if(proxy && !proxy->GetSession()->IsProcessingRemoteNotification())
      {
      // Update Proxy
      proxy->EnableLocalPushOnly();
      proxy->LoadState(msg, locator);
      proxy->UpdateVTKObjects();
      proxy->DisableLocalPushOnly();

      // Fire event so the Qt layer could trigger a render
      this->Manager->InvokeEvent(vtkSMCollaborationManager::CameraChanged);
      }
    else if(proxy->GetSession()->IsProcessingRemoteNotification())
      {
      this->PendingCameraUpdate.CopyFrom(*msg);
      }
    }

  void StopProcessingRemoteNotificationCallback(vtkObject*, unsigned long, void*)
    {
    if(this->PendingCameraUpdate.has_global_id())
      {
      this->UpdateCamera(&this->PendingCameraUpdate);
      this->PendingCameraUpdate.Clear();
      }
    }

  vtkWeakPointer<vtkSMCollaborationManager> Manager;
  std::map<int, std::string>          UserNames;
  std::vector<int>                       Users;
  int                                       Me;
  int                                       UserToFollow;
  int                                       Master;
  vtkSMMessage                              State;
  vtkSMMessage                              PendingCameraUpdate;
  std::map<int, vtkSMMessage>            LocalCameraStateCache;
  unsigned long                             ObserverTag;
};
//****************************************************************************
vtkStandardNewMacro(vtkSMCollaborationManager);
//----------------------------------------------------------------------------
vtkTypeUInt32 vtkSMCollaborationManager::GetReservedGlobalID()
{
  return vtkReservedRemoteObjectIds::RESERVED_COLLABORATION_COMMUNICATOR_ID;
}
//----------------------------------------------------------------------------
vtkSMCollaborationManager::vtkSMCollaborationManager()
{
  this->SetLocation(vtkPVSession::DATA_SERVER_ROOT);
  this->Internal = new vtkInternal(this);
  this->SetGlobalID(vtkSMCollaborationManager::GetReservedGlobalID());
}

//----------------------------------------------------------------------------
vtkSMCollaborationManager::~vtkSMCollaborationManager()
{
  delete this->Internal;
  this->Internal = NULL;
}

//----------------------------------------------------------------------------
void vtkSMCollaborationManager::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}
//---------------------------------------------------------------------------
void vtkSMCollaborationManager::LoadState( const vtkSMMessage* msg,
                                           vtkSMProxyLocator* vtkNotUsed(locator))
{
  // Check if it's a local state or if it's a state that use the
  // CollaborationManager as communication channel accros clients
  if(msg->ExtensionSize(ClientsInformation::user) > 0)
    {
    // For me
    if(this->Internal->LoadState(msg))
      {
      this->InvokeEvent(UpdateUserList);
      }
    }
  else
    {
    // Handle camera synchro
    if(this->Internal->UserToFollow == this->Internal->StoreCameraByUser(msg) && this->Internal->UserToFollow != -1)
      {
      this->Internal->UpdateCamera(msg);
      }

    // For Observers
    vtkSMMessage* msgCopy = new vtkSMMessage();
    msgCopy->CopyFrom(*msg);
    this->InvokeEvent(CollaborationNotification, msgCopy);
    }
}

//----------------------------------------------------------------------------
vtkTypeUInt32 vtkSMCollaborationManager::GetGlobalID()
{
  if(!this->HasGlobalID())
    {
    this->SetGlobalID(vtkSMCollaborationManager::GetReservedGlobalID());
    }
  return this->Superclass::GetGlobalID();
}

//----------------------------------------------------------------------------
void vtkSMCollaborationManager::SendToOtherClients(vtkSMMessage* msg)
{
  this->PushState(msg);
}
//----------------------------------------------------------------------------
void vtkSMCollaborationManager::PromoteToMaster(int clientId)
{
  this->Internal->UpdateMaster(clientId);
  this->UpdateUserInformations();
}
//----------------------------------------------------------------------------
int vtkSMCollaborationManager::GetFollowedUser()
{
  return this->Internal->UserToFollow;
}
//----------------------------------------------------------------------------
void vtkSMCollaborationManager::FollowUser(int clientId)
{
  if( this->Internal->UserToFollow == clientId)
    {
    return;
    }

  if(this->IsMaster())
    {
    this->Internal->UpdateState(clientId);
    this->UpdateUserInformations();
    }
  else // Follow someone else on my own
    {
     this->Internal->UserToFollow =  clientId;
    }

  // Update the camera
  if( clientId != -1 && this->Internal->LocalCameraStateCache.find(clientId) !=
      this->Internal->LocalCameraStateCache.end() )
    {
    this->Internal->UpdateCamera(&this->Internal->LocalCameraStateCache[clientId]);
    }
}

//----------------------------------------------------------------------------
bool vtkSMCollaborationManager::IsMaster()
{
  return (this->Internal->Me == this->Internal->Master);
}

//----------------------------------------------------------------------------
int vtkSMCollaborationManager::GetMasterId()
{
  return this->Internal->Master;
}
//----------------------------------------------------------------------------
int vtkSMCollaborationManager::GetUserId()
{
  return this->Internal->Me;
}
//----------------------------------------------------------------------------
int vtkSMCollaborationManager::GetUserId(int index)
{
  return this->Internal->Users[index];
}
//----------------------------------------------------------------------------
const char* vtkSMCollaborationManager::GetUserLabel(int userID)
{
  return this->Internal->GetUserName(userID);
}
//----------------------------------------------------------------------------
void vtkSMCollaborationManager::SetUserLabel(const char* userName)
{
  this->SetUserLabel(this->Internal->Me, userName);
}

//----------------------------------------------------------------------------
void vtkSMCollaborationManager::SetUserLabel(int userId, const char* userName)
{
  if(this->Internal->UpdateUserName(userId, userName))
    {
    this->UpdateUserInformations();
    }
}

//----------------------------------------------------------------------------
int vtkSMCollaborationManager::GetNumberOfConnectedClients()
{
  return static_cast<int>(this->Internal->Users.size());
}

//----------------------------------------------------------------------------
const vtkSMMessage* vtkSMCollaborationManager::GetFullState()
{
  this->Internal->State.set_location(vtkPVSession::DATA_SERVER_ROOT);
  this->Internal->State.set_global_id(vtkSMCollaborationManager::GetReservedGlobalID());
  this->Internal->State.SetExtension(DefinitionHeader::client_class, "vtkSMCollaborationManager");
  this->Internal->State.SetExtension(DefinitionHeader::server_class, "vtkSICollaborationManager");

 return &this->Internal->State;
}
//----------------------------------------------------------------------------
void vtkSMCollaborationManager::UpdateUserInformations()
{
  // Make sure to add declaration to state message
  this->GetFullState();
  this->PushState(&this->Internal->State);

  // If we are the only client fetch the data of ourself
  if(this->GetNumberOfConnectedClients() == 0)
    {
    vtkSMMessage msg;
    msg.CopyFrom(*this->GetFullState());
    this->PullState(&msg);
    this->LoadState(&msg, NULL);
    }
}
//----------------------------------------------------------------------------
void vtkSMCollaborationManager::SetSession(vtkSMSession* session)
{
  this->Superclass::SetSession(session);
  this->Internal->Init();
}