File: ServerApp.cpp

package info (click to toggle)
fastdds 3.3.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,540 kB
  • sloc: cpp: 793,735; xml: 15,283; python: 5,902; sh: 219; makefile: 95; ansic: 12
file content (355 lines) | stat: -rw-r--r-- 10,135 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// 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 "ServerApp.hpp"

#include <memory>
#include <string>
#include <thread>

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/qos/DomainParticipantExtendedQos.hpp>
#include <fastdds/dds/domain/qos/ReplierQos.hpp>

#include "app_utils.hpp"
#include "CLIParser.hpp"
#include "types/calculatorServer.hpp"
#include "types/calculatorServerImpl.hpp"

namespace eprosima {
namespace fastdds {
namespace examples {
namespace rpc {

using namespace calculator_example;
using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::dds::rpc;

ServerApp::ServerApp(
        const CLIParser::config& config,
        const std::string& service_name)
    : server_impl_(nullptr)
    , participant_(nullptr)
    , thread_pool_size_(config.thread_pool_size)
    , stop_(false)
    , timeout_thread_([&config, this]
            {
                if (config.timeout > 0)
                {
                    std::this_thread::sleep_for(std::chrono::seconds(config.timeout));
                    this->stop();
                }
            })
{
    create_participant();
    create_server(service_name);

    client_server_info("ServerApp", "Server initialized with ID: " << participant_->guid().guidPrefix);
}

ServerApp::~ServerApp()
{
    timeout_thread_.join();

    // As a precautionary measure, delete the server here because participant_->delete_contained_entities()
    // does not automatically disable the service. This line can be removed once the RPC internal API
    // supports service disabling.
    server_.reset();

    if (participant_)
    {
        // Delete DDS entities contained within the DomainParticipant
        participant_->delete_contained_entities();

        // Delete DomainParticipant
        DomainParticipantFactory::get_shared_instance()->delete_participant(participant_);
    }
}

void ServerApp::run()
{
    if (is_stopped())
    {
        return;
    }

    server_->run();

    client_server_info("ServerApp", "Server running");
}

void ServerApp::stop()
{
    stop_.store(true);
    server_->stop();

    client_server_info("ServerApp", "Server execution stopped");
}

void ServerApp::create_participant()
{
    // Create the participant
    auto factory = DomainParticipantFactory::get_shared_instance();

    if (!factory)
    {
        throw std::runtime_error("Failed to get participant factory instance");
    }

    participant_ = factory->create_participant_with_default_profile();

    if (!participant_)
    {
        throw std::runtime_error("Participant initialization failed");
    }
}

void ServerApp::create_server(
        const std::string& service_name)
{
    ReplierQos qos;

    server_impl_ = ServerImpl::create();

    server_ = create_CalculatorServer(
        *participant_,
        service_name.c_str(),
        qos,
        thread_pool_size_,
        server_impl_);

    if (!server_)
    {
        throw std::runtime_error("Server initialization failed");
    }
}

calculator_example::detail::Calculator_representation_limits_Out ServerApp::ServerImpl::representation_limits(
        const eprosima::fastdds::dds::rpc::RpcRequest& info)
{
    static_cast<void>(info);
    calculator_example::detail::Calculator_representation_limits_Out limits;
    limits.min_value = std::numeric_limits<int32_t>::min();
    limits.max_value = std::numeric_limits<int32_t>::max();
    return limits;
}

int32_t ServerApp::ServerImpl::addition(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ int32_t value1,
        /*in*/ int32_t value2)
{
    static_cast<void>(info);

    int32_t result = value1 + value2;
    bool negative_1 = value1 < 0;
    bool negative_2 = value2 < 0;
    bool negative_result = result < 0;

    if ((negative_1 == negative_2) && (negative_result != negative_1))
    {
        throw calculator_example::OverflowException();
    }

    return result;
}

int32_t ServerApp::ServerImpl::subtraction(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ int32_t value1,
        /*in*/ int32_t value2)
{
    static_cast<void>(info);

    int32_t result = value1 - value2;
    bool negative_1 = value1 < 0;
    bool negative_2 = value2 < 0;
    bool negative_result = result < 0;

    if ((negative_1 != negative_2) && (negative_result != negative_1))
    {
        throw calculator_example::OverflowException();
    }

    return result;
}

void ServerApp::ServerImpl::fibonacci_seq(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ uint32_t n_results,
        /*result*/ eprosima::fastdds::dds::rpc::RpcServerWriter<int32_t>& result_writer)
{
    static_cast<void>(info);

    int32_t a = 1;
    int32_t b = 1;
    int32_t c = 0;

    for (uint32_t i = 0; i < n_results; ++i)
    {
        if (a < 0)
        {
            throw calculator_example::OverflowException(
                      "Overflow in Fibonacci sequence. "
                      "The result is too large to be represented.");
        }

        result_writer.write(a);
        c = a + b;
        a = b;
        b = c;
    }
}

int32_t ServerApp::ServerImpl::sum_all(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ eprosima::fastdds::dds::rpc::RpcServerReader<int32_t>& value)
{
    static_cast<void>(info);

    int32_t sum_all_result = 0;

    try
    {
        int32_t value_to_add = 0;
        while (value.read(value_to_add))
        {
            int32_t new_sum = sum_all_result + value_to_add;
            bool current_negative = sum_all_result < 0;
            bool new_negative = value_to_add < 0;
            bool result_negative = new_sum < 0;
            if ((current_negative == new_negative) && (result_negative != current_negative))
            {
                throw calculator_example::OverflowException();
            }
            sum_all_result = new_sum;
        }
    }
    catch (const eprosima::fastdds::dds::rpc::RpcFeedCancelledException& ex)
    {
        static_cast<void>(ex);
        // Feed was cancelled, do nothing and return the current sum
    }

    return sum_all_result;
}

void ServerApp::ServerImpl::accumulator(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ eprosima::fastdds::dds::rpc::RpcServerReader<int32_t>& value,
        /*result*/ eprosima::fastdds::dds::rpc::RpcServerWriter<int32_t>& result_writer)
{
    static_cast<void>(info);

    int32_t current_sum = 0;

    try
    {
        int32_t value_to_add = 0;
        while (value.read(value_to_add))
        {
            int32_t new_sum = current_sum + value_to_add;
            bool current_negative = current_sum < 0;
            bool new_negative = value_to_add < 0;
            bool result_negative = new_sum < 0;
            if ((current_negative == new_negative) && (result_negative != current_negative))
            {
                throw calculator_example::OverflowException();
            }
            current_sum = new_sum;
            result_writer.write(current_sum);
        }
    }
    catch (const eprosima::fastdds::dds::rpc::RpcFeedCancelledException& ex)
    {
        static_cast<void>(ex);
        // Feed was cancelled, do nothing
    }
}

void ServerApp::ServerImpl::filter(
        const eprosima::fastdds::dds::rpc::RpcRequest& info,
        /*in*/ eprosima::fastdds::dds::rpc::RpcServerReader<int32_t>& value,
        /*in*/ calculator_example::FilterKind filter_kind,
        /*result*/ eprosima::fastdds::dds::rpc::RpcServerWriter<int32_t>& result_writer)
{
    static_cast<void>(info);

    try
    {
        int32_t value_to_filter = 0;
        while (value.read(value_to_filter))
        {
            switch (filter_kind)
            {
                case calculator_example::FilterKind::EVEN:
                {
                    if (value_to_filter % 2 == 0)
                    {
                        result_writer.write(value_to_filter);
                    }
                    break;
                }
                case calculator_example::FilterKind::ODD:
                {
                    if (value_to_filter % 2 != 0)
                    {
                        result_writer.write(value_to_filter);
                    }
                    break;
                }
                case calculator_example::FilterKind::PRIME:
                {
                    bool prime = true;
                    if (value_to_filter <= 1)
                    {
                        prime = false;
                    }
                    else
                    {
                        for (int i = 2; i < value_to_filter; i++)
                        {
                            if (value_to_filter % i == 0)
                            {
                                prime = false;
                                break;
                            }
                        }
                    }

                    if (prime)
                    {
                        result_writer.write(value_to_filter);
                    }

                    break;
                }

                default:
                    // Invalid filter kind
                    throw eprosima::fastdds::dds::rpc::RemoteInvalidArgumentError();
            }
        }
    }
    catch (const eprosima::fastdds::dds::rpc::RpcFeedCancelledException& ex)
    {
        static_cast<void>(ex);
        // Feed was cancelled, do nothing
    }
}

} // namespace rpc
} // namespace examples
} // namespace fastdds
} // namespace eprosima