Skip to content

Commit

Permalink
Refs #21185: Apply suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: elianalf <[email protected]>
  • Loading branch information
elianalf committed Jul 15, 2024
1 parent df0a0bf commit 48a1596
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 98 deletions.
139 changes: 61 additions & 78 deletions examples/cpp/flow_control/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,42 +185,40 @@ class CLIParser
}
else if (arg == "--max-bytes")
{
if (config.entity == CLIParser::EntityKind::PUBLISHER)
if (config.entity != CLIParser::EntityKind::PUBLISHER)
{
if (++i < argc)
EPROSIMA_LOG_ERROR(CLI_PARSER, "max-bytes argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}

if (++i < argc)
{
try
{
try
{
unsigned long input = std::stoul(argv[i]);

if (input > static_cast<unsigned long>(std::numeric_limits<int32_t>::max()))
{
throw std::out_of_range("max-bytes argument " + std::string(
argv[i]) + " out of range [0, 2147483647].");
}
config.max_bytes_per_period = static_cast<int32_t>(input);
}
catch (const std::invalid_argument& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid max-bytes argument " + std::string(
argv[i]) + ": " + std::string(e.what()));
print_help(EXIT_FAILURE);
}
catch (const std::out_of_range& e)
unsigned long input = std::stoul(argv[i]);

if (input > static_cast<unsigned long>(std::numeric_limits<int32_t>::max()))
{
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
print_help(EXIT_FAILURE);
throw std::out_of_range("max-bytes argument " + std::string(
argv[i]) + " out of range [0, 2147483647].");
}
config.max_bytes_per_period = static_cast<int32_t>(input);
}
else
catch (const std::invalid_argument& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid max-bytes argument " + std::string(
argv[i]) + ": " + std::string(e.what()));
print_help(EXIT_FAILURE);
}
catch (const std::out_of_range& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "parsing max-bytes argument");
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
print_help(EXIT_FAILURE);
}
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "max-bytes argument is only valid for publisher entity");
EPROSIMA_LOG_ERROR(CLI_PARSER, "parsing max-bytes argument");
print_help(EXIT_FAILURE);
}
}
Expand All @@ -236,25 +234,14 @@ class CLIParser
{
try
{
uint64_t input = std::stoull(argv[i]);
if (input > std::numeric_limits<uint64_t>::max())
{
throw std::out_of_range("period argument " + std::string(
argv[i]) + " out of range.");
}
config.period = input;
config.period = std::stoull(argv[i]);
}
catch (const std::invalid_argument& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid period argument " + std::string(
argv[i]) + ": " + std::string(e.what()));
print_help(EXIT_FAILURE);
}
catch (const std::out_of_range& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, std::string(e.what()));
print_help(EXIT_FAILURE);
}
}
else
{
Expand All @@ -265,38 +252,38 @@ class CLIParser
}
else if (arg == "--scheduler")
{
if (config.entity != CLIParser::EntityKind::PUBLISHER)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "scheduler argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}

if (++i < argc)
{
std::string scheduler = argv[i];
if (config.entity == CLIParser::EntityKind::PUBLISHER)

if (scheduler == "FIFO")
{
if (scheduler == "FIFO")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::FIFO;
}
else if (scheduler == "ROUND-ROBIN")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::ROUND_ROBIN;
}
else if (scheduler == "HIGH-PRIORITY")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::HIGH_PRIORITY;
}
else if (scheduler == "PRIORITY-RESERVATION")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::PRIORITY_WITH_RESERVATION;
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown argument ");
print_help(EXIT_FAILURE);
}
config.scheduler = rtps::FlowControllerSchedulerPolicy::FIFO;
}
else if (scheduler == "ROUND-ROBIN")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::ROUND_ROBIN;
}
else if (scheduler == "HIGH-PRIORITY")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::HIGH_PRIORITY;
}
else if (scheduler == "PRIORITY-RESERVATION")
{
config.scheduler = rtps::FlowControllerSchedulerPolicy::PRIORITY_WITH_RESERVATION;
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "scheduler argument is only valid for publisher entity");
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown argument ");
print_help(EXIT_FAILURE);
}

}
else
{
Expand All @@ -306,6 +293,12 @@ class CLIParser
}
else if (arg == "--bandwidth")
{
if (config.entity != CLIParser::EntityKind::PUBLISHER)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "bandwidth argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}

if (++i < argc)
{
try
Expand All @@ -317,15 +310,7 @@ class CLIParser
argv[i]) + " out of range.");
}

if (config.entity == CLIParser::EntityKind::PUBLISHER)
{
config.bandwidth = argv[i];
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "bandwidth argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}
config.bandwidth = argv[i];
}
catch (const std::invalid_argument& e)
{
Expand All @@ -347,6 +332,12 @@ class CLIParser
}
else if (arg == "--priority")
{
if (config.entity != CLIParser::EntityKind::PUBLISHER)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "priority argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}

if (++i < argc)
{
try
Expand All @@ -358,15 +349,7 @@ class CLIParser
argv[i]) + " out of range.");
}

if (config.entity == CLIParser::EntityKind::PUBLISHER)
{
config.priority = argv[i];
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "priority argument is only valid for publisher entity");
print_help(EXIT_FAILURE);
}
config.priority = argv[i];
}
catch (const std::invalid_argument& e)
{
Expand Down
7 changes: 3 additions & 4 deletions examples/cpp/flow_control/PublisherApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@

#include "PublisherApp.hpp"

#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <stdexcept>

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

#include "FlowControl.hpp"
#include "FlowControlPubSubTypes.hpp"

using namespace eprosima::fastdds::dds;
Expand Down Expand Up @@ -181,8 +182,6 @@ void PublisherApp::run()
// Publication code
FlowControl msg;

/* Initialize your structure here */

while (!is_stopped() && ((samples_ == 0) || (msg.index() < samples_)))
{
msg.index(msg.index() + 1);
Expand Down
26 changes: 13 additions & 13 deletions examples/cpp/flow_control/SubscriberApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "SubscriberApp.hpp"

#include <condition_variable>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <stdexcept>
Expand Down Expand Up @@ -138,17 +138,17 @@ void SubscriberApp::on_data_available(
static unsigned int fast_messages = 0;
static unsigned int slow_messages = 0;

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

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

if (it_f != fast_writer_guid.end())
if (it_f != fast_writer_guid_.end())
{
fast_messages++;
std::cout << "Sample RECEIVED from FAST writer with id " << *it_f << ", index=" << msg.index() <<
std::endl;
}
else if (it_s != slow_writer_guid.end())
else if (it_s != slow_writer_guid_.end())
{
slow_messages++;
std::cout << "Sample RECEIVED from SLOW writer with id " << *it_s << ", index=" << msg.index() <<
Expand Down Expand Up @@ -176,18 +176,18 @@ void SubscriberApp::on_data_writer_discovery(
if (info.status ==
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
{
fast_writer_guid.push_back(info.info.guid());
fast_writer_guid_.push_back(info.info.guid());

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

if (it != fast_writer_guid.end())
if (it != fast_writer_guid_.end())
{
fast_writer_guid.erase(it);
fast_writer_guid_.erase(it);
std::cout << "Fast writer with id " << info.info.guid() << " removed" << std::endl;
}
}
Expand All @@ -197,18 +197,18 @@ void SubscriberApp::on_data_writer_discovery(
if (info.status ==
eprosima::fastdds::rtps::WriterDiscoveryInfo::DISCOVERY_STATUS::DISCOVERED_WRITER)
{
slow_writer_guid.push_back(info.info.guid());
slow_writer_guid_.push_back(info.info.guid());

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

if (it != slow_writer_guid.end())
if (it != slow_writer_guid_.end())
{
slow_writer_guid.erase(it);
slow_writer_guid_.erase(it);
std::cout << "Slow writer with id " << info.info.guid() << " removed" << std::endl;
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/cpp/flow_control/SubscriberApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <atomic>
#include <condition_variable>
#include <cstdlib>
#include <cstdint>
#include <mutex>

#include <fastdds/dds/domain/DomainParticipant.hpp>
Expand All @@ -32,6 +32,7 @@
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastdds/dds/topic/Topic.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

#include "Application.hpp"
#include "CLIParser.hpp"
Expand Down Expand Up @@ -96,9 +97,9 @@ class SubscriberApp : public Application, public DomainParticipantListener

std::condition_variable terminate_cv_;

std::vector<eprosima::fastdds::rtps::GUID_t> slow_writer_guid;
std::vector<eprosima::fastdds::rtps::GUID_t> slow_writer_guid_;

std::vector<eprosima::fastdds::rtps::GUID_t> fast_writer_guid;
std::vector<eprosima::fastdds::rtps::GUID_t> fast_writer_guid_;

};

Expand Down

0 comments on commit 48a1596

Please sign in to comment.