-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcorbaComm_impl.cc
More file actions
779 lines (689 loc) · 22.4 KB
/
Copy pathcorbaComm_impl.cc
File metadata and controls
779 lines (689 loc) · 22.4 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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#include <map>
#include <string>
#include <thread>
#include <vector>
#include <iostream>
#include <algorithm>
#include <array>
#include <chrono>
#include <sstream>
#include <cstring>
#include "corbaComm_impl.h"
#include "corbaComm.hh"
#include "cos.h"
#include "notify_impl.h"
#include "provider.h"
#include <omniORB4/omniZIOP.h>
static cc::CorbaCommImpl* _impl;
static void consumeCallback(const CosN::StructuredEvent& event)
{
const char* check = (const char*)event.filterable_data[1].name;
if (0 == std::strcmp(check, "offer services")) {
::_impl->trySetProviderInfo(event);
}
else if (0 == std::strcmp(check, "want services")) {
::_impl->tryPublishOfferService(event);
}
else {
::_impl->tryDispatchEvent(event);
}
}
// command provider will call this special command, cmd == _hostId
// command requester to set provider info according to this command
//
static std::string cmdProviderResponse(const std::string& cmd,
const std::string& param)
{
std::istringstream strm(param);
std::string command;
getline(strm, command, ';');
std::string provider;
getline(strm, provider, ':');
cc::CorbaCommImpl::Cmd2ProviderInfo info = {command, provider};
::_impl->trySetProviderInfo(info);
return "";
}
cc::CorbaCommImpl::CorbaCommImpl(const char* hostId,
cc::Commands offerCommands,
cc::Commands wantCommands,
int argc, char* argv[])
: _pushSupplier{nullptr}
, _pushConsumer{nullptr}
, _orb{CORBA::ORB::_nil()}
, _poa{PortableServer::POA::_nil()}
, _nameCtx{CosNaming::NamingContext::_nil()}
, _orbRunning{false}
{
_hostId = hostId;
_offerCommands = offerCommands;
_wantCommands = wantCommands;
// * * * * * * * * N O T E * * * * * * * *
//
// this ctor will throw exception
// it means the host can't initialize CORBA enviroment,
// and it never works
//
// throw exception to crash the host process
// developers must figure out the root cause and fix any errors
//
// * * * * * * * * N O T E * * * * * * * *
try {
const char* options[][2] = {
{ "maxGIOPConnectionPerServer", "50" },
{ (const char*)0, (const char*)0 },
};
CORBA::PolicyList pl;
omniZIOP::setGlobalPolicies(pl);
_orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
CORBA::Object_var obj;
obj = _orb->resolve_initial_references("RootPOA");
_poa = PortableServer::POA::_narrow(obj);
obj = _orb->resolve_initial_references("NameService");
_nameCtx = CosNaming::NamingContext::_narrow(obj);
if (!initPushSupplier())
throw cc::CorbaCommImpl::SupplierFailureException();
if (!initPushConsumer())
throw cc::CorbaCommImpl::ConsumerFailureException();
// every app is a 'command provider', and offer a special command
// which command name is identical to _hostId.
// this command is to receive provider's response
newProviderCorbaObject();
onCmd(_hostId.c_str(), cmdProviderResponse);
if (_offerCommands.size() > 0 )
publishOfferCommands(offerCommands);
if (wantCommands.size() > 0)
publishWantCommands(wantCommands);
::_impl = this;
}
catch (CORBA::TRANSIENT& ex) {
std::cerr << "Caught CORBA::TRANSIENT, can't contact Name Service\n";
throw ex;
}
catch (CORBA::SystemException& ex) {
std::cerr << "Caught CORBA::SystemException: " << ex._name() << "\n";
throw;
}
catch (CORBA::Exception& ex) {
std::cerr << "Caught CORBA::Exception: " << ex._name() << "\n";
throw;
}
catch (SupplierFailureException& ex) {
std::cerr << "Can't initial push supplier.\n";
throw ex;
}
catch (CorbaObjectImplFailure& ex) {
std::cerr << "Can't create Provider CORBA object.\n";
throw ex;
}
catch (...) {
std::cerr << "Caught unknown exception.\n"
<< "The process can't work, please figure out the reason.\n";
// re-throw to crash the host process
//
throw;
}
}
cc::CorbaCommImpl::~CorbaCommImpl()
{
}
void cc::CorbaCommImpl::tryDispatchEvent(
const CosN::StructuredEvent& event) const
{
const char* param;
const char* ev;
event.filterable_data[1].value >>= ev;
event.remainder_of_body >>= param;
auto itr = _subscribeMap.find(ev);
if (itr != _subscribeMap.end()) {
auto allCallbacks = itr->second;
for (auto callback: allCallbacks)
(*callback)(ev, param);
}
}
void cc::CorbaCommImpl::trySetProviderInfo(const CosN::StructuredEvent& event)
{
if (_wantCommands.size() > 0 ) {
const char* cmd;
event.filterable_data[1].value >>= cmd;
if (std::find(_wantCommands.begin(), _wantCommands.end(), cmd)
!= _wantCommands.end())
{
const char* provider;
event.filterable_data[0].value >>= provider;
_providerInfoMap[cmd] = std::string(provider);
clearObjReference(provider);
}
unblockedCmd(cmd);
}
}
void cc::CorbaCommImpl::trySetProviderInfo(
const cc::CorbaCommImpl::Cmd2ProviderInfo& info)
{
if (_wantCommands.size() > 0) {
if (std::find(_wantCommands.begin(), _wantCommands.end(), info.first)
!= _wantCommands.end())
{
_providerInfoMap[info.first] = info.second;
}
unblockedCmd(info.first);
}
}
void cc::CorbaCommImpl::tryPublishOfferService(
const CosN::StructuredEvent& event) const
{
if (_offerCommands.size() > 0) {
const char* querier;
event.filterable_data[0].value >>= querier;
const char* cmd;
event.filterable_data[1].value >>= cmd;
auto itr = std::find(std::begin(_offerCommands),
std::end(_offerCommands), cmd);
// the provider will 'execCmd' to notify command requester
//
if (itr != std::end(_offerCommands)) {
CosNaming::Name name;
name.length(2);
name[0].id = "edwardlintw";
name[0].kind = "com";
name[1].id = querier;
name[1].kind = "provider";
try {
CORBA::Object_var obj = resolveObjectReference(name);
CorbaCommModule::Provider_ptr providerRef =
CorbaCommModule::Provider::_narrow(obj);
std::string param;
param.append(cmd).append(";").append(_hostId);
providerRef->execCmd(querier, param.c_str());
}
catch (...) {
;
}
}
}
}
cc::SID cc::CorbaCommImpl::onEvent(const char* topic,
cc::EventCallback_t callback)
{
if (nullptr == topic|| 0 == std::strcmp(topic,""))
return "";
bool ok = false;
auto which = _subscribeMap.find(topic);
if (which == _subscribeMap.end()) {
_subscribeMap[topic] = {callback};
ok = true;
}
else {
auto all = which->second;
auto itr = std::find(std::begin(all), std::end(all), callback);
if (itr == std::end(all)) {
which->second.push_back(callback);
ok = true;
}
}
if (ok) {
auto sid = genSID();
_evtInfoMap[sid] = {topic, callback};
return sid;
}
else {
return "";
}
}
void cc::CorbaCommImpl::detachEvent(const SID& sid)
{
auto evtInfoItr = _evtInfoMap.find(sid);
if (evtInfoItr != _evtInfoMap.end()) {
auto topic= evtInfoItr->second.first;
auto callback = evtInfoItr->second.second;
auto which = _subscribeMap.find(topic);
if (which != _subscribeMap.end()) {
auto& callbacks = which->second;
which->second.erase(
std::remove(std::begin(callbacks), std::end(callbacks), callback));
}
_evtInfoMap.erase(evtInfoItr);
}
}
bool cc::CorbaCommImpl::pushEvent(const char* topic,
const char* param) const
{
cc::CorbaCommImpl::Filters filters = {{
std::make_pair(std::string("sender"), _hostId),
std::make_pair(std::string("command"), std::string(topic))
}};
// bridge to the other overloading 'pushEvent'
//
return pushEvent(topic, param, filters);
}
bool cc::CorbaCommImpl::pushEvent(
const char* topic,
const char* param,
const cc::CorbaCommImpl::Filters& filters) const
{
CosN::StructuredEvent* ev = new CosN::StructuredEvent;
try {
// setup event header (for filtering)
//
ev->header.fixed_header.event_type.domain_name = "";
ev->header.fixed_header.event_type.type_name = "";
ev->header.variable_header.length(0);
ev->filterable_data.length(filters.size());
size_t i = 0;
for (const auto& filter : filters) {
ev->filterable_data[i].name = filter.first.c_str();
ev->filterable_data[i].value <<= filter.second.c_str();
++i;
}
ev->remainder_of_body <<= param;
_pushSupplier->push(*ev);
delete ev;
return true;
}
catch (...) {
std::cerr << "send failure\n";
delete ev;
return false;
}
}
std::string cc::CorbaCommImpl::execCmd(const char* cmd,
const char* param)
{
// lookup who is provider
//
re_run:
auto whois = _providerInfoMap.find(cmd);
if (whois == _providerInfoMap.end()) {
_wantCommands.push_back(cmd);
auto itr = _syncMap.find(cmd);
if (itr == _syncMap.end()) {
_syncMap[cmd] = SyncObj();
itr = _syncMap.find(cmd);
}
std::unique_lock<std::mutex> lock(*itr->second._mutex);
itr->second._cmdReady = false;
std::thread ([this,cmd]() {
publishWantCommands({cmd});
}).detach();
using namespace std::chrono_literals;
itr->second._cv->wait_for(lock, 100ms,
[this,itr]() {
return itr->second._cmdReady;
});
if (false == itr->second._cmdReady)
return "";
else {
goto re_run;
}
}
// lookup Provider's Object reference
//
std::string provider = whois->second;
auto which = _objRefMap.find(provider);
if (which != _objRefMap.end()) {
try {
CORBA::String_var ret;
std::string result;
ret =
which->second->execCmd(cmd, param);
result = (const char*)ret;
return result;
}
catch (... ) {
// can't reach target host (maybe host is down)
//
_objRefMap.erase(which);
return "";
}
}
else {
CosNaming::Name name;
name.length(2);
name[0].id = "edwardlintw";
name[0].kind = "com";
name[1].id = provider.c_str();
name[1].kind = "provider";
CORBA::Object_var obj = resolveObjectReference(name);
CorbaCommModule::Provider_ptr providerRef =
CorbaCommModule::Provider::_narrow(obj);
std::string result;
try {
CORBA::String_var ret;
ret = providerRef->execCmd(cmd, param);
result = (const char*)ret;
_objRefMap[provider] = providerRef;
}
catch (...) {
result = "";
}
return result;
}
}
void cc::CorbaCommImpl::onCmd(const char* cmd,
cc::CommandCallback_t func)
{
if (_hostId != cmd
&&
std::find(std::begin(_offerCommands),
std::end(_offerCommands), cmd) == std::end(_offerCommands)
)
{
publishOfferCommands({cmd});
_offerCommands.push_back(cmd);
}
auto which = _providerMap.find(cmd);
if (which != _providerMap.end()) {
// call more than once 'offerRequest' with the same 'cmd'
// do nothing
//
return;
}
_providerMap[cmd] = func;
_providerImpl->onCmd(cmd, func);
}
cc::SID cc::CorbaCommImpl::genSID() const
{
using namespace std::chrono;
auto now = high_resolution_clock::now();
auto ns = duration_cast<nanoseconds>(now.time_since_epoch()).count();
std::stringstream ostrm;
ostrm << ns;
return ostrm.str();
}
void cc::CorbaCommImpl::unblockedCmd(const std::string& cmd)
{
auto itr = _syncMap.find(cmd);
if (itr != _syncMap.end())
{
std::lock_guard<std::mutex> lock(*itr->second._mutex);
itr->second._cmdReady = true;
itr->second._cv->notify_all();
}
}
void cc::CorbaCommImpl::clearObjReference(const std::string& provider)
{
auto itr = _objRefMap.find(provider);
if (itr != _objRefMap.end())
_objRefMap.erase(itr);
}
void cc::CorbaCommImpl::newProviderCorbaObject()
{
CosNaming::Name name;
name.length(2);
name[0].id = "edwardlintw";
name[0].kind = "com";
name[1].id = _hostId.c_str();
name[1].kind = "provider";
// implement CORBA Object 'Provider'
// to provide request service
//
if (!initProviderImpl(name))
throw cc::CorbaCommImpl::CorbaObjectImplFailure();
}
bool cc::CorbaCommImpl::initPushConsumer()
{
CosNCA::EventChannel_ptr channel;
channel = getOrCreateChannel();
if (CORBA::is_nil(channel)) {
std::cerr << "Can't create event channel.\n";
return false;
}
try {
CosN::EventTypeSeq evs;
evs.length(0);
char constraint[80]; // filter constraint
std::sprintf(constraint, "$sender != '%s'", _hostId.c_str());
_pushConsumer =
PushConsumer_i::create(_orb, channel, "Push Consumer", consumeCallback,
nullptr, &evs, constraint);
if (!_pushConsumer) {
std::cerr << "Can't construct push consumer.\n";
return false;
}
CosNC::StructuredPushConsumer_var
pushConsumerRef = _pushConsumer->_this();
_pushConsumer->_remove_ref();
_pushConsumer->connect();
if (!_orbRunning) {
PortableServer::POAManager_var pman = _poa->the_POAManager();
pman->activate();
// since ORB::run() will block execution
// make ORB::run() in detached thread
// or this methond won't return
//
std::thread([&]() { _orb->run(); }).detach();
_orbRunning = true;
}
return true;
}
catch (...) {
// TODO: more concrete info here
//
std::cerr << "Catch unknown exception\n";
return false;
}
}
bool cc::CorbaCommImpl::initPushSupplier()
{
CosNCA::EventChannel_ptr channel;
channel = getOrCreateChannel();
if (CORBA::is_nil(channel)) {
std::cerr << "Can't create event channel.\n";
return false;
}
try {
CosN::EventTypeSeq evs;
evs.length(0);
_pushSupplier =
PushSupplier_i::create(_orb, channel, "Push Supplier",
nullptr, &evs, nullptr);
if (!_pushSupplier) {
std::cerr << "Can't construct push supplier.\n";
return false;
}
CosNC::StructuredPushSupplier_var
pushSupplierRef = _pushSupplier->_this();
_pushSupplier->_remove_ref();
_pushSupplier->connect();
if (!_orbRunning) {
PortableServer::POAManager_var pman = _poa->the_POAManager();
pman->activate();
// since ORB::run() will block execution
// make ORB::run() in detached thread
// or this methond won't return
//
std::thread([&]() { _orb->run(); }).detach();
_orbRunning = true;
}
return true;
}
catch (...) {
// TODO: more concrete info here
//
std::cerr << "Catch unknown exception\n";
return false;
}
}
bool cc::CorbaCommImpl::initProviderImpl(const CosNaming::Name& name)
{
try {
Compression::CompressorIdLevelList ids;
ids.length(1);
ids[0].compressor_id = Compression::COMPRESSORID_ZLIB;
ids[0].compression_level = 6;
CORBA::PolicyList pl;
pl.length(2);
pl[0] = omniZIOP::create_compression_enabling_policy(1);
pl[1] = omniZIOP::create_compression_id_level_list_policy(ids);
PortableServer::POAManager_var pman = _poa->the_POAManager();
PortableServer::POA_var poa =
_poa->create_POA("Custom POA", pman, pl);
_providerImpl = new ProviderImpl();
PortableServer::ObjectId_var
providerId = poa->activate_object(_providerImpl);
CORBA::Object_var obj = _providerImpl->_this();
return bindObjectToName(name, obj);
}
catch (CORBA::SystemException& ex) {
std::cerr << "Caught CORBA::SystemException: " << ex._name() << ".\n";
return false;
}
catch (CORBA::Exception& ex) {
std::cerr << "Caught Exception: " << ex._name() << ".\n";
return false;
}
catch (...) {
std::cerr << "Caught unknown exception.\n";
return false;
}
}
bool cc::CorbaCommImpl::bindObjectToName(const CosNaming::Name& objName,
CORBA::Object_ptr obj)
{
try {
CosNaming::NamingContext_var ctx = _nameCtx;
CosNaming::Name ctxName;
auto length = objName.length();
// from the beginning to last 2 (id,kind) pair are context
//
for (CORBA::ULong i = 0; i < length-1; ++i) {
try {
ctxName.length(1);
ctxName[0].id = objName[i].id;
ctxName[0].kind = objName[i].kind;
ctx = ctx->bind_new_context(ctxName);
}
catch (CosNaming::NamingContext::AlreadyBound& ex) {
// this it is a context (already exists)
// resolve and use the old one
//
CORBA::Object_var obj = ctx->resolve(ctxName);
ctx = CosNaming::NamingContext::_narrow(obj);
if (CORBA::is_nil(ctx)) {
std::cerr << "Failed to bind name context\n";
return 0;
}
}
}
// the last 1 (id,kind) pair is a object
//
try {
ctxName.length(1);
ctxName[0].id = objName[length-1].id;
ctxName[0].kind = objName[length-1].kind;
ctx->bind(ctxName, obj);
}
catch (CosNaming::NamingContext::AlreadyBound& ex) {
// already exists, bind new one (discard old one)
// this approach is good for
// either 'transient' or 'persistant' server objects
//
ctx->rebind(ctxName, obj);
}
}
catch (CORBA::TRANSIENT& ex) {
std::cerr << "Caught CORAB::TRANSIENT, can't contact Name Server\n";
return 0;
}
catch (CORBA::SystemException& ex) {
std::cerr << "Caught CORBA::SystemException: "
<< ex._name() << std::endl;
return 0;
}
catch (...) {
std::cerr << "Caught unknown exception\n";
return 0;
}
return 1;
}
CosNCA::EventChannel_ptr cc::CorbaCommImpl::getOrCreateChannel()
{
CosNCA::EventChannel_ptr channel = CosNCA::EventChannel::_nil();
CosNaming::Name name;
// resolve from Name Server
//
name.length(1);
name[0].id = _channelName.c_str();
name[0].kind = _channelName.c_str();
try {
CORBA::Object_var obj = _nameCtx->resolve(name);
channel = CosNCA::EventChannel::_narrow(obj);
}
catch (...) {
// just do nothing, try creating EventChannel later
//
}
// already exists
//
if (!CORBA::is_nil(channel))
return channel;
// try to create a new one
//
CosNCA::EventChannelFactory_ptr factory;
name.length(1);
name[0].id = _factoryName.c_str();
name[0].kind = _factoryName.c_str();
try {
CORBA::Object_var obj = _nameCtx->resolve(name);
factory = CosNCA::EventChannelFactory::_narrow(obj);
}
catch (...) {
factory = CosNCA::EventChannelFactory::_nil();
}
if (CORBA::is_nil(factory)) {
return channel;
}
CosNCA::ChannelID channelId;
try {
CosN::QoSProperties qosProp;
CosN::AdminProperties adminProp;
qosProp.length(0);
adminProp.length(0);
channel = factory->create_channel(qosProp, adminProp, channelId);
}
catch (...) {
// TODO: more concrete info
//
}
return channel;
}
CORBA::Object_ptr cc::CorbaCommImpl::resolveObjectReference(
const CosNaming::Name& name) const
{
try {
return _nameCtx->resolve(name);
}
catch (CosNaming::NamingContext::NotFound&) {
std::cerr << "Context not found\n";
}
catch (CORBA::TRANSIENT&) {
std::cerr << "Caught CORBA::TRANSIENT, unable to contact NameService\n";
}
catch (CORBA::SystemException& ex) {
std::cerr << "Caught CORBA::SystemException: " << ex._name() << "\n";
}
catch (...) {
std::cerr << "Caught unknown exception\n";
}
return CORBA::Object::_nil();
}
void cc::CorbaCommImpl::publishCommandsType(const cc::Commands& services,
std::string type) const
{
for (const auto& cmd : services) {
cc::CorbaCommImpl::Filters filters = {{
std::make_pair(std::string("sender"), _hostId),
std::make_pair(type, cmd)
}};
pushEvent("-1", "", filters);
}
}
void cc::CorbaCommImpl::publishOfferCommands(
const cc::Commands& services) const
{
publishCommandsType(services, "offer services");
}
void cc::CorbaCommImpl::publishWantCommands(
const cc::Commands& services) const
{
publishCommandsType(services, "want services");
}