Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20903] Add Remote discovered xtypes introspection section #808

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/fastdds/xtypes/discovery_matching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,71 @@ Remote types discovery example

Please, refer to :ref:`use-case-remote-type-discovery-and-matching` for more information about how to leverage this
feature.

Remote discovered types introspection
-------------------------------------

The following code demonstrates how to implement remote type introspection using FastDDS in C++.
This feature allows a subscriber to introspect the remotly discovered type and serialize it into a more manageable
and understandable format.
Once a type is discovered, it can be registered and every time the subscriber receives new data related to this type,
the corresponding DynamicData can be obtained from the DynamicDataFactory and serialized into a JSON string format.

.. code-block:: c++

class TypeIntrospectionSubscriber : public DomainParticipantListener
{
void on_type_discovered_and_registered_(
const DynamicType::_ref_type& type)
{
// Copy dynamic type
dyn_type_ = type;

// Register type
TypeSupport m_type(new DynamicPubSubType(type));
m_type.register_type(participant_);

// Create topic
topic_ = participant_->create_topic(
topic_name_,
m_type->getName(),
TOPIC_QOS_DEFAULT);

if (topic_ == nullptr)
{
return;
}

// Create DataReader
reader_ = subscriber_->create_datareader(
topic_,
DATAREADER_QOS_DEFAULT,
this);
}

/* Custom Callback on_data_available */
void on_data_available(
DataReader* reader)
{
// Dynamic DataType
DynamicData::_ref_type new_data =
DynamicDataFactory::get_instance()->create_data(dyn_type_);

// Serialize DynamicData into JSON string format
json_serialize(new_data, output, DynamicDataJsonFormat::EPROSIMA);
}

DomainParticipant* participant_;

Subscriber* subscriber_;

Topic* topic_;

DataReader* reader_;

TypeSupport type_;

std::string topic_name_;

DynamicType::_ref_type dyn_type_;
};