Skip to content

Commit ae6efc9

Browse files
committed
Refs #21185: Throw error if a publication fail
Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com>
1 parent d89d2eb commit ae6efc9

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

examples/cpp/flow_control/PublisherApp.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ PublisherApp::PublisherApp(
8080

8181
if (publisher_ == nullptr)
8282
{
83-
throw std::runtime_error("Fast Publisher initialization failed");
83+
throw std::runtime_error("Publisher initialization failed");
8484
}
8585

8686
// Create Topic
@@ -182,18 +182,27 @@ void PublisherApp::run()
182182
FlowControl msg;
183183

184184
/* Initialize your structure here */
185-
uint16_t samples_fast = 0;
186-
uint16_t samples_slow = 0;
187-
while (!is_stopped() && ((samples_ == 0) || ((samples_fast < samples_) && (samples_slow < samples_))))
185+
186+
while (!is_stopped() && ((samples_ == 0) || (msg.index() < samples_)))
188187
{
189-
if (publish(slow_writer_, samples_slow, msg))
188+
msg.index(msg.index() + 1);
189+
190+
if (publish(slow_writer_, msg))
191+
{
192+
std::cout << "Message SENT from SLOW WRITER, count=" << msg.index() << std::endl;
193+
}
194+
else
190195
{
191-
std::cout << "Message SENT from SLOW WRITER, count=" << samples_slow << std::endl;
196+
throw std::runtime_error("Slow Publisher failed sending a message");
192197
}
193198

194-
if (publish(fast_writer_, samples_fast, msg))
199+
if (publish(fast_writer_, msg))
200+
{
201+
std::cout << "Message SENT from FAST WRITER, count=" << msg.index() << std::endl;
202+
}
203+
else
195204
{
196-
std::cout << "Message SENT from FAST WRITER, count=" << samples_fast << std::endl;
205+
throw std::runtime_error("Fast Publisher failed sending a message");
197206
}
198207

199208
// Wait for period or stop event
@@ -207,7 +216,6 @@ void PublisherApp::run()
207216

208217
bool PublisherApp::publish(
209218
DataWriter* writer_,
210-
uint16_t& samples,
211219
FlowControl msg)
212220
{
213221
bool ret = false;
@@ -221,8 +229,6 @@ bool PublisherApp::publish(
221229

222230
if (!is_stopped())
223231
{
224-
++samples;
225-
msg.index(samples);
226232
ret = writer_->write(&msg);
227233
}
228234
return ret;

examples/cpp/flow_control/PublisherApp.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class PublisherApp : public Application, public DataWriterListener
7272
//! Publish a sample
7373
bool publish(
7474
DataWriter* writer_,
75-
uint16_t& samples,
7675
FlowControl msg);
7776

7877
DomainParticipant* participant_;

0 commit comments

Comments
 (0)