Skip to content

Commit 16018d9

Browse files
committed
Refs #21886: Add matched argument to hello_world example
Signed-off-by: Mario Dominguez <[email protected]>
1 parent b13f3cc commit 16018d9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

examples/cpp/hello_world/CLIParser.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class CLIParser
4646
struct publisher_config
4747
{
4848
uint16_t samples = 0;
49+
uint16_t matched = 1;
4950
};
5051

5152
//! Subscriber application configuration structure
@@ -83,6 +84,9 @@ class CLIParser
8384
std::cout << " -s <num>, --samples <num> Number of samples to send or receive" << std::endl;
8485
std::cout << " [0 <= <num> <= 65535]" << std::endl;
8586
std::cout << " (Default: 0 [unlimited])" << std::endl;
87+
std::cout << "Publisher options:" << std::endl;
88+
std::cout << " -m, --matched Number of participants to discover" << std::endl;
89+
std::cout << " before start publishing (Default: 1)" << std::endl;
8690
std::cout << "Subscriber options:" << std::endl;
8791
std::cout << " -w, --waitset Use waitset & read condition" << std::endl;
8892
std::exit(return_code);
@@ -194,6 +198,40 @@ class CLIParser
194198
print_help(EXIT_FAILURE);
195199
}
196200
}
201+
else if (arg == "-m" || arg == "--matched")
202+
{
203+
try
204+
{
205+
int input = std::stoi(argv[++i]);
206+
if (input < std::numeric_limits<std::uint16_t>::min() ||
207+
input > std::numeric_limits<std::uint16_t>::max())
208+
{
209+
throw std::out_of_range("matched argument out of range");
210+
}
211+
else
212+
{
213+
if (config.entity == CLIParser::EntityKind::PUBLISHER)
214+
{
215+
config.pub_config.matched = static_cast<uint16_t>(input);
216+
}
217+
else
218+
{
219+
EPROSIMA_LOG_ERROR(CLI_PARSER, "matched can only be used with the publisher entity");
220+
print_help(EXIT_FAILURE);
221+
}
222+
}
223+
}
224+
catch (const std::invalid_argument& e)
225+
{
226+
EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid sample argument for " + arg + ": " + e.what());
227+
print_help(EXIT_FAILURE);
228+
}
229+
catch (const std::out_of_range& e)
230+
{
231+
EPROSIMA_LOG_ERROR(CLI_PARSER, "sample argument out of range for " + arg + ": " + e.what());
232+
print_help(EXIT_FAILURE);
233+
}
234+
}
197235
else
198236
{
199237
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown option " + arg);

examples/cpp/hello_world/PublisherApp.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ PublisherApp::PublisherApp(
4747
, type_(new HelloWorldPubSubType())
4848
, matched_(0)
4949
, samples_(config.samples)
50+
, expected_matches_(config.matched)
5051
, stop_(false)
5152
{
5253
// Set up the data type with initial values
@@ -152,7 +153,7 @@ bool PublisherApp::publish()
152153
cv_.wait(matched_lock, [&]()
153154
{
154155
// at least one has been discovered
155-
return ((matched_ > 0) || is_stopped());
156+
return ((matched_ >= expected_matches_) || is_stopped());
156157
});
157158

158159
if (!is_stopped())

examples/cpp/hello_world/PublisherApp.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class PublisherApp : public Application, public DataWriterListener
8282

8383
uint16_t samples_;
8484

85+
uint16_t expected_matches_;
86+
8587
std::mutex mutex_;
8688

8789
std::condition_variable cv_;

0 commit comments

Comments
 (0)