Skip to content

Commit 4ff3ee5

Browse files
committed
Refs #21185: Send User Data to share the information about the kind of writer
Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com>
1 parent b68d6a3 commit 4ff3ee5

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

examples/cpp/flow_control/PublisherApp.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ PublisherApp::PublisherApp(
9090
// Disable Data Sharing to force the communication on a Transport,
9191
// since a Flow Control is applied only on Transports.
9292
wsqos.data_sharing().off();
93+
// Set a user data value to share with the DataReader the information about the kind of Data Writer
94+
// (0 for Slow and 1 for Fast)
95+
wsqos.user_data().data_vec({0});
9396
slow_writer_ = publisher_->create_datawriter(topic_, wsqos, this, StatusMask::all());
9497
if (slow_writer_ == nullptr)
9598
{
@@ -104,7 +107,9 @@ PublisherApp::PublisherApp(
104107
wfqos.publish_mode().kind = ASYNCHRONOUS_PUBLISH_MODE;
105108
// Disable Data Sharing to be consistent with the Slow Writer
106109
wfqos.data_sharing().off();
107-
110+
// Set a user data value to share with the DataReader the information about the kind of Data Writer
111+
// (0 for Slow and 1 for Fast)
112+
wfqos.user_data().data_vec({1});
108113
fast_writer_ = publisher_->create_datawriter(topic_, wfqos, this, StatusMask::all());
109114
if (fast_writer_ == nullptr)
110115
{

examples/cpp/flow_control/SubscriberApp.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ SubscriberApp::SubscriberApp(
4444
, type_(new FlowControlPubSubType())
4545
, samples_(config.samples)
4646
, stop_(false)
47+
, slow_writer_guid({0})
48+
, fast_writer_guid({0})
4749
{
50+
StatusMask status_mask = StatusMask::none();
51+
status_mask << StatusMask::data_available();
52+
status_mask << StatusMask::subscription_matched();
4853
// Create Participant
49-
participant_ = DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
54+
participant_ = DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT, this, status_mask);
5055
if (participant_ == nullptr)
5156
{
5257
throw std::runtime_error("Participant initialization failed");
@@ -125,15 +130,15 @@ void SubscriberApp::on_data_available(
125130
static unsigned int fastMessages = 0;
126131
static unsigned int slowMessages = 0;
127132

128-
if (msg.wasFast())
133+
if (info.sample_identity.writer_guid().entityId == fast_writer_guid)
129134
{
130135
fastMessages++;
131-
std::cout << "Sample RECEIVED from fast writer, count=" << fastMessages << std::endl;
136+
std::cout << "Sample RECEIVED from FAST writer, count=" << fastMessages << std::endl;
132137
}
133-
else
138+
else if (info.sample_identity.writer_guid().entityId == slow_writer_guid)
134139
{
135140
slowMessages++;
136-
std::cout << "Sample RECEIVED from slow writer, count=" << slowMessages << std::endl;
141+
std::cout << "Sample RECEIVED from SLOW writer, count=" << slowMessages << std::endl;
137142
}
138143

139144
if ((samples_ > 0) && (fastMessages >= samples_) && (slowMessages >= samples_))
@@ -144,6 +149,26 @@ void SubscriberApp::on_data_available(
144149
}
145150
}
146151

152+
void SubscriberApp::on_data_writer_discovery(
153+
DomainParticipant* /*participant*/,
154+
eprosima::fastdds::rtps::WriterDiscoveryInfo&& info,
155+
bool& /*should_be_ignored*/)
156+
{
157+
std::vector<eprosima::fastdds::rtps::octet> slow_writer_id = {0};
158+
std::vector<eprosima::fastdds::rtps::octet> fast_writer_id = {1};
159+
160+
if (info.info.m_qos.m_userData.data_vec() == fast_writer_id)
161+
{
162+
fast_writer_guid = info.info.guid().entityId;
163+
std::cout << "Fast writer id " << fast_writer_guid << std::endl;
164+
}
165+
else if(info.info.m_qos.m_userData.data_vec() == slow_writer_id)
166+
{
167+
slow_writer_guid = info.info.guid().entityId;
168+
std::cout << "Slow writer id " << slow_writer_guid << std::endl;
169+
}
170+
}
171+
147172
void SubscriberApp::run()
148173
{
149174
std::unique_lock<std::mutex> lck(terminate_cv_mtx_);

examples/cpp/flow_control/SubscriberApp.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <condition_variable>
2525

2626
#include <fastdds/dds/domain/DomainParticipant.hpp>
27+
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
2728
#include <fastdds/dds/subscriber/Subscriber.hpp>
2829
#include <fastdds/dds/subscriber/DataReader.hpp>
2930
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
@@ -40,7 +41,7 @@ namespace fastdds {
4041
namespace examples {
4142
namespace flow_control {
4243

43-
class SubscriberApp : public Application, public DataReaderListener
44+
class SubscriberApp : public Application, public DomainParticipantListener
4445
{
4546
public:
4647

@@ -58,6 +59,11 @@ class SubscriberApp : public Application, public DataReaderListener
5859
DataReader* reader,
5960
const SubscriptionMatchedStatus& info) override;
6061

62+
void on_data_writer_discovery(
63+
DomainParticipant* participant,
64+
eprosima::fastdds::rtps::WriterDiscoveryInfo&& info,
65+
bool& should_be_ignored) override;
66+
6167
//! Run subscriber
6268
void run() override;
6369

@@ -86,6 +92,11 @@ class SubscriberApp : public Application, public DataReaderListener
8692
mutable std::mutex terminate_cv_mtx_;
8793

8894
std::condition_variable terminate_cv_;
95+
96+
eprosima::fastdds::rtps::EntityId_t slow_writer_guid;
97+
98+
eprosima::fastdds::rtps::EntityId_t fast_writer_guid;
99+
89100
};
90101

91102
} // namespace flow_control

0 commit comments

Comments
 (0)