Skip to content

Commit

Permalink
Refs #20543: Include sample CLI argument
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
  • Loading branch information
JesusPoderoso committed Mar 13, 2024
1 parent a508fe3 commit c6e0741
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 98 deletions.
10 changes: 5 additions & 5 deletions examples/cpp/hello_world/HelloWorld_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ int main(
char** argv)
{
auto ret = EXIT_SUCCESS;
hello_world_config config = parse_cli_options(argc, argv);
CLIParser::hello_world_config config = CLIParser::parse_cli_options(argc, argv);

if (config.entity == "publisher")
{
try
{
HelloWorldPublisher hello_world_publisher;
HelloWorldPublisher hello_world_publisher(config);
hello_world_publisher.run();
}
catch (const std::runtime_error& e)
Expand All @@ -55,7 +55,7 @@ int main(
{
try
{
HelloWorldSubscriberWaitset hello_world_subscriber_waitset;
HelloWorldSubscriberWaitset hello_world_subscriber_waitset(config);
hello_world_subscriber_waitset.run();
}
catch (const std::runtime_error& e)
Expand All @@ -68,7 +68,7 @@ int main(
{
try
{
HelloWorldSubscriber hello_world_subscriber;
HelloWorldSubscriber hello_world_subscriber(config);
hello_world_subscriber.run();
}
catch (const std::runtime_error& e)
Expand All @@ -82,7 +82,7 @@ int main(
else
{
EPROSIMA_LOG_ERROR(CLI_PARSE, "unknown entity " + config.entity);
print_help(EXIT_FAILURE);
CLIParser::print_help(EXIT_FAILURE);
}

Log::Reset();
Expand Down
31 changes: 25 additions & 6 deletions examples/cpp/hello_world/Publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
using namespace eprosima::fastdds::dds;

std::atomic<bool> HelloWorldPublisher::stop_(false);
std::condition_variable HelloWorldPublisher::matched_cv_;

HelloWorldPublisher::HelloWorldPublisher()
HelloWorldPublisher::HelloWorldPublisher(
const CLIParser::hello_world_config& config)
: participant_(nullptr)
, publisher_(nullptr)
, topic_(nullptr)
Expand All @@ -44,6 +46,10 @@ HelloWorldPublisher::HelloWorldPublisher()
// Set up the data type with initial values
hello_.index(0);
hello_.message("Hello world");
matched_ = 0;

// Get CLI options
samples_ = config.samples;

// Create the participant
auto factory = DomainParticipantFactory::get_instance();
Expand Down Expand Up @@ -119,7 +125,7 @@ void HelloWorldPublisher::run()
{
std::thread pub_thread([&]
{
while (!is_stopped())
while (!is_stopped() && (samples_ == 0 || hello_.index() < samples_))
{
if (publish())
{
Expand All @@ -129,7 +135,15 @@ void HelloWorldPublisher::run()
std::this_thread::sleep_for(std::chrono::milliseconds(period_));
}
});
std::cout << "Publisher running. Please press Ctrl+C to stop the Publisher at any time." << std::endl;
if (samples_ == 0)
{
std::cout << "Publisher running. Please press Ctrl+C to stop the Publisher at any time." << std::endl;
}
else
{
std::cout << "Publisher running " << samples_ <<
" samples. Please press Ctrl+C to stop the Publisher at any time." << std::endl;
}
signal(SIGINT, [](int /*signum*/)
{
std::cout << "\nSIGINT received, stopping Publisher execution." << std::endl;
Expand Down Expand Up @@ -162,10 +176,14 @@ bool HelloWorldPublisher::publish()
matched_cv_.wait(matched_lock, [&]()
{
// at least one has been discovered
return matched_ > 0;
return matched_ > 0 || is_stopped();
});
hello_.index(hello_.index() + 1);
return writer_->write(&hello_);
if (!is_stopped())
{
hello_.index(hello_.index() + 1);
return writer_->write(&hello_);
}
return false;
}

bool HelloWorldPublisher::is_stopped()
Expand All @@ -176,4 +194,5 @@ bool HelloWorldPublisher::is_stopped()
void HelloWorldPublisher::stop()
{
stop_.store(true);
matched_cv_.notify_one();
}
8 changes: 6 additions & 2 deletions examples/cpp/hello_world/Publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <fastdds/dds/publisher/DataWriterListener.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

#include "cli_options.hpp"
#include "HelloWorldPubSubTypes.h"

using namespace eprosima::fastdds::dds;
Expand All @@ -34,7 +35,8 @@ class HelloWorldPublisher : public DataWriterListener
{
public:

HelloWorldPublisher();
HelloWorldPublisher(
const CLIParser::hello_world_config& config);

~HelloWorldPublisher() override;

Expand Down Expand Up @@ -73,9 +75,11 @@ class HelloWorldPublisher : public DataWriterListener

int16_t matched_;

uint16_t samples_;

std::mutex mutex_;

std::condition_variable matched_cv_;
static std::condition_variable matched_cv_;

const uint8_t period_ = 100; // in ms
};
Expand Down
23 changes: 21 additions & 2 deletions examples/cpp/hello_world/Subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ using namespace eprosima::fastdds::dds;
std::atomic<bool> HelloWorldSubscriber::stop_(false);
std::condition_variable HelloWorldSubscriber::terminate_cv_;

HelloWorldSubscriber::HelloWorldSubscriber()
HelloWorldSubscriber::HelloWorldSubscriber(
const CLIParser::hello_world_config& config)
: participant_(nullptr)
, subscriber_(nullptr)
, topic_(nullptr)
, reader_(nullptr)
, type_(new HelloWorldPubSubType())
{
// Get CLI options
samples_ = config.samples;
received_samples_ = 0;

// Create the participant
auto factory = DomainParticipantFactory::get_instance();
participant_ = factory->create_participant_with_default_profile(nullptr, StatusMask::none());
Expand Down Expand Up @@ -119,16 +124,30 @@ void HelloWorldSubscriber::on_data_available(
{
if (info.instance_state == ALIVE_INSTANCE_STATE && info.valid_data)
{
received_samples_++;
// Print Hello world message data
std::cout << "Message: '" << hello_.message() << "' with index: '" << hello_.index()
<< "' RECEIVED" << std::endl;
if (samples_ > 0 && (received_samples_ >= samples_))
{
stop();
}
}
}
}

void HelloWorldSubscriber::run()
{
std::cout << "Subscriber running. Please press Ctrl+C to stop the Subscriber at any time." << std::endl;
if (samples_ == 0)
{
std::cout << "Subscriber running. Please press Ctrl+C to stop the Subscriber at any time." << std::endl;
}
else
{
std::cout << "Subscriber running until " << samples_ <<
" samples have been received. Please press Ctrl+C to stop the Subscriber at any time." << std::endl;
}

signal(SIGINT, [](int /*signum*/)
{
std::cout << "\nSIGINT received, stopping Subscriber execution." << std::endl;
Expand Down
8 changes: 7 additions & 1 deletion examples/cpp/hello_world/Subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

#include "cli_options.hpp"
#include "HelloWorldPubSubTypes.h"

using namespace eprosima::fastdds::dds;
Expand All @@ -34,7 +35,8 @@ class HelloWorldSubscriber : public DataReaderListener
{
public:

HelloWorldSubscriber();
HelloWorldSubscriber(
const CLIParser::hello_world_config& config);

virtual ~HelloWorldSubscriber();

Expand Down Expand Up @@ -70,6 +72,10 @@ class HelloWorldSubscriber : public DataReaderListener

TypeSupport type_;

uint16_t samples_;

uint16_t received_samples_;

static std::atomic<bool> stop_;

mutable std::mutex terminate_cv_mtx_;
Expand Down
24 changes: 21 additions & 3 deletions examples/cpp/hello_world/SubscriberWaitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ using namespace eprosima::fastdds::dds;
std::atomic<bool> HelloWorldSubscriberWaitset::stop_(false);
GuardCondition HelloWorldSubscriberWaitset::terminate_condition_;

HelloWorldSubscriberWaitset::HelloWorldSubscriberWaitset()
HelloWorldSubscriberWaitset::HelloWorldSubscriberWaitset(
const CLIParser::hello_world_config& config)
: participant_(nullptr)
, subscriber_(nullptr)
, topic_(nullptr)
, reader_(nullptr)
, type_(new HelloWorldPubSubType())
{
// Get CLI options
samples_ = config.samples;
received_samples_ = 0;

// Create the participant
auto factory = DomainParticipantFactory::get_instance();
participant_ = factory->create_participant_with_default_profile(nullptr, StatusMask::none());
Expand Down Expand Up @@ -145,9 +150,14 @@ void HelloWorldSubscriberWaitset::run()
{
if (info.instance_state == ALIVE_INSTANCE_STATE && info.valid_data)
{
received_samples_++;
// Print Hello world message data
std::cout << "Message: '" << hello_.message() << "' with index: '"
<< hello_.index() << "' RECEIVED" << std::endl;
if (samples_ > 0 && (received_samples_ >= samples_))
{
stop();
}
}
}
}
Expand All @@ -156,8 +166,16 @@ void HelloWorldSubscriberWaitset::run()
}
});

std::cout << "Waitset Subscriber running. Please press Ctrl+C to stop the Waitset Subscriber at any time."
<< std::endl;
if (samples_ == 0)
{
std::cout << "Waitset Subscriber running. Please press Ctrl+C to stop the Waitset Subscriber at any time."
<< std::endl;
}
else
{
std::cout << "Waitset Subscriber running until " << samples_ <<
" samples have been received. Please press Ctrl+C to stop the Waitset Subscriber at any time." << std::endl;
}
signal(SIGINT, [](int /*signum*/)
{
std::cout << "\nSIGINT received, stopping Waitset Subscriber execution." << std::endl;
Expand Down
8 changes: 7 additions & 1 deletion examples/cpp/hello_world/SubscriberWaitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

#include "cli_options.hpp"
#include "HelloWorldPubSubTypes.h"

using namespace eprosima::fastdds::dds;
Expand All @@ -36,7 +37,8 @@ class HelloWorldSubscriberWaitset
{
public:

HelloWorldSubscriberWaitset();
HelloWorldSubscriberWaitset(
const CLIParser::hello_world_config& config);

virtual ~HelloWorldSubscriberWaitset();

Expand Down Expand Up @@ -65,6 +67,10 @@ class HelloWorldSubscriberWaitset

WaitSet wait_set_;

uint16_t samples_;

uint16_t received_samples_;

static std::atomic<bool> stop_;

static GuardCondition terminate_condition_;
Expand Down
Loading

0 comments on commit c6e0741

Please sign in to comment.