-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
996 lines (802 loc) · 37.4 KB
/
main.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
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/* Copyright (C) neoliant.com - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary
* Written by neoliant - [email protected], February 2021
*/
#include "DataReaderListenerImpl.h"
#include "MessageEventDataReaderListenerImpl.h"
#include "Printer.h"
// Generated at build time
#include <SimpleDataTypeTypeSupportImpl.h>
#include <MessageEventTypeSupportImpl.h>
// Needed to use the libopcddsservices library
#include "libopcddsservices/headers/Client.h"
#include "libopcddsservices/headers/ClientParams.h"
#include "libopcddsservices/headers/LicenseManager.h"
// Logging provided by ACE
#include <ace/Log_Msg.h>
// Boost is used to manage asynchronous calls, using futures
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread/future.hpp>
#include <exception>
#include <stdlib.h>
#include <thread>
// OpenDDS provides the DDS infrastructure
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/WaitSet.h>
#include <dds/DdsDcpsSubscriptionC.h>
#include <dds/DdsDcpsCoreC.h>
using namespace boost;
using namespace std;
using namespace OpenDDS::DCPS;
using namespace OMG::DDSOPCUA::OPCUA2DDS;
std::mutex mtx;
#ifndef UNUSED
#define UNUSED( expr ) do { ( void )( expr ); } while ( 0 )
#endif
bool running = true;
static void stopHandler ( int sign )
{
UNUSED ( sign );
cout << "Received Ctrl-C" << endl;
running = false;
}
#ifdef OPENDDS_SECURITY
#include <dds/DCPS/security/framework/Properties.h>
const char auth_ca_file_from_tests[] = "certs/identity/identity_ca_cert.pem";
const char perm_ca_file_from_tests[] = "certs/permissions/permissions_ca_cert.pem";
const char id_cert_file_from_tests[] = "certs/identity/ddstestsubscriber_cert.pem";
const char id_key_file_from_tests[] = "certs/identity/ddstestsubscriber_private_key.pem";
const char * home = getenv( "HOME" );
string governance_file_s = string( "file:" ) + home + "/security/DDS/governance_signed.p7s";
const char * governance_file = governance_file_s.c_str();
string permissions_file_s = string( "file:" ) + home + "/security/DDS/permissions_ddstestsubscriber_signed.p7s";
const char * permissions_file = permissions_file_s.c_str();
void append ( DDS::PropertySeq& props, const char* name, const char* value, bool propagate = false )
{
const DDS::Property_t prop = {name, value, propagate};
const unsigned int len = props.length();
props.length ( len + 1 );
props[len] = prop;
}
void setSecureEnvironment ( DDS::PropertySeq & props )
{
const char * home = getenv( "HOME" );
OPENDDS_STRING path_to_tests = string( "file:" ) + home + "/security/DDS/";
const OPENDDS_STRING auth_ca_file = path_to_tests + auth_ca_file_from_tests;
const OPENDDS_STRING perm_ca_file = path_to_tests + perm_ca_file_from_tests;
const OPENDDS_STRING id_cert_file = path_to_tests + id_cert_file_from_tests;
const OPENDDS_STRING id_key_file = path_to_tests + id_key_file_from_tests;
if ( TheServiceParticipant->get_security() ) {
append ( props, DDS::Security::Properties::AuthIdentityCA, auth_ca_file.c_str() );
append ( props, DDS::Security::Properties::AuthIdentityCertificate, id_cert_file.c_str() );
append ( props, DDS::Security::Properties::AuthPrivateKey, id_key_file.c_str() );
append ( props, DDS::Security::Properties::AccessPermissionsCA, perm_ca_file.c_str() );
append ( props, DDS::Security::Properties::AccessGovernance, governance_file );
append ( props, DDS::Security::Properties::AccessPermissions, permissions_file );
}
}
#endif
vector<std::chrono::duration<long, std::ratio<1, 1000000>>::rep> methodLatencies;
void test_synchronous_method ( METHOD::Client & method_client )
{
try {
ResponseHeader respHeader;
string serverId = "TestServer";
vector<METHOD::CallMethodResult> results;
vector<DiagnosticInfo> diagnosticInfos;
vector<METHOD::CallMethodRequest> methodsToCall;
METHOD::CallMethodRequest request;
// The server itself exposes the method
NodeId objectNodeId;
objectNodeId.identifier_type.numeric_id ( 85 );
objectNodeId.namespace_index = 0;
request.object_id = objectNodeId;
// Method that adds delta to each value of an array of 5 Int32
NodeId methodNodeId;
methodNodeId.identifier_type.string_id ( "IncInt32ArrayValues" );
methodNodeId.namespace_index = 1;
request.method_id = methodNodeId;
int array[ 5 ] = { 0, 1, 2, 3, 4 };
CORBA::Long delta = 3;
BaseDataTypeList baseDataTypeList;
baseDataTypeList.length ( 2 );
Variant inputArray;
UInt32Seq dim;
dim.length ( 1 );
dim[0] = 5;
inputArray.array_dimensions = dim;
VariantValueSeq seq;
seq.length ( 5 );
for ( int i = 0; i < 5; i++ ) {
VariantValue variantValue;
variantValue.int32_value ( array[i] );
seq[i] = variantValue;
}
inputArray.value = seq;
Variant inputDelta;
UInt32Seq dimDelta;
dimDelta.length ( 1 );
dimDelta[0] = 1;
inputDelta.array_dimensions = dimDelta;
VariantValueSeq alone;
alone.length ( 1 );
VariantValue variantValue;
variantValue.int32_value ( delta );
alone[0] = variantValue;
inputDelta.value = alone;
baseDataTypeList[0] = inputArray;
baseDataTypeList[1] = inputDelta;
request.input_arguments = baseDataTypeList;
methodsToCall.push_back ( request );
method_client.call ( respHeader, serverId, results, diagnosticInfos, methodsToCall );
if ( respHeader.service_result == DDS::RETCODE_TIMEOUT ) {
cout << "main() : Timeout reached while performing synchronous call" << endl;
}
for ( auto result : results ) {
if ( result.status_code != 0 ) {
cout << "status_code != 0 while calling the OPC Method" << endl;
} else {
for ( int j = 0; j < result.output_arguments.length(); j++ ) {
cout << "Back in test_synchronous, let's display the result : ";
Variant variant = result.output_arguments[ j ];
Printer printer;
cout << printer.PrintVariant ( variant ) << endl;
}
}
}
} catch ( std::exception & ex ) {
cout << "Exception in METHOD::Client::call() : " << ex.what() << endl;
} catch ( ... ) {
cout << "Unknown exception in METHOD::Client::call()" << endl;
}
}
int kounter = 0;
void test_asynchronous_method ( METHOD::Client & method_client )
{
try {
string serverId = "TestServer";
vector<METHOD::CallMethodRequest> methodsToCall;
METHOD::CallMethodRequest request;
// The server itself exposes the method
NodeId objectNodeId;
objectNodeId.identifier_type.numeric_id ( 85 );
objectNodeId.namespace_index = 0;
request.object_id = objectNodeId;
// Method that adds delta to each value of an array of 5 Int32
NodeId methodNodeId;
methodNodeId.identifier_type.string_id ( "IncInt32ArrayValues" );
methodNodeId.namespace_index = 1;
request.method_id = methodNodeId;
int array[5] = { 10, 11, 12, 13, 14 };
CORBA::Long delta = 5;
request.input_arguments.length ( 2 );
Variant inputArray;
UInt32Seq dim;
dim.length ( 1 );
dim[0] = 5;
inputArray.array_dimensions = dim;
VariantValueSeq seq;
seq.length ( 5 );
for ( int i = 0; i < 5; i++ ) {
seq[i].int32_value ( array[i] );
}
inputArray.value = seq;
Variant inputDelta;
UInt32Seq dimDelta;
dimDelta.length ( 1 );
dimDelta[0] = 1;
inputDelta.array_dimensions = dimDelta;
VariantValueSeq alone;
alone.length ( 1 );
alone[0].int32_value ( delta );
inputDelta.value = alone;
request.input_arguments[0] = inputArray;
request.input_arguments[1] = inputDelta;
methodsToCall.push_back ( request );
for ( int i = 0; i < 3; i++ ) {
method_client
.call_async ( serverId, methodsToCall )
.then ( [] ( boost::future<METHOD::Method_call_Out> && method_fut ) {
kounter++;
auto results = method_fut.get();
if ( results.results.length() > 0 ) {
for ( int j = 0; j < results.results.length(); j++ ) {
if ( results.results[j].status_code != 0 ) {
cout << "status_code != 0 while calling the OPC Method" << endl;
} else {
for ( int k = 0; k < results.results[ j ].output_arguments.length(); k++ ) {
mtx.lock();
cout << "Back in test_asynchronous, let's display the result : ";
Variant variant = results.results[ j ].output_arguments[ k ];
Printer printer;
cout << printer.PrintVariant ( variant ) << endl;
mtx.unlock();
}
}
}
} else
cerr << "no data in method_fut.get()" << endl;
} );
}
} catch ( std::exception & ex ) {
cout << "Exception in test_asynchronous_method() : " << ex.what() << endl;
} catch ( ... ) {
cout << "Unknown exception in test_asynchronous_method()" << endl;
}
}
METHOD::Client * test_synchronous_method_timeout ( DDS::DomainParticipant_var participant )
{
RPC::ClientParams clientParams;
// 100us timeout
DDS::Duration_t maxWait;
maxWait.sec = 0;
maxWait.nanosec = 100000;
clientParams.SetTimeoutForSynchronousCalls ( maxWait );
clientParams.domain_participant ( participant );
auto client = new METHOD::Client ( clientParams );
test_synchronous_method ( *client );
return client;
}
ATTRIBUTE::Client * test_asynchronous_attribute_read ( DDS::DomainParticipant_var participant )
{
try {
RPC::ClientParams clientParams;
clientParams.domain_participant ( participant );
auto client = new ATTRIBUTE::Client ( clientParams );
//Let's prepare the request
ResponseHeader responseHeader;
string serverId = "TestServer";
NodeId idOfNodeToRead;
idOfNodeToRead.identifier_type.string_id ( "Int64TestNode" );
idOfNodeToRead.namespace_index = 1;
ReadValueId readValueId;
readValueId.attribute_id = 13; //(UA_ATTRIBUTEID_VALUE)
readValueId.node_id = idOfNodeToRead;
vector<ReadValueId> nodesToRead;
nodesToRead.push_back ( readValueId );
// 2 minutes
Duration duration { 120000 };
auto ttr = TimestampsToReturn::BOTH_TIMESTAMPS_TO_RETURN;
client->
read_async ( serverId, duration, ttr, nodesToRead )
.then ( [] ( boost::future< ATTRIBUTE::Attribute_read_Out> && read_fut ) {
auto results = read_fut.get();
cout << "Back in test_asynchronous_attribute_read, let's display the results : ";
for ( int i = 0; i < results.results.length(); i++ ) {
cout << "Node index in request = " << i << ", Value = ";
Printer printer;
cout << printer.PrintVariant ( results.results[ i ].value ) << endl;
}
} );
cout << "Done with asynchronously sending request in main()" << endl;
std::this_thread::sleep_for ( std::chrono::seconds ( 2 ) );
return client;
} catch ( std::exception & ex ) {
cout << "Exception in test_asynchronous_read() : " << ex.what() << endl;
} catch ( ... ) {
cout << "Unknown exception in test_asynchronous_read()" << endl;
}
return nullptr;
}
ATTRIBUTE::Client * test_asynchronous_attribute_write ( DDS::DomainParticipant_var participant )
{
try {
RPC::ClientParams clientParams;
clientParams.domain_participant ( participant );
auto client = new ATTRIBUTE::Client ( clientParams );
//Let's prepare the request
ResponseHeader responseHeader;
string serverId = "TestServer";
NodeId idOfNodeToWrite;
idOfNodeToWrite.identifier_type.string_id ( "UInt16TestNode" );
idOfNodeToWrite.namespace_index = 1;
CORBA::UShort input = 22;
Variant inputVariant;
UInt32Seq dimDelta;
dimDelta.length ( 1 );
dimDelta[0] = 1;
inputVariant.array_dimensions = dimDelta;
VariantValueSeq alone;
alone.length ( 1 );
alone[0].short_value ( input );
inputVariant.value = alone;
ATTRIBUTE::WriteValue writeValue;
writeValue.attribute_id = 13; //(UA_ATTRIBUTEID_VALUE)
writeValue.node_id = idOfNodeToWrite;
writeValue.value.value = inputVariant;
vector<ATTRIBUTE::WriteValue> nodesToWrite;
nodesToWrite.push_back ( writeValue );
Printer printer;
cout << "Writing node : " << printer.PrintNodeId ( idOfNodeToWrite ) << endl;
client->
write_async ( serverId, nodesToWrite )
.then ( [] ( future<ATTRIBUTE::Attribute_write_Out> && write_fut ) {
auto results = write_fut.get();
cout << "Back in test_asynchronous_attribute_write, let's display the results : ";
for ( int i = 0; i < results.results.length(); i++ ) {
cout << "Status Code of writing node " << i << " : ";
if ( results.results[ i ] == 0 )
cout << "GOOD" << endl;
else
cout << "BAD" << endl;
}
} );
cout << "Done with asynchronously sending request in main()" << endl;
std::this_thread::sleep_for ( std::chrono::seconds ( 2 ) );
return client;
} catch ( std::exception & ex ) {
cout << "Exception in test_asynchronous_write() : " << ex.what() << endl;
} catch ( ... ) {
cout << "Unknown exception in test_asynchronous_write()" << endl;
}
return nullptr;
}
void test_synchronous_view_translate_browse_path_to_nodeid ( VIEW::Client * client )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
cout << "Translating BrowsePath to NodeId..." << endl;
vector<BrowsePath> browsePaths;
BrowsePath browsePath1, browsePath2;
NodeId startingNode1;
startingNode1.namespace_index = 0;
startingNode1.identifier_type.numeric_id ( 85 ); // Objects folder
browsePath1.starting_node = startingNode1;
RelativePath relativePath1;
relativePath1.elements.length ( 3 );
RelativePathElement element1, element2, element3;
NodeId NS0ID_ORGANIZES;
NS0ID_ORGANIZES.namespace_index = 0;
NS0ID_ORGANIZES.identifier_type.numeric_id ( 35 );
NodeId NS0ID_HASCOMPONENT;
NS0ID_HASCOMPONENT.namespace_index = 0;
NS0ID_HASCOMPONENT.identifier_type.numeric_id ( 47 );
element1.reference_type_id = NS0ID_ORGANIZES;
element2.reference_type_id = NS0ID_HASCOMPONENT;
element3.reference_type_id = NS0ID_HASCOMPONENT;
QualifiedName targetName1, targetName2, targetName3;
targetName1.namespace_index = 0;
targetName2.namespace_index = 0;
targetName3.namespace_index = 0;
targetName1.name = "Server";
targetName2.name = "ServerStatus";
targetName3.name = "State";
element1.include_subtypes = false;
element1.is_inverse = false;
element1.target_name = targetName1;
element2.include_subtypes = false;
element2.is_inverse = false;
element2.target_name = targetName2;
element3.include_subtypes = false;
element3.is_inverse = false;
element3.target_name = targetName3;
relativePath1.elements[0] = element1;
relativePath1.elements[1] = element2;
relativePath1.elements[2] = element3;
browsePath1.relative_path = relativePath1;
browsePaths.push_back ( browsePath1 );
vector<BrowsePathResult> results;
vector<DiagnosticInfo> diagnosticInfos;
client->translate_browse_paths_to_node_ids ( responseHeader, serverId, results, diagnosticInfos, browsePaths );
mtx.lock();
cout << "Back in test_synchronous_view_translate_browse_path_to_nodeid, service return code = " << responseHeader.service_result << "; let's display the results : " << endl;
cout << "results.size() = " << results.size() << "; ";
if ( results.size() > 0 ) {
for ( int i = 0; i < results.size(); i++ ) {
cout << "Result " << i << " status code = " << results[ i ].status_code << ", nodes size = " << results[ i ].targets.length() << " : ";
for ( int j = 0; j < results[ i ].targets.length(); j++ ) {
cout << results[ i ].targets[ j ].target_id.node_id.identifier_type.numeric_id() << " / ";
}
cout << endl;
}
}
mtx.unlock();
}
void test_synchronous_view_register_nodes ( VIEW::Client * client )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
cout << "Registering nodes..." << endl;
vector<NodeId> registeredNodes;
vector<NodeId> nodesToRegister;
NodeId id1, id2;
id1.namespace_index = 1;
id2.namespace_index = 1;
id1.identifier_type.string_id ( "ByteMatrixTestNode" );
id2.identifier_type.string_id ( "ExpandedNodeIdArrayTestNode" );
nodesToRegister.push_back ( id1 );
nodesToRegister.push_back ( id2 );
client->register_nodes ( responseHeader, serverId, registeredNodes, nodesToRegister );
mtx.lock();
cout << "Back in test_synchronous_view_register_nodes, service return code = " << responseHeader.service_result << "; let's display the results : " << endl;
cout << "registeredNodes.size() = " << registeredNodes.size() << "; ";
if ( registeredNodes.size() > 0 ) {
for ( int i = 0; i < registeredNodes.size(); i++ ) {
cout << "ns = " << registeredNodes[ i ].namespace_index;
cout << "; s = " << registeredNodes[ i ].identifier_type.string_id() << " / ";
}
}
cout << endl;
mtx.unlock();
}
void test_synchronous_view_unregister_nodes ( VIEW::Client * client )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
cout << "Unregistering nodes..." << endl;
vector<NodeId> nodesToUnregister;
NodeId id1, id2;
id1.namespace_index = 1;
id2.namespace_index = 1;
id1.identifier_type.string_id ( "ByteMatrixTestNode" );
id2.identifier_type.string_id ( "ExpandedNodeIdArrayTestNode" );
nodesToUnregister.push_back ( id1 );
nodesToUnregister.push_back ( id2 );
client->unregister_nodes ( responseHeader, serverId, nodesToUnregister );
mtx.lock();
cout << "Back in test_synchronous_view_unregister_nodes, service return code : " << responseHeader.service_result << endl;
mtx.unlock();
}
VIEW::Client * test_synchronous_view_browse_next ( VIEW::Client * client )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
cout << "Browsing next in Server folder..." << endl;
// Should do browse before browse_next.
ViewDescription view;
view.view_id.namespace_index = 0;
view.view_id.identifier_type.numeric_id ( 0 );
Counter requested_max_references_per_node {3};
vector<BrowseDescription> browse_descriptions;
BrowseDescription browse_description;
NodeId idOfNodeToBrowse;
idOfNodeToBrowse.identifier_type.numeric_id ( 2253 ); // Server Folder
idOfNodeToBrowse.namespace_index = 0;
browse_description.node_id = idOfNodeToBrowse;
browse_description.result_mask = 63; // UA_BROWSERESULTMASK_ALL, return everything
browse_description.browse_direction = BrowseDirection::FORWARD_BROWSE_DIRECTION; // UA_BROWSEDIRECTION_BOTH
browse_description.include_subtypes = true;
browse_description.node_class_mask = 0;
browse_description.reference_type_id.namespace_index = 0;
browse_description.reference_type_id.identifier_type.numeric_id ( 0 );
browse_descriptions.push_back ( browse_description );
vector<BrowseResult> browseResults;
vector<DiagnosticInfo> browse_diagnostic_infos;
client->browse ( responseHeader, serverId, browseResults, browse_diagnostic_infos, view, requested_max_references_per_node, browse_descriptions );
// Then we can browse_next
if ( browseResults.size() == 0 ) {
cout << "second browse did not work" << endl;
return client;
}
vector<ContinuationPoint> continuationPoints;
ContinuationPoint continuationPoint = browseResults[ 0 ].continuation_point;
continuationPoints.push_back ( continuationPoint );
cout << "\tCP: ";
for ( int i = 0; i < continuationPoint.length(); i++ ) {
cout << continuationPoint[ i ];
}
cout << endl;
bool releaseContinuationPoints = false;
vector<BrowseResult> browseNextResults;
vector<DiagnosticInfo> browse_next_diagnostic_infos;
// cout << "Browse OK, continuation point : " << browseResults[ 0 ].continuation_point.get_buffer() << endl;
//sleep(1);
client->browse_next ( responseHeader, serverId, browseNextResults, browse_next_diagnostic_infos, releaseContinuationPoints, continuationPoints );
mtx.lock();
cout << "Back in test_synchronous_view_browse_next, let's display the results : " << endl;
cout << "resultSize = " << browseNextResults.size() << endl;
if ( browseNextResults.size() > 0 ) {
for ( int i = 0; i < browseNextResults.size(); i++ ) {
if ( browseNextResults[ 0 ].references.length() == 0 )
cout << "problem with the result of browse_next request" << endl;
cout << "referencesSize = " << browseNextResults[ 0 ].references.length() << endl;
for ( int j = 0; j < browseNextResults[ i ].references.length(); j++ ) {
if ( browseNextResults[ i ].references[ j ].node_id.node_id.identifier_type._d() == NodeIdentifierKind::NUMERIC_NODE_ID ) {
cout << "Namespace: " << browseNextResults[ i ].references[ j ].node_id.node_id.namespace_index << ", nodeId: " << browseNextResults[ i ].references[ j ].node_id.node_id.identifier_type.numeric_id();
} else if ( browseNextResults[ i ].references[ j ].node_id.node_id.identifier_type._d() == NodeIdentifierKind::STRING_NODE_ID ) {
cout << "Namespace: " << browseNextResults[ i ].references[ j ].node_id.node_id.namespace_index << ", nodeId: " << browseNextResults[ i ].references[ j ].node_id.node_id.identifier_type.string_id();
}
cout << ", browseName: " << browseNextResults[ i ].references[ j ].browse_name.name;
cout << ", displayName: " << browseNextResults[ i ].references[ j ].display_name.text << endl;
}
}
}
mtx.unlock();
return client;
}
void test_synchronous_view_browse ( VIEW::Client * client )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
cout << "Browsing nodes in objects folder:" << endl;
ViewDescription view;
view.view_id.namespace_index = 0;
view.view_id.identifier_type.numeric_id ( 0 );
Counter requested_max_references_per_node {0};
// TODO : find a way to set defaults for BrowseDescription
vector<BrowseDescription> browse_descriptions;
BrowseDescription browse_description;
NodeId idOfNodeToBrowse;
idOfNodeToBrowse.identifier_type.numeric_id ( 85 ); // Objects Folder
idOfNodeToBrowse.namespace_index = 0;
browse_description.node_id = idOfNodeToBrowse;
browse_description.result_mask = 63; // UA_BROWSERESULTMASK_ALL, return everything
browse_description.browse_direction = BrowseDirection::BOTH_BROWSE_DIRECTION; // UA_BROWSEDIRECTION_BOTH
browse_description.include_subtypes = true;
browse_description.node_class_mask = 0;
browse_description.reference_type_id.namespace_index = 0;
browse_description.reference_type_id.identifier_type.numeric_id ( 0 );
browse_descriptions.push_back ( browse_description );
vector<BrowseResult> results;
vector<DiagnosticInfo> diagnostic_infos;
client->browse ( responseHeader, serverId, results, diagnostic_infos, view, requested_max_references_per_node, browse_descriptions );
mtx.lock();
cout << "Back in test_synchronous_view_browse, let's display the results : " << endl;
cout << "resultSize = " << results.size() << endl;
if ( results.size() > 0 ) {
cout << "referencesSize = " << results[ 0 ].references.length() << endl;
for ( int i = 0; i < results.size(); i++ ) {
for ( int j = 0; j < results[ i ].references.length(); j++ ) {
if ( results[ i ].references[ j ].node_id.node_id.identifier_type._d() == NodeIdentifierKind::NUMERIC_NODE_ID ) {
cout << "Namespace: " << results[ i ].references[ j ].node_id.node_id.namespace_index << ", nodeId: " << results[ i ].references[ j ].node_id.node_id.identifier_type.numeric_id();
} else if ( results[ i ].references[ j ].node_id.node_id.identifier_type._d() == NodeIdentifierKind::STRING_NODE_ID ) {
cout << "Namespace: " << results[ i ].references[ j ].node_id.node_id.namespace_index << ", nodeId: " << results[ i ].references[ j ].node_id.node_id.identifier_type.string_id();
}
cout << ", browseName: " << results[ i ].references[ j ].browse_name.name;
cout << ", displayName: " << results[ i ].references[ j ].display_name.text << endl;
}
//cout << "\tContinuation Point = " << results[ i ].continuation_point.get_buffer() << endl;
}
}
mtx.unlock();
}
ATTRIBUTE::Client * test_synchronous_attribute_read ( DDS::DomainParticipant_var participant )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
RPC::ClientParams clientParams;
clientParams.domain_participant ( participant );
auto client = new ATTRIBUTE::Client ( clientParams );
NodeId idOfNodeToRead;
idOfNodeToRead.identifier_type.string_id ( "UInt64TestNode" );
idOfNodeToRead.namespace_index = 1;
ReadValueId readValueId;
readValueId.attribute_id = 13; //(UA_ATTRIBUTEID_VALUE)
readValueId.node_id = idOfNodeToRead;
vector<ReadValueId> nodesToRead;
nodesToRead.push_back ( readValueId );
// 2 minutes
Duration maxAge { 120000 };
auto ttr = TimestampsToReturn::BOTH_TIMESTAMPS_TO_RETURN;
vector<DataValue> results;
vector<DiagnosticInfo> diagnostic_infos;
client->read ( responseHeader, serverId, results, diagnostic_infos, maxAge, ttr, nodesToRead );
if ( responseHeader.service_result == DDS::RETCODE_TIMEOUT ) {
cout << "main() : Timeout reached while performing synchronous call" << endl;
}
cout << "Back in test_synchronous_attribute_read, let's display the result : " << endl;
Printer printer;
for ( int i = 0; i < results.size(); i++ ) {
cout << std::dec << "Node " << printer.PrintNodeId ( nodesToRead[ i ].node_id ) << ", DataValue : " << printer.PrintDataValue ( results[ i ] ) << endl;
}
return client;
}
ATTRIBUTE::Client * test_synchronous_attribute_write ( DDS::DomainParticipant_var participant )
{
ResponseHeader responseHeader;
string serverId = "TestServer";
RPC::ClientParams clientParams;
clientParams.domain_participant ( participant );
auto client = new ATTRIBUTE::Client ( clientParams );
NodeId idOfNodeToWrite;
idOfNodeToWrite.identifier_type.string_id ( "Int32TestNode" );
idOfNodeToWrite.namespace_index = 1;
CORBA::Long input = 187;
Variant inputVariant;
UInt32Seq dimDelta;
dimDelta.length ( 1 );
dimDelta[0] = 1;
inputVariant.array_dimensions = dimDelta;
VariantValueSeq alone;
alone.length ( 1 );
alone[0].int32_value ( input );
inputVariant.value = alone;
ATTRIBUTE::WriteValue writeValue;
writeValue.attribute_id = 13; //(UA_ATTRIBUTEID_VALUE)
writeValue.node_id = idOfNodeToWrite;
writeValue.value.value = inputVariant;
writeValue.value.server_pico_sec = 0;
writeValue.value.server_timestamp = 0;
// A améliorer
writeValue.value.source_timestamp = 0;
writeValue.value.source_pico_sec = 0;
writeValue.value.status = 0;
vector<ATTRIBUTE::WriteValue> nodesToWrite;
nodesToWrite.push_back ( writeValue );
vector<StatusCode> results;
vector<DiagnosticInfo> diagnostic_infos;
client->write ( responseHeader, serverId, results, diagnostic_infos, nodesToWrite );
if ( responseHeader.service_result == DDS::RETCODE_TIMEOUT ) {
cout << "main() : Timeout reached while performing synchronous call" << endl;
}
cout << "Back in test_synchronous_attribute_write, let's display the result : " << endl;
Printer printer;
for ( int i = 0; i < results.size(); i++ ) {
if ( results[i] != 0 ) {
cout << "Node to write : " << printer.PrintNodeId ( nodesToWrite[ i ].node_id ) << ", result = BAD" << endl;
} else if ( results[ i ] == 0 ) {
cout << "Node to write : " << printer.PrintNodeId ( nodesToWrite[ i ].node_id ) << ", result = GOOD" << endl;
}
}
return client;
}
int ACE_TMAIN ( int argc, ACE_TCHAR *argv[] )
{
ACE_LOG_MSG->priority_mask ( LM_DEBUG|LM_NOTICE|LM_WARNING|LM_ERROR|LM_CRITICAL|LM_ALERT|LM_EMERGENCY, ACE_Log_Msg::PROCESS );
signal ( SIGINT, stopHandler );
LicenseManager licenseManager;
licenseManager.ActivateTrial();
// licenseManager.SetLicenseKey ( "Key String" );
// licenseManager.ActivateLicense();
try {
// Initialize DomainParticipantFactory
DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs ( argc, argv );
DDS::DomainParticipantQos_var dp_qos = new DDS::DomainParticipantQos;
auto ret = dpf->get_default_participant_qos ( dp_qos );
if ( ret != DDS::RETCODE_OK ) {
cerr << "Domain participant factory could not get default participant qos..." << endl;
}
// Optional
// For use with RtpsRelay in docker, see Object Computing Inc. documentation
/*
DDS::PropertySeq & props = dp_qos->property.value;
const DDS::Property_t prop = { "OpenDDS.RtpsRelay.Groups", "Gateway", true };
const unsigned int len = props.length();
props.length ( len + 1 );
props[ len ] = prop;
*/
#ifdef OPENDDS_SECURITY
DDS::PropertySeq & props = dp_qos->property.value;
setSecureEnvironment ( props );
#endif
::DDS::DomainParticipant_var participant = dpf->create_participant ( 4,
dp_qos,
0,
DEFAULT_STATUS_MASK );
if ( !participant ) {
ACE_ERROR_RETURN ( ( LM_ERROR, ACE_TEXT ( "ERROR: %N:%l: main() - create_participant failed!\n" ) ), -1 );
}
// Register Types
SimpleDataTypeTypeSupport_var simpleDataType = new SimpleDataTypeTypeSupportImpl;
if ( simpleDataType->register_type ( participant, "SimpleDataType" ) != DDS::RETCODE_OK ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " register_type failed!\n" ) ), -1 );
}
MessageEventTypeSupport_var messageEventType = new MessageEventTypeSupportImpl;
if ( messageEventType->register_type ( participant, "MessageEvent" ) != DDS::RETCODE_OK ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " register_type failed!\n" ) ), -1 );
}
DDS::Topic_var constantsTopic = participant->create_topic( "GatewayConstantTest",
"SimpleDataType",
TOPIC_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !constantsTopic ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " create_topic failed!\n" ) ), -1 );
}
DDS::Topic_var scalarTopic = participant->create_topic ( "SimpleScalarTest",
"SimpleDataType",
TOPIC_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !scalarTopic ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " create_topic failed!\n" ) ), -1 );
}
DDS::Topic_var eventTopic = participant->create_topic ( "SimpleEventTest",
"MessageEvent",
TOPIC_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !eventTopic ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " find_topic failed!\n" ) ), -1 );
}
// Create Subscriber
DDS::Subscriber_var subscriber =
participant->create_subscriber ( SUBSCRIBER_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !subscriber ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " create_subscriber failed!\n" ) ), -1 );
}
// Create DataReaders
DDS::DataReaderListener_var constantsListener ( new SimpleDataTypeDataReaderListenerImpl );
DDS::DataReader_var constantsReader =
subscriber->create_datareader ( constantsTopic,
DATAREADER_QOS_DEFAULT,
constantsListener,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !constantsReader ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " create_datareader failed!\n" ) ), -1 );
}
DDS::DataReaderListener_var scalarListener ( new SimpleDataTypeDataReaderListenerImpl );
DDS::DataReader_var scalarReader =
subscriber->create_datareader ( scalarTopic,
DATAREADER_QOS_DEFAULT,
scalarListener,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !scalarReader ) {
ACE_ERROR_RETURN ( ( LM_ERROR,
ACE_TEXT ( "ERROR: %N:%l: main() -" )
ACE_TEXT ( " create_datareader failed!\n" ) ), -1 );
}
DDS::DataReaderListener_var eventListener ( new MessageEventDataReaderListenerImpl );
DDS::DataReader_var eventReader =
subscriber->create_datareader ( eventTopic,
DATAREADER_QOS_DEFAULT,
eventListener,
OpenDDS::DCPS::DEFAULT_STATUS_MASK );
if ( !eventReader ) {
ACE_ERROR_RETURN ( ( LM_ERROR, ACE_TEXT ( "ERROR: %N:%l: main() - create_datareader failed!\n" ) ), -1 );
}
cout << "DDS initialized" << endl;
// Just there for E2E validation testing. The time for the opcserver and gateway to start
sleep( 50 );
// METHOD Test
RPC::ClientParams clientParams;
// 3s timeout
DDS::Duration_t maxWait;
maxWait.sec = 3;
maxWait.nanosec = 0;
clientParams.SetTimeoutForSynchronousCalls ( maxWait );
clientParams.domain_participant ( participant );
METHOD::Client client ( clientParams );
test_synchronous_method ( client );
test_asynchronous_method ( client );
// Test Method synchronous call timeout triggering
auto methodClient = test_synchronous_method_timeout ( participant );
auto client1 = test_asynchronous_attribute_read ( participant );
auto client2 = test_synchronous_attribute_read ( participant );
auto client3 = test_asynchronous_attribute_write ( participant );
auto client4 = test_synchronous_attribute_write ( participant );
UNUSED ( methodClient );
UNUSED ( client1 );
UNUSED ( client2 );
UNUSED ( client3 );
UNUSED ( client4 );
RPC::ClientParams viewClientParams;
clientParams.domain_participant ( participant );
VIEW::Client viewClient ( clientParams );
test_synchronous_view_browse ( &viewClient );
test_synchronous_view_browse_next ( &viewClient );
test_synchronous_view_translate_browse_path_to_nodeid ( &viewClient );
test_synchronous_view_register_nodes ( &viewClient );
test_synchronous_view_unregister_nodes ( &viewClient );
while ( running ) {
sleep ( 1000 );
}
// Clean-up!
participant->delete_contained_entities();
dpf->delete_participant ( participant );
TheServiceParticipant->shutdown();
licenseManager.DeactivateLicense();
ACE_OS::exit(0);
} catch ( const CORBA::Exception& e ) {
e._tao_print_exception ( "Exception caught in main():" );
return -1;
}
}