Skip to content

Commit

Permalink
Refs #21886: Add matched argument to hello_world example
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
  • Loading branch information
Mario-DL committed Nov 12, 2024
1 parent b13f3cc commit 16018d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
38 changes: 38 additions & 0 deletions examples/cpp/hello_world/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CLIParser
struct publisher_config
{
uint16_t samples = 0;
uint16_t matched = 1;
};

//! Subscriber application configuration structure
Expand Down Expand Up @@ -83,6 +84,9 @@ class CLIParser
std::cout << " -s <num>, --samples <num> Number of samples to send or receive" << std::endl;
std::cout << " [0 <= <num> <= 65535]" << std::endl;
std::cout << " (Default: 0 [unlimited])" << std::endl;
std::cout << "Publisher options:" << std::endl;
std::cout << " -m, --matched Number of participants to discover" << std::endl;
std::cout << " before start publishing (Default: 1)" << std::endl;
std::cout << "Subscriber options:" << std::endl;
std::cout << " -w, --waitset Use waitset & read condition" << std::endl;
std::exit(return_code);
Expand Down Expand Up @@ -194,6 +198,40 @@ class CLIParser
print_help(EXIT_FAILURE);
}
}
else if (arg == "-m" || arg == "--matched")
{
try
{
int input = std::stoi(argv[++i]);
if (input < std::numeric_limits<std::uint16_t>::min() ||
input > std::numeric_limits<std::uint16_t>::max())
{
throw std::out_of_range("matched argument out of range");
}
else
{
if (config.entity == CLIParser::EntityKind::PUBLISHER)
{
config.pub_config.matched = static_cast<uint16_t>(input);
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "matched can only be used with the publisher entity");
print_help(EXIT_FAILURE);
}
}
}
catch (const std::invalid_argument& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid sample argument for " + arg + ": " + e.what());
print_help(EXIT_FAILURE);
}
catch (const std::out_of_range& e)
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "sample argument out of range for " + arg + ": " + e.what());
print_help(EXIT_FAILURE);
}
}
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown option " + arg);
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/hello_world/PublisherApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ PublisherApp::PublisherApp(
, type_(new HelloWorldPubSubType())
, matched_(0)
, samples_(config.samples)
, expected_matches_(config.matched)
, stop_(false)
{
// Set up the data type with initial values
Expand Down Expand Up @@ -152,7 +153,7 @@ bool PublisherApp::publish()
cv_.wait(matched_lock, [&]()
{
// at least one has been discovered
return ((matched_ > 0) || is_stopped());
return ((matched_ >= expected_matches_) || is_stopped());
});

if (!is_stopped())
Expand Down
2 changes: 2 additions & 0 deletions examples/cpp/hello_world/PublisherApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class PublisherApp : public Application, public DataWriterListener

uint16_t samples_;

uint16_t expected_matches_;

std::mutex mutex_;

std::condition_variable cv_;
Expand Down

0 comments on commit 16018d9

Please sign in to comment.