-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnode-opendds.cpp
726 lines (642 loc) · 26.5 KB
/
node-opendds.cpp
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
#include "NodeDRListener.h"
#include "NodeValueReader.h"
#include <nan.h>
#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/Registered_Data_Types.h>
#include <dds/DCPS/Marked_Default_Qos.h>
#include <ace/DLL_Manager.h>
#include <ace/Init_ACE.h>
#include <string>
#include <vector>
#include <cstring>
using namespace v8;
using OpenDDS::DCPS::Data_Types_Register;
using NodeOpenDDS::NodeDRListener;
using NodeOpenDDS::NodeValueReader;
namespace {
std::vector<DDS::DomainParticipant_var> participants_;
std::string cft_name("CFT000001"); // unique names for ContentFilteredTopic
void increment(std::string& name)
{
const size_t i = cft_name.find_first_not_of('0', 3);
if (name[i] == '9') {
if (i == 3) {
name = "CFT000001";
} else {
name[i] = '0';
name[i - 1] = '1';
}
} else {
++name[i];
}
}
void create_participant(const Nan::FunctionCallbackInfo<Value>& fci);
void delete_participant(const Nan::FunctionCallbackInfo<Value>& fci);
void subscribe(const Nan::FunctionCallbackInfo<Value>& fci);
void unsubscribe(const Nan::FunctionCallbackInfo<Value>& fci);
void create_datawriter(const Nan::FunctionCallbackInfo<Value>& fci);
void register_instance(const Nan::FunctionCallbackInfo<Value>& fci);
void write(const Nan::FunctionCallbackInfo<Value>& fci);
void wait_for_acknowledgments(const Nan::FunctionCallbackInfo<Value>& fci);
void unregister_instance(const Nan::FunctionCallbackInfo<Value>& fci);
void dispose(const Nan::FunctionCallbackInfo<Value>& fci);
void initialize(const Nan::FunctionCallbackInfo<Value>& fci)
{
ACE::init();
std::vector<std::string> arg_storage;
arg_storage.reserve(fci.Length());
ACE_ARGV_T<char> argv(false /*substitute env vars*/);
for (int i = 0; i < fci.Length(); ++i) {
arg_storage.push_back(std::string(*Nan::Utf8String(fci[i])));
std::string& str = arg_storage.back();
argv.add(str.c_str());
}
int argc = argv.argc();
DDS::DomainParticipantFactory_var dpf =
TheParticipantFactoryWithArgs(argc, argv.argv());
const Local<ObjectTemplate> ot = Nan::New<ObjectTemplate>();
ot->SetInternalFieldCount(1);
Nan::SetMethod(ot, "create_participant", create_participant);
Nan::SetMethod(ot, "delete_participant", delete_participant);
const Local<Object> obj = ot->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(obj, 0, dpf._retn());
fci.GetReturnValue().Set(obj);
}
void create_participant(const Nan::FunctionCallbackInfo<Value>& fci)
{
const Local<Object> this_js = fci.This();
DDS::DomainId_t domain = 0;
if (fci.Length() > 0) {
domain = static_cast<DDS::DomainId_t>(fci[0]->NumberValue(Nan::GetCurrentContext()).FromMaybe(0.0));
}
void* const internal = Nan::GetInternalFieldPointer(this_js, 0);
DDS::DomainParticipantFactory* const dpf =
static_cast<DDS::DomainParticipantFactory*>(internal);
DDS::DomainParticipantQos qos;
dpf->get_default_participant_qos(qos);
if (fci.Length() > 1 && !fci[1]->ToObject(Nan::GetCurrentContext()).IsEmpty()) {
const Local<Object> qos_js = fci[1]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
try {
NodeValueReader nvr(qos_js);
if (!OpenDDS::DCPS::vread(nvr, qos)) {
throw std::runtime_error("create_participant: Unable to convert participant qos policy");
}
} catch (const std::runtime_error& e) {
Nan::ThrowError(e.what());
fci.GetReturnValue().SetUndefined();
return;
}
}
DDS::DomainParticipant_var dp = dpf->create_participant(domain, qos, 0, 0);
if (!dp) {
Nan::ThrowError("create_participant: couldn't create DomainParticipant");
fci.GetReturnValue().SetUndefined();
return;
}
participants_.push_back(dp);
const Local<ObjectTemplate> ot = Nan::New<ObjectTemplate>();
ot->SetInternalFieldCount(1);
Nan::SetMethod(ot, "subscribe", subscribe);
Nan::SetMethod(ot, "unsubscribe", unsubscribe);
Nan::SetMethod(ot, "create_datawriter", create_datawriter);
const Local<Object> obj = ot->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(obj, 0, dp._retn());
fci.GetReturnValue().Set(obj);
}
void delete_participant(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() == 0) {
Nan::ThrowTypeError("delete_participant: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
} else if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("delete_participant: Argument must be of object type");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> js_obj = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
void* const internal = Nan::GetInternalFieldPointer(js_obj, 0);
const DDS::DomainParticipant_var part =
static_cast<DDS::DomainParticipant*>(internal);
Nan::SetInternalFieldPointer(js_obj, 0, 0);
const std::vector<DDS::DomainParticipant_var>::iterator i =
std::find(participants_.begin(), participants_.end(), part);
if (i != participants_.end()) participants_.erase(i);
part->delete_contained_entities();
const DDS::DomainParticipantFactory_var dpf = TheParticipantFactory;
dpf->delete_participant(part);
fci.GetReturnValue().SetUndefined();
}
void subscribe(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 3) {
Nan::ThrowTypeError("subscribe: At least 3 arguments required");
fci.GetReturnValue().SetUndefined();
return;
}
if (!fci[fci.Length() - 1]->IsFunction()) {
Nan::ThrowTypeError("subscribe: Last argument must be a function");
fci.GetReturnValue().SetUndefined();
return;
}
void* const internal = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DomainParticipant* const dp =
static_cast<DDS::DomainParticipant*>(internal);
const Nan::Utf8String topic_name(fci[0]);
const Nan::Utf8String topic_type(fci[1]);
OpenDDS::DCPS::TypeSupport* ts =
Registered_Data_Types->lookup(dp, *topic_type);
if (!ts) {
ts = Registered_Data_Types->lookup(0, *topic_type);
if (!ts) {
Nan::ThrowError("subscribe: TypeSupport was not registered");
fci.GetReturnValue().SetUndefined();
return;
}
Registered_Data_Types->register_type(dp, *topic_type, ts);
}
DDS::Topic_var real_topic =
dp->create_topic(*topic_name, *topic_type, TOPIC_QOS_DEFAULT, 0, 0);
DDS::TopicDescription_var topic =
DDS::TopicDescription::_duplicate(real_topic);
if (!topic) {
Nan::ThrowError("subscribe: couldn't create Topic");
fci.GetReturnValue().SetUndefined();
return;
}
Local<Object> qos_js;
if (fci[2]->IsObject()) {
qos_js = fci[2]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
}
Nan::MaybeLocal<String> cft_str = Nan::New<String>("ContentFilteredTopic");
const Local<String> cft_lstr = cft_str.ToLocalChecked();
if (*qos_js && Nan::Has(qos_js, cft_lstr).ToChecked()) {
const Local<Value> cft_js_lv = Nan::Get(qos_js, cft_lstr).ToLocalChecked();
if (cft_js_lv->IsObject()) {
const Local<Object> cft_js = cft_js_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
Nan::MaybeLocal<String> fe_str = Nan::New<String>("filter_expression"),
ep_str = Nan::New<String>("expression_parameters");
const Local<String> fe_lstr = fe_str.ToLocalChecked();
if (!Nan::Has(cft_js, fe_lstr).ToChecked()) {
Nan::ThrowError("subscribe: filter_expression is required in "
"ContentFilteredTopic.");
fci.GetReturnValue().SetUndefined();
return;
}
const Nan::Utf8String filt(Nan::Get(cft_js, fe_lstr).ToLocalChecked());
DDS::StringSeq params;
const Local<String> ep_lstr = ep_str.ToLocalChecked();
if (Nan::Has(cft_js, ep_lstr).ToChecked()) {
const Local<Value> params_js_lv = Nan::Get(cft_js, ep_lstr).ToLocalChecked();
if (params_js_lv->IsObject()) {
const Local<Object> params_js = params_js_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
const Nan::Maybe<uint32_t> len =
Nan::To<uint32_t>(Nan::Get(params_js, Nan::New<String>("length").ToLocalChecked()).ToLocalChecked());
params.length(len.FromMaybe(0));
for (uint32_t i = 0; i < params.length(); ++i) {
const Nan::Utf8String pstr(Nan::Get(params_js, i).ToLocalChecked());
params[i] = *pstr;
}
}
}
topic = dp->create_contentfilteredtopic(cft_name.c_str(), real_topic,
*filt, params);
increment(cft_name);
}
}
DDS::SubscriberQos sub_qos;
dp->get_default_subscriber_qos(sub_qos);
Nan::MaybeLocal<String> subqos_str = Nan::New<String>("SubscriberQos");
const Local<String> subqos_lstr = subqos_str.ToLocalChecked();
if (*qos_js && Nan::Has(qos_js, subqos_lstr).ToChecked()) {
const Local<Value> subqos_lv = Nan::Get(qos_js, subqos_lstr).ToLocalChecked();
if (subqos_lv->IsObject()) {
try {
NodeValueReader nvr(subqos_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
if (!OpenDDS::DCPS::vread(nvr, sub_qos)) {
throw std::runtime_error("subscribe: Unable to convert subscriber qos policy");
}
} catch (const std::runtime_error& e) {
Nan::ThrowError(e.what());
fci.GetReturnValue().SetUndefined();
return;
}
}
}
const DDS::Subscriber_var sub = dp->create_subscriber(sub_qos, 0, 0);
if (!sub) {
Nan::ThrowError("subscribe: couldn't create Subscriber");
fci.GetReturnValue().SetUndefined();
return;
}
Local<Value> cb = fci[fci.Length() - 1];
NodeDRListener* const ndrl = new NodeDRListener(dp, cb.As<Function>());
const DDS::DataReaderListener_var listen(ndrl);
DDS::DataReaderQos dr_qos;
sub->get_default_datareader_qos(dr_qos);
Nan::MaybeLocal<String> drqos_str = Nan::New<String>("DataReaderQos");
const Local<String> drqos_lstr = drqos_str.ToLocalChecked();
if (*qos_js && Nan::Has(qos_js, drqos_lstr).ToChecked()) {
Local<Value> drqos_lv = Nan::Get(qos_js, drqos_lstr).ToLocalChecked();
if (drqos_lv->IsObject()) {
try {
NodeValueReader nvr(drqos_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
if (!OpenDDS::DCPS::vread(nvr, dr_qos)) {
throw std::runtime_error("subscribe: Unable to convert datareader qos policy");
}
} catch (const std::runtime_error& e) {
Nan::ThrowError(e.what());
fci.GetReturnValue().SetUndefined();
return;
}
}
}
DDS::DataReader_var dr = sub->create_datareader(topic, dr_qos, listen,
DDS::DATA_AVAILABLE_STATUS);
if (!dr) {
Nan::ThrowError("subscribe: couldn't create DataReader");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<ObjectTemplate> ot = Nan::New<ObjectTemplate>();
ot->SetInternalFieldCount(1);
const Local<Object> obj = ot->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(obj, 0, dr._retn());
ndrl->set_javascript_datareader(obj);
fci.GetReturnValue().Set(obj);
}
void unsubscribe(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("unsubscribe: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
} else if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("unsubscribe: Argument must be of object type");
fci.GetReturnValue().SetUndefined();
return;
}
// Get the NodeDRListener
const Local<Object> dr_js = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
void* const dr_obj = Nan::GetInternalFieldPointer(dr_js, 0);
DDS::DataReader* dr = static_cast<DDS::DataReader*>(dr_obj);
const DDS::DataReaderListener_var drl = dr->get_listener();
NodeDRListener* const ndrl = dynamic_cast<NodeDRListener*>(drl.in());
ndrl->unsubscribe();
fci.GetReturnValue().SetUndefined();
}
void create_datawriter(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 3) {
Nan::ThrowTypeError("create_datawriter: At least 3 arguments required");
fci.GetReturnValue().SetUndefined();
return;
}
void* const internal = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DomainParticipant* const dp =
static_cast<DDS::DomainParticipant*>(internal);
const Nan::Utf8String topic_name(fci[0]);
const Nan::Utf8String topic_type(fci[1]);
OpenDDS::DCPS::TypeSupport* ts =
Registered_Data_Types->lookup(dp, *topic_type);
if (!ts) {
ts = Registered_Data_Types->lookup(0, *topic_type);
if (!ts) {
Nan::ThrowError("create_datawriter: TypeSupport was not registered");
fci.GetReturnValue().SetUndefined();
return;
}
Registered_Data_Types->register_type(dp, *topic_type, ts);
}
DDS::Topic_var topic =
dp->create_topic(*topic_name, *topic_type, TOPIC_QOS_DEFAULT, 0, 0);
Local<Object> qos_js;
if (fci[2]->IsObject()) {
qos_js = fci[2]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
}
DDS::PublisherQos pub_qos;
dp->get_default_publisher_qos(pub_qos);
Nan::MaybeLocal<String> pubqos_str = Nan::New<String>("PublisherQos");
const Local<String> pubqos_lstr = pubqos_str.ToLocalChecked();
if (*qos_js && Nan::Has(qos_js, pubqos_lstr).ToChecked()) {
const Local<Value> pubqos_lv = Nan::Get(qos_js, pubqos_lstr).ToLocalChecked();
if (pubqos_lv->IsObject()) {
try {
NodeValueReader nvr(pubqos_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
if (!OpenDDS::DCPS::vread(nvr, pub_qos)) {
throw std::runtime_error("create_datawriter: Unable to convert publisher qos policy");
}
} catch (const std::runtime_error& e) {
Nan::ThrowError(e.what());
fci.GetReturnValue().SetUndefined();
return;
}
}
}
const DDS::Publisher_var pub = dp->create_publisher(pub_qos, 0, 0);
if (!pub) {
Nan::ThrowError("create_datawriter: couldn't create Publisher");
fci.GetReturnValue().SetUndefined();
return;
}
DDS::DataWriterQos dw_qos;
pub->get_default_datawriter_qos(dw_qos);
const Local<String> dwqos_lstr = Nan::New<String>("DataWriterQos").ToLocalChecked();
if (*qos_js && Nan::Has(qos_js, dwqos_lstr).ToChecked()) {
const Local<Value> dwqos_lv = Nan::Get(qos_js, dwqos_lstr).ToLocalChecked();
if (dwqos_lv->IsObject()) {
try {
NodeValueReader nvr(dwqos_lv->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
if (!OpenDDS::DCPS::vread(nvr, dw_qos)) {
throw std::runtime_error("create_datawriter: Unable to convert datawriter qos policy");
}
} catch (const std::runtime_error& e) {
Nan::ThrowError(e.what());
fci.GetReturnValue().SetUndefined();
return;
}
}
}
DDS::DataWriter_var dw = pub->create_datawriter(topic, dw_qos, 0, 0);
if (!dw) {
Nan::ThrowError("create_datawriter: couldn't create DataWriter");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<ObjectTemplate> ot = Nan::New<ObjectTemplate>();
ot->SetInternalFieldCount(2);
Nan::SetMethod(ot, "register_instance", register_instance);
Nan::SetMethod(ot, "write", write);
Nan::SetMethod(ot, "wait_for_acknowledgments", wait_for_acknowledgments);
Nan::SetMethod(ot, "unregister_instance", unregister_instance);
Nan::SetMethod(ot, "dispose", dispose);
const Local<Object> obj = ot->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(obj, 0, dw._retn());
Nan::SetInternalFieldPointer(obj, 1, ts);
fci.GetReturnValue().Set(obj);
}
void register_instance(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("register_instance: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
}
if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("register_instance: Argument must be an object");
fci.GetReturnValue().SetUndefined();
return;
}
void* const dw_i = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DataWriter* const dw = static_cast<DDS::DataWriter*>(dw_i);
const void* const ts_i = Nan::GetInternalFieldPointer(fci.This(), 1);
const OpenDDS::DCPS::TypeSupport* const ts = static_cast<const OpenDDS::DCPS::TypeSupport*>(ts_i);
const OpenDDS::DCPS::ValueDispatcher* const vd = dynamic_cast<const OpenDDS::DCPS::ValueDispatcher* const>(ts);
if (!vd) {
Nan::ThrowError("register_instance: invalid typesupport attached to writer");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> sample_obj = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
if (sample_obj.IsEmpty()) {
Nan::ThrowError("register_instance: invalid sample passed to writer");
fci.GetReturnValue().SetUndefined();
return;
}
void* sample_vp = vd->new_value();
NodeValueReader nvr(sample_obj);
bool read_result = vd->read(nvr, sample_vp);
if (!read_result) {
vd->delete_value(sample_vp);
Nan::ThrowError("register_instance: couldn't convert instance");
fci.GetReturnValue().SetUndefined();
return;
}
DDS::InstanceHandle_t handle = vd->register_instance_helper(dw, sample_vp);
vd->delete_value(sample_vp);
if (handle == DDS::HANDLE_NIL) {
Nan::ThrowError("register_instance: couldn't register instance");
fci.GetReturnValue().SetUndefined();
return;
}
fci.GetReturnValue().Set(handle);
}
void write(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("write: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
}
if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("write: Sample argument must be an object");
fci.GetReturnValue().SetUndefined();
return;
}
void* const dw_i = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DataWriter* const dw = static_cast<DDS::DataWriter*>(dw_i);
const void* const ts_i = Nan::GetInternalFieldPointer(fci.This(), 1);
const OpenDDS::DCPS::TypeSupport* const ts = static_cast<const OpenDDS::DCPS::TypeSupport*>(ts_i);
const OpenDDS::DCPS::ValueDispatcher* const vd = dynamic_cast<const OpenDDS::DCPS::ValueDispatcher* const>(ts);
if (!vd) {
Nan::ThrowError("write: invalid typesupport attached to writer");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> sample_obj = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
DDS::InstanceHandle_t handle = DDS::HANDLE_NIL;
if (fci.Length() > 1) {
if (!fci[1]->IsNumber()) {
Nan::ThrowTypeError("write: Instance handle argument must be an Integer");
fci.GetReturnValue().SetUndefined();
return;
}
handle = fci[1]->ToInteger(Nan::GetCurrentContext()).ToLocalChecked()->Value();
}
void* sample_vp = vd->new_value();
NodeValueReader nvr(sample_obj);
bool read_result = vd->read(nvr, sample_vp);
if (!read_result) {
vd->delete_value(sample_vp);
Nan::ThrowError("write: couldn't convert instance");
fci.GetReturnValue().SetUndefined();
return;
}
DDS::ReturnCode_t return_code = vd->write_helper(dw, sample_vp, handle);
vd->delete_value(sample_vp);
if (return_code != DDS::RETCODE_OK) {
Nan::ThrowError("write: couldn't write sample");
fci.GetReturnValue().SetUndefined();
return;
}
fci.GetReturnValue().Set(return_code);
}
void wait_for_acknowledgments(const Nan::FunctionCallbackInfo<Value>& fci)
{
void* const dw_i = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DataWriter* const dw = static_cast<DDS::DataWriter*>(dw_i);
// TODO Handle non-infinite durations
const DDS::Duration_t delay = {DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC};
const DDS::ReturnCode_t return_code = dw->wait_for_acknowledgments(delay);
if (return_code != DDS::RETCODE_OK) {
Nan::ThrowError("wait_for_acknowledgments: couldn't wait for acknowledgments");
fci.GetReturnValue().SetUndefined();
return;
}
fci.GetReturnValue().Set(return_code);
}
void unregister_instance(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("unregister_instance: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
}
if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("unregister_instance: Sample argument must be an object");
fci.GetReturnValue().SetUndefined();
return;
}
void* const dw_i = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DataWriter* const dw = static_cast<DDS::DataWriter*>(dw_i);
const void* const ts_i = Nan::GetInternalFieldPointer(fci.This(), 1);
const OpenDDS::DCPS::TypeSupport* const ts = static_cast<const OpenDDS::DCPS::TypeSupport*>(ts_i);
const OpenDDS::DCPS::ValueDispatcher* const vd = dynamic_cast<const OpenDDS::DCPS::ValueDispatcher* const>(ts);
if (!vd) {
Nan::ThrowError("unregister_instance: invalid typesupport attached to writer");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> sample_obj = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
DDS::InstanceHandle_t handle = DDS::HANDLE_NIL;
if (fci.Length() > 1) {
if (!fci[1]->IsNumber()) {
Nan::ThrowTypeError("unregister_instance: Instance handle argument must be an Integer");
fci.GetReturnValue().SetUndefined();
return;
}
handle = fci[1]->ToInteger(Nan::GetCurrentContext()).ToLocalChecked()->Value();
}
void* sample_vp = vd->new_value();
NodeValueReader nvr(sample_obj);
bool read_result = vd->read(nvr, sample_vp);
if (!read_result) {
vd->delete_value(sample_vp);
Nan::ThrowError("unregister_instance: couldn't convert instance");
fci.GetReturnValue().SetUndefined();
return;
}
DDS::ReturnCode_t return_code = vd->unregister_instance_helper(dw, sample_vp, handle);
vd->delete_value(sample_vp);
if (return_code != DDS::RETCODE_OK) {
Nan::ThrowError("unregister_instance: couldn't unregister instance");
fci.GetReturnValue().SetUndefined();
return;
}
fci.GetReturnValue().Set(return_code);
}
void dispose(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("dispose: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
}
if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("dispose: Sample argument must be an object");
fci.GetReturnValue().SetUndefined();
return;
}
void* const dw_i = Nan::GetInternalFieldPointer(fci.This(), 0);
DDS::DataWriter* const dw = static_cast<DDS::DataWriter*>(dw_i);
const void* const ts_i = Nan::GetInternalFieldPointer(fci.This(), 1);
const OpenDDS::DCPS::TypeSupport* const ts = static_cast<const OpenDDS::DCPS::TypeSupport*>(ts_i);
const OpenDDS::DCPS::ValueDispatcher* const vd = dynamic_cast<const OpenDDS::DCPS::ValueDispatcher* const>(ts);
if (!vd) {
Nan::ThrowError("dispose: invalid typesupport attached to writer");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> sample_obj = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
DDS::InstanceHandle_t handle = DDS::HANDLE_NIL;
if (fci.Length() > 1) {
if (!fci[1]->IsNumber()) {
Nan::ThrowTypeError("dispose: Instance handle argument must be an Integer");
fci.GetReturnValue().SetUndefined();
return;
}
handle = fci[1]->ToInteger(Nan::GetCurrentContext()).ToLocalChecked()->Value();
}
void* sample_vp = vd->new_value();
NodeValueReader nvr(sample_obj);
bool read_result = vd->read(nvr, sample_vp);
if (!read_result) {
vd->delete_value(sample_vp);
Nan::ThrowError("dispose: couldn't convert instance");
fci.GetReturnValue().SetUndefined();
return;
}
DDS::ReturnCode_t return_code = vd->dispose_helper(dw, sample_vp, handle);
vd->delete_value(sample_vp);
if (return_code != DDS::RETCODE_OK) {
Nan::ThrowError("dispose: couldn't dispose instance");
fci.GetReturnValue().SetUndefined();
return;
}
fci.GetReturnValue().Set(return_code);
}
void finalize(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() < 1) {
Nan::ThrowTypeError("finalize: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
} else if (!fci[0]->IsObject()) {
Nan::ThrowTypeError("finalize: Argument must be of object type");
fci.GetReturnValue().SetUndefined();
return;
}
const Local<Object> dpf_js = fci[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
void* const internal = Nan::GetInternalFieldPointer(dpf_js, 0);
const DDS::DomainParticipantFactory_var dpf =
static_cast<DDS::DomainParticipantFactory*>(internal);
Nan::SetInternalFieldPointer(dpf_js, 0, 0);
for (size_t i = 0; i < participants_.size(); ++i) {
DDS::DomainParticipant_var& part = participants_[i];
part->delete_contained_entities();
dpf->delete_participant(part);
}
participants_.clear();
TheServiceParticipant->shutdown();
ACE::fini();
fci.GetReturnValue().SetUndefined();
}
void load(const Nan::FunctionCallbackInfo<Value>& fci)
{
if (fci.Length() == 0 || !fci[0]->IsString()) {
Nan::ThrowTypeError("load: 1 argument required");
fci.GetReturnValue().SetUndefined();
return;
}
const Nan::Utf8String lib_js(fci[0]);
const ACE_TString lib(ACE_TEXT_CHAR_TO_TCHAR(*lib_js));
const bool ok =
ACE_DLL_Manager::instance()->open_dll(lib.c_str(),
ACE_DEFAULT_SHLIB_MODE, 0);
fci.GetReturnValue().Set(ok);
}
NAN_MODULE_INIT(init_node_opendds)
{
Nan::SetMethod(target, "initialize", initialize);
Nan::SetMethod(target, "finalize", finalize);
Nan::SetMethod(target, "load", load);
}
}
#ifdef ACE_LINUX
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
NODE_MODULE(node_opendds, init_node_opendds);
#ifdef ACE_LINUX
#pragma GCC diagnostic pop
#endif