Skip to content

Commit

Permalink
Strip dynamic types management from the McapHandler
Browse files Browse the repository at this point in the history
Signed-off-by: tempate <[email protected]>
  • Loading branch information
Tempate committed May 10, 2024
1 parent 4fcc5f1 commit 66bc804
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,35 +206,9 @@ class McapHandler : public BaseHandler
mcap::SchemaId get_schema_id_nts_(
const std::string& schema_name);

/**
* @brief Serialize type identifier and object, and insert the result into a \c DynamicTypesCollection .
*
* @param [in] type_name Name of the type to be stored, used as key in \c dynamic_types map.
*/
void store_dynamic_type_(
const std::string& type_name,
DynamicTypesCollection& dynamic_types) const;

/**
* @brief Serialize type identifier and object, and insert the result into a \c DynamicTypesCollection .
*
* @param [in] type_identifier Type identifier to be serialized and stored.
* @param [in] type_object Type object to be serialized and stored.
* @param [in] type_name Name of the type to be stored, used as key in \c dynamic_types map.
* @param [in,out] dynamic_types Collection where to store serialized dynamic type.
*/
void store_dynamic_type_(
const eprosima::fastrtps::types::TypeIdentifier* type_identifier,
const eprosima::fastrtps::types::TypeObject* type_object,
const std::string& type_name,
DynamicTypesCollection& dynamic_types) const;

//! MCAP writer
McapWriter mcap_writer_;

//! Received types set
std::set<std::string> received_types_;

//! Schemas map
std::map<std::string, mcap::Schema> schemas_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <thread>
#include <utility>

Expand Down Expand Up @@ -274,6 +276,26 @@ class BaseHandler : public ddspipe::participants::ISchemaHandler
*/
void remove_outdated_samples_nts_();

/**
* @brief Create a \c DynamicType and insert it into \c dynamic_types_ .
*
* @param [in] type_name Name of the type to be stored, used as key in \c dynamic_types map.
*/
void store_dynamic_type_(
const std::string& type_name);

/**
* @brief Create a \c DynamicType and insert it into \c dynamic_types_ .
*
* @param [in] type_identifier Type identifier to serialize and store.
* @param [in] type_object Type object to serialize and store.
* @param [in] type_name Name of the type to store, used as key in \c dynamic_types map.
*/
void store_dynamic_type_(
const fastrtps::types::TypeIdentifier* type_identifier,
const fastrtps::types::TypeObject* type_object,
const std::string& type_name);

//! Handler configuration
McapHandlerConfiguration configuration_;

Expand Down Expand Up @@ -314,6 +336,16 @@ class BaseHandler : public ddspipe::participants::ISchemaHandler

//! Structure where messages (received in PAUSED state) with unknown type are kept
std::map<std::string, std::list<const BaseMessage*>> pending_samples_paused_;

//////////////////////////////
// DYNAMIC TYPES COLLECTION //
//////////////////////////////

//! Received types set
std::set<std::string> received_types_;

//! Dynamic types collection
DynamicTypesCollection dynamic_types_;
};

} /* namespace participants */
Expand Down
88 changes: 1 addition & 87 deletions ddsrecorder_participants/src/cpp/recorder/mcap/McapHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@

#include <ddspipe_core/types/dynamic_types/schema.hpp>

#if FASTRTPS_VERSION_MAJOR <= 2 && FASTRTPS_VERSION_MINOR < 13
#include <fastcdr/Cdr.h>
#include <fastcdr/FastBuffer.h>
#include <fastcdr/FastCdr.h>
#include <ddsrecorder_participants/common/types/dynamic_types_collection/v1/DynamicTypesCollection.hpp>
#include <ddsrecorder_participants/common/types/dynamic_types_collection/v1/DynamicTypesCollectionPubSubTypes.hpp>
#else
#include <fastdds/rtps/common/CdrSerialization.hpp>
#include <ddsrecorder_participants/common/types/dynamic_types_collection/v2/DynamicTypesCollection.hpp>
#include <ddsrecorder_participants/common/types/dynamic_types_collection/v2/DynamicTypesCollectionPubSubTypes.hpp>
#endif // if FASTRTPS_VERSION_MAJOR <= 2 && FASTRTPS_VERSION_MINOR < 13

#include <ddsrecorder_participants/constants.hpp>
#include <ddsrecorder_participants/recorder/mcap/McapHandler.hpp>
#include <ddsrecorder_participants/recorder/message/McapMessage.hpp>
Expand Down Expand Up @@ -152,7 +140,7 @@ void McapHandler::add_schema(
received_types_.insert(type_name);

// Every time a dynamic type is added the attachment is newly calculated
store_dynamic_type_(type_name, dynamic_types_);
store_dynamic_type_(type_name);

if (configuration_.record_types)
{
Expand Down Expand Up @@ -360,80 +348,6 @@ mcap::SchemaId McapHandler::get_schema_id_nts_(
}
}

void McapHandler::store_dynamic_type_(
const std::string& type_name,
DynamicTypesCollection& dynamic_types) const
{
const eprosima::fastrtps::types::TypeIdentifier* type_identifier = nullptr;
const eprosima::fastrtps::types::TypeObject* type_object = nullptr;
const eprosima::fastrtps::types::TypeInformation* type_information = nullptr;

type_information =
eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_information(type_name);
if (type_information != nullptr)
{
auto dependencies = type_information->complete().dependent_typeids();
std::string dependency_name;
unsigned int dependency_index = 0;
for (auto dependency: dependencies)
{
type_identifier = &dependency.type_id();
type_object = eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_object(
type_identifier);
dependency_name = type_name + "_" + std::to_string(dependency_index);

// Store dependency in dynamic_types collection
store_dynamic_type_(type_identifier, type_object, dependency_name, dynamic_types);

// Increment suffix counter
dependency_index++;
}
}

type_identifier = nullptr;
type_object = nullptr;

type_identifier = eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_identifier(type_name,
true);
if (type_identifier)
{
type_object =
eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_object(type_name, true);
}

// If complete not found, try with minimal
if (!type_object)
{
type_identifier = eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_identifier(
type_name, false);
if (type_identifier)
{
type_object = eprosima::fastrtps::types::TypeObjectFactory::get_instance()->get_type_object(type_name,
false);
}
}

// Store dynamic type in dynamic_types collection
store_dynamic_type_(type_identifier, type_object, type_name, dynamic_types);
}

void McapHandler::store_dynamic_type_(
const eprosima::fastrtps::types::TypeIdentifier* type_identifier,
const eprosima::fastrtps::types::TypeObject* type_object,
const std::string& type_name,
DynamicTypesCollection& dynamic_types) const
{
if (type_identifier != nullptr && type_object != nullptr)
{
DynamicType dynamic_type;
dynamic_type.type_name(type_name);
dynamic_type.type_information(utils::base64_encode(Serializer::serialize(*type_identifier)));
dynamic_type.type_object(utils::base64_encode(Serializer::serialize(*type_object)));

dynamic_types.dynamic_types().push_back(dynamic_type);
}
}

} /* namespace participants */
} /* namespace ddsrecorder */
} /* namespace eprosima */
76 changes: 76 additions & 0 deletions ddsrecorder_participants/src/cpp/recorder/output/BaseHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <algorithm>
#include <chrono>

#include <fastrtps/types/TypeObjectFactory.h>

#include <cpp_utils/time/time_utils.hpp>

#include <ddspipe_core/types/dynamic_types/schema.hpp>
Expand All @@ -36,6 +38,7 @@
#include <ddsrecorder_participants/recorder/mcap/utils.hpp>
#include <ddsrecorder_participants/recorder/message/BaseMessage.hpp>
#include <ddsrecorder_participants/recorder/output/BaseHandler.hpp>
#include <ddsrecorder_participants/recorder/output/Serializer.hpp>

namespace eprosima {
namespace ddsrecorder {
Expand Down Expand Up @@ -497,6 +500,79 @@ void BaseHandler::remove_outdated_samples_nts_()
}
}

void BaseHandler::store_dynamic_type_(
const std::string& type_name)
{
auto type_object_factory = fastrtps::types::TypeObjectFactory::get_instance();
const auto type_information = type_object_factory->get_type_information(type_name);

if (type_information != nullptr)
{
// Store dependencies as dynamic types
auto dependencies = type_information->complete().dependent_typeids();
unsigned int dependency_index = 0;

for (auto dependency : dependencies)
{
const auto type_identifier = &dependency.type_id();
const auto type_object = type_object_factory->get_type_object(type_identifier);
const auto dependency_name = type_name + "_" + std::to_string(dependency_index);

// Store dependency in dynamic_types collection
store_dynamic_type_(type_identifier, type_object, dependency_name);

// Increment suffix counter
dependency_index++;
}
}

const fastrtps::types::TypeObject* type_object = nullptr;
const fastrtps::types::TypeIdentifier* type_identifier = type_object_factory->get_type_identifier(type_name, true);

if (type_identifier)
{
type_object = type_object_factory->get_type_object(type_name, true);

// If complete not found, try with minimal
if (!type_object)
{
type_identifier = type_object_factory->get_type_identifier(type_name, false);

if (type_identifier)
{
type_object = type_object_factory->get_type_object(type_name, false);
}
}
}

// Store dynamic type in dynamic_types collection
store_dynamic_type_(type_identifier, type_object, type_name);
}

void BaseHandler::store_dynamic_type_(
const fastrtps::types::TypeIdentifier* type_identifier,
const fastrtps::types::TypeObject* type_object,
const std::string& type_name)
{
if (type_identifier == nullptr)
{
logWarning(DDSRECORDER_MCAP_HANDLER, "Attempting to store a DynamicType without a type identifier. Exiting.");
return;
}

if (type_object == nullptr)
{
logWarning(DDSRECORDER_MCAP_HANDLER, "Attempting to store a DynamicType without a type object. Exiting.");
return;
}

DynamicType dynamic_type;
dynamic_type.type_name(type_name);
dynamic_type.type_information(utils::base64_encode(Serializer::serialize(*type_identifier)));
dynamic_type.type_object(utils::base64_encode(Serializer::serialize(*type_object)));
dynamic_types_.dynamic_types().push_back(dynamic_type);
}

} /* namespace participants */
} /* namespace ddsrecorder */
} /* namespace eprosima */

0 comments on commit 66bc804

Please sign in to comment.