diff --git a/examples/cpp/rpc/Application.hpp b/examples/cpp/rpc/Application.hpp index 0c0589d9306..088bd94b38f 100644 --- a/examples/cpp/rpc/Application.hpp +++ b/examples/cpp/rpc/Application.hpp @@ -43,6 +43,9 @@ class Application //! Trigger the end of execution virtual void stop() = 0; + //! Check if the application is stopped + virtual bool is_stopped() const = 0; + //! Factory method to create applications based on configuration static std::shared_ptr make_app( const CLIParser::config& config, diff --git a/examples/cpp/rpc/ClientApp.hpp b/examples/cpp/rpc/ClientApp.hpp index 5ff1f4b9900..99ce9f76823 100644 --- a/examples/cpp/rpc/ClientApp.hpp +++ b/examples/cpp/rpc/ClientApp.hpp @@ -221,6 +221,11 @@ class ClientApp : public Application void stop() override; + bool is_stopped() const override + { + return stop_.load(); + } + protected: //! Create a participant for internal RPCDDS entities @@ -238,11 +243,6 @@ class ClientApp : public Application bool ping_server( std::size_t attempts); - bool is_stopped() - { - return stop_.load(); - } - std::shared_ptr client_; dds::DomainParticipant* participant_; CLIParser::config config_; diff --git a/examples/cpp/rpc/ServerApp.hpp b/examples/cpp/rpc/ServerApp.hpp index 28a3ce1a514..06b124c3322 100644 --- a/examples/cpp/rpc/ServerApp.hpp +++ b/examples/cpp/rpc/ServerApp.hpp @@ -45,6 +45,11 @@ class ServerApp : public Application void stop() override; + bool is_stopped() const override + { + return stop_.load(); + } + protected: void create_participant(); @@ -52,11 +57,6 @@ class ServerApp : public Application void create_server( const std::string& server_name); - bool is_stopped() - { - return stop_.load(); - } - private: class ServerImpl : public calculator_example::CalculatorServerImplementation, diff --git a/examples/cpp/rpc/main.cpp b/examples/cpp/rpc/main.cpp index 8a2a1ef7435..a996abaa8d7 100644 --- a/examples/cpp/rpc/main.cpp +++ b/examples/cpp/rpc/main.cpp @@ -17,13 +17,16 @@ * */ +#include #include #include #include #include +#include #include #include #include +#include #include "app_utils.hpp" #include "Application.hpp" @@ -34,6 +37,8 @@ using eprosima::fastdds::dds::Log; using namespace eprosima::fastdds::examples::rpc; std::function stop_app_handler; +std::atomic stop_requested {false}; +std::atomic received_signum {0}; void signal_handler( int signum) @@ -67,10 +72,8 @@ int main( stop_app_handler = [&](int signum) { - client_server_info("main", - CLIParser::parse_signal(signum) << " received, stopping " << app_name << " execution."); - - app->stop(); + received_signum.store(signum); + stop_requested.store(true); }; signal(SIGINT, signal_handler); @@ -83,6 +86,23 @@ int main( client_server_info("main", app_name << " running. Please press Ctrl+C to stop the " << app_name << " at any time."); + while (!app->is_stopped()) + { + if (stop_requested.load()) + { + client_server_info("main", + CLIParser::parse_signal( + received_signum.load()) << " received, stopping " << app_name << " execution."); + + app->stop(); + break; + } + else + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + thread.join(); }