-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMessageControl.h
More file actions
482 lines (384 loc) · 15.9 KB
/
MessageControl.h
File metadata and controls
482 lines (384 loc) · 15.9 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
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
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2022 RDK Management
*
* 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 "MessageOutput.h"
#include <functional>
namespace Thunder {
namespace Plugin {
class MessageControl : public PluginHost::JSONRPC, public PluginHost::IPluginExtended, public PluginHost::IWebSocket, public Exchange::IMessageControl {
private:
using Cleanups = std::vector<uint32_t>;
class WorkerThread : public Core::Thread {
public:
WorkerThread() = delete;
WorkerThread(const WorkerThread&) = delete;
WorkerThread& operator= (const WorkerThread&) = delete;
WorkerThread(MessageControl& parent)
: Core::Thread()
, _parent(parent)
{
}
~WorkerThread() override = default;
private:
uint32_t Worker() override
{
_parent.Dispatch();
return (Core::infinite);
}
private:
MessageControl& _parent;
};
private:
struct ICollect {
struct ICallback {
virtual void Message(const Core::Messaging::MessageInfo& metadata, const string& text) = 0;
virtual ~ICallback() = default;
};
virtual uint32_t Callback(ICallback* callback) = 0;
virtual ~ICollect() = default;
};
private:
using OutputList = std::vector<Publishers::IPublish*>;
class Config : public Core::JSON::Container {
private:
class NetworkNode : public Core::JSON::Container {
public:
NetworkNode();
NetworkNode(const NetworkNode& copy);
~NetworkNode() = default;
public:
Core::NodeId NodeId() const {
return (Core::NodeId(Binding.Value().c_str(), Port.Value()));
}
public:
Core::JSON::DecUInt16 Port;
Core::JSON::String Binding;
Core::JSON::String Interface;
};
public:
Config()
: Core::JSON::Container()
, Console(false)
, SysLog(false)
, FileName()
, Abbreviated(true)
, MaxExportConnections(Publishers::WebSocketOutput::DefaultMaxConnections)
, Remote()
{
Add(_T("console"), &Console);
Add(_T("syslog"), &SysLog);
Add(_T("filepath"), &FileName);
Add(_T("abbreviated"), &Abbreviated);
Add(_T("maxexportconnections"), &MaxExportConnections);
Add(_T("remote"), &Remote);
}
~Config() = default;
Config(const Config&) = delete;
Config& operator=(const Config&) = delete;
Core::JSON::Boolean Console;
Core::JSON::Boolean SysLog;
Core::JSON::String FileName;
Core::JSON::Boolean Abbreviated;
Core::JSON::DecUInt16 MaxExportConnections;
NetworkNode Remote;
};
class Observer
: public RPC::IRemoteConnection::INotification
, public Plugin::MessageControl::ICollect::ICallback {
private:
enum state {
ATTACHING,
DETACHING,
OBSERVING
};
using Observers = std::unordered_map<uint32_t, state>;
public:
Observer() = delete;
Observer(const Observer&) = delete;
Observer& operator= (const Observer&) = delete;
explicit Observer(MessageControl& parent)
: _parent(parent)
, _adminLock()
, _observing()
, _job(*this)
{
}
~Observer() override {
_job.Revoke();
}
public:
//
// Exchange::IMessageControl::INotification
// ----------------------------------------------------------
void Message(const Core::Messaging::MessageInfo& metadata, const string& message) override {
_parent.Message(metadata, message);
}
//
// RPC::IRemoteConnection::INotification
// ----------------------------------------------------------
void Activated(RPC::IRemoteConnection* connection) override
{
uint32_t id = connection->Id();
_adminLock.Lock();
// Seems the ID is already in here, thats odd, and impossible :-)
Observers::iterator index = _observing.find(id);
if (index == _observing.end()) {
_observing.emplace(std::piecewise_construct,
std::make_tuple(id),
std::make_tuple(state::ATTACHING));
}
else if (index->second == state::DETACHING) {
index->second = state::OBSERVING;
}
_adminLock.Unlock();
_job.Submit();
}
void Deactivated(RPC::IRemoteConnection* connection) override
{
ASSERT(connection != nullptr);
RPC::IMonitorableProcess* controlled (connection->QueryInterface<RPC::IMonitorableProcess>());
if (controlled != nullptr) {
// This is a connection that is controleld by Thunder. For this we can wait till we
// receive a terminated.
controlled->Release();
}
else {
// We have no clue where this is coming from, just assume that if there are message files
// with this ID that it can be closed.
Drop(connection->Id());
}
}
void Terminated(RPC::IRemoteConnection* connection) override
{
ASSERT(connection != nullptr);
Drop(connection->Id());
}
BEGIN_INTERFACE_MAP(Observer)
INTERFACE_ENTRY(RPC::IRemoteConnection::INotification)
END_INTERFACE_MAP
private:
friend class Core::ThreadPool::JobType<Observer&>;
void Drop (const uint32_t id)
{
_adminLock.Lock();
// Seems the ID is already in here, thats odd, and impossible :-)
Observers::iterator index = _observing.find(id);
// ASSERT(index != _observing.end());
if (index != _observing.end()) {
if (index->second == state::ATTACHING) {
_observing.erase(index);
}
else if (index->second == state::OBSERVING) {
index->second = state::DETACHING;
}
}
_adminLock.Unlock();
_job.Submit();
}
void Dispatch()
{
std::vector<uint32_t> attaching;
std::vector<uint32_t> detaching;
_adminLock.Lock();
Observers::iterator index = _observing.begin();
while (index != _observing.end()) {
if (index->second == state::ATTACHING) {
attaching.emplace_back(index->first);
index->second = state::OBSERVING;
index++;
}
else if (index->second == state::DETACHING) {
detaching.emplace_back(index->first);
index = _observing.erase(index);
}
else {
index++;
}
}
_adminLock.Unlock();
for (const uint32_t& id : detaching) {
_parent.Detach(id);
}
for (const uint32_t& id : attaching) {
_parent.Attach(id);
}
}
private:
MessageControl& _parent;
Core::CriticalSection _adminLock;
Observers _observing;
Core::WorkerPool::JobType<Observer&> _job;
};
public:
MessageControl(const MessageControl&) = delete;
MessageControl& operator=(const MessageControl&) = delete;
MessageControl();
~MessageControl() override
{
_worker.Stop();
_worker.Wait(Core::Thread::STOPPED, Core::infinite);
_client.ClearInstances();
}
BEGIN_INTERFACE_MAP(MessageControl)
INTERFACE_ENTRY(PluginHost::IPlugin)
INTERFACE_ENTRY(PluginHost::IDispatcher)
INTERFACE_ENTRY(PluginHost::IPluginExtended)
INTERFACE_ENTRY(PluginHost::IWebSocket)
INTERFACE_ENTRY(Exchange::IMessageControl)
END_INTERFACE_MAP
public:
using PluginHost::JSONRPC::Attach;
using PluginHost::JSONRPC::Detach;
const string Initialize(PluginHost::IShell* service) override;
void Deinitialize(PluginHost::IShell* service) override;
string Information() const override;
bool Attach(PluginHost::Channel&) override;
void Detach(PluginHost::Channel&) override;
Core::ProxyType<Core::JSON::IElement> Inbound(const string& identifier) override;
Core::ProxyType<Core::JSON::IElement> Inbound(const uint32_t ID, const Core::ProxyType<Core::JSON::IElement>& element) override;
private:
void Announce(Publishers::IPublish* output)
{
_outputLock.Lock();
ASSERT(std::find(_outputDirector.begin(), _outputDirector.end(), output) == _outputDirector.end());
_outputDirector.emplace_back(output);
_outputLock.Unlock();
}
void Message(const Core::Messaging::MessageInfo& metadata, const string& message)
{
// Time to start sending it to all interested parties...
_outputLock.Lock();
for (auto& entry : _outputDirector) {
entry->Message(metadata, message);
}
_webSocketExporter.Message(metadata, message);
_outputLock.Unlock();
}
public:
uint32_t Callback(Plugin::MessageControl::ICollect::ICallback* callback)
{
_adminLock.Lock();
ASSERT((_callback != nullptr) ^ (callback != nullptr));
if (_callback != nullptr) {
_worker.Block();
_client.SkipWaiting();
_adminLock.Unlock();
_worker.Wait(Core::Thread::BLOCKED, Core::infinite);
_adminLock.Lock();
}
_callback = callback;
if (_callback != nullptr) {
_worker.Run();
}
_adminLock.Unlock();
return (Core::ERROR_NONE);
}
void Attach(const uint32_t id)
{
_adminLock.Lock();
_client.AddInstance(id);
Cleanup();
_adminLock.Unlock();
}
void Detach(const uint32_t id)
{
_adminLock.Lock();
_client.RemoveInstance(id);
_cleaning.emplace_back(id);
Cleanup();
_adminLock.Unlock();
}
public:
void Cleanup()
{
// Also have a list through the cleanup we should do, just to make sure
Cleanups::iterator index = _cleaning.begin();
while (index != _cleaning.end()) {
bool destructed = true;
string filter (_dispatcherIdentifier + '.' + Core::NumberType<uint32_t>(*index).Text() + _T(".*"));
Core::Directory directory (_dispatcherBasePath.c_str(), filter.c_str());
while ((destructed == true) && (directory.Next() == true)) {
Core::File file (directory.Current());
destructed = destructed && (Core::File(directory.Current()).Destroy());
}
if (destructed == true) {
index = _cleaning.erase(index);
}
else {
index++;
}
}
}
Core::hresult Enable(const messagetype type, const string& category, const string& module, const bool enabled) override
{
_client.Enable({static_cast<Core::Messaging::Metadata::type>(type), category, module}, enabled);
return (Core::ERROR_NONE);
}
Core::hresult Modules(Exchange::IMessageControl::IStringIterator*& modules) const override
{
std::vector<string> list;
_client.Modules(list);
using Implementation = RPC::IteratorType<RPC::IStringIterator>;
modules = Core::ServiceType<Implementation>::Create<RPC::IStringIterator>(list);
return (Core::ERROR_NONE);
}
Core::hresult Controls(const string& module, Exchange::IMessageControl::IControlIterator*& controls) const override
{
std::vector<Exchange::IMessageControl::Control> list;
Messaging::MessageUnit::Iterator index;
_client.Controls(index, module);
while (index.Next() == true) {
list.push_back( { static_cast<messagetype>(index.Type()), index.Category(), index.Module(), index.Enabled() } );
}
using Implementation = RPC::IteratorType<Exchange::IMessageControl::IControlIterator>;
controls = Core::ServiceType<Implementation>::Create<Exchange::IMessageControl::IControlIterator>(list);
return (Core::ERROR_NONE);
}
private:
void Dispatch()
{
_client.WaitForUpdates(Core::infinite);
_client.PopMessagesAndCall([this](const Core::ProxyType<Core::Messaging::MessageInfo>& metadata, const Core::ProxyType<Core::Messaging::IEvent>& message) {
// Turn data into piecies to trasfer over the wire
Message(*metadata, message->Data());
});
}
private:
Core::CriticalSection _adminLock;
Core::CriticalSection _outputLock;
Config _config;
OutputList _outputDirector;
Publishers::WebSocketOutput _webSocketExporter;
MessageControl::ICollect::ICallback* _callback;
Cleanups _cleaning;
Core::SinkType<Observer> _observer;
PluginHost::IShell* _service;
const string _dispatcherIdentifier;
const string _dispatcherBasePath;
Messaging::MessageClient _client;
WorkerThread _worker;
Messaging::TraceFactoryType<Core::Messaging::IStore::Tracing, Core::Messaging::TextMessage> _tracingFactory;
Messaging::TraceFactoryType<Core::Messaging::IStore::Logging, Core::Messaging::TextMessage> _loggingFactory;
Messaging::TraceFactoryType<Core::Messaging::IStore::WarningReporting, Core::Messaging::TextMessage> _warningReportingFactory;
Messaging::TraceFactoryType<Core::Messaging::IStore::OperationalStream, Core::Messaging::TextMessage> _operationalStreamFactory;
Messaging::TraceFactoryType<Core::Messaging::IStore::Assert, Core::Messaging::TextMessage> _assertFactory;
};
} // namespace Plugin
}