-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkZeqManager.cxx
425 lines (374 loc) · 14.8 KB
/
vtkZeqManager.cxx
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
/*=========================================================================
Project : pv-zeq
Module : vtkZeqManager.h
Authors:
John Biddiscombe
=========================================================================*/
#include "vtkZeqManager.h"
//
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
//
// For PARAVIEW_USE_MPI
#include "vtkPVConfig.h"
#ifdef PARAVIEW_USE_MPI
#include "vtkMPI.h"
#include "vtkMPIController.h"
#include "vtkMPICommunicator.h"
#endif
// Otherwise
#include "vtkMultiProcessController.h"
//
#include "vtkProcessModule.h"
#include "vtkPVOptions.h"
#include "vtkPVServerOptions.h"
#include "vtkClientSocket.h"
#include "vtkMultiThreader.h"
#include "vtkMutexLock.h"
#include "vtkConditionVariable.h"
//
#include <servus/uri.h>
#include <zeq/vocabulary.h>
#include <monsteer/streaming/vocabulary.h>
#include <boost/bind.hpp>
#include <random>
//----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkZeqManager, Controller, vtkMultiProcessController);
//----------------------------------------------------------------------------
VTK_EXPORT VTK_THREAD_RETURN_TYPE vtkZeqManagerNotificationThread(void *arg)
{
vtkZeqManager *zeqManager = static_cast<vtkZeqManager*>(
static_cast<vtkMultiThreader::ThreadInfo*>(arg)->UserData);
zeqManager->NotificationThread();
return(VTK_THREAD_RETURN_VALUE);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
struct vtkZeqManager::vtkZeqManagerInternals
{
vtkZeqManagerInternals() {
this->NotificationThread = vtkSmartPointer<vtkMultiThreader>::New();
// Updated event
this->IsUpdated = false;
this->UpdatedCond = vtkSmartPointer<vtkConditionVariable>::New();
this->UpdatedMutex = vtkSmartPointer<vtkMutexLock>::New();
// NotifThreadCreated event
this->IsNotifThreadCreated = false;
this->NotifThreadCreatedCond = vtkSmartPointer<vtkConditionVariable>::New();
this->NotifThreadCreatedMutex = vtkSmartPointer<vtkMutexLock>::New();
}
~vtkZeqManagerInternals() {
// if (this->NotificationThread->IsThreadActive(this->NotificationThreadID)) {
// this->NotificationThread->TerminateThread(this->NotificationThreadID);
// }
}
void SignalNotifThreadCreated() {
this->NotifThreadCreatedMutex->Lock();
this->IsNotifThreadCreated = true;
this->NotifThreadCreatedCond->Signal();
this->NotifThreadCreatedMutex->Unlock();
}
void WaitForNotifThreadCreated() {
this->NotifThreadCreatedMutex->Lock();
while (!this->IsNotifThreadCreated) {
this->NotifThreadCreatedCond->Wait(this->NotifThreadCreatedMutex);
}
this->NotifThreadCreatedMutex->Unlock();
}
vtkSmartPointer<vtkClientSocket> NotificationSocket;
vtkSmartPointer<vtkMultiThreader> NotificationThread;
int NotificationThreadID;
// Updated event
bool IsUpdated;
vtkSmartPointer<vtkMutexLock> UpdatedMutex;
vtkSmartPointer<vtkConditionVariable> UpdatedCond;
// NotifThreadCreated event
bool IsNotifThreadCreated;
vtkSmartPointer<vtkMutexLock> NotifThreadCreatedMutex;
vtkSmartPointer<vtkConditionVariable> NotifThreadCreatedCond;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
extern vtkObject* vtkInstantiatorvtkZeqManagerNew();
vtkZeqManager *vtkZeqManager::ZeqManagerSingleton = NULL;
//----------------------------------------------------------------------------
#undef ErrorMacro
#define ErrorMacro(x) \
{ \
if (vtkObject::GetGlobalWarningDisplay()) \
{ \
vtkOStreamWrapper::EndlType endl; \
vtkOStreamWrapper::UseEndl(endl); \
vtkOStrStreamWrapper vtkmsg; \
vtkmsg << "ERROR: In " __FILE__ ", line " << __LINE__ \
<< "\n" x << "\n\n"; \
vtkOutputWindowDisplayErrorText(vtkmsg.str()); \
vtkmsg.rdbuf()->freeze(0); vtkObject::BreakOnError(); \
} \
}
//----------------------------------------------------------------------------
vtkZeqManager *vtkZeqManager::New()
{
if (vtkZeqManager::ZeqManagerSingleton) {
// increase the reference count
vtkZeqManager::ZeqManagerSingleton->Register(NULL);
return vtkZeqManager::ZeqManagerSingleton;
}
//
vtkZeqManager *temp = new vtkZeqManager();
if (temp!=vtkZeqManager::ZeqManagerSingleton) {
ErrorMacro(<<"A serious (probably thread related error) has occured in vtkZeqManager singleton creation")
}
return vtkZeqManager::ZeqManagerSingleton;
}
//----------------------------------------------------------------------------
vtkObject *vtkInstantiatorvtkZeqManagerNew()
{
return vtkZeqManager::New();
}
//----------------------------------------------------------------------------
// Singleton creation, not really thread-safe, but unlikely to ever be tested
vtkZeqManager::vtkZeqManager() : _servicename("_hbp._tcp"), _service(_servicename)
{
this->UpdatePiece = 0;
this->UpdateNumPieces = 0;
this->HostsDescription = NULL;
this->abort_poll = 0;
this->thread_done = 1;
this->_subscriber_hbp = NULL;
this->_subscriber_monsteer = NULL;
this->ClientSideMode = 0;
//
#ifdef VTK_USE_MPI
this->Controller = NULL;
this->SetController(vtkMultiProcessController::GetGlobalController());
this->UpdatePiece = this->Controller->GetLocalProcessId();
this->UpdateNumPieces = this->Controller->GetNumberOfProcesses();
#endif
if (vtkZeqManager::ZeqManagerSingleton==NULL) {
vtkZeqManager::ZeqManagerSingleton = this;
}
this->ZeqManagerInternals = new vtkZeqManagerInternals();
}
//----------------------------------------------------------------------------
vtkZeqManager::~vtkZeqManager()
{
this->abort_poll = 1;
while (!this->thread_done) {
sleep(1);
}
//
if (_subscriber_hbp) delete _subscriber_hbp;
if (_subscriber_monsteer) delete _subscriber_monsteer;
//
if (this->ZeqManagerInternals) delete this->ZeqManagerInternals;
this->ZeqManagerInternals = NULL;
//
this->SetHostsDescription(NULL);
//
vtkZeqManager::ZeqManagerSingleton=NULL;
}
//---------------------------------------------------------------------------
void vtkZeqManager::Discover()
{
_hosts = _service.discover( servus::Servus::IF_ALL, 50 );
if( _hosts.empty() ) {
vtkErrorMacro("No hosts found");
}
for (auto &name : _hosts) {
std::cout << name.c_str() << std::endl;
}
}
//---------------------------------------------------------------------------
void vtkZeqManager::onHBPCamera( const zeq::Event& event )
{
// forward the event directly to the client
event_data data = {event.getType(), event.getSize() };
this->ZeqManagerInternals->NotificationSocket->Send(&data, sizeof(event_data));
this->ZeqManagerInternals->NotificationSocket->Send(event.getData(), event.getSize());
}
//---------------------------------------------------------------------------
void vtkZeqManager::onSelectedIds( const zeq::Event& event )
{
// forward the event directly to the client
std::cout << "Forwarding selection " << event.getSize() << "\n";
event_data data = {event.getType(), event.getSize() };
this->ZeqManagerInternals->NotificationSocket->Send(&data, sizeof(event_data));
this->ZeqManagerInternals->NotificationSocket->Send(event.getData(), event.getSize());
}
//---------------------------------------------------------------------------
void vtkZeqManager::onSpike( const zeq::Event& event )
{
// forward the event directly to the client
std::cout << "Forwarding spikes " << event.getSize() << "\n";
event_data data = {event.getType(), event.getSize() };
this->ZeqManagerInternals->NotificationSocket->Send(&data, sizeof(event_data));
this->ZeqManagerInternals->NotificationSocket->Send(event.getData(), event.getSize());
}
//---------------------------------------------------------------------------
void vtkZeqManager::Start()
{
if (this->UpdatePiece == 0) {
this->Create();
this->Discover();
}
}
//----------------------------------------------------------------------------
void* vtkZeqManager::NotificationThread()
{
event_data data = {zeq::make_uint128("zeq::hbp::NewConnection"), 0 };
this->ZeqManagerInternals->SignalNotifThreadCreated();
//
if (!this->ClientSideMode) {
this->ZeqManagerInternals->NotificationSocket->Send(&data, sizeof(event_data));
}
//
int FAIL_FAIL = 0;
while (!this->abort_poll && this->WaitForUnlock(NULL) != FAIL_FAIL) {
// poll for 100ms, if nothing try again.
// we do it like this so that we can exit more cleanly than setting timeout to zero
// and getting a segfault when we destruct
int event = _subscriber_hbp->receive(10);
if (event) {
this->WaitForUpdated();
}
event = _subscriber_monsteer->receive(10);
if (event) {
this->WaitForUpdated();
}
}
this->thread_done = 1;
return((void *)this);
}
//----------------------------------------------------------------------------
void vtkZeqManager::SignalUpdated()
{
this->ZeqManagerInternals->UpdatedMutex->Lock();
this->ZeqManagerInternals->IsUpdated = true;
this->ZeqManagerInternals->UpdatedCond->Signal();
vtkDebugMacro("Sent updated condition signal");
this->ZeqManagerInternals->UpdatedMutex->Unlock();
}
//----------------------------------------------------------------------------
void vtkZeqManager::WaitForUpdated()
{
this->ZeqManagerInternals->UpdatedMutex->Lock();
if (!this->ZeqManagerInternals->IsUpdated) {
vtkDebugMacro("Thread going into wait for pipeline updated...");
this->ZeqManagerInternals->UpdatedCond->Wait(this->ZeqManagerInternals->UpdatedMutex);
vtkDebugMacro("Thread received updated signal");
}
if (this->ZeqManagerInternals->IsUpdated) {
this->ZeqManagerInternals->IsUpdated = false;
}
this->ZeqManagerInternals->UpdatedMutex->Unlock();
}
//----------------------------------------------------------------------------
int vtkZeqManager:: WaitForUnlock(const void *flag) { return 1; }
//----------------------------------------------------------------------------
int vtkZeqManager::CreateNotificationSocket()
{
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
vtkPVOptions *pvOptions = pm->GetOptions();
vtkPVServerOptions* options = vtkPVServerOptions::SafeDownCast(pvOptions);
const char *pvClientHostName = NULL;
if (options) {
pvClientHostName = options->GetClientHostName();
}
else {
pvClientHostName = pvOptions->GetHostName();
}
int notificationPort = VTK_ZEQ_MANAGER_DEFAULT_NOTIFICATION_PORT;
if ((this->UpdatePiece == 0) && pvClientHostName && pvClientHostName[0]) {
int r, tryConnect = 0;
this->ZeqManagerInternals->NotificationSocket = vtkSmartPointer<vtkClientSocket>::New();
do {
std::cout << "Creating notification socket to "
<< pvClientHostName << " on port " << notificationPort << "...";
r = this->ZeqManagerInternals->NotificationSocket->ConnectToServer(pvClientHostName,
notificationPort);
if (r == 0) {
std::cout << "Connected pv-zeq server to client" << std::endl;
} else {
std::cout << "Failed to connect pv-zeq server to client" << std::endl;
tryConnect++;
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
} while (r < 0 && tryConnect < 5);
if (r < 0) {
this->ZeqManagerInternals->NotificationSocket = NULL;
return(-1);
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkZeqManager::Create()
{
this->UpdatePiece = this->Controller->GetLocalProcessId();
this->UpdateNumPieces = this->Controller->GetNumberOfProcesses();
if (this->ZeqManagerInternals->NotificationSocket!=NULL) {
// already created
return 0;
}
if (!this->ClientSideMode) {
this->CreateNotificationSocket();
}
_subscriber_hbp = new zeq::Subscriber( servus::URI( "hbp://" ));
_subscriber_monsteer = new zeq::Subscriber( servus::URI( "monsteer://" ));
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0 + 1024,60000 + 1024);
_port = distribution(generator);
const servus::Servus::Result& result = _service.announce( _port,
boost::lexical_cast< std::string >( _port ));
if ( _service.getName() != _servicename)
{
vtkErrorMacro("Something is wrong after service announce");
}
if( !servus::Servus::isAvailable( ))
{
std::cout << "result == servus::Servus::Result::NOT_SUPPORTED" << result << std::endl;
return 0;
}
if (!this->ClientSideMode) {
this->SelectionCallback = zeq_callback(boost::bind( &vtkZeqManager::onSelectedIds, this, _1 ));
this->CameraCallback = zeq_callback(boost::bind( &vtkZeqManager::onHBPCamera, this, _1 ));
this->SpikeCallback = zeq_callback(boost::bind( &vtkZeqManager::onSpike, this, _1 ));
}
_subscriber_hbp->registerHandler( zeq::hbp::EVENT_CAMERA, this->CameraCallback);
_subscriber_hbp->registerHandler( zeq::hbp::EVENT_SELECTEDIDS, this->SelectionCallback);
_subscriber_hbp->registerHandler( monsteer::streaming::EVENT_SPIKES, this->SpikeCallback);
_subscriber_monsteer->registerHandler( zeq::hbp::EVENT_CAMERA, this->CameraCallback);
_subscriber_monsteer->registerHandler( zeq::hbp::EVENT_SELECTEDIDS, this->SelectionCallback);
_subscriber_monsteer->registerHandler( monsteer::streaming::EVENT_SPIKES, this->SpikeCallback);
//
// start thread to listen on zeq events
//
this->thread_done = 0;
this->ZeqManagerInternals->NotificationThreadID =
this->ZeqManagerInternals->NotificationThread->SpawnThread(
vtkZeqManagerNotificationThread, (void *) this);
this->ZeqManagerInternals->WaitForNotifThreadCreated();
return 0;
}
//----------------------------------------------------------------------------
void vtkZeqManager::SetSelectionCallback(zeq_callback cb)
{
this->SelectionCallback = cb;
}
//----------------------------------------------------------------------------
void vtkZeqManager::SetCameraCallback(zeq_callback cb)
{
this->CameraCallback = cb;
}
//----------------------------------------------------------------------------
void vtkZeqManager::SetSpikeCallback(zeq_callback cb)
{
this->SpikeCallback = cb;
}