-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnablerParticipant.cpp
More file actions
1281 lines (1132 loc) · 42.4 KB
/
Copy pathEnablerParticipant.cpp
File metadata and controls
1281 lines (1132 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
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 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.
/**
* @file EnablerParticipant.cpp
*/
#include <ddspipe_core/types/data/RtpsPayloadData.hpp>
#include <ddspipe_core/types/data/RpcPayloadData.hpp>
#include <ddspipe_core/types/dds/Payload.hpp>
#include <ddspipe_core/types/dynamic_types/types.hpp>
#include <ddspipe_participants/participant/rtps/CommonParticipant.hpp>
#include <ddspipe_participants/reader/auxiliar/BlankReader.hpp>
#include <ddsenabler_participants/Handler.hpp>
#include <ddsenabler_participants/Serialization.hpp>
#include <ddsenabler_participants/EnablerParticipant.hpp>
namespace eprosima {
namespace ddsenabler {
namespace participants {
using namespace eprosima::ddspipe::core;
using namespace eprosima::ddspipe::core::types;
using namespace eprosima::ddspipe::participants;
EnablerParticipant::EnablerParticipant(
std::shared_ptr<EnablerParticipantConfiguration> participant_configuration,
std::shared_ptr<PayloadPool> payload_pool,
std::shared_ptr<DiscoveryDatabase> discovery_database,
std::shared_ptr<ISchemaHandler> schema_handler)
: ddspipe::participants::SchemaParticipant(participant_configuration, payload_pool, discovery_database,
schema_handler)
, handler_(std::static_pointer_cast<Handler>(schema_handler_))
{
}
std::shared_ptr<IReader> EnablerParticipant::create_reader(
const ITopic& topic)
{
if (is_type_object_topic(topic))
{
return std::make_shared<BlankReader>();
}
std::shared_ptr<InternalReader> reader;
{
std::lock_guard<std::mutex> lck(mtx_);
auto dds_topic = dynamic_cast<const DdsTopic&>(topic);
std::shared_ptr<RpcInfo> rpc_info = std::make_shared<RpcInfo>(dds_topic.m_topic_name);
if (RpcType::NONE != rpc_info->rpc_type)
{
if (ServiceType::NONE != rpc_info->service_type)
{
reader = std::make_shared<InternalRpcReader>(id(), dds_topic);
}
else
{
reader = std::make_shared<InternalReader>(id());
}
// Only notify the discovery of topics that do not originate from a topic query callback
if (dds_topic.topic_discoverer() != this->id())
{
if (RpcType::SERVICE == rpc_info->rpc_type)
{
if (service_discovered_nts_(rpc_info, dds_topic))
{
try
{
RpcTopic service = services_.find(rpc_info->service_name)->second->get_service();
handler_->add_service(service);
}
catch (const std::exception& e)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to add service " << rpc_info->service_name << ": " << e.what());
return std::make_shared<BlankReader>();
}
}
}
else if (RpcType::ACTION == rpc_info->rpc_type)
{
if (action_discovered_nts_(rpc_info, dds_topic))
{
try
{
auto action = actions_.find(rpc_info->action_name)->second->get_action();
handler_->add_action(action);
}
catch (const std::exception& e)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to add action " << rpc_info->action_name << ": " << e.what());
return std::make_shared<BlankReader>();
}
}
}
}
}
else
{
reader = std::make_shared<InternalReader>(id());
// Only notify the discovery of topics that do not originate from a topic query callback
if (dds_topic.topic_discoverer() != this->id())
{
handler_->add_topic(dds_topic);
}
}
readers_[dds_topic] = reader;
}
cv_.notify_all();
return reader;
}
bool EnablerParticipant::publish(
const std::string& topic_name,
const std::string& json)
{
if (topic_name.empty())
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data: topic name is empty.");
return false;
}
std::unique_lock<std::mutex> lck(mtx_);
std::string type_name;
auto i_reader = lookup_reader_nts_(topic_name, type_name);
if (nullptr == i_reader)
{
DdsTopic topic;
if (!query_topic_nts_(topic_name, topic))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic_name);
return false;
}
if (!create_topic_writer_nts_(topic, i_reader, lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic_name << " : writer creation failed.");
return false;
}
type_name = topic.type_name;
// (Optionally) wait for writer created in DDS participant to match with external readers, to avoid losing this
// message when not using transient durability
std::this_thread::sleep_for(std::chrono::milliseconds(std::static_pointer_cast<EnablerParticipantConfiguration>(
configuration_)->initial_publish_wait));
}
auto reader = std::dynamic_pointer_cast<InternalReader>(i_reader);
auto data = std::make_unique<RtpsPayloadData>();
Payload payload;
if (!handler_->get_serialized_data(type_name, json, payload))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic_name << " : data serialization failed.");
return false;
}
if (!payload_pool_->get_payload(payload, data->payload))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic_name << " : get_payload failed.");
return false;
}
reader->simulate_data_reception(std::move(data));
return true;
}
bool EnablerParticipant::publish_rpc(
const std::string& topic_name,
const std::string& json,
const uint64_t request_id)
{
std::unique_lock<std::mutex> lck(mtx_);
std::shared_ptr<RpcInfo> rpc_info = std::make_shared<RpcInfo>(topic_name);
if (ServiceType::NONE == rpc_info->service_type)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic_name << " : not a service topic.");
return false;
}
auto it = services_.find(rpc_info->service_name);
if (it == services_.end())
{
// There is no case where none of the service topics are discovered and yet the publish should be done
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in service " << rpc_info->service_name << " : service does not exist.");
return false;
}
if (!it->second->external_server && rpc_info->service_type == ServiceType::REQUEST)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in service " << rpc_info->service_name <<
" : service is only announced on the enabler side.");
return false;
}
std::string type_name;
auto reader = std::dynamic_pointer_cast<InternalRpcReader>(lookup_reader_nts_(topic_name, type_name));
if (nullptr == reader)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in service " << rpc_info->service_name << " : service does not exist.");
return false;
}
DdsTopic topic;
if (!it->second->get_topic(rpc_info->service_type, topic))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in service " << rpc_info->service_name << " : topic not found.");
return false;
}
if (type_name != topic.type_name)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic.m_topic_name << " : type name mismatch.");
return false;
}
auto data = std::make_unique<RpcPayloadData>();
Payload payload;
if (!handler_->get_serialized_data(type_name, json, payload))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic.m_topic_name << " : data serialization failed.");
return false;
}
if (!payload_pool_->get_payload(payload, data->payload))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to publish data in topic " << topic.m_topic_name << " : get_payload failed.");
return false;
}
data->origin_sequence_number = eprosima::fastdds::rtps::SequenceNumber_t(request_id);
data->sent_sequence_number = eprosima::fastdds::rtps::SequenceNumber_t(request_id);
data->participant_receiver = id();
fastdds::rtps::SampleIdentity sample_identity;
sample_identity.sequence_number(fastdds::rtps::SequenceNumber_t(request_id));
sample_identity.writer_guid(reader->guid());
data->write_params.get_reference().sample_identity(sample_identity);
data->write_params.get_reference().related_sample_identity(sample_identity);
reader->simulate_data_reception(std::move(data));
return true;
}
bool EnablerParticipant::announce_service(
const std::string& service_name,
Protocol Protocol)
{
std::unique_lock<std::mutex> lck(mtx_);
auto it = services_.find(service_name);
if (it != services_.end())
{
if (it->second->enabler_as_server)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce service " << service_name << " : service already announced.");
return false;
}
it->second->enabler_as_server = true;
return true;
}
std::shared_ptr<ServiceDiscovered> service = std::make_shared<ServiceDiscovered>(service_name, Protocol);
if (!query_service_nts_(service, Protocol))
{
return false;
}
if (create_service_request_writer_nts_(service, lck))
{
services_.insert_or_assign(service_name, service);
return true;
}
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce service " << service_name << " : service writer creation failed.");
return false;
}
// TODO after revoking the service the client is still matched
bool EnablerParticipant::revoke_service(
const std::string& service_name)
{
std::unique_lock<std::mutex> lck(mtx_);
return revoke_service_nts_(service_name);
}
bool EnablerParticipant::send_service_request(
const std::string& service_name,
const std::string& json,
uint64_t& request_id,
Protocol Protocol)
{
std::string prefix, suffix;
switch (Protocol)
{
case Protocol::ROS2:
prefix = ROS_REQUEST_PREFIX;
suffix = ROS_REQUEST_SUFFIX;
break;
case Protocol::DDS:
prefix = DDS_REQUEST_PREFIX;
suffix = DDS_REQUEST_SUFFIX;
break;
default:
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send service request to service " << service_name << ": unknown RPC protocol.");
return false;
}
request_id = handler_->get_new_request_id();
if (!publish_rpc(
prefix + service_name + suffix,
json,
request_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send service request to service " << service_name);
return false;
}
return true;
}
bool EnablerParticipant::send_service_reply(
const std::string& service_name,
const std::string& json,
const uint64_t request_id)
{
Protocol Protocol = get_service_protocol(service_name);
std::string prefix, suffix;
switch (Protocol)
{
case Protocol::ROS2:
prefix = ROS_REPLY_PREFIX;
suffix = ROS_REPLY_SUFFIX;
break;
case Protocol::DDS:
prefix = DDS_REPLY_PREFIX;
suffix = DDS_REPLY_SUFFIX;
break;
default:
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send service reply to unknown service " << service_name);
return false;
}
return publish_rpc(
prefix + service_name + suffix,
json,
request_id);
}
Protocol EnablerParticipant::get_service_protocol(
const std::string& service_name)
{
std::unique_lock<std::mutex> lck(mtx_);
auto it = services_.find(service_name);
if (it != services_.end())
{
return it->second->get_protocol();
}
return Protocol::PROTOCOL_UNKNOWN;
}
bool EnablerParticipant::announce_action(
const std::string& action_name,
Protocol Protocol)
{
std::unique_lock<std::mutex> lck(mtx_);
if (Protocol != Protocol::ROS2)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action_name << " : only ROS2 actions are currently supported.");
return false;
}
{
auto it = actions_.find(action_name);
if (it != actions_.end())
{
if (it->second->enabler_as_server)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action_name << " : action already announced.");
return false;
}
// Erase the action, to allow re-announcing it
auto goal = it->second->goal.lock();
auto result = it->second->result.lock();
auto cancel = it->second->cancel.lock();
if (goal && result && cancel)
{
it->second->enabler_as_server = true;
goal->enabler_as_server = true;
result->enabler_as_server = true;
cancel->enabler_as_server = true;
return true;
}
actions_.erase(it);
}
}
std::shared_ptr<ActionDiscovered> action = std::make_shared<ActionDiscovered>(action_name, Protocol);
if (!query_action_nts_(*action, Protocol, lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action_name << " : action type request failed.");
return false;
}
actions_.insert_or_assign(action_name, action);
return true;
}
bool EnablerParticipant::revoke_action(
const std::string& action_name)
{
std::unique_lock<std::mutex> lck(mtx_);
auto it = actions_.find(action_name);
if (it == actions_.end())
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to revoke action " << action_name << " : action not found.");
return false;
}
if (!it->second->enabler_as_server)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to revoke action " << action_name << " : action not announced as server.");
return false;
}
if (it->second->external_server)
{
it->second->enabler_as_server = false;
return true;
}
auto action = it->second;
auto goal = action->goal.lock();
auto result = action->result.lock();
auto cancel = action->cancel.lock();
if (!goal || !result || !cancel)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to revoke action " << action_name << " : action services not fully discovered.");
return false;
}
if (this->revoke_service_nts_(goal->service_name) &&
this->revoke_service_nts_(result->service_name) &&
this->revoke_service_nts_(cancel->service_name))
{
action->enabler_as_server = false;
action->fully_discovered = false;
return true;
}
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to revoke action " << action_name << " : error revoking action services.");
return false;
}
bool EnablerParticipant::send_action_goal(
const std::string& action_name,
const std::string& json,
UUID& action_id,
Protocol Protocol)
{
std::string goal_json = RpcUtils::create_goal_request_msg(json, action_id);
std::string goal_request_topic = action_name + ACTION_INFIX + ACTION_GOAL_SUFFIX;
uint64_t goal_request_id = 0;
if (!send_service_request(
goal_request_topic,
goal_json,
goal_request_id,
Protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action goal request to action " << action_name);
return false;
}
if (!handler_->store_action_request(
action_name,
action_id,
goal_request_id,
ActionType::GOAL,
Protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to store action goal request to action " << action_name);
handler_->erase_action_UUID(action_id, ActionEraseReason::FORCED);
return false;
}
return true;
}
bool EnablerParticipant::cancel_action_goal(
const std::string& action_name,
const UUID& goal_id,
const int64_t timestamp)
{
if (goal_id != UUID() &&
!handler_->is_UUID_active(action_name, goal_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to cancel action goal for action " << action_name
<< ": goal id not found.");
return false;
}
std::string cancel_json = RpcUtils::create_cancel_request_msg(goal_id, timestamp);
Protocol protocol = handler_->get_action_protocol(action_name, goal_id);
uint64_t cancel_request_id = 0;
std::string cancel_request_topic = action_name + ACTION_INFIX + ACTION_CANCEL_SUFFIX;
if (send_service_request(
cancel_request_topic,
cancel_json,
cancel_request_id,
protocol))
{
return true;
}
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action cancel goal to action " << action_name);
return false;
}
bool EnablerParticipant::send_action_get_result_request(
const std::string& action_name,
const UUID& action_id)
{
std::string json = RpcUtils::create_result_request_msg(action_id);
std::string get_result_request_topic = action_name + ACTION_INFIX + ACTION_RESULT_SUFFIX;
uint64_t get_result_request_id = 0;
Protocol protocol = handler_->get_action_protocol(action_name, action_id);
if (!send_service_request(
get_result_request_topic,
json,
get_result_request_id,
protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action get result request to action " << action_name);
return false;
}
if (!handler_->store_action_request(
action_name,
action_id,
get_result_request_id,
ActionType::RESULT))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to store action get result request to action " << action_name
<< ": cancelling.");
cancel_action_goal(action_name, action_id, 0);
return false;
}
return true;
}
void EnablerParticipant::send_action_send_goal_reply(
const std::string& action_name,
const uint64_t goal_id,
bool accepted)
{
std::string reply_json = RpcUtils::create_goal_reply_msg(accepted);
if (!send_service_reply(
action_name + ACTION_INFIX + ACTION_GOAL_SUFFIX,
reply_json,
goal_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action goal reply to action " << action_name
<< ": goal id not found.");
}
return;
}
bool EnablerParticipant::send_action_cancel_goal_reply(
const char* action_name,
const std::vector<UUID>& goal_ids,
const CancelCode& cancel_code,
const uint64_t request_id)
{
std::vector<std::pair<UUID, std::chrono::system_clock::time_point>> cancelling_goals;
for (const auto& goal_id : goal_ids)
{
std::chrono::system_clock::time_point timestamp;
if (!handler_->is_UUID_active(action_name, goal_id, ×tamp))
{
continue;
}
cancelling_goals.emplace_back(goal_id, timestamp);
}
std::string reply_json = RpcUtils::create_cancel_reply_msg(cancelling_goals, cancel_code);
if (!send_service_reply(
std::string(action_name) + ACTION_INFIX + ACTION_CANCEL_SUFFIX,
reply_json,
request_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action cancel reply to action " << action_name
<< ": request id not found.");
return false;
}
return true;
}
bool EnablerParticipant::send_action_result(
const char* action_name,
const UUID& goal_id,
const StatusCode& status_code,
const char* json)
{
if (!handler_->is_UUID_active(action_name, goal_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action result to action " << action_name
<< ": goal id not found.");
return false;
}
std::string reply_json = RpcUtils::create_result_reply_msg(status_code, json);
return handler_->handle_action_result(action_name, goal_id, reply_json);
}
bool EnablerParticipant::send_action_get_result_reply(
const std::string& action_name,
const UUID& goal_id,
const std::string& reply_json,
const uint64_t request_id)
{
std::string result_topic = action_name + ACTION_INFIX + ACTION_RESULT_SUFFIX;
if (send_service_reply(
result_topic,
reply_json,
request_id))
{
handler_->erase_action_UUID(goal_id, ActionEraseReason::FORCED);
return true;
}
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action get result to action " << action_name);
return false;
}
bool EnablerParticipant::send_action_feedback(
const char* action_name,
const char* json,
const UUID& goal_id)
{
if (!handler_->is_UUID_active(action_name, goal_id))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send action feedback to action " << action_name
<< ": goal id not found.");
return false;
}
Protocol protocol = handler_->get_action_protocol(action_name, goal_id);
std::string prefix;
switch (protocol)
{
case Protocol::ROS2:
prefix = ROS_TOPIC_PREFIX;
break;
case Protocol::DDS:
prefix = FASTDDS_TOPIC_PREFIX;
break;
default:
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send feedback to action " << action_name
<< ": unsupported RPC protocol.");
return false;
}
std::string feedback_json = RpcUtils::create_feedback_msg(json, goal_id);
std::string feedback_topic = prefix + std::string(action_name) + ACTION_INFIX + ACTION_FEEDBACK_SUFFIX;
return publish(feedback_topic, feedback_json);
}
bool EnablerParticipant::update_action_status(
const std::string& action_name,
const UUID& goal_id,
const StatusCode& status_code)
{
std::chrono::system_clock::time_point goal_accepted_stamp;
if (!handler_->is_UUID_active(action_name, goal_id, &goal_accepted_stamp))
{
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to update action status to action " << action_name
<< ": goal id not found.");
return false;
}
Protocol protocol = handler_->get_action_protocol(action_name, goal_id);
std::string prefix;
switch (protocol)
{
case Protocol::ROS2:
prefix = ROS_TOPIC_PREFIX;
break;
case Protocol::DDS:
prefix = FASTDDS_TOPIC_PREFIX;
break;
default:
EPROSIMA_LOG_ERROR(DDSENABLER_EXECUTION,
"Failed to send status to action " << action_name
<< ": unsupported RPC protocol.");
return false;
}
std::string status_json = RpcUtils::create_status_msg(goal_id, status_code, goal_accepted_stamp);
std::string status_topic = prefix + action_name + ACTION_INFIX + ACTION_STATUS_SUFFIX;
return publish(status_topic, status_json);
}
bool EnablerParticipant::query_topic_nts_(
const std::string& topic_name,
DdsTopic& topic)
{
if (!topic_query_callback_)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to query data from topic " << topic_name <<
" : topic is unknown and topic query callback not set.");
return false;
}
TopicInfo topic_info;
if (!topic_query_callback_(topic_name.c_str(), topic_info))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to query data from topic " << topic_name << " : topic query callback failed.");
return false;
}
return fill_topic_struct_nts_(topic_name, topic_info, topic);
}
bool EnablerParticipant::query_service_nts_(
std::shared_ptr<ServiceDiscovered> service,
Protocol Protocol)
{
if (!service_query_callback_)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce service " << service->service_name <<
" : service is unknown and service request callback not set.");
return false;
}
std::string request_type_name;
std::string serialized_request_qos_content;
std::string reply_type_name;
std::string serialized_reply_qos_content;
ServiceInfo service_info;
if (!service_query_callback_(service->service_name.c_str(), service_info))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce service " << service->service_name << " : service type request failed.");
return false;
}
return fill_service_type_nts_(service_info, service, Protocol);
}
bool EnablerParticipant::query_action_nts_(
ActionDiscovered& action,
Protocol Protocol,
std::unique_lock<std::mutex>& lck)
{
if (!action_query_callback_)
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name <<
" : action is unknown and action request callback not set.");
return false;
}
ActionInfo action_info;
if (!action_query_callback_(action.action_name.c_str(),
action_info))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : action type request failed.");
return false;
}
std::string goal_service_name = action.action_name + ACTION_INFIX + ACTION_GOAL_SUFFIX;
std::string cancel_service_name = action.action_name + ACTION_INFIX + ACTION_CANCEL_SUFFIX;
std::string result_service_name = action.action_name + ACTION_INFIX + ACTION_RESULT_SUFFIX;
std::vector<std::string> topics_names =
{
goal_service_name,
cancel_service_name,
result_service_name
};
for (const auto& topic_name : topics_names)
{
auto it = services_.find(topic_name);
if (it != services_.end())
{
// Erase the service, to allow re-announcing it
services_.erase(it);
}
}
std::shared_ptr<ServiceDiscovered> goal_service = std::make_shared<ServiceDiscovered>(goal_service_name,
Protocol);
if (!fill_service_type_nts_(
action_info.goal,
goal_service,
Protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : goal service type not found.");
return false;
}
if (!create_service_request_writer_nts_(goal_service, lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : goal service writer creation failed.");
return false;
}
services_.insert_or_assign(goal_service_name, goal_service);
action.goal = goal_service;
std::shared_ptr<ServiceDiscovered> cancel_service = std::make_shared<ServiceDiscovered>(cancel_service_name,
Protocol);
if (!fill_service_type_nts_(
action_info.cancel,
cancel_service,
Protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : cancel service type not found.");
return false;
}
if (!create_service_request_writer_nts_(cancel_service, lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : cancel service writer creation failed.");
return false;
}
services_.insert_or_assign(cancel_service_name, cancel_service);
action.cancel = cancel_service;
std::shared_ptr<ServiceDiscovered> result_service = std::make_shared<ServiceDiscovered>(result_service_name,
Protocol);
if (!fill_service_type_nts_(
action_info.result,
result_service,
Protocol))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : result service type not found.");
return false;
}
if (!create_service_request_writer_nts_(result_service, lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : result service writer creation failed.");
return false;
}
services_.insert_or_assign(result_service_name, result_service);
action.result = result_service;
std::string prefix;
switch (Protocol)
{
case Protocol::ROS2:
prefix = ROS_TOPIC_PREFIX;
break;
case Protocol::DDS:
prefix = FASTDDS_TOPIC_PREFIX;
break;
default:
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : unsupported RPC protocol.");
return false;
}
std::string feedback_topic_name = prefix + action.action_name + ACTION_INFIX + ACTION_FEEDBACK_SUFFIX;
DdsTopic feedback_topic;
if (!fill_topic_struct_nts_(
feedback_topic_name,
action_info.feedback,
feedback_topic))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : feedback topic type not found.");
return false;
}
{
auto feedback_reader = lookup_reader_nts_(feedback_topic_name);
if (!feedback_reader)
{
if (!create_topic_writer_nts_(
feedback_topic,
feedback_reader,
lck))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name <<
" : feedback topic writer creation failed.");
return false;
}
}
}
action.feedback = feedback_topic;
action.feedback_discovered = true;
std::string status_topic_name = prefix + action.action_name + ACTION_INFIX + ACTION_STATUS_SUFFIX;
DdsTopic status_topic;
if (!fill_topic_struct_nts_(
status_topic_name,
action_info.status,
status_topic))
{
EPROSIMA_LOG_ERROR(DDSENABLER_ENABLER_PARTICIPANT,
"Failed to announce action " << action.action_name << " : status topic type not found.");
return false;
}
{
auto status_reader = lookup_reader_nts_(action.status.m_topic_name);
if (!status_reader)
{
if (!create_topic_writer_nts_(
status_topic,
status_reader,