-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCalculatorServer.cpp
More file actions
185 lines (149 loc) · 4.45 KB
/
Copy pathCalculatorServer.cpp
File metadata and controls
185 lines (149 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
#include <fastdds/dds/domain/qos/ReplierQos.hpp>
#include "ServerImplementation.hpp"
#include "types/calculatorServer.hpp"
#include "types/calculatorServerImpl.hpp"
using namespace calculator_example;
using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::dds::rpc;
class Server
{
public:
Server(
const std::string& service_name)
: server_(nullptr)
, participant_(nullptr)
, service_name_(service_name)
, stop_(false)
{
}
//!--DESTRUCTOR
~Server()
{
server_.reset();
if (participant_)
{
// Delete DDS entities contained within the DomainParticipant
participant_->delete_contained_entities();
// Delete DomainParticipant
DomainParticipantFactory::get_shared_instance()->delete_participant(participant_);
}
}
//!--
//!--INIT
void init()
{
// Create the participant in Domain 0 with default QoS
auto factory = DomainParticipantFactory::get_shared_instance();
if (!factory)
{
throw std::runtime_error("Failed to get participant factory instance");
}
DomainParticipantQos participant_qos;
participant_ = factory->create_participant(0, participant_qos);
if (!participant_)
{
throw std::runtime_error("Failed to create participant");
}
// Create the server with default QoS, using the user-implemented operations
std::shared_ptr<CalculatorServer_IServerImplementation> server_impl =
std::make_shared<ServerImplementation>();
ReplierQos qos;
server_ = create_CalculatorServer(
*participant_,
service_name_.c_str(),
qos,
0,
server_impl);
if (!server_)
{
throw std::runtime_error("Server initialization failed");
}
std::cout << "Server initialized with ID: " << participant_->guid().guidPrefix << std::endl;
}
//!--
//!--STOP
void stop()
{
stop_.store(true);
server_->stop();
std::cout << "Server execution stopped" << std::endl;
}
//!--
bool is_stopped()
{
return stop_.load();
}
//!--RUN
void run()
{
if (is_stopped())
{
return;
}
server_->run();
std::cout << "Server running" << std::endl;
}
//!--
//!--SERVER_PROTECTED_MEMBERS
protected:
std::shared_ptr<RpcServer> server_;
DomainParticipant* participant_;
std::string service_name_;
std::atomic<bool> stop_;
//!--
};
volatile std::sig_atomic_t stop_requested = 0;
void signal_handler(
int signum)
{
(void)signum;
stop_requested = 1;
}
//!--MAIN
int main(
int argc,
char** argv)
{
// Create the server
std::shared_ptr<Server> server = std::make_shared<Server>("CalculatorService");
server->init();
std::thread thread(&Server::run, server);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
#endif // _WIN32
std::cout << "Server running. Please press Ctrl+C to stop the server at any time." << std::endl;
while (!stop_requested)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "Signal received, stopping execution." << std::endl;
server->stop();
thread.join();
}
//!--