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
|
/*=========================================================================
Program: ParaView
Module: $RCSfile$
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkSMSession.h"
#include "vtkCommand.h"
#include "vtkDebugLeaks.h"
#include "vtkObjectFactory.h"
#include "vtkPVCatalystSessionCore.h"
#include "vtkPVServerInformation.h"
#include "vtkPVSessionCore.h"
#include "vtkProcessModule.h"
#include "vtkProcessModuleAutoMPI.h"
#include "vtkReservedRemoteObjectIds.h"
#include "vtkSMDeserializerProtobuf.h"
#include "vtkSMMessage.h"
#include "vtkSMPluginManager.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMProxy.h"
#include "vtkSMProxyLocator.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSessionClient.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMStateLocator.h"
#include "vtkSMUndoStackBuilder.h"
#include "vtkWeakPointer.h"
#include <sstream>
#include <vtkNew.h>
#include <assert.h>
//----------------------------------------------------------------------------
// STATICS
vtkSmartPointer<vtkProcessModuleAutoMPI> vtkSMSession::AutoMPI =
vtkSmartPointer<vtkProcessModuleAutoMPI>::New();
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkSMSession);
//----------------------------------------------------------------------------
vtkSMSession* vtkSMSession::New(vtkPVSessionBase *otherSession)
{
return vtkSMSession::New(otherSession->GetSessionCore());
}
//----------------------------------------------------------------------------
vtkSMSession* vtkSMSession::New(vtkPVSessionCore *otherSessionCore)
{
vtkSMSession* session = new vtkSMSession(true, otherSessionCore);
#ifdef VTK_DEBUG_LEAKS
vtkDebugLeaks::ConstructClass("vtkSMSession");
#endif
return session;
}
//----------------------------------------------------------------------------
vtkSMSession::vtkSMSession(bool initialize_during_constructor/*=true*/,
vtkPVSessionCore* preExistingSessionCore/*=NULL*/)
: vtkPVSessionBase(preExistingSessionCore ? preExistingSessionCore : vtkPVSessionCore::New())
{
if(!preExistingSessionCore)
{
this->SessionCore->UnRegister(NULL);
}
this->SessionProxyManager = NULL;
this->StateLocator = vtkSMStateLocator::New();
this->IsAutoMPI = false;
// Create and setup deserializer for the local ProxyLocator
vtkNew<vtkSMDeserializerProtobuf> deserializer;
deserializer->SetStateLocator(this->StateLocator);
// Create and setup proxy locator
this->ProxyLocator = vtkSMProxyLocator::New();
this->ProxyLocator->SetDeserializer(deserializer.GetPointer());
this->ProxyLocator->UseSessionToLocateProxy(true);
// don't set the session on ProxyLocator right now since the
// SessionProxyManager is not setup until Initialize().
// we will initialize it in this->Initialize().
if (initialize_during_constructor)
{
this->Initialize();
}
}
//----------------------------------------------------------------------------
vtkSMSession::~vtkSMSession()
{
if (vtkSMProxyManager::IsInitialized())
{
vtkSMProxyManager::GetProxyManager()->GetPluginManager()->UnRegisterSession(this);
}
this->StateLocator->Delete();
this->ProxyLocator->Delete();
if (this->SessionProxyManager)
{
this->SessionProxyManager->Delete();
this->SessionProxyManager = NULL;
}
}
//----------------------------------------------------------------------------
vtkSMSession::ServerFlags vtkSMSession::GetProcessRoles()
{
if (vtkProcessModule::GetProcessModule() &&
vtkProcessModule::GetProcessModule()->GetPartitionId() > 0 &&
!vtkProcessModule::GetProcessModule()->GetSymmetricMPIMode())
{
return vtkPVSession::SERVERS;
}
return vtkPVSession::CLIENT_AND_SERVERS;
}
//----------------------------------------------------------------------------
void vtkSMSession::PushState(vtkSMMessage* msg)
{
// Manage Undo/Redo if possible
this->UpdateStateHistory(msg);
this->Superclass::PushState(msg);
}
//----------------------------------------------------------------------------
void vtkSMSession::UpdateStateHistory(vtkSMMessage* msg)
{
// check is global-undo-stack builder is set.
vtkSMUndoStackBuilder* usb =
vtkSMProxyManager::GetProxyManager()->GetUndoStackBuilder();
if (usb == NULL ||
(this->GetProcessRoles() & vtkPVSession::CLIENT) == 0)
{
return;
}
vtkTypeUInt32 globalId = msg->global_id();
vtkSMRemoteObject *remoteObj =
vtkSMRemoteObject::SafeDownCast(this->GetRemoteObject(globalId));
if(remoteObj && !remoteObj->IsPrototype() && remoteObj->GetFullState())
{
vtkSMMessage newState;
newState.CopyFrom(*remoteObj->GetFullState());
// Need to provide id/location as the full state may not have them yet
newState.set_global_id(globalId);
newState.set_location(msg->location());
// Store state in cache
vtkSMMessage oldState;
bool createAction = !this->StateLocator->FindState( globalId, &oldState,
/* We want only a local lookup => false */ false );
// This is a filtering Hack, I don't like it. :-(
if (newState.GetExtension(ProxyState::xml_name) != "Camera")
{
this->StateLocator->RegisterState(&newState);
}
// Propagate to undo stack builder if possible
if (createAction)
{
usb->OnCreateObject(this, &newState);
}
else if (oldState.SerializeAsString() != newState.SerializeAsString())
{
// Update
usb->OnStateChange( this, globalId, &oldState, &newState);
}
}
}
//----------------------------------------------------------------------------
void vtkSMSession::Initialize()
{
assert(this->SessionProxyManager == NULL);
// Remember, although vtkSMSession is always only created on the client side,
// in batch mode, vtkSMSession is created on all nodes.
// All these initializations need to be done on all nodes in symmetric-batch
// mode. In non-symmetric-batch mode. Which means we are a CLIENT,
// so if we are not then we stop the initialisation here !
if( !(this->GetProcessRoles() & vtkPVSession::CLIENT) )
{
return;
}
// Initialize the proxy manager.
// this updates proxy definitions if we are connected to a remote server.
this->SessionProxyManager = vtkSMSessionProxyManager::New(this);
this->ProxyLocator->SetSession(this);
// Initialize the plugin manager.
vtkSMProxyManager::GetProxyManager()->GetPluginManager()->RegisterSession(this);
// Setup default mapper parameters.
const char *group = "misc";
const char *name = "GlobalMapperProperties";
// First try and get the prototype, if its NULL then the proxy is not
// available.
vtkSMProxy* prototype =
this->SessionProxyManager->GetPrototypeProxy(group, name);
if (!prototype)
return;
vtkSMProxy* globalMapperProperties =
this->SessionProxyManager->NewProxy(group, name);
if (globalMapperProperties)
{
vtkSMPropertyHelper(globalMapperProperties, "Mode").Set("ShiftZBuffer");
vtkSMPropertyHelper(globalMapperProperties, "ZShift").Set(2.0e-3);
globalMapperProperties->UpdateVTKObjects();
globalMapperProperties->Delete();
}
}
//----------------------------------------------------------------------------
int vtkSMSession::GetNumberOfProcesses(vtkTypeUInt32 servers)
{
(void)servers;
return vtkProcessModule::GetProcessModule()->GetNumberOfLocalPartitions();
}
//----------------------------------------------------------------------------
bool vtkSMSession::IsMPIInitialized(vtkTypeUInt32)
{
return vtkProcessModule::GetProcessModule()->IsMPIInitialized();
}
//----------------------------------------------------------------------------
void vtkSMSession::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkSMSession::Disconnect(vtkSMSession* session)
{
if (!session)
{
return;
}
// Ensure that the session no longer sends state updates to the server-side.
// This is critical to ensure we don't mess up the collaboration state.
session->PreDisconnection();
// Unregister all proxies.
session->GetSessionProxyManager()->UnRegisterProxies();
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
pm->UnRegisterSession(session);
// Although I'd like to have this check, when Disconnect() is called from
// Python, we have 1 reference that gets cleared with the Python call returns.
//if (session != NULL)
// {
// vtkGenericWarningMacro(
// "vtkSMSession wasn't destroyed after UnRegisterSession. "
// "This indicates a development problem. Please notify the "
// "developers.");
// }
}
//----------------------------------------------------------------------------
void vtkSMSession::Disconnect(vtkIdType sid)
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
vtkWeakPointer<vtkSMSession> session = vtkSMSession::SafeDownCast(pm->GetSession(sid));
if (!session)
{
vtkGenericWarningMacro(
"Failed to locate session " << sid << ". Cannot disconnect.");
return;
}
vtkSMSession::Disconnect(session);
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ConnectToSelf()
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
vtkIdType sid = 0;
if (vtkSMSession::AutoMPI->IsPossible())
{
int port = vtkSMSession::AutoMPI->ConnectToRemoteBuiltInSelf();
if (port > 0)
{
sid = vtkSMSession::ConnectToRemoteInternal("localhost", port, true);
if (sid > 0)
{
return sid;
}
}
vtkGenericWarningMacro(
"Failed to automatically launch 'pvserver' for multi-core support. "
"Defaulting to local session.");
}
vtkSMSession* session = vtkSMSession::New();
sid = pm->RegisterSession(session);
session->Delete();
return sid;
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ConnectToCatalyst()
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
vtkIdType sid = 0;
vtkNew<vtkPVCatalystSessionCore> sessionCore;
vtkSMSession* session = vtkSMSession::New(sessionCore.GetPointer());
sid = pm->RegisterSession(session);
session->Delete();
return sid;
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ConnectToRemote(const char* hostname, int port)
{
return vtkSMSession::ConnectToRemoteInternal(hostname, port, false);
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ConnectToRemoteInternal(
const char* hostname, int port, bool is_auto_mpi)
{
std::ostringstream sname;
sname << "cs://" << hostname << ":" << port;
vtkSMSessionClient* session = vtkSMSessionClient::New();
session->IsAutoMPI = is_auto_mpi;
vtkIdType sid = 0;
if (session->Connect(sname.str().c_str()))
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
sid = pm->RegisterSession(session);
}
session->Delete();
return sid;
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ConnectToRemote(const char* dshost, int dsport,
const char* rshost, int rsport)
{
std::ostringstream sname;
sname << "cdsrs://" << dshost << ":" << dsport << "/"
<< rshost << ":" << rsport;
vtkSMSessionClient* session = vtkSMSessionClient::New();
vtkIdType sid = 0;
if (session->Connect(sname.str().c_str()))
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
sid = pm->RegisterSession(session);
}
session->Delete();
return sid;
}
//----------------------------------------------------------------------------
namespace
{
class vtkTemp
{
public:
bool (*Callback) ();
vtkSMSessionClient* Session;
vtkTemp()
{
this->Callback = NULL;
this->Session = NULL;
}
void OnEvent()
{
if (this->Callback != NULL)
{
bool continue_waiting = (*this->Callback)();
if (!continue_waiting && this->Session )
{
this->Session->SetAbortConnect(true);
}
}
}
};
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ReverseConnectToRemote(int port, bool (*callback)())
{
return vtkSMSession::ReverseConnectToRemote(port, -1, callback);
}
//----------------------------------------------------------------------------
vtkIdType vtkSMSession::ReverseConnectToRemote(
int dsport, int rsport, bool (*callback)())
{
vtkTemp temp;
temp.Callback = callback;
std::ostringstream sname;
if (rsport <= -1)
{
sname << "csrc://localhost:" << dsport;
}
else
{
sname << "cdsrsrc://localhost:" << dsport << "/localhost:"<< rsport;
}
vtkSMSessionClient* session = vtkSMSessionClient::New();
temp.Session = session;
unsigned long id = session->AddObserver(vtkCommand::ProgressEvent,
&temp, &vtkTemp::OnEvent);
vtkIdType sid = 0;
if (session->Connect(sname.str().c_str()))
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
sid = pm->RegisterSession(session);
}
session->RemoveObserver(id);
session->Delete();
return sid;
}
//----------------------------------------------------------------------------
unsigned int vtkSMSession::GetRenderClientMode()
{
if (this->GetIsAutoMPI())
{
return vtkSMSession::RENDERING_SPLIT;
}
if (this->GetController(vtkPVSession::DATA_SERVER_ROOT) !=
this->GetController(vtkPVSession::RENDER_SERVER_ROOT))
{
// when the two controller are different, we have a separate render-server
// and data-server session.
return vtkSMSession::RENDERING_SPLIT;
}
vtkPVServerInformation* server_info = this->GetServerInformation();
if (server_info && server_info->GetNumberOfMachines() > 0)
{
return vtkSMSession::RENDERING_SPLIT;
}
return vtkSMSession::RENDERING_UNIFIED;
}
//----------------------------------------------------------------------------
void vtkSMSession::ProcessNotification(const vtkSMMessage* message)
{
vtkTypeUInt32 id = message->global_id();
// Find the object for whom this message is meant.
vtkSMRemoteObject* remoteObj =
vtkSMRemoteObject::SafeDownCast(this->GetRemoteObject(id));
//cout << "########## Server notification ##########" << endl;
//cout << id << " = " << remoteObj << "(" << (remoteObj?
// remoteObj->GetClassName() : "null") << ")" << endl;
//state.PrintDebugString();
//cout << "###################################################" << endl;
// ProcessingRemoteNotification = true prevent
// "ignore_synchronization" properties to be loaded...
// Therefore camera properties won't be shared
// (I don't understand this comment, but copying it from the original code).
if(remoteObj)
{
bool previousValue = this->StartProcessingRemoteNotification();
remoteObj->EnableLocalPushOnly();
remoteObj->LoadState(message, this->GetProxyLocator());
vtkSMProxy* proxy = vtkSMProxy::SafeDownCast(remoteObj);
if (proxy)
{
proxy->UpdateVTKObjects();
}
remoteObj->DisableLocalPushOnly();
this->StopProcessingRemoteNotification(previousValue);
}
}
|