-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathTypes.h
648 lines (557 loc) · 19.8 KB
/
Types.h
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "Module.h"
#include "IPlugin.h"
#include "IShell.h"
namespace Thunder {
namespace PluginHost {
template <typename INTERFACE, typename HANDLER>
class PluginMonitorType {
private:
class Sink : public PluginHost::IPlugin::INotification {
private:
enum state : uint8_t {
RUNNING,
REGISTRING,
LOADED
};
public:
Sink() = delete;
Sink(const Sink&) = delete;
Sink& operator=(const Sink&) = delete;
Sink(Sink&&) = delete;
Sink& operator=(Sink&&) = delete;
Sink(PluginMonitorType<INTERFACE, HANDLER>& parent)
: _adminLock()
, _parent(parent)
, _state(state::RUNNING)
, _callsign()
, _designated(nullptr)
{
}
~Sink() override = default;
public:
bool IsOperational() const
{
return (_designated != nullptr);
}
// set callsign (must be called before Register, cannot be used to change callsign when monitoring active)
void Callsign(const string& callsign)
{
_callsign = callsign;
}
void Register(IShell* shell)
{
ASSERT(shell != nullptr);
_adminLock.Lock();
_state = state::REGISTRING;
_adminLock.Unlock();
shell->Register(this);
_adminLock.Lock();
if (_state == state::LOADED) {
INTERFACE* entry = _designated->QueryInterface<INTERFACE>();
_designated->Release();
_designated = entry;
_state = state::RUNNING;
if (entry != nullptr) {
_parent.Activated(entry);
}
} else {
_state = state::RUNNING;
}
_adminLock.Unlock();
}
void Unregister(IShell* shell)
{
ASSERT(shell != nullptr);
if (shell != nullptr) {
_adminLock.Lock();
shell->Unregister(this);
_callsign.clear();
if (_designated != nullptr) {
_designated->Release();
_designated = nullptr;
_parent.Deactivated();
}
_adminLock.Unlock();
}
}
INTERFACE* Interface()
{
INTERFACE* result = nullptr;
Core::SafeSyncType<Core::CriticalSection> lock(_adminLock);
if ((_state == state::RUNNING) && (_designated != nullptr)) {
result = _designated->QueryInterface<INTERFACE>();
}
return (result);
}
const INTERFACE* Interface() const
{
const INTERFACE* result = nullptr;
Core::SafeSyncType<Core::CriticalSection> lock(_adminLock);
if ((_state == state::RUNNING) && (_designated != nullptr)) {
result = _designated->QueryInterface<INTERFACE>();
}
return (result);
}
BEGIN_INTERFACE_MAP(Sink)
INTERFACE_ENTRY(PluginHost::IPlugin::INotification)
END_INTERFACE_MAP
private:
void Activated(const string& name, PluginHost::IShell* plugin) override
{
if (_callsign == name) {
ASSERT(_designated == nullptr);
_adminLock.Lock();
if (_state == state::REGISTRING) {
_state = state::LOADED;
_designated = plugin;
_designated->AddRef();
} else {
INTERFACE* entry = plugin->QueryInterface<INTERFACE>();
_designated = entry;
ASSERT(_state == state::RUNNING);
if (entry != nullptr) {
_parent.Activated(entry);
}
}
_adminLock.Unlock();
}
}
void Deactivated(const string& name, PluginHost::IShell* plugin) override
{
if (_callsign == name) {
_adminLock.Lock();
if (_designated != nullptr) {
_designated->Release();
_designated = nullptr;
if (_state != state::RUNNING) {
_state = state::REGISTRING;
} else {
_parent.Deactivated();
}
}
_adminLock.Unlock();
}
}
void Unavailable(const string& name, PluginHost::IShell* plugin) override
{
}
private:
mutable Core::CriticalSection _adminLock;
PluginMonitorType<INTERFACE, HANDLER>& _parent;
state _state;
string _callsign;
Core::IUnknown* _designated;
};
public:
PluginMonitorType() = delete;
PluginMonitorType(const PluginMonitorType<INTERFACE, HANDLER>&) = delete;
PluginMonitorType<INTERFACE, HANDLER>& operator=(const PluginMonitorType<INTERFACE, HANDLER>&) = delete;
PluginMonitorType(PluginMonitorType<INTERFACE, HANDLER>&&) = delete;
PluginMonitorType<INTERFACE, HANDLER>& operator=(PluginMonitorType<INTERFACE, HANDLER>&&) = delete;
template <typename... Args>
PluginMonitorType(Args&&... args)
: _reporter(std::forward<Args>(args)...)
, _sink(*this)
{
}
~PluginMonitorType() = default;
public:
inline bool IsOperational() const
{
return (_sink.IsOperational());
}
// set callsign (must be called before Register, cannot be used to change callsign when monitoring active)
void Callsign(const string& callsign)
{
_sink.Callsign(callsign);
}
void Register(PluginHost::IShell* shell)
{
_sink.Register(shell);
}
void Register(PluginHost::IShell* shell, const string& callsign)
{
_sink.Callsign(callsign);
_sink.Register(shell);
}
void Unregister(PluginHost::IShell* shell)
{
_sink.Unregister(shell);
}
INTERFACE* Interface()
{
return (_sink.Interface());
}
const INTERFACE* Interface() const
{
return (_sink.Interface());
}
private:
void Activated(INTERFACE* element)
{
_reporter.Activated(element);
}
void Deactivated()
{
_reporter.Deactivated();
}
private:
HANDLER _reporter;
Core::SinkType<Sink> _sink;
};
}
/*
*
* Use the SmartInterfaceType to interact with (a) COMRPC interface(s) exposed by a plugin (identified by its callsign).
* This class will handle the situation where that plugin could be activated and deactivated.
* This class is intended to be used from code which is NOT a plugin.
* If you require this functionality from inside a plugin please see the PluginSmartInterfaceType below.
*
*/
namespace RPC {
template <typename INTERFACE, Core::ProxyType<RPC::IIPCServer> ENGINE() = DefaultInvokeServer>
class SmartInterfaceType {
private:
using Monitor = PluginHost::PluginMonitorType<INTERFACE, SmartInterfaceType<INTERFACE, ENGINE>&>;
public:
SmartInterfaceType(const SmartInterfaceType<INTERFACE, ENGINE>&) = delete;
SmartInterfaceType<INTERFACE, ENGINE>& operator=(const SmartInterfaceType<INTERFACE, ENGINE>&) = delete;
SmartInterfaceType(SmartInterfaceType<INTERFACE, ENGINE>&&) = delete;
SmartInterfaceType<INTERFACE, ENGINE>& operator=(SmartInterfaceType<INTERFACE, ENGINE>&&) = delete;
PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
SmartInterfaceType()
: _controller(nullptr)
, _administrator()
, _monitor(*this)
, _connectionId(~0)
{
}
POP_WARNING()
virtual ~SmartInterfaceType()
{
ASSERT(_controller == nullptr);
}
public:
uint32_t ConnectionId() const {
return (_connectionId);
}
bool IsOperational() const
{
return (_monitor.IsOperational());
}
uint32_t Open(const uint32_t waitTime, const Core::NodeId& node, const string& callsign)
{
Core::IUnknown* controller = RPC::ConnectorController::Instance().Controller();
ASSERT(_controller == nullptr);
if (controller != nullptr) {
// Seems like we already have a connection to the IShell of the Controller plugin, reuse it.
_controller = controller->QueryInterface<PluginHost::IShell>();
controller->Release();
}
if (_controller == nullptr) {
_controller = _administrator.template Acquire<PluginHost::IShell>(waitTime, node, _T(""), ~0);
}
if (_controller != nullptr) {
_monitor.Register(_controller, callsign);
Core::ProxyType<CommunicatorClient> channel(_administrator.Communicator(node));
if (channel.IsValid() == true) {
_connectionId = channel->ConnectionId();
}
}
return (_controller != nullptr ? Core::ERROR_NONE : Core::ERROR_UNAVAILABLE);
}
uint32_t Close(const uint32_t waitTime)
{
if (_controller != nullptr) {
_monitor.Unregister(_controller);
_controller->Release();
_controller = nullptr;
}
return (Core::ERROR_NONE);
}
template <typename EXPECTED_INTERFACE>
EXPECTED_INTERFACE* Acquire(const uint32_t waitTime, const Core::NodeId& nodeId, const string className, const uint32_t version = ~0)
{
return (_administrator.template Acquire<EXPECTED_INTERFACE>(waitTime, nodeId, className, version));
}
INTERFACE* Interface()
{
return (_monitor.Interface());
}
const INTERFACE* Interface() const
{
return (_monitor.Interface());
}
PluginHost::IShell* ControllerInterface()
{
_controller->AddRef();
return _controller;
}
const PluginHost::IShell* ControllerInterface() const
{
_controller->AddRef();
return _controller;
}
// Allow a derived class to take action on a new interface, or almost dissapeared interface..
virtual void Operational(const bool upAndRunning)
{
}
static Core::NodeId Connector()
{
string comPath;
if (Core::SystemInfo::GetEnvironment(_T("COMMUNICATOR_CONNECTOR"), comPath) == false) {
#ifdef __WINDOWS__
comPath = _T("127.0.0.1:62000");
#else
comPath = _T("/tmp/communicator");
#endif
}
return Core::NodeId(comPath.c_str());
}
private:
friend Monitor;
void Activated(INTERFACE* plugin)
{
Operational(true);
}
void Deactivated()
{
Operational(false);
}
private:
PluginHost::IShell* _controller;
ConnectorType<ENGINE> _administrator;
Monitor _monitor;
uint32_t _connectionId;
};
/*
*
* Use the PluginSmartInterfaceType to interact with (a) COMRPC interface(s) exposed by a plugin (identified by its callsign).
* This class will handle the situation where that plugin could be activated and deactivated.
* This class is intended to be used from code which is a plugin. It can be used from both an in process as well as an
* out of process plugin.
* WARNING: if you use this class in the out of process part of a plugin this plugin must have a minimum workerpool thread count
* of 2 otherwise deadlocks can occur!!!!
*
* If you require this functionality from code that is NOT a plugin please see the SmartInterfaceType above.
*
*/
template <typename INTERFACE>
class PluginSmartInterfaceType {
private:
using Monitor = PluginHost::PluginMonitorType<INTERFACE, PluginSmartInterfaceType<INTERFACE>&>;
public:
PluginSmartInterfaceType(const PluginSmartInterfaceType<INTERFACE>&) = delete;
PluginSmartInterfaceType<INTERFACE>& operator=(const PluginSmartInterfaceType<INTERFACE>&) = delete;
PluginSmartInterfaceType(PluginSmartInterfaceType<INTERFACE>&&) = delete;
PluginSmartInterfaceType<INTERFACE>& operator=(PluginSmartInterfaceType<INTERFACE>&&) = delete;
PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
PluginSmartInterfaceType()
: _shell(nullptr)
, _monitor(*this)
, _job(*this)
{
}
POP_WARNING()
virtual ~PluginSmartInterfaceType()
{
ASSERT(_shell == nullptr);
ASSERT(_job.IsIdle() == true);
}
class RegisterJob {
public:
RegisterJob(RegisterJob&&) = delete;
RegisterJob(const RegisterJob&) = delete;
RegisterJob& operator=(const RegisterJob&) = delete;
RegisterJob& operator=(RegisterJob&&) = delete;
RegisterJob(PluginSmartInterfaceType& parent)
: _parent(parent)
{
}
~RegisterJob() = default;
public:
void Dispatch()
{
_parent.Register();
}
private:
PluginSmartInterfaceType& _parent;
};
using Job = Core::IWorkerPool::JobType<RegisterJob>;
public:
bool IsOperational() const
{
return (_monitor.IsOperational());
}
uint32_t Open(PluginHost::IShell* shell, const string& callsign)
{
ASSERT(_shell == nullptr);
ASSERT(_job.IsIdle() == true);
if(shell != nullptr) {
_shell = shell;
_shell->AddRef();
_monitor.Callsign(callsign);
_job.Submit();
}
return (Core::ERROR_NONE);
}
uint32_t Close()
{
ASSERT(_shell != nullptr);
if(_shell != nullptr) {
_job.Revoke();
_monitor.Unregister(_shell);
_shell->Release();
_shell = nullptr;
}
return (Core::ERROR_NONE);
}
INTERFACE* Interface()
{
return (_monitor.Interface());
}
const INTERFACE* Interface() const
{
return (_monitor.Interface());
}
PluginHost::IShell* PluginInterface()
{
_shell->AddRef();
return _shell;
}
const PluginHost::IShell* PluginInterface() const
{
_shell->AddRef();
return _shell;
}
// Allow a derived class to take action on a new interface, or almost dissapeared interface..
virtual void Operational(const bool upAndRunning)
{
}
private:
friend Monitor;
friend Job;
void Activated(INTERFACE* plugin)
{
Operational(true);
}
void Deactivated()
{
Operational(false);
}
void Register()
{
_monitor.Register(_shell);
}
private:
PluginHost::IShell* _shell;
Monitor _monitor;
Job _job;
};
template <typename INTERFACE, Core::ProxyType<RPC::IIPCServer> ENGINE() = DefaultInvokeServer>
class SmartControllerInterfaceType {
public:
SmartControllerInterfaceType(const SmartControllerInterfaceType<INTERFACE, ENGINE>&) = delete;
SmartControllerInterfaceType<INTERFACE, ENGINE>& operator=(const SmartControllerInterfaceType<INTERFACE, ENGINE>&) = delete;
SmartControllerInterfaceType()
: _controller(nullptr)
, _administrator()
, _connectionId(~0)
{
}
~SmartControllerInterfaceType()
{
ASSERT(_controller == nullptr);
}
public:
uint32_t ConnectionId() const {
return (_connectionId);
}
bool IsOperational() const
{
return (_controller != nullptr);
}
uint32_t Open(const uint32_t waitTime, const Core::NodeId& node)
{
Core::IUnknown* controller = RPC::ConnectorController::Instance().Controller();
ASSERT(_controller == nullptr);
if (controller != nullptr) {
// Seems like we already have a connection to the IShell of the Controller plugin, reuse it.
_controller = controller->QueryInterface<PluginHost::IShell>();
controller->Release();
}
if (_controller == nullptr) {
_controller = _administrator.template Acquire<PluginHost::IShell>(waitTime, node, _T(""), ~0);
}
if (_controller != nullptr) {
Core::ProxyType<CommunicatorClient> channel(_administrator.Communicator(node));
if (channel.IsValid() == true) {
_connectionId = channel->ConnectionId();
}
}
return (_controller != nullptr ? Core::ERROR_NONE : Core::ERROR_UNAVAILABLE);
}
uint32_t Close(const uint32_t waitTime)
{
if (_controller != nullptr) {
_controller->Release();
_controller = nullptr;
}
return (Core::ERROR_NONE);
}
INTERFACE* Interface()
{
INTERFACE* result = nullptr;
if(_controller != nullptr) {
result = _controller->QueryInterface<INTERFACE>();
}
return result;
}
const INTERFACE* Interface() const
{
INTERFACE* result = nullptr;
if(_controller != nullptr) {
result = _controller->QueryInterface<INTERFACE>();
}
return result;
}
PluginHost::IShell* ControllerInterface()
{
_controller->AddRef();
return _controller;
}
const PluginHost::IShell* ControllerInterface() const
{
_controller->AddRef();
return _controller;
}
static Core::NodeId Connector()
{
return SmartInterfaceType<PluginHost::IShell>::Connector();
}
private:
PluginHost::IShell* _controller;
ConnectorType<ENGINE> _administrator;
uint32_t _connectionId;
};
}
}