Skip to content

Commit f9e57f7

Browse files
Improve signal handling in RPC example (#5974) (#5991)
* Refs #23569: Improve signal handling * Refs #23569: Fix signal handling * Refs #23569: Review - Apply suggestions --------- (cherry picked from commit 0a9b827) Signed-off-by: Carlosespicur <[email protected]> Co-authored-by: Carlos Espinoza Curto <[email protected]>
1 parent 1a840c5 commit f9e57f7

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

examples/cpp/rpc/Application.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class Application
4343
//! Trigger the end of execution
4444
virtual void stop() = 0;
4545

46+
//! Check if the application is stopped
47+
virtual bool is_stopped() const = 0;
48+
4649
//! Factory method to create applications based on configuration
4750
static std::shared_ptr<Application> make_app(
4851
const CLIParser::config& config,

examples/cpp/rpc/ClientApp.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ class ClientApp : public Application
221221

222222
void stop() override;
223223

224+
bool is_stopped() const override
225+
{
226+
return stop_.load();
227+
}
228+
224229
protected:
225230

226231
//! Create a participant for internal RPCDDS entities
@@ -238,11 +243,6 @@ class ClientApp : public Application
238243
bool ping_server(
239244
std::size_t attempts);
240245

241-
bool is_stopped()
242-
{
243-
return stop_.load();
244-
}
245-
246246
std::shared_ptr<calculator_example::Calculator> client_;
247247
dds::DomainParticipant* participant_;
248248
CLIParser::config config_;

examples/cpp/rpc/ServerApp.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ class ServerApp : public Application
4545

4646
void stop() override;
4747

48+
bool is_stopped() const override
49+
{
50+
return stop_.load();
51+
}
52+
4853
protected:
4954

5055
void create_participant();
5156

5257
void create_server(
5358
const std::string& server_name);
5459

55-
bool is_stopped()
56-
{
57-
return stop_.load();
58-
}
59-
6060
private:
6161

6262
class ServerImpl : public calculator_example::CalculatorServerImplementation,

examples/cpp/rpc/main.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
*
1818
*/
1919

20+
#include <condition_variable>
2021
#include <csignal>
2122
#include <functional>
2223
#include <iostream>
2324
#include <memory>
25+
#include <mutex>
2426
#include <stdexcept>
2527
#include <string>
2628
#include <thread>
29+
#include <atomic>
2730

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

3639
std::function<void(int)> stop_app_handler;
40+
std::atomic<bool> stop_requested {false};
41+
std::atomic<int> received_signum {0};
3742

3843
void signal_handler(
3944
int signum)
@@ -67,10 +72,8 @@ int main(
6772

6873
stop_app_handler = [&](int signum)
6974
{
70-
client_server_info("main",
71-
CLIParser::parse_signal(signum) << " received, stopping " << app_name << " execution.");
72-
73-
app->stop();
75+
received_signum.store(signum);
76+
stop_requested.store(true);
7477
};
7578

7679
signal(SIGINT, signal_handler);
@@ -83,6 +86,23 @@ int main(
8386
client_server_info("main",
8487
app_name << " running. Please press Ctrl+C to stop the " << app_name << " at any time.");
8588

89+
while (!app->is_stopped())
90+
{
91+
if (stop_requested.load())
92+
{
93+
client_server_info("main",
94+
CLIParser::parse_signal(
95+
received_signum.load()) << " received, stopping " << app_name << " execution.");
96+
97+
app->stop();
98+
break;
99+
}
100+
else
101+
{
102+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
103+
}
104+
}
105+
86106
thread.join();
87107
}
88108

0 commit comments

Comments
 (0)