Skip to content

Commit

Permalink
Refs #20543: Apply rev suggestions (3)
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
  • Loading branch information
JesusPoderoso committed Mar 14, 2024
1 parent 35433d2 commit fe33b04
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 218 deletions.
24 changes: 18 additions & 6 deletions examples/cpp/hello_world/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class CLIParser

CLIParser() = delete;

enum entity_kind
{
PUBLISHER,
SUBSCRIBER,
UNDEFINED
};

struct publisher_config
{
uint16_t samples = 0;
Expand All @@ -40,7 +47,7 @@ class CLIParser

struct hello_world_config
{
std::string entity = "";
entity_kind entity = entity_kind::UNDEFINED;
publisher_config pub_config;
subscriber_config sub_config;
};
Expand Down Expand Up @@ -71,9 +78,13 @@ class CLIParser

std::string first_argument = argv[1];

if (first_argument == "publisher" || first_argument == "subscriber")
if (first_argument == "publisher" )
{
config.entity = first_argument;
config.entity = entity_kind::PUBLISHER;
}
else if ( first_argument == "subscriber")
{
config.entity = entity_kind::SUBSCRIBER;
}
else
{
Expand Down Expand Up @@ -102,11 +113,11 @@ class CLIParser
try
{
uint16_t samples = static_cast<uint16_t>(std::stoi(argv[++i]));
if (config.entity == "publisher")
if (config.entity == entity_kind::PUBLISHER)
{
config.pub_config.samples = samples;
}
else if (config.entity == "subscriber")
else if (config.entity == entity_kind::SUBSCRIBER)
{
config.sub_config.samples = samples;
}
Expand Down Expand Up @@ -136,7 +147,7 @@ class CLIParser
}
else if (arg == "-w" || arg == "--waitset")
{
if (config.entity == "subscriber")
if (config.entity == entity_kind::SUBSCRIBER)
{
config.sub_config.use_waitset = true;
}
Expand All @@ -155,6 +166,7 @@ class CLIParser

return config;
}

};

#endif // _FASTDDS_HELLO_WORLD_CLI_PARSER_HPP_
2 changes: 1 addition & 1 deletion examples/cpp/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ target_compile_definitions(hello_world PRIVATE
)
target_link_libraries(hello_world fastdds fastcdr)
install(TARGETS hello_world
RUNTIME DESTINATION examples/cpp/hello_world/${BIN_INSTALL_DIR})
RUNTIME DESTINATION fastdds/examples/cpp/hello_world/${BIN_INSTALL_DIR})

# Copy the XML files over to the build directory
file(GLOB_RECURSE XML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.xml)
Expand Down
177 changes: 145 additions & 32 deletions examples/cpp/hello_world/HelloWorld_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*
*/

#include <csignal>
#include <stdexcept>
#include <thread>

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/log/Log.hpp>
Expand All @@ -29,61 +31,172 @@

using eprosima::fastdds::dds::Log;

std::function<void(std::string)> signal_handler;

int main(
int argc,
char** argv)
{
auto ret = EXIT_SUCCESS;
HelloWorldPublisher* publisher = nullptr;
HelloWorldSubscriber* subscriber = nullptr;
HelloWorldSubscriberWaitset* subscriber_waitset = nullptr;
std::thread* thread = nullptr;
const std::string topic_name = "hello_world_topic";
std::string entity_name = "undefined";
uint16_t samples = 0;
CLIParser::hello_world_config config = CLIParser::parse_cli_options(argc, argv);

if (config.entity == "publisher")
switch (config.entity)
{
try
{
HelloWorldPublisher hello_world_publisher(config.pub_config, topic_name);
hello_world_publisher.run();
}
catch (const std::runtime_error& e)
{
EPROSIMA_LOG_ERROR(PUBLISHER, e.what());
ret = EXIT_FAILURE;
}
}
else if (config.entity == "subscriber")
{
if (config.sub_config.use_waitset)
{
case CLIParser::entity_kind::PUBLISHER:
entity_name = "Publisher";
samples = config.pub_config.samples;
try
{
HelloWorldSubscriberWaitset hello_world_subscriber_waitset(config.sub_config, topic_name);
hello_world_subscriber_waitset.run();
publisher = new HelloWorldPublisher(config.pub_config, topic_name);
thread = new std::thread(&HelloWorldPublisher::run, publisher);
}
catch (const std::runtime_error& e)
{
EPROSIMA_LOG_ERROR(SUBSCRIBER, e.what());
EPROSIMA_LOG_ERROR(PUBLISHER, e.what());
ret = EXIT_FAILURE;
}
}
else
{
try
break;
case CLIParser::entity_kind::SUBSCRIBER:
samples = config.sub_config.samples;
if (config.sub_config.use_waitset)
{
HelloWorldSubscriber hello_world_subscriber(config.sub_config, topic_name);
hello_world_subscriber.run();
entity_name = "Waitset Subscriber";
try
{
subscriber_waitset = new HelloWorldSubscriberWaitset(config.sub_config, topic_name);
thread = new std::thread(&HelloWorldSubscriberWaitset::run, subscriber_waitset);
}
catch (const std::runtime_error& e)
{
EPROSIMA_LOG_ERROR(SUBSCRIBER, e.what());
ret = EXIT_FAILURE;
}
}
catch (const std::runtime_error& e)
else
{
EPROSIMA_LOG_ERROR(SUBSCRIBER_WAITSET, e.what());
ret = EXIT_FAILURE;
entity_name = "Subscriber";
try
{
subscriber = new HelloWorldSubscriber(config.sub_config, topic_name);
thread = new std::thread(&HelloWorldSubscriber::run, subscriber);
}
catch (const std::runtime_error& e)
{
EPROSIMA_LOG_ERROR(SUBSCRIBER_WAITSET, e.what());
ret = EXIT_FAILURE;
}
}
}
break;
default:
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown entity");
CLIParser::print_help(EXIT_FAILURE);
break;
}

if (samples == 0)
{
std::cout << entity_name << " running. Please press Ctrl+C to stop the "
<< entity_name << " at any time." << std::endl;
}
// example should never reach this point
else
{
EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown entity " + config.entity);
CLIParser::print_help(EXIT_FAILURE);
switch (config.entity)
{
case CLIParser::entity_kind::PUBLISHER:
std::cout << entity_name << " running " << samples << " samples. Please press Ctrl+C to stop the "
<< entity_name << " at any time." << std::endl;
break;
case CLIParser::entity_kind::SUBSCRIBER:
default:
std::cout << entity_name << " running until " << samples << " samples have been received. Please press "
<< "Ctrl+C to stop the " << entity_name << " at any time." << std::endl;
break;
}
}

signal_handler = [&](std::string signal)
{
std::cout << "\n" << signal << " received, stopping " << entity_name << " execution." << std::endl;
switch (config.entity)
{
case CLIParser::entity_kind::PUBLISHER:
if (nullptr != publisher)
{
publisher->stop();
}
break;
case CLIParser::entity_kind::SUBSCRIBER:
default:
if (config.sub_config.use_waitset)
{
if (nullptr != subscriber_waitset)
{
subscriber_waitset->stop();
}
}
else
{
if (nullptr != subscriber)
{
subscriber->stop();
}
}
break;
}
};
signal(SIGINT, [](int /*signum*/)
{
signal_handler("SIGINT");
});
signal(SIGTERM, [](int /*signum*/)
{
signal_handler("SIGTERM");
});
#ifndef _WIN32
signal(SIGQUIT, [](int /*signum*/)
{
signal_handler("SIGQUIT");
});
signal(SIGHUP, [](int /*signum*/)
{
signal_handler("SIGHUP");
});
#endif // _WIN32

thread->join();
delete thread;
switch (config.entity)
{
case CLIParser::entity_kind::PUBLISHER:
if (nullptr != publisher)
{
delete publisher;
}
break;
case CLIParser::entity_kind::SUBSCRIBER:
default:
if (config.sub_config.use_waitset)
{
if (nullptr != subscriber_waitset)
{
delete subscriber_waitset;
}
}
else
{
if (nullptr != subscriber)
{
delete subscriber;
}
}
break;
}

Log::Reset();
Expand Down
55 changes: 9 additions & 46 deletions examples/cpp/hello_world/Publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

using namespace eprosima::fastdds::dds;

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

HelloWorldPublisher::HelloWorldPublisher(
const CLIParser::publisher_config& config,
const std::string& topic_name)
Expand All @@ -43,13 +40,14 @@ HelloWorldPublisher::HelloWorldPublisher(
, topic_(nullptr)
, writer_(nullptr)
, type_(new HelloWorldPubSubType())
, stop_(false)
, matched_(0)
, samples_ (config.samples)
{
// Set up the data type with initial values
hello_.index(0);
hello_.message("Hello world");

// Create the participant
auto factory = DomainParticipantFactory::get_instance();
participant_ = factory->create_participant_with_default_profile(nullptr, StatusMask::none());
Expand Down Expand Up @@ -125,50 +123,15 @@ void HelloWorldPublisher::on_publication_matched(

void HelloWorldPublisher::run()
{
std::thread pub_thread([&]
{
while (!is_stopped() && (samples_ == 0 || hello_.index() < samples_))
{
if (publish())
{
std::cout << "Message: '" << hello_.message() << "' with index: '" << hello_.index()
<< "' SENT" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(period_ms_));
}
});
if (samples_ == 0)
while (!is_stopped() && (samples_ == 0 || hello_.index() < samples_))
{
std::cout << "Publisher running. Please press Ctrl+C to stop the Publisher at any time." << std::endl;
if (publish())
{
std::cout << "Message: '" << hello_.message() << "' with index: '" << hello_.index()
<< "' SENT" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(period_ms_));
}
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;
HelloWorldPublisher::stop();
});
signal(SIGTERM, [](int /*signum*/)
{
std::cout << "\nSIGTERM received, stopping Publisher execution." << std::endl;
HelloWorldPublisher::stop();
});
#ifndef _WIN32
signal(SIGQUIT, [](int /*signum*/)
{
std::cout << "\nSIGQUIT received, stopping Publisher execution." << std::endl;
HelloWorldPublisher::stop();
});
signal(SIGHUP, [](int /*signum*/)
{
std::cout << "\nSIGHUP received, stopping Publisher execution." << std::endl;
HelloWorldPublisher::stop();
});
#endif // _WIN32
pub_thread.join();
}

bool HelloWorldPublisher::publish()
Expand Down
Loading

0 comments on commit fe33b04

Please sign in to comment.