@@ -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
0 commit comments