-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathCalculatorServer.cpp
More file actions
203 lines (156 loc) · 6.31 KB
/
Copy pathCalculatorServer.cpp
File metadata and controls
203 lines (156 loc) · 6.31 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022 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 <condition_variable>
#include <mutex>
#include <vector>
#include "CalculatorPubSubTypes.h"
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/rtps/common/WriteParams.h>
class CalculatorServer
{
class Listener : public eprosima::fastdds::dds::DataReaderListener
{
public:
Listener(
eprosima::fastdds::dds::DataWriter* writer)
: writer_(writer)
{
}
void on_data_available(
eprosima::fastdds::dds::DataReader* reader) override
{
RequestType request;
eprosima::fastdds::dds::SampleInfo sample_info;
reader->take_next_sample(&request, &sample_info);
if (eprosima::fastdds::dds::InstanceStateKind::ALIVE_INSTANCE_STATE == sample_info.instance_state)
{
ReplyType reply;
if (OperationType::ADDITION == request.operation())
{
reply.z(request.x() + request.y());
}
else if (OperationType::SUBTRACTION == request.operation())
{
reply.z(request.x() - request.y());
}
else if (OperationType::MULTIPLICATION == request.operation())
{
reply.z(request.x() * request.y());
}
else if (OperationType::DIVISION == request.operation())
{
if (0 != request.y())
{
reply.z(request.x() / request.y());
}
}
eprosima::fastdds::rtps::WriteParams write_params;
write_params.related_sample_identity().writer_guid(sample_info.sample_identity.writer_guid());
write_params.related_sample_identity().sequence_number(sample_info.sample_identity.sequence_number());
writer_->write(reinterpret_cast<void*>(&reply), write_params);
}
}
private:
eprosima::fastdds::dds::DataWriter* writer_ = nullptr;
};
public:
bool init()
{
participant_ = eprosima::fastdds::dds::DomainParticipantFactory::get_instance()->create_participant(0,
eprosima::fastdds::dds::PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant_)
{
return false;
}
request_type_ = eprosima::fastdds::dds::TypeSupport(new RequestTypePubSubType());
reply_type_ = eprosima::fastdds::dds::TypeSupport(new ReplyTypePubSubType());
participant_->register_type(request_type_);
participant_->register_type(reply_type_);
publisher_ = participant_->create_publisher(eprosima::fastdds::dds::PUBLISHER_QOS_DEFAULT);
if (nullptr == publisher_)
{
return false;
}
subscriber_ = participant_->create_subscriber(eprosima::fastdds::dds::SUBSCRIBER_QOS_DEFAULT);
if (nullptr == subscriber_)
{
return false;
}
request_topic_ = participant_->create_topic("CalculatorRequest",
request_type_.get_type_name(), eprosima::fastdds::dds::TOPIC_QOS_DEFAULT);
if (nullptr == request_topic_)
{
return false;
}
reply_topic_ = participant_->create_topic("CalculatorReply",
reply_type_.get_type_name(), eprosima::fastdds::dds::TOPIC_QOS_DEFAULT);
if (nullptr == reply_topic_)
{
return false;
}
eprosima::fastdds::dds::DataWriterQos writer_qos;
writer_qos.history().kind = eprosima::fastdds::dds::KEEP_ALL_HISTORY_QOS;
reply_writer_ = publisher_->create_datawriter(reply_topic_, writer_qos);
if (nullptr == reply_writer_)
{
return false;
}
listener_ = { reply_writer_};
eprosima::fastdds::dds::DataReaderQos reader_qos;
reader_qos.reliability().kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS;
reader_qos.durability().kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS;
reader_qos.history().kind = eprosima::fastdds::dds::KEEP_ALL_HISTORY_QOS;
request_reader_ = subscriber_->create_datareader(request_topic_, reader_qos, &listener_);
if (nullptr == request_reader_)
{
return false;
}
return true;
}
void deinit()
{
if (nullptr != participant_)
{
participant_->delete_contained_entities();
eprosima::fastdds::dds::DomainParticipantFactory::get_instance()->delete_participant(participant_);
}
}
private:
eprosima::fastdds::dds::DomainParticipant* participant_ = nullptr;
eprosima::fastdds::dds::Publisher* publisher_ = nullptr;
eprosima::fastdds::dds::Subscriber* subscriber_ = nullptr;
eprosima::fastdds::dds::Topic* request_topic_ = nullptr;
eprosima::fastdds::dds::Topic* reply_topic_ = nullptr;
eprosima::fastdds::dds::DataWriter* reply_writer_ = nullptr;
eprosima::fastdds::dds::DataReader* request_reader_ = nullptr;
eprosima::fastdds::dds::TypeSupport request_type_;
eprosima::fastdds::dds::TypeSupport reply_type_;
Listener listener_ = {nullptr};
};
int main()
{
CalculatorServer server;
server.init();
std::cout << "Press a key + ENTER to close the server" << std::endl;
char c;
std::cin >> c;
server.deinit();
return 0;
}