Skip to content

Commit

Permalink
Refs #20543: Apply configuration example suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <[email protected]>
  • Loading branch information
JesusPoderoso committed May 17, 2024
1 parent 6575439 commit fbafb51
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
32 changes: 32 additions & 0 deletions examples/cpp/hello_world/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,41 @@ class CLIParser

CLIParser() = delete;

//! Entity kind enumeration
enum class EntityKind : uint8_t
{
PUBLISHER,
SUBSCRIBER,
UNDEFINED
};

//! Publisher configuration structure (shared for both publisher and subscriber applications)
struct publisher_config
{
uint16_t samples = 0;
};

//! Subscriber application configuration structure
struct subscriber_config : public publisher_config
{
bool use_waitset = false;
};

//! Configuration structure for the application
struct hello_world_config
{
CLIParser::EntityKind entity = CLIParser::EntityKind::UNDEFINED;
publisher_config pub_config;
subscriber_config sub_config;
};

/**
* @brief Print usage help message and exit with the given return code
*
* @param return_code return code to exit with
*
* @warning This method finishes the execution of the program with the input return code
*/
static void print_help(
uint8_t return_code)
{
Expand All @@ -77,6 +88,15 @@ class CLIParser
std::exit(return_code);
}

/**
* @brief Parse the command line options and return the configuration_config object
*
* @param argc number of arguments
* @param argv array of arguments
* @return configuration_config object with the parsed options
*
* @warning This method finishes the execution of the program if the input arguments are invalid
*/
static hello_world_config parse_cli_options(
int argc,
char* argv[])
Expand Down Expand Up @@ -184,6 +204,12 @@ class CLIParser
return config;
}

/**
* @brief Parse the signal number into the signal name
*
* @param signum signal number
* @return std::string signal name
*/
static std::string parse_signal(
const int& signum)
{
Expand All @@ -204,6 +230,12 @@ class CLIParser
}
}

/**
* @brief Parse the entity kind into std::string
*
* @param entity entity kind
* @return std::string entity kind
*/
static std::string parse_entity_kind(
const EntityKind& entity)
{
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/hello_world/WaitsetSubscriberApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void WaitsetSubscriberApp::run()
{
ConditionSeq triggered_conditions;
ReturnCode_t ret_code = wait_set_.wait(triggered_conditions, eprosima::fastrtps::c_TimeInfinite);
if (ReturnCode_t::RETCODE_OK != ret_code)
if (RETCODE_OK != ret_code)
{
EPROSIMA_LOG_ERROR(SUBSCRIBER_WAITSET, "Error waiting for conditions");
continue;
Expand Down Expand Up @@ -151,7 +151,7 @@ void WaitsetSubscriberApp::run()
{
SampleInfo info;
while ((!is_stopped()) &&
(ReturnCode_t::RETCODE_OK == reader_->take_next_sample(&hello_, &info)))
(RETCODE_OK == reader_->take_next_sample(&hello_, &info)))
{
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
{
Expand Down
54 changes: 29 additions & 25 deletions examples/cpp/hello_world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,38 @@ int main(
ret = EXIT_FAILURE;
}

std::thread thread(&Application::run, app);

if (samples == 0)
{
std::cout << app_name << " running. Please press Ctrl+C to stop the "
<< app_name << " at any time." << std::endl;
}
else
if (EXIT_FAILURE != ret)
{
std::cout << app_name << " running for " << samples << " samples. Please press Ctrl+C to stop the "
<< app_name << " at any time." << std::endl;
std::thread thread(&Application::run, app);

if (samples == 0)
{
std::cout << app_name << " running. Please press Ctrl+C to stop the "
<< app_name << " at any time." << std::endl;
}
else
{
std::cout << app_name << " running for " << samples << " samples. Please press Ctrl+C to stop the "
<< app_name << " at any time." << std::endl;
}

stop_app_handler = [&](int signum)
{
std::cout << "\n" << CLIParser::parse_signal(signum) << " received, stopping " << app_name
<< " execution." << std::endl;
app->stop();
};

signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
#endif // _WIN32

thread.join();
}

stop_app_handler = [&](int signum)
{
std::cout << "\n" << CLIParser::parse_signal(signum) << " received, stopping " << app_name
<< " execution." << std::endl;
app->stop();
};

signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
#endif // _WIN32

thread.join();
Log::Reset();
return ret;
}

0 comments on commit fbafb51

Please sign in to comment.