forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_graph.cpp
More file actions
1053 lines (941 loc) · 31.6 KB
/
node_graph.cpp
File metadata and controls
1053 lines (941 loc) · 31.6 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
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
997
998
999
1000
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// 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.
#include "rclcpp/node_interfaces/node_graph.hpp"
#include <algorithm>
#include <cstddef>
#include <map>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "rcl/graph.h"
#include "rcl/remap.h"
#include "rclcpp/event.hpp"
#include "rclcpp/exceptions.hpp"
#include "rclcpp/expand_topic_or_service_name.hpp"
#include "rclcpp/graph_listener.hpp"
#include "rclcpp/node_interfaces/node_graph_interface.hpp"
#include "rcpputils/scope_exit.hpp"
using rclcpp::node_interfaces::NodeGraph;
using rclcpp::exceptions::throw_from_rcl_error;
using rclcpp::graph_listener::GraphListener;
NodeGraph::NodeGraph(rclcpp::node_interfaces::NodeBaseInterface * node_base)
: node_base_(node_base),
should_add_to_graph_listener_(true),
graph_users_count_(0)
{}
NodeGraph::~NodeGraph()
{
// Remove self from graph listener.
// Exchange with false to prevent others from trying to add this node to the
// graph listener after checking that it was not here.
if (!should_add_to_graph_listener_.exchange(false)) {
// If it was already false, then it needs to now be removed.
node_base_->get_context()->get_graph_listener()->remove_node(this);
}
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_topic_names_and_types(bool no_demangle) const
{
rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types();
rcl_allocator_t allocator = rcl_get_default_allocator();
auto ret = rcl_get_topic_names_and_types(
node_base_->get_rcl_node_handle(),
&allocator,
no_demangle,
&topic_names_and_types);
if (ret != RCL_RET_OK) {
auto error_msg = std::string("failed to get topic names and types: ") +
rcl_get_error_string().str;
rcl_reset_error();
if (rcl_names_and_types_fini(&topic_names_and_types) != RCL_RET_OK) {
error_msg += std::string(", failed also to cleanup topic names and types, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw std::runtime_error(error_msg);
}
std::map<std::string, std::vector<std::string>> topics_and_types;
for (size_t i = 0; i < topic_names_and_types.names.size; ++i) {
std::string topic_name = topic_names_and_types.names.data[i];
for (size_t j = 0; j < topic_names_and_types.types[i].size; ++j) {
topics_and_types[topic_name].emplace_back(topic_names_and_types.types[i].data[j]);
}
}
ret = rcl_names_and_types_fini(&topic_names_and_types);
if (ret != RCL_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not destroy topic names and types: ") + rcl_get_error_string().str);
// *INDENT-ON*
}
return topics_and_types;
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_service_names_and_types() const
{
rcl_names_and_types_t service_names_and_types = rcl_get_zero_initialized_names_and_types();
rcl_allocator_t allocator = rcl_get_default_allocator();
auto ret = rcl_get_service_names_and_types(
node_base_->get_rcl_node_handle(),
&allocator,
&service_names_and_types);
if (ret != RCL_RET_OK) {
auto error_msg = std::string("failed to get service names and types: ") +
rcl_get_error_string().str;
rcl_reset_error();
if (rcl_names_and_types_fini(&service_names_and_types) != RCL_RET_OK) {
error_msg +=
std::string(", failed also to cleanup service names and types, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw std::runtime_error(error_msg);
}
std::map<std::string, std::vector<std::string>> services_and_types;
for (size_t i = 0; i < service_names_and_types.names.size; ++i) {
std::string service_name = service_names_and_types.names.data[i];
for (size_t j = 0; j < service_names_and_types.types[i].size; ++j) {
services_and_types[service_name].emplace_back(service_names_and_types.types[i].data[j]);
}
}
ret = rcl_names_and_types_fini(&service_names_and_types);
if (ret != RCL_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not destroy service names and types: ") + rcl_get_error_string().str);
// *INDENT-ON*
}
return services_and_types;
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_service_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_) const
{
rcl_names_and_types_t service_names_and_types = rcl_get_zero_initialized_names_and_types();
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_service_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
node_name.c_str(),
namespace_.c_str(),
&service_names_and_types);
if (ret != RCL_RET_OK) {
auto error_msg = std::string("failed to get service names and types by node: ") +
rcl_get_error_string().str;
rcl_reset_error();
if (rcl_names_and_types_fini(&service_names_and_types) != RCL_RET_OK) {
error_msg +=
std::string(", failed also to cleanup service names and types, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw std::runtime_error(error_msg);
}
std::map<std::string, std::vector<std::string>> services_and_types;
for (size_t i = 0; i < service_names_and_types.names.size; ++i) {
std::string service_name = service_names_and_types.names.data[i];
for (size_t j = 0; j < service_names_and_types.types[i].size; ++j) {
services_and_types[service_name].emplace_back(service_names_and_types.types[i].data[j]);
}
}
ret = rcl_names_and_types_fini(&service_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "could not destroy service names and types");
}
return services_and_types;
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_client_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_) const
{
rcl_names_and_types_t service_names_and_types = rcl_get_zero_initialized_names_and_types();
auto service_names_and_types_finalizer = rcpputils::make_scope_exit(
[&service_names_and_types]() {
if (rcl_names_and_types_fini(&service_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy service names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_client_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
node_name.c_str(),
namespace_.c_str(),
&service_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get service names and types by node");
}
std::map<std::string, std::vector<std::string>> services_and_types;
for (size_t i = 0; i < service_names_and_types.names.size; ++i) {
std::string service_name = service_names_and_types.names.data[i];
for (size_t j = 0; j < service_names_and_types.types[i].size; ++j) {
services_and_types[service_name].emplace_back(service_names_and_types.types[i].data[j]);
}
}
return services_and_types;
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_publisher_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_,
bool no_demangle) const
{
rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types();
auto topic_names_and_types_finalizer = rcpputils::make_scope_exit(
[&topic_names_and_types]() {
if (rcl_names_and_types_fini(&topic_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy topic names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_publisher_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
no_demangle,
node_name.c_str(),
namespace_.c_str(),
&topic_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get topic names and types by node");
}
std::map<std::string, std::vector<std::string>> topics_and_types;
for (size_t i = 0; i < topic_names_and_types.names.size; ++i) {
std::string topic_name = topic_names_and_types.names.data[i];
for (size_t j = 0; j < topic_names_and_types.types[i].size; ++j) {
topics_and_types[topic_name].emplace_back(topic_names_and_types.types[i].data[j]);
}
}
return topics_and_types;
}
std::map<std::string, std::vector<std::string>>
NodeGraph::get_subscriber_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_,
bool no_demangle) const
{
rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types();
auto topic_names_and_types_finalizer = rcpputils::make_scope_exit(
[&topic_names_and_types]() {
if (rcl_names_and_types_fini(&topic_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy topic names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_subscriber_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
no_demangle,
node_name.c_str(),
namespace_.c_str(),
&topic_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get topic names and types by node");
}
std::map<std::string, std::vector<std::string>> topics_and_types;
for (size_t i = 0; i < topic_names_and_types.names.size; ++i) {
std::string topic_name = topic_names_and_types.names.data[i];
for (size_t j = 0; j < topic_names_and_types.types[i].size; ++j) {
topics_and_types[topic_name].emplace_back(topic_names_and_types.types[i].data[j]);
}
}
return topics_and_types;
}
std::vector<std::string>
NodeGraph::get_node_names() const
{
std::vector<std::string> nodes;
auto names_and_namespaces = get_node_names_and_namespaces();
std::transform(
names_and_namespaces.begin(),
names_and_namespaces.end(),
std::back_inserter(nodes),
[](std::pair<std::string, std::string> nns) {
std::string return_string;
if (nns.second.back() == '/') {
return_string = nns.second + nns.first;
} else {
return_string = nns.second + '/' + nns.first;
}
// Quick check to make sure that we start with a slash
// Since fully-qualified strings need to
if (return_string.front() != '/') {
return_string = "/" + return_string;
}
return return_string;
}
);
return nodes;
}
std::vector<std::tuple<std::string, std::string, std::string>>
NodeGraph::get_node_names_with_enclaves() const
{
rcutils_string_array_t node_names_c =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_namespaces_c =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_enclaves_c =
rcutils_get_zero_initialized_string_array();
auto allocator = rcl_get_default_allocator();
auto ret = rcl_get_node_names_with_enclaves(
node_base_->get_rcl_node_handle(),
allocator,
&node_names_c,
&node_namespaces_c,
&node_enclaves_c);
if (ret != RCL_RET_OK) {
auto error_msg =
std::string("failed to get node names with enclaves: ") + rcl_get_error_string().str;
rcl_reset_error();
if (rcutils_string_array_fini(&node_names_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node names, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (rcutils_string_array_fini(&node_namespaces_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node namespaces, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (rcutils_string_array_fini(&node_enclaves_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node enclaves, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw std::runtime_error(error_msg);
}
std::vector<std::tuple<std::string, std::string, std::string>> node_tuples;
for (size_t i = 0; i < node_names_c.size; ++i) {
if (node_names_c.data[i] && node_namespaces_c.data[i] && node_enclaves_c.data[i]) {
node_tuples.emplace_back(
std::make_tuple(node_names_c.data[i], node_namespaces_c.data[i], node_enclaves_c.data[i]));
}
}
std::string error("failed to finalize array");
rcl_ret_t ret_names = rcutils_string_array_fini(&node_names_c);
if (ret_names != RCUTILS_RET_OK) {
error += std::string(", could not destroy node names, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
rcl_ret_t ret_ns = rcutils_string_array_fini(&node_namespaces_c);
if (ret_ns != RCUTILS_RET_OK) {
error += std::string(", could not destroy node namespaces, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
rcl_ret_t ret_ecv = rcutils_string_array_fini(&node_enclaves_c);
if (ret_ecv != RCUTILS_RET_OK) {
error += std::string(", could not destroy node enclaves, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (ret_names != RCUTILS_RET_OK || ret_ns != RCUTILS_RET_OK || ret_ecv != RCUTILS_RET_OK) {
throw std::runtime_error(error);
}
return node_tuples;
}
std::vector<std::pair<std::string, std::string>>
NodeGraph::get_node_names_and_namespaces() const
{
rcutils_string_array_t node_names_c =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_namespaces_c =
rcutils_get_zero_initialized_string_array();
auto allocator = rcl_get_default_allocator();
auto ret = rcl_get_node_names(
node_base_->get_rcl_node_handle(),
allocator,
&node_names_c,
&node_namespaces_c);
if (ret != RCL_RET_OK) {
auto error_msg = std::string("failed to get node names: ") + rcl_get_error_string().str;
rcl_reset_error();
if (rcutils_string_array_fini(&node_names_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node names, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (rcutils_string_array_fini(&node_namespaces_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node namespaces, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
RCUTILS_LOG_ERROR_NAMED("rclcpp", "%s", error_msg.c_str());
throw std::runtime_error(error_msg);
}
std::vector<std::pair<std::string, std::string>> node_names;
node_names.reserve(node_names_c.size);
for (size_t i = 0; i < node_names_c.size; ++i) {
if (node_names_c.data[i] && node_namespaces_c.data[i]) {
node_names.emplace_back(node_names_c.data[i], node_namespaces_c.data[i]);
}
}
std::string error;
rcl_ret_t ret_names = rcutils_string_array_fini(&node_names_c);
if (ret_names != RCUTILS_RET_OK) {
error = std::string("could not destroy node names: ") + rcl_get_error_string().str;
}
rcl_ret_t ret_ns = rcutils_string_array_fini(&node_namespaces_c);
if (ret_ns != RCUTILS_RET_OK) {
error += std::string(", could not destroy node namespaces: ") + rcl_get_error_string().str;
}
if (ret_names != RCUTILS_RET_OK || ret_ns != RCUTILS_RET_OK) {
RCUTILS_LOG_ERROR_NAMED("rclcpp", "%s", error.c_str());
throw std::runtime_error(error);
}
return node_names;
}
size_t
NodeGraph::count_publishers(const std::string & topic_name) const
{
auto rcl_node_handle = node_base_->get_rcl_node_handle();
auto fqdn = rclcpp::expand_topic_or_service_name(
topic_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
false); // false = not a service
size_t count;
auto ret = rcl_count_publishers(rcl_node_handle, fqdn.c_str(), &count);
if (ret != RMW_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not count publishers: ") + rmw_get_error_string().str);
// *INDENT-ON*
}
return count;
}
size_t
NodeGraph::count_subscribers(const std::string & topic_name) const
{
auto rcl_node_handle = node_base_->get_rcl_node_handle();
auto fqdn = rclcpp::expand_topic_or_service_name(
topic_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
false); // false = not a service
size_t count;
auto ret = rcl_count_subscribers(rcl_node_handle, fqdn.c_str(), &count);
if (ret != RMW_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not count subscribers: ") + rmw_get_error_string().str);
// *INDENT-ON*
}
return count;
}
size_t
NodeGraph::count_clients(const std::string & service_name) const
{
auto rcl_node_handle = node_base_->get_rcl_node_handle();
auto fqdn = rclcpp::expand_topic_or_service_name(
service_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
true);
size_t count;
auto ret = rcl_count_clients(rcl_node_handle, fqdn.c_str(), &count);
if (ret != RMW_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not count clients: ") + rmw_get_error_string().str);
// *INDENT-ON*
}
return count;
}
size_t
NodeGraph::count_services(const std::string & service_name) const
{
auto rcl_node_handle = node_base_->get_rcl_node_handle();
auto fqdn = rclcpp::expand_topic_or_service_name(
service_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
true);
size_t count;
auto ret = rcl_count_services(rcl_node_handle, fqdn.c_str(), &count);
if (ret != RMW_RET_OK) {
// *INDENT-OFF*
throw std::runtime_error(
std::string("could not count services: ") + rmw_get_error_string().str);
// *INDENT-ON*
}
return count;
}
const rcl_guard_condition_t *
NodeGraph::get_graph_guard_condition() const
{
return rcl_node_get_graph_guard_condition(node_base_->get_rcl_node_handle());
}
void
NodeGraph::notify_graph_change()
{
{
std::lock_guard<std::mutex> graph_changed_lock(graph_mutex_);
bool bad_ptr_encountered = false;
for (auto & event_wptr : graph_events_) {
auto event_ptr = event_wptr.lock();
if (event_ptr) {
event_ptr->set();
} else {
bad_ptr_encountered = true;
}
}
if (bad_ptr_encountered) {
// remove invalid pointers with the erase-remove idiom
graph_events_.erase(
std::remove_if(
graph_events_.begin(),
graph_events_.end(),
[](const rclcpp::Event::WeakPtr & wptr) {
return wptr.expired();
}),
graph_events_.end());
// update graph_users_count_
graph_users_count_.store(graph_events_.size());
}
}
graph_cv_.notify_all();
try {
node_base_->trigger_notify_guard_condition();
} catch (const rclcpp::exceptions::RCLError & ex) {
throw std::runtime_error(
std::string("failed to notify wait set on graph change: ") + ex.what());
}
}
void
NodeGraph::notify_shutdown()
{
// notify here anything that will not be woken up by ctrl-c or rclcpp::shutdown().
graph_cv_.notify_all();
}
rclcpp::Event::SharedPtr
NodeGraph::get_graph_event()
{
auto event = rclcpp::Event::make_shared();
{
std::lock_guard<std::mutex> graph_changed_lock(graph_mutex_);
graph_events_.push_back(event);
graph_users_count_++;
}
// on first call, add node to graph_listener_
if (should_add_to_graph_listener_.exchange(false)) {
node_base_->get_context()->get_graph_listener()->add_node(this);
node_base_->get_context()->get_graph_listener()->start_if_not_started();
}
return event;
}
void
NodeGraph::wait_for_graph_change(
rclcpp::Event::SharedPtr event,
std::chrono::nanoseconds timeout)
{
using rclcpp::exceptions::InvalidEventError;
using rclcpp::exceptions::EventNotRegisteredError;
if (!event) {
throw InvalidEventError();
}
{
std::lock_guard<std::mutex> graph_changed_lock(graph_mutex_);
bool event_in_graph_events = false;
for (const auto & event_wptr : graph_events_) {
if (event == event_wptr.lock()) {
event_in_graph_events = true;
break;
}
}
if (!event_in_graph_events) {
throw EventNotRegisteredError();
}
}
auto pred = [&event, context = node_base_->get_context()]() {
return event->check() || !rclcpp::ok(context);
};
std::unique_lock<std::mutex> graph_lock(graph_mutex_);
if (!pred()) {
graph_cv_.wait_for(graph_lock, timeout, pred);
}
}
size_t
NodeGraph::count_graph_users() const
{
return graph_users_count_.load();
}
static
std::vector<rclcpp::TopicEndpointInfo>
convert_to_topic_info_list(const rcl_topic_endpoint_info_array_t & info_array)
{
std::vector<rclcpp::TopicEndpointInfo> topic_info_list;
topic_info_list.reserve(info_array.size);
for (size_t i = 0; i < info_array.size; ++i) {
topic_info_list.emplace_back(info_array.info_array[i]);
}
return topic_info_list;
}
template<const char * EndpointType, typename FunctionT>
static std::vector<rclcpp::TopicEndpointInfo>
get_info_by_topic(
rclcpp::node_interfaces::NodeBaseInterface * node_base,
const std::string & topic_name,
bool no_mangle,
FunctionT rcl_get_info_by_topic)
{
std::string fqdn;
auto rcl_node_handle = node_base->get_rcl_node_handle();
if (no_mangle) {
fqdn = topic_name;
} else {
fqdn = rclcpp::expand_topic_or_service_name(
topic_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
false); // false = not a service
// Get the node options
const rcl_node_options_t * node_options = rcl_node_get_options(rcl_node_handle);
if (nullptr == node_options) {
throw std::runtime_error("Need valid node options in get_info_by_topic()");
}
const rcl_arguments_t * global_args = nullptr;
if (node_options->use_global_arguments) {
global_args = &(rcl_node_handle->context->global_arguments);
}
char * remapped_topic_name = nullptr;
rcl_ret_t ret = rcl_remap_topic_name(
&(node_options->arguments),
global_args,
fqdn.c_str(),
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
node_options->allocator,
&remapped_topic_name);
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, std::string("Failed to remap topic name ") + fqdn);
} else if (nullptr != remapped_topic_name) {
fqdn = remapped_topic_name;
node_options->allocator.deallocate(remapped_topic_name, node_options->allocator.state);
}
}
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rcl_topic_endpoint_info_array_t info_array = rcl_get_zero_initialized_topic_endpoint_info_array();
rcl_ret_t ret =
rcl_get_info_by_topic(rcl_node_handle, &allocator, fqdn.c_str(), no_mangle, &info_array);
if (RCL_RET_OK != ret) {
auto error_msg =
std::string("Failed to get information by topic for ") + EndpointType + std::string(":");
if (RCL_RET_UNSUPPORTED == ret) {
error_msg += std::string("function not supported by RMW_IMPLEMENTATION");
} else {
error_msg += rcl_get_error_string().str;
}
rcl_reset_error();
if (RCL_RET_OK != rcl_topic_endpoint_info_array_fini(&info_array, &allocator)) {
error_msg += std::string(", failed also to cleanup topic info array, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw_from_rcl_error(ret, error_msg);
}
std::vector<rclcpp::TopicEndpointInfo> topic_info_list = convert_to_topic_info_list(info_array);
ret = rcl_topic_endpoint_info_array_fini(&info_array, &allocator);
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "rcl_topic_info_array_fini failed.");
}
return topic_info_list;
}
static constexpr char kPublisherEndpointTypeName[] = "publishers";
std::vector<rclcpp::TopicEndpointInfo>
NodeGraph::get_publishers_info_by_topic(
const std::string & topic_name,
bool no_mangle) const
{
return get_info_by_topic<kPublisherEndpointTypeName>(
node_base_,
topic_name,
no_mangle,
rcl_get_publishers_info_by_topic);
}
static constexpr char kSubscriptionEndpointTypeName[] = "subscriptions";
std::vector<rclcpp::TopicEndpointInfo>
NodeGraph::get_subscriptions_info_by_topic(
const std::string & topic_name,
bool no_mangle) const
{
return get_info_by_topic<kSubscriptionEndpointTypeName>(
node_base_,
topic_name,
no_mangle,
rcl_get_subscriptions_info_by_topic);
}
static
std::vector<rclcpp::ServiceEndpointInfo>
convert_to_service_info_list(const rcl_service_endpoint_info_array_t & info_array)
{
std::vector<rclcpp::ServiceEndpointInfo> service_info_list;
service_info_list.reserve(info_array.size);
for (size_t i = 0; i < info_array.size; ++i) {
service_info_list.emplace_back(info_array.info_array[i]);
}
return service_info_list;
}
template<const char * EndpointType, typename FunctionT>
static std::vector<rclcpp::ServiceEndpointInfo>
get_info_by_service(
rclcpp::node_interfaces::NodeBaseInterface * node_base,
const std::string & service_name,
bool no_mangle,
FunctionT rcl_get_info_by_service)
{
std::string fqdn;
auto rcl_node_handle = node_base->get_rcl_node_handle();
if (no_mangle) {
fqdn = service_name;
} else {
fqdn = rclcpp::expand_topic_or_service_name(
service_name,
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
true);
// Get the node options
const rcl_node_options_t * node_options = rcl_node_get_options(rcl_node_handle);
if (nullptr == node_options) {
throw std::runtime_error("Need valid node options in get_info_by_service()");
}
const rcl_arguments_t * global_args = nullptr;
if (node_options->use_global_arguments) {
global_args = &(rcl_node_handle->context->global_arguments);
}
char * remapped_service_name = nullptr;
rcl_ret_t ret = rcl_remap_service_name(
&(node_options->arguments),
global_args,
fqdn.c_str(),
rcl_node_get_name(rcl_node_handle),
rcl_node_get_namespace(rcl_node_handle),
node_options->allocator,
&remapped_service_name);
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, std::string("Failed to remap service name ") + fqdn);
} else if (nullptr != remapped_service_name) {
fqdn = remapped_service_name;
node_options->allocator.deallocate(remapped_service_name, node_options->allocator.state);
}
}
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rcl_service_endpoint_info_array_t info_array =
rcl_get_zero_initialized_service_endpoint_info_array();
rcl_ret_t ret =
rcl_get_info_by_service(rcl_node_handle, &allocator, fqdn.c_str(), no_mangle, &info_array);
if (RCL_RET_OK != ret) {
auto error_msg =
std::string("Failed to get information by service for ") + EndpointType + std::string(":");
if (RCL_RET_UNSUPPORTED == ret) {
error_msg += std::string("function not supported by RMW_IMPLEMENTATION");
} else {
error_msg += rcl_get_error_string().str;
}
rcl_reset_error();
if (RCL_RET_OK != rcl_service_endpoint_info_array_fini(&info_array, &allocator)) {
error_msg += std::string(", failed also to cleanup service info array, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw_from_rcl_error(ret, error_msg);
}
std::vector<rclcpp::ServiceEndpointInfo> service_info_list =
convert_to_service_info_list(info_array);
ret = rcl_service_endpoint_info_array_fini(&info_array, &allocator);
if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "rcl_service_info_array_fini failed.");
}
return service_info_list;
}
static constexpr char kClientEndpointTypeName[] = "clients";
std::vector<rclcpp::ServiceEndpointInfo>
NodeGraph::get_clients_info_by_service(
const std::string & service_name,
bool no_mangle) const
{
return get_info_by_service<kClientEndpointTypeName>(
node_base_,
service_name,
no_mangle,
rcl_get_clients_info_by_service);
}
static constexpr char kServerEndpointTypeName[] = "servers";
std::vector<rclcpp::ServiceEndpointInfo>
NodeGraph::get_servers_info_by_service(
const std::string & service_name,
bool no_mangle) const
{
return get_info_by_service<kServerEndpointTypeName>(
node_base_,
service_name,
no_mangle,
rcl_get_servers_info_by_service);
}
std::string &
rclcpp::TopicEndpointInfo::node_name()
{
return node_name_;
}
const std::string &
rclcpp::TopicEndpointInfo::node_name() const
{
return node_name_;
}
std::string &
rclcpp::TopicEndpointInfo::node_namespace()
{
return node_namespace_;
}
const std::string &
rclcpp::TopicEndpointInfo::node_namespace() const
{
return node_namespace_;
}
std::string &
rclcpp::TopicEndpointInfo::topic_type()
{
return topic_type_;
}
const std::string &
rclcpp::TopicEndpointInfo::topic_type() const
{
return topic_type_;
}
rclcpp::EndpointType &
rclcpp::TopicEndpointInfo::endpoint_type()
{
return endpoint_type_;
}
const rclcpp::EndpointType &
rclcpp::TopicEndpointInfo::endpoint_type() const
{
return endpoint_type_;
}
std::array<uint8_t, RMW_GID_STORAGE_SIZE> &
rclcpp::TopicEndpointInfo::endpoint_gid()
{
return endpoint_gid_;
}
const std::array<uint8_t, RMW_GID_STORAGE_SIZE> &
rclcpp::TopicEndpointInfo::endpoint_gid() const
{
return endpoint_gid_;
}
rclcpp::QoS &
rclcpp::TopicEndpointInfo::qos_profile()
{
return qos_profile_;
}
const rclcpp::QoS &
rclcpp::TopicEndpointInfo::qos_profile() const
{
return qos_profile_;
}
rosidl_type_hash_t &
rclcpp::TopicEndpointInfo::topic_type_hash()
{
return topic_type_hash_;
}
const rosidl_type_hash_t &
rclcpp::TopicEndpointInfo::topic_type_hash() const
{
return topic_type_hash_;
}
std::string &
rclcpp::ServiceEndpointInfo::node_name()
{
return node_name_;
}
const std::string &
rclcpp::ServiceEndpointInfo::node_name() const
{
return node_name_;
}
std::string &
rclcpp::ServiceEndpointInfo::node_namespace()
{
return node_namespace_;
}
const std::string &
rclcpp::ServiceEndpointInfo::node_namespace() const
{
return node_namespace_;
}
std::string &
rclcpp::ServiceEndpointInfo::service_type()
{
return service_type_;
}
const std::string &
rclcpp::ServiceEndpointInfo::service_type() const
{
return service_type_;
}
rclcpp::EndpointType &
rclcpp::ServiceEndpointInfo::endpoint_type()
{
return endpoint_type_;
}