Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/cpp/rpc/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Application> make_app(
const CLIParser::config& config,
Expand Down
10 changes: 5 additions & 5 deletions examples/cpp/rpc/ClientApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -238,11 +243,6 @@ class ClientApp : public Application
bool ping_server(
std::size_t attempts);

bool is_stopped()
{
return stop_.load();
}

std::shared_ptr<calculator_example::Calculator> client_;
dds::DomainParticipant* participant_;
CLIParser::config config_;
Expand Down
10 changes: 5 additions & 5 deletions examples/cpp/rpc/ServerApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ class ServerApp : public Application

void stop() override;

bool is_stopped() const override
{
return stop_.load();
}

protected:

void create_participant();

void create_server(
const std::string& server_name);

bool is_stopped()
{
return stop_.load();
}

private:

class ServerImpl : public calculator_example::CalculatorServerImplementation,
Expand Down
28 changes: 24 additions & 4 deletions examples/cpp/rpc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*
*/

#include <condition_variable>
#include <csignal>
#include <functional>
#include <iostream>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <atomic>

#include "app_utils.hpp"
#include "Application.hpp"
Expand All @@ -34,6 +37,8 @@ using eprosima::fastdds::dds::Log;
using namespace eprosima::fastdds::examples::rpc;

std::function<void(int)> stop_app_handler;
std::atomic<bool> stop_requested {false};
std::atomic<int> received_signum {0};

void signal_handler(
int signum)
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
Loading