|
| 1 | +// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @file XmlDynTypesParticipant.cpp |
| 17 | + */ |
| 18 | + |
| 19 | +#include <memory> |
| 20 | + |
| 21 | +#include <fastdds/dds/domain/DomainParticipantFactory.hpp> |
| 22 | +#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp> |
| 23 | +#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp> |
| 24 | +#include <fastdds/dds/xtypes/type_representation/TypeObject.hpp> |
| 25 | +#include <fastdds/rtps/attributes/RTPSParticipantAttributes.hpp> |
| 26 | +#include <fastdds/rtps/RTPSDomain.hpp> |
| 27 | + |
| 28 | +#include <cpp_utils/Log.hpp> |
| 29 | + |
| 30 | +#include <ddspipe_core/monitoring/producers/TopicsMonitorProducer.hpp> |
| 31 | +#include <ddspipe_core/types/dynamic_types/types.hpp> |
| 32 | +#include <ddspipe_participants/reader/auxiliar/BlankReader.hpp> |
| 33 | +#include <ddspipe_participants/reader/rtps/SpecificQoSReader.hpp> |
| 34 | +#include <ddspipe_participants/writer/auxiliar/BlankWriter.hpp> |
| 35 | + |
| 36 | +#include <ddspipe_participants/participant/dynamic_types/XmlDynTypesParticipant.hpp> |
| 37 | + |
| 38 | +namespace eprosima { |
| 39 | +namespace ddspipe { |
| 40 | +namespace participants { |
| 41 | + |
| 42 | +using namespace eprosima::ddspipe::core; |
| 43 | + |
| 44 | +XmlDynTypesParticipant::XmlDynTypesParticipant( |
| 45 | + std::shared_ptr<XmlParticipantConfiguration> participant_configuration, |
| 46 | + std::shared_ptr<PayloadPool> payload_pool, |
| 47 | + std::shared_ptr<DiscoveryDatabase> discovery_database) |
| 48 | + : XmlParticipant( |
| 49 | + participant_configuration, |
| 50 | + payload_pool, |
| 51 | + discovery_database) |
| 52 | + , type_object_reader_(std::make_shared<InternalReader>( |
| 53 | + this->id())) |
| 54 | +{ |
| 55 | + // Do nothing |
| 56 | +} |
| 57 | + |
| 58 | +std::shared_ptr<IWriter> XmlDynTypesParticipant::create_writer( |
| 59 | + const ITopic& topic) |
| 60 | +{ |
| 61 | + // This participant does not write anything |
| 62 | + return std::make_shared<BlankWriter>(); |
| 63 | +} |
| 64 | + |
| 65 | +std::shared_ptr<IReader> XmlDynTypesParticipant::create_reader( |
| 66 | + const ITopic& topic) |
| 67 | +{ |
| 68 | + // If type object topic, return the internal reader for type objects |
| 69 | + if (core::types::is_type_object_topic(topic)) |
| 70 | + { |
| 71 | + return type_object_reader_; |
| 72 | + } |
| 73 | + |
| 74 | + // If not type object, use the parent method |
| 75 | + return dds::XmlParticipant::create_reader(topic); |
| 76 | +} |
| 77 | + |
| 78 | +XmlDynTypesParticipant::XmlDynTypesDdsListener::XmlDynTypesDdsListener( |
| 79 | + std::shared_ptr<SimpleParticipantConfiguration> conf, |
| 80 | + std::shared_ptr<core::DiscoveryDatabase> ddb, |
| 81 | + std::shared_ptr<InternalReader> internal_reader) |
| 82 | + : dds::CommonParticipant::DdsListener(conf, ddb) |
| 83 | + , type_object_reader_(internal_reader) |
| 84 | +{ |
| 85 | +} |
| 86 | + |
| 87 | +void XmlDynTypesParticipant::XmlDynTypesDdsListener::on_data_reader_discovery( |
| 88 | + fastdds::dds::DomainParticipant* participant, |
| 89 | + fastdds::rtps::ReaderDiscoveryStatus reason, |
| 90 | + const fastdds::dds::SubscriptionBuiltinTopicData& info, |
| 91 | + bool& should_be_ignored) |
| 92 | +{ |
| 93 | + if (info.guid.guidPrefix != participant->guid().guidPrefix) |
| 94 | + { |
| 95 | + // Get type information |
| 96 | + const auto type_info = info.type_information.type_information; |
| 97 | + const auto type_name = info.type_name.to_string(); |
| 98 | + |
| 99 | + dds::CommonParticipant::DdsListener::on_data_reader_discovery(participant, reason, info, should_be_ignored); |
| 100 | + |
| 101 | + notify_type_discovered_(type_info, type_name); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void XmlDynTypesParticipant::XmlDynTypesDdsListener::on_data_writer_discovery( |
| 106 | + fastdds::dds::DomainParticipant* participant, |
| 107 | + fastdds::rtps::WriterDiscoveryStatus reason, |
| 108 | + const fastdds::dds::PublicationBuiltinTopicData& info, |
| 109 | + bool& should_be_ignored) |
| 110 | +{ |
| 111 | + if (info.guid.guidPrefix != participant->guid().guidPrefix) |
| 112 | + { |
| 113 | + // Get type information |
| 114 | + const auto type_info = info.type_information.type_information; |
| 115 | + const auto type_name = info.type_name.to_string(); |
| 116 | + |
| 117 | + dds::CommonParticipant::DdsListener::on_data_writer_discovery(participant, reason, info, should_be_ignored); |
| 118 | + |
| 119 | + notify_type_discovered_(type_info, type_name); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void XmlDynTypesParticipant::XmlDynTypesDdsListener::notify_type_discovered_( |
| 124 | + const fastdds::dds::xtypes::TypeInformation& type_info, |
| 125 | + const std::string& type_name) |
| 126 | +{ |
| 127 | + // Check if it exists already |
| 128 | + if (received_types_.find(type_name) != received_types_.end()) |
| 129 | + { |
| 130 | + EPROSIMA_LOG_INFO(DDSPIPE_DYNTYPES_PARTICIPANT, |
| 131 | + "Type " << type_name << " was already received, aborting propagation."); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + const auto type_identifier = type_info.complete().typeid_with_size().type_id(); |
| 136 | + fastdds::dds::xtypes::TypeObject dyn_type_object; |
| 137 | + if (fastdds::dds::RETCODE_OK != |
| 138 | + fastdds::dds::DomainParticipantFactory::get_instance()->type_object_registry().get_type_object( |
| 139 | + type_identifier, |
| 140 | + dyn_type_object)) |
| 141 | + { |
| 142 | + EPROSIMA_LOG_INFO(DDSPIPE_DYNTYPES_PARTICIPANT, |
| 143 | + "Failed to get type object of " << type_name << " type"); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + // Create Dynamic Type |
| 148 | + fastdds::dds::DynamicType::_ref_type dyn_type = |
| 149 | + fastdds::dds::DynamicTypeBuilderFactory::get_instance()->create_type_w_type_object( |
| 150 | + dyn_type_object)->build(); |
| 151 | + if (!dyn_type) |
| 152 | + { |
| 153 | + EPROSIMA_LOG_WARNING(DDSPIPE_DYNTYPES_PARTICIPANT, |
| 154 | + "Failed to create Dynamic Type " << type_name); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // Notify type_identifier |
| 159 | + // NOTE: We assume each type_name corresponds to only one type_identifier |
| 160 | + EPROSIMA_LOG_INFO(DDSPIPE_DYNTYPES_PARTICIPANT, |
| 161 | + "Participant " << configuration_->id << " discovered type object " << dyn_type->get_name()); |
| 162 | + |
| 163 | + monitor_type_discovered(type_name); |
| 164 | + |
| 165 | + // If not, add it to the received types set |
| 166 | + received_types_.insert(type_name); |
| 167 | + |
| 168 | + // Create data containing Dynamic Type |
| 169 | + auto data = std::make_unique<core::types::DynamicTypeData>(); |
| 170 | + data->dynamic_type = dyn_type; // TODO: add constructor with param |
| 171 | + data->type_identifier = type_identifier; |
| 172 | + |
| 173 | + // Insert new data in internal reader queue |
| 174 | + type_object_reader_->simulate_data_reception(std::move(data)); |
| 175 | +} |
| 176 | + |
| 177 | +std::unique_ptr<fastdds::dds::DomainParticipantListener> XmlDynTypesParticipant::create_listener_() |
| 178 | +{ |
| 179 | + return std::make_unique<XmlDynTypesDdsListener>(configuration_, discovery_database_, type_object_reader_); |
| 180 | +} |
| 181 | + |
| 182 | +} /* namespace participants */ |
| 183 | +} /* namespace ddspipe */ |
| 184 | +} /* namespace eprosima */ |
0 commit comments