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
|
// 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 TypeLookupServicePublisher.cpp
*/
#include "TypeLookupServicePublisher.h"
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
#include <fastdds/LibrarySettings.hpp>
using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::rtps;
TypeLookupServicePublisher::~TypeLookupServicePublisher()
{
if (nullptr != participant_)
{
participant_->delete_contained_entities();
DomainParticipantFactory::get_instance()->delete_participant(participant_);
participant_ = nullptr;
}
for (auto& thread : create_types_threads)
{
thread.join();
}
}
bool TypeLookupServicePublisher::init(
uint32_t domain_id,
std::vector<std::string> known_types,
uint32_t builtin_flow_controller_bytes)
{
domain_id_ = domain_id;
create_type_creator_functions();
LibrarySettings settings;
settings.intraprocess_delivery = INTRAPROCESS_OFF;
DomainParticipantFactory::get_instance()->set_library_settings(settings);
auto qos = PARTICIPANT_QOS_DEFAULT;
if (builtin_flow_controller_bytes > 0)
{
auto new_flow_controller = std::make_shared<eprosima::fastdds::rtps::FlowControllerDescriptor>();
new_flow_controller->name = "MyFlowController";
new_flow_controller->max_bytes_per_period = builtin_flow_controller_bytes;
new_flow_controller->period_ms = static_cast<uint64_t>(100000);
qos.flow_controllers().push_back(new_flow_controller);
qos.wire_protocol().builtin.flow_controller_name = new_flow_controller->name;
}
participant_ = DomainParticipantFactory::get_instance()
->create_participant(domain_id, qos, this);
if (participant_ == nullptr)
{
std::cout << "ERROR TypeLookupServicePublisher: create_participant" << std::endl;
return false;
}
for (const auto& type : known_types)
{
if (!create_known_type(type))
{
return false;
}
}
return true;
}
bool TypeLookupServicePublisher::setup_publisher(
PubKnownType& a_type)
{
std::string type_name = a_type.type_sup_.get_type_name();
// CREATE THE PUBLISHER
Publisher* publisher = participant_->create_publisher(PUBLISHER_QOS_DEFAULT);
if (publisher == nullptr)
{
std::cout << "ERROR TypeLookupServicePublisher: create_publisher: " << type_name << std::endl;
return false;
}
// CREATE THE TOPIC
std::ostringstream topic_name;
topic_name << type_name << "_" << asio::ip::host_name() << "_" << domain_id_;
Topic* topic = participant_->create_topic(topic_name.str(), a_type.type_sup_.get_type_name(), TOPIC_QOS_DEFAULT);
if (topic == nullptr)
{
std::cout << "ERROR TypeLookupServicePublisher: create_topic: " << type_name << std::endl;
return false;
}
// CREATE THE DATAWRITER
DataWriterQos wqos = publisher->get_default_datawriter_qos();
wqos.data_sharing().off();
wqos.reliability().kind = RELIABLE_RELIABILITY_QOS;
wqos.durability().kind = TRANSIENT_LOCAL_DURABILITY_QOS;
a_type.writer_ = publisher->create_datawriter(topic, wqos);
if (a_type.writer_ == nullptr)
{
std::cout << "ERROR TypeLookupServicePublisher: create_datawriter" << std::endl;
return false;
}
return true;
}
bool TypeLookupServicePublisher::create_known_type(
const std::string& type)
{
// Check if the type is already created
if (nullptr != participant_->find_type(type))
{
return false;
}
// Find the type creator in the map
auto it = type_creator_functions_.find(type);
if (it != type_creator_functions_.end())
{
// Call the associated type creator function
return it->second(type);
}
else
{
std::cout << "ERROR TypeLookupServicePublisher: init unknown type: " << type << std::endl;
return false;
}
}
template <typename Type, typename TypePubSubType>
bool TypeLookupServicePublisher::create_known_type_impl(
const std::string& type)
{
// Create a new PubKnownType for the given type
PubKnownType a_type;
a_type.type_.reset(new Type());
a_type.type_sup_.reset(new TypePubSubType());
a_type.type_sup_.register_type(participant_);
if (!setup_publisher(a_type))
{
return false;
}
std::lock_guard<std::mutex> guard(known_types_mutex_);
known_types_.emplace(type, a_type);
return true;
}
bool TypeLookupServicePublisher::create_discovered_type(
const SubscriptionBuiltinTopicData& info)
{
std::string new_type_name = info.type_name.to_string();
// Check if the type is already created
if (nullptr != participant_->find_type(new_type_name))
{
return false;
}
PubKnownType a_type;
//CREATE THE DYNAMIC TYPE
xtypes::TypeObject type_object;
if (RETCODE_OK != DomainParticipantFactory::get_instance()->type_object_registry().get_type_object(
info.type_information.type_information.complete().typeid_with_size().type_id(), type_object))
{
std::cout << "ERROR: TypeObject cannot be retrieved for type: " << new_type_name << std::endl;
return false;
}
// Create DynamicType
a_type.dyn_type_ = DynamicTypeBuilderFactory::get_instance()->create_type_w_type_object(type_object)->build();
if (!a_type.dyn_type_)
{
std::cout << "ERROR: DynamicType cannot be created for type: " << new_type_name << std::endl;
return false;
}
// Register the data type
a_type.type_sup_.reset(new DynamicPubSubType(a_type.dyn_type_));
if (RETCODE_OK != a_type.type_sup_.register_type(participant_))
{
std::cout << "ERROR: DynamicType cannot be registered for type: " << new_type_name << std::endl;
return false;
}
if (!setup_publisher(a_type))
{
return false;
}
std::lock_guard<std::mutex> guard(known_types_mutex_);
known_types_.emplace(new_type_name, a_type);
return true;
}
bool TypeLookupServicePublisher::check_registered_type(
const xtypes::TypeInformationParameter& type_info)
{
xtypes::TypeObject type_obj;
return RETCODE_OK == DomainParticipantFactory::get_instance()->type_object_registry().get_type_object(
type_info.type_information.complete().typeid_with_size().type_id(), type_obj);
}
bool TypeLookupServicePublisher::wait_discovery(
uint32_t expected_matches,
uint32_t timeout)
{
expected_matches_ = expected_matches;
std::unique_lock<std::mutex> lock(mutex_);
bool result = cv_.wait_for(lock, std::chrono::seconds(timeout),
[&]()
{
return matched_ == static_cast<int32_t>(expected_matches_);
});
if (!result)
{
std::cout << "ERROR TypeLookupServicePublisher discovery Timeout with matched = " <<
matched_ << std::endl;
return false;
}
return true;
}
bool TypeLookupServicePublisher::run(
uint32_t samples,
uint32_t timeout)
{
std::unique_lock<std::mutex> lock(mutex_);
bool result = cv_.wait_for(
lock, std::chrono::seconds(timeout), [&]
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
uint32_t current_sample {0};
std::string type_name;
while (samples > current_sample)
{
for (auto& known_type : known_types_)
{
type_name = known_type.second.type_sup_.get_type_name();
if (known_type.second.dyn_type_)
{
DynamicData::_ref_type sample =
DynamicDataFactory::get_instance()->create_data(known_type.second.dyn_type_);
if (RETCODE_OK != known_type.second.writer_->write(&sample))
{
std::cout << "ERROR TypeLookupServicePublisher fails writing sample " <<
current_sample + 1 << std::endl;
return false;
}
}
if (known_type.second.type_)
{
void* sample = known_type.second.type_sup_.create_data();
if (RETCODE_OK != known_type.second.writer_->write(sample))
{
std::cout << "ERROR TypeLookupServicePublisher fails writing sample " <<
current_sample + 1 << std::endl;
return false;
}
known_type.second.type_sup_.delete_data(sample);
}
++sent_samples_[known_type.second.writer_->guid()];
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
++current_sample;
}
return true;
});
if (!result)
{
std::cout << "ERROR TypeLookupServicePublisher" << std::endl;
if (expected_matches_ != sent_samples_.size())
{
std::cout << "Expected_matches_ = " << expected_matches_ <<
" Working_writers_ = " << sent_samples_.size() << std::endl;
}
for (auto& sent_sample : sent_samples_)
{
if (samples != sent_sample.second)
{
std::cout << sent_sample.first << "Wrote: "
<< sent_sample.second << " samples" << std::endl;
}
}
return false;
}
return true;
}
void TypeLookupServicePublisher::on_publication_matched(
DataWriter* /*writer*/,
const PublicationMatchedStatus& info)
{
std::unique_lock<std::mutex> lock(mutex_);
matched_ += info.current_count_change;
cv_.notify_all();
}
void TypeLookupServicePublisher::on_data_reader_discovery(
DomainParticipant* /*participant*/,
ReaderDiscoveryStatus reason,
const SubscriptionBuiltinTopicData& info,
bool& should_be_ignored)
{
should_be_ignored = false;
std::string discovered_reader_type_name = info.type_name.to_string();
if (eprosima::fastdds::rtps::ReaderDiscoveryStatus::DISCOVERED_READER == reason)
{
// Check if the type is already created
if (nullptr == participant_->find_type(discovered_reader_type_name))
{
// Check for TypeObjectRegistry inconsistency
std::string modified_type_name = discovered_reader_type_name;
std::replace(modified_type_name.begin(), modified_type_name.end(), ':', '_');
const bool has_type_object = types_without_typeobject_.find(modified_type_name) ==
types_without_typeobject_.end();
const bool is_registered = check_registered_type(info.type_information);
if ((has_type_object && !is_registered))
{
throw std::runtime_error("TypeLookupServiceSubscriber: Type '" +
discovered_reader_type_name + "' is not registered but it should be.");
}
if ((!has_type_object && is_registered))
{
throw std::runtime_error("TypeLookupServiceSubscriber: Type '" +
discovered_reader_type_name + "' is registered but it should not be.");
}
// Create new publisher for the type
if (has_type_object)
{
create_types_threads.emplace_back(&TypeLookupServicePublisher::create_discovered_type, this, info);
}
else
{
create_types_threads.emplace_back(
&TypeLookupServicePublisher::create_known_type, this, discovered_reader_type_name);
}
}
}
}
|