Skip to content

Commit 089843f

Browse files
committed
Refs #21185: Manage receiving messages from multiple publishers
Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com>
1 parent ae6efc9 commit 089843f

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

examples/cpp/flow_control/SubscriberApp.cpp

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ SubscriberApp::SubscriberApp(
8989
// Create DataReader
9090
DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT;
9191
subscriber_->get_default_datareader_qos(reader_qos);
92-
reader_ = subscriber_->create_datareader(topic_, reader_qos, this, StatusMask::all());
93-
92+
reader_ = subscriber_->create_datareader(topic_, reader_qos);
93+
9494
if (reader_ == nullptr)
9595
{
9696
throw std::runtime_error("DataReader initialization failed");
@@ -137,21 +137,25 @@ void SubscriberApp::on_data_available(
137137
{
138138
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
139139
{
140-
static unsigned int fastMessages = 0;
141-
static unsigned int slowMessages = 0;
140+
static unsigned int fast_messages = 0;
141+
static unsigned int slow_messages = 0;
142+
143+
auto it_f = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.sample_identity.writer_guid());
144+
145+
auto it_s = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.sample_identity.writer_guid());
142146

143-
if (info.sample_identity.writer_guid().entityId == fast_writer_guid)
147+
if (it_f != fast_writer_guid.end())
144148
{
145-
fastMessages++;
146-
std::cout << "Sample RECEIVED from FAST writer, index=" << msg.index() << std::endl;
149+
fast_messages++;
150+
std::cout << "Sample RECEIVED from FAST writer with id " << *it_f << ", index=" << msg.index() << std::endl;
147151
}
148-
else if (info.sample_identity.writer_guid().entityId == slow_writer_guid)
152+
else if (it_s != slow_writer_guid.end())
149153
{
150-
slowMessages++;
151-
std::cout << "Sample RECEIVED from SLOW writer, index=" << msg.index() << std::endl;
154+
slow_messages++;
155+
std::cout << "Sample RECEIVED from SLOW writer with id " << *it_s << ", index=" << msg.index() << std::endl;
152156
}
153157

154-
if ((samples_ > 0) && (fastMessages >= samples_) && (slowMessages >= samples_))
158+
if ((samples_ > 0) && (fast_messages >= samples_) && (slow_messages >= samples_))
155159
{
156160
stop();
157161
}
@@ -169,13 +173,45 @@ void SubscriberApp::on_data_writer_discovery(
169173

170174
if (info.info.m_qos.m_userData.data_vec() == fast_writer_id)
171175
{
172-
fast_writer_guid = info.info.guid().entityId;
173-
std::cout << "Fast writer id " << fast_writer_guid << std::endl;
176+
if (info.status ==
177+
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
178+
{
179+
fast_writer_guid.push_back(info.info.guid());
180+
181+
std::cout << "Fast writer with id " << info.info.guid() << " matched" << std::endl;
182+
}
183+
else if (info.status ==
184+
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::REMOVED_WRITER)
185+
{
186+
auto it = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.info.guid());
187+
188+
if (it != fast_writer_guid.end())
189+
{
190+
fast_writer_guid.erase(it);
191+
std::cout << "Fast writer with id " << info.info.guid() << " removed" << std::endl;
192+
}
193+
}
174194
}
175195
else if (info.info.m_qos.m_userData.data_vec() == slow_writer_id)
176196
{
177-
slow_writer_guid = info.info.guid().entityId;
178-
std::cout << "Slow writer id " << slow_writer_guid << std::endl;
197+
if (info.status ==
198+
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
199+
{
200+
slow_writer_guid.push_back(info.info.guid());
201+
202+
std::cout << "Slow writer with id " << info.info.guid() << " matched" << std::endl;
203+
}
204+
else if (info.status ==
205+
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::REMOVED_WRITER)
206+
{
207+
auto it = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.info.guid());
208+
209+
if (it != slow_writer_guid.end())
210+
{
211+
slow_writer_guid.erase(it);
212+
std::cout << "Slow writer with id " << info.info.guid() << " removed" << std::endl;
213+
}
214+
}
179215
}
180216
}
181217

examples/cpp/flow_control/SubscriberApp.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ class SubscriberApp : public Application, public DomainParticipantListener
9696

9797
std::condition_variable terminate_cv_;
9898

99-
eprosima::fastdds::rtps::EntityId_t slow_writer_guid;
99+
std::vector<eprosima::fastdds::rtps::GUID_t> slow_writer_guid;
100100

101-
eprosima::fastdds::rtps::EntityId_t fast_writer_guid;
101+
std::vector<eprosima::fastdds::rtps::GUID_t> fast_writer_guid;
102102

103103
};
104104

0 commit comments

Comments
 (0)