Skip to content

Commit 8469835

Browse files
committed
Refs #21185: Apply suggastion
Signed-off-by: elianalf <62831776+elianalf@users.noreply.github.com>
1 parent df0a0bf commit 8469835

4 files changed

Lines changed: 81 additions & 98 deletions

File tree

examples/cpp/flow_control/CLIParser.hpp

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -185,42 +185,40 @@ class CLIParser
185185
}
186186
else if (arg == "--max-bytes")
187187
{
188-
if (config.entity == CLIParser::EntityKind::PUBLISHER)
188+
if (config.entity != CLIParser::EntityKind::PUBLISHER)
189189
{
190-
if (++i < argc)
190+
EPROSIMA_LOG_ERROR(CLI_PARSER, "max-bytes argument is only valid for publisher entity");
191+
print_help(EXIT_FAILURE);
192+
}
193+
194+
if (++i < argc)
195+
{
196+
try
191197
{
192-
try
193-
{
194-
unsigned long input = std::stoul(argv[i]);
195-
196-
if (input > static_cast<unsigned long>(std::numeric_limits<int32_t>::max()))
197-
{
198-
throw std::out_of_range("max-bytes argument " + std::string(
199-
argv[i]) + " out of range [0, 2147483647].");
200-
}
201-
config.max_bytes_per_period = static_cast<int32_t>(input);
202-
}
203-
catch (const std::invalid_argument& e)
204-
{
205-
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid max-bytes argument " + std::string(
206-
argv[i]) + ": " + std::string(e.what()));
207-
print_help(EXIT_FAILURE);
208-
}
209-
catch (const std::out_of_range& e)
198+
unsigned long input = std::stoul(argv[i]);
199+
200+
if (input > static_cast<unsigned long>(std::numeric_limits<int32_t>::max()))
210201
{
211-
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
212-
print_help(EXIT_FAILURE);
202+
throw std::out_of_range("max-bytes argument " + std::string(
203+
argv[i]) + " out of range [0, 2147483647].");
213204
}
205+
config.max_bytes_per_period = static_cast<int32_t>(input);
214206
}
215-
else
207+
catch (const std::invalid_argument& e)
208+
{
209+
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid max-bytes argument " + std::string(
210+
argv[i]) + ": " + std::string(e.what()));
211+
print_help(EXIT_FAILURE);
212+
}
213+
catch (const std::out_of_range& e)
216214
{
217-
EPROSIMA_LOG_ERROR(CLI_PARSER, "parsing max-bytes argument");
215+
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
218216
print_help(EXIT_FAILURE);
219217
}
220218
}
221219
else
222220
{
223-
EPROSIMA_LOG_ERROR(CLI_PARSER, "max-bytes argument is only valid for publisher entity");
221+
EPROSIMA_LOG_ERROR(CLI_PARSER, "parsing max-bytes argument");
224222
print_help(EXIT_FAILURE);
225223
}
226224
}
@@ -236,25 +234,14 @@ class CLIParser
236234
{
237235
try
238236
{
239-
uint64_t input = std::stoull(argv[i]);
240-
if (input > std::numeric_limits<uint64_t>::max())
241-
{
242-
throw std::out_of_range("period argument " + std::string(
243-
argv[i]) + " out of range.");
244-
}
245-
config.period = input;
237+
config.period = std::stoull(argv[i]);
246238
}
247239
catch (const std::invalid_argument& e)
248240
{
249241
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid period argument " + std::string(
250242
argv[i]) + ": " + std::string(e.what()));
251243
print_help(EXIT_FAILURE);
252244
}
253-
catch (const std::out_of_range& e)
254-
{
255-
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
256-
print_help(EXIT_FAILURE);
257-
}
258245
}
259246
else
260247
{
@@ -265,38 +252,38 @@ class CLIParser
265252
}
266253
else if (arg == "--scheduler")
267254
{
255+
if (config.entity != CLIParser::EntityKind::PUBLISHER)
256+
{
257+
EPROSIMA_LOG_ERROR(CLI_PARSER, "scheduler argument is only valid for publisher entity");
258+
print_help(EXIT_FAILURE);
259+
}
260+
268261
if (++i < argc)
269262
{
270263
std::string scheduler = argv[i];
271-
if (config.entity == CLIParser::EntityKind::PUBLISHER)
264+
265+
if (scheduler == "FIFO")
272266
{
273-
if (scheduler == "FIFO")
274-
{
275-
config.scheduler = rtps::FlowControllerSchedulerPolicy::FIFO;
276-
}
277-
else if (scheduler == "ROUND-ROBIN")
278-
{
279-
config.scheduler = rtps::FlowControllerSchedulerPolicy::ROUND_ROBIN;
280-
}
281-
else if (scheduler == "HIGH-PRIORITY")
282-
{
283-
config.scheduler = rtps::FlowControllerSchedulerPolicy::HIGH_PRIORITY;
284-
}
285-
else if (scheduler == "PRIORITY-RESERVATION")
286-
{
287-
config.scheduler = rtps::FlowControllerSchedulerPolicy::PRIORITY_WITH_RESERVATION;
288-
}
289-
else
290-
{
291-
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown argument ");
292-
print_help(EXIT_FAILURE);
293-
}
267+
config.scheduler = rtps::FlowControllerSchedulerPolicy::FIFO;
268+
}
269+
else if (scheduler == "ROUND-ROBIN")
270+
{
271+
config.scheduler = rtps::FlowControllerSchedulerPolicy::ROUND_ROBIN;
272+
}
273+
else if (scheduler == "HIGH-PRIORITY")
274+
{
275+
config.scheduler = rtps::FlowControllerSchedulerPolicy::HIGH_PRIORITY;
276+
}
277+
else if (scheduler == "PRIORITY-RESERVATION")
278+
{
279+
config.scheduler = rtps::FlowControllerSchedulerPolicy::PRIORITY_WITH_RESERVATION;
294280
}
295281
else
296282
{
297-
EPROSIMA_LOG_ERROR(CLI_PARSER, "scheduler argument is only valid for publisher entity");
283+
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown argument ");
298284
print_help(EXIT_FAILURE);
299285
}
286+
300287
}
301288
else
302289
{
@@ -306,6 +293,12 @@ class CLIParser
306293
}
307294
else if (arg == "--bandwidth")
308295
{
296+
if (config.entity != CLIParser::EntityKind::PUBLISHER)
297+
{
298+
EPROSIMA_LOG_ERROR(CLI_PARSER, "bandwidth argument is only valid for publisher entity");
299+
print_help(EXIT_FAILURE);
300+
}
301+
309302
if (++i < argc)
310303
{
311304
try
@@ -317,15 +310,7 @@ class CLIParser
317310
argv[i]) + " out of range.");
318311
}
319312

320-
if (config.entity == CLIParser::EntityKind::PUBLISHER)
321-
{
322-
config.bandwidth = argv[i];
323-
}
324-
else
325-
{
326-
EPROSIMA_LOG_ERROR(CLI_PARSER, "bandwidth argument is only valid for publisher entity");
327-
print_help(EXIT_FAILURE);
328-
}
313+
config.bandwidth = argv[i];
329314
}
330315
catch (const std::invalid_argument& e)
331316
{
@@ -347,6 +332,12 @@ class CLIParser
347332
}
348333
else if (arg == "--priority")
349334
{
335+
if (config.entity != CLIParser::EntityKind::PUBLISHER)
336+
{
337+
EPROSIMA_LOG_ERROR(CLI_PARSER, "priority argument is only valid for publisher entity");
338+
print_help(EXIT_FAILURE);
339+
}
340+
350341
if (++i < argc)
351342
{
352343
try
@@ -358,15 +349,7 @@ class CLIParser
358349
argv[i]) + " out of range.");
359350
}
360351

361-
if (config.entity == CLIParser::EntityKind::PUBLISHER)
362-
{
363-
config.priority = argv[i];
364-
}
365-
else
366-
{
367-
EPROSIMA_LOG_ERROR(CLI_PARSER, "priority argument is only valid for publisher entity");
368-
print_help(EXIT_FAILURE);
369-
}
352+
config.priority = argv[i];
370353
}
371354
catch (const std::invalid_argument& e)
372355
{

examples/cpp/flow_control/PublisherApp.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919

2020
#include "PublisherApp.hpp"
2121

22+
#include <chrono>
2223
#include <condition_variable>
2324
#include <cstdlib>
2425
#include <iostream>
2526
#include <mutex>
2627
#include <stdexcept>
2728

2829
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
29-
#include <fastdds/dds/domain/DomainParticipant.hpp>
30+
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
3031
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
31-
#include <fastdds/dds/topic/TypeSupport.hpp>
3232

33+
#include "FlowControl.hpp"
3334
#include "FlowControlPubSubTypes.hpp"
3435

3536
using namespace eprosima::fastdds::dds;
@@ -181,8 +182,6 @@ void PublisherApp::run()
181182
// Publication code
182183
FlowControl msg;
183184

184-
/* Initialize your structure here */
185-
186185
while (!is_stopped() && ((samples_ == 0) || (msg.index() < samples_)))
187186
{
188187
msg.index(msg.index() + 1);

examples/cpp/flow_control/SubscriberApp.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "SubscriberApp.hpp"
2121

2222
#include <condition_variable>
23-
#include <cstdlib>
23+
#include <cstdint>
2424
#include <iostream>
2525
#include <mutex>
2626
#include <stdexcept>
@@ -138,17 +138,17 @@ void SubscriberApp::on_data_available(
138138
static unsigned int fast_messages = 0;
139139
static unsigned int slow_messages = 0;
140140

141-
auto it_f = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.sample_identity.writer_guid());
141+
auto it_f = std::find(fast_writer_guid_.begin(), fast_writer_guid_.end(), info.sample_identity.writer_guid());
142142

143-
auto it_s = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.sample_identity.writer_guid());
143+
auto it_s = std::find(slow_writer_guid_.begin(), slow_writer_guid_.end(), info.sample_identity.writer_guid());
144144

145-
if (it_f != fast_writer_guid.end())
145+
if (it_f != fast_writer_guid_.end())
146146
{
147147
fast_messages++;
148148
std::cout << "Sample RECEIVED from FAST writer with id " << *it_f << ", index=" << msg.index() <<
149149
std::endl;
150150
}
151-
else if (it_s != slow_writer_guid.end())
151+
else if (it_s != slow_writer_guid_.end())
152152
{
153153
slow_messages++;
154154
std::cout << "Sample RECEIVED from SLOW writer with id " << *it_s << ", index=" << msg.index() <<
@@ -176,18 +176,18 @@ void SubscriberApp::on_data_writer_discovery(
176176
if (info.status ==
177177
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
178178
{
179-
fast_writer_guid.push_back(info.info.guid());
179+
fast_writer_guid_.push_back(info.info.guid());
180180

181181
std::cout << "Fast writer with id " << info.info.guid() << " matched" << std::endl;
182182
}
183183
else if (info.status ==
184184
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::REMOVED_WRITER)
185185
{
186-
auto it = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.info.guid());
186+
auto it = std::find(fast_writer_guid_.begin(), fast_writer_guid_.end(), info.info.guid());
187187

188-
if (it != fast_writer_guid.end())
188+
if (it != fast_writer_guid_.end())
189189
{
190-
fast_writer_guid.erase(it);
190+
fast_writer_guid_.erase(it);
191191
std::cout << "Fast writer with id " << info.info.guid() << " removed" << std::endl;
192192
}
193193
}
@@ -197,18 +197,18 @@ void SubscriberApp::on_data_writer_discovery(
197197
if (info.status ==
198198
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
199199
{
200-
slow_writer_guid.push_back(info.info.guid());
200+
slow_writer_guid_.push_back(info.info.guid());
201201

202202
std::cout << "Slow writer with id " << info.info.guid() << " matched" << std::endl;
203203
}
204204
else if (info.status ==
205205
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::REMOVED_WRITER)
206206
{
207-
auto it = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.info.guid());
207+
auto it = std::find(slow_writer_guid_.begin(), slow_writer_guid_.end(), info.info.guid());
208208

209-
if (it != slow_writer_guid.end())
209+
if (it != slow_writer_guid_.end())
210210
{
211-
slow_writer_guid.erase(it);
211+
slow_writer_guid_.erase(it);
212212
std::cout << "Slow writer with id " << info.info.guid() << " removed" << std::endl;
213213
}
214214
}

examples/cpp/flow_control/SubscriberApp.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <atomic>
2525
#include <condition_variable>
26-
#include <cstdlib>
26+
#include <cstdint>
2727
#include <mutex>
2828

2929
#include <fastdds/dds/domain/DomainParticipant.hpp>
@@ -32,6 +32,7 @@
3232
#include <fastdds/dds/subscriber/DataReader.hpp>
3333
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
3434
#include <fastdds/dds/topic/Topic.hpp>
35+
#include <fastdds/dds/topic/TypeSupport.hpp>
3536

3637
#include "Application.hpp"
3738
#include "CLIParser.hpp"
@@ -96,9 +97,9 @@ class SubscriberApp : public Application, public DomainParticipantListener
9697

9798
std::condition_variable terminate_cv_;
9899

99-
std::vector<eprosima::fastdds::rtps::GUID_t> slow_writer_guid;
100+
std::vector<eprosima::fastdds::rtps::GUID_t> slow_writer_guid_;
100101

101-
std::vector<eprosima::fastdds::rtps::GUID_t> fast_writer_guid;
102+
std::vector<eprosima::fastdds::rtps::GUID_t> fast_writer_guid_;
102103

103104
};
104105

0 commit comments

Comments
 (0)