-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathShapeSubscriber.cpp
206 lines (186 loc) · 6.8 KB
/
ShapeSubscriber.cpp
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
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// This file is part of eProsima Fast DDS Shapes Demo.
//
// eProsima Fast DDS Shapes Demo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// eProsima Fast DDS Shapes Demo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with eProsima Fast DDS Shapes Demo. If not, see <https://www.gnu.org/licenses/>.
/**
* @file ShapeSubscriber.cpp
*
*/
#include <fastdds/dds/builtin/topic/PublicationBuiltinTopicData.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/rtps/common/InstanceHandle.h>
#include <fastrtps/utils/TimeConversion.h>
#include <eprosimashapesdemo/qt/ContentFilterSelector.h>
#include <eprosimashapesdemo/qt/mainwindow.h>
#include <eprosimashapesdemo/shapesdemo/ShapeSubscriber.h>
ShapeSubscriber::ShapeSubscriber(
MainWindow* win,
ShapesDemo* sd,
Topic* topic)
: mp_sd(sd)
, mp_datareader(nullptr)
, mp_subscriber(nullptr)
, mp_topic(topic)
, listener_(this)
, hasReceived(false)
, m_mutex(QMutex::Recursive)
, mp_contentFilter(nullptr)
, m_mainWindow(win)
{
// TODO Auto-generated constructor stub
}
ShapeSubscriber::~ShapeSubscriber()
{
if (mp_sd->getParticipant() && mp_subscriber && mp_datareader)
{
if (ReturnCode_t::RETCODE_OK != mp_subscriber->delete_datareader(mp_datareader))
{
std::cerr << "Error deleting datareader: " << mp_datareader->guid() << std::endl;
return;
}
}
if (mp_sd->getParticipant() && mp_filtered_topic)
{
if (ReturnCode_t::RETCODE_OK != mp_sd->getParticipant()->delete_contentfilteredtopic(mp_filtered_topic))
{
std::cerr << "Error deleting filtered topic" << std::endl;
// NOTE: don't return since we want to try and remove the subscriber
}
}
if (mp_sd->getParticipant() && mp_subscriber)
{
if (ReturnCode_t::RETCODE_OK != mp_sd->getParticipant()->delete_subscriber(mp_subscriber))
{
std::cerr << "Error deleting subscriber: " << std::endl;
return;
}
}
if (mp_contentFilter != nullptr)
{
delete(mp_contentFilter);
}
}
bool ShapeSubscriber::initSubscriber()
{
mp_subscriber = mp_sd->getParticipant()->create_subscriber(m_sub_qos);
eprosima::fastdds::dds::TopicDescription* topic = mp_topic;
if (m_shapeHistory.m_filter.m_useContentFilter)
{
static uint64_t filter_index = 0;
std::vector<std::string> parameters;
parameters.emplace_back(std::to_string(m_shapeHistory.m_filter.m_minX));
parameters.emplace_back(std::to_string(m_shapeHistory.m_filter.m_maxX));
parameters.emplace_back(std::to_string(m_shapeHistory.m_filter.m_minY));
parameters.emplace_back(std::to_string(m_shapeHistory.m_filter.m_maxY));
mp_filtered_topic = mp_sd->getParticipant()->create_contentfilteredtopic(
std::to_string(filter_index++), mp_topic,
"x > %0 AND x < %1 AND y > %2 AND y < %3", parameters);
if (nullptr != mp_filtered_topic)
{
topic = mp_filtered_topic;
}
else
{
m_mainWindow->addMessageToOutput(QString(
"Error creating content filtered topic. Using application filtering."));
}
}
mp_datareader = mp_subscriber->create_datareader(topic, m_dr_qos, &listener_);
return nullptr != mp_datareader;
}
void ShapeSubscriber::SubListener::on_data_available(
DataReader* reader)
{
Shape shape;
shape.m_type = parent_->m_shapeType;
eprosima::fastdds::dds::SampleInfo info;
while (reader->take_next_sample(&shape.m_shape, &info) == ReturnCode_t::RETCODE_OK)
{
shape.m_time = info.source_timestamp.to_duration_t();
QMutexLocker locck(&parent_->m_mutex);
if (info.instance_state == ALIVE_INSTANCE_STATE)
{
parent_->hasReceived = true;
parent_->m_shapeHistory.addToHistory(shape);
}
else
{
SD_COLOR color = getColorFromInstanceHandle(info.instance_handle);
if (info.instance_state == NOT_ALIVE_DISPOSED_INSTANCE_STATE)
{
parent_->m_shapeHistory.dispose(color);
}
else
{
parent_->m_shapeHistory.unregister(color);
}
}
}
}
void ShapeSubscriber::adjustContentFilter(
ShapeFilter& filter)
{
m_mutex.lock();
m_shapeHistory.adjustContentFilter(filter);
if (nullptr != mp_filtered_topic)
{
std::vector<std::string> parameters;
parameters.emplace_back(std::to_string(filter.m_minX));
parameters.emplace_back(std::to_string(filter.m_maxX));
parameters.emplace_back(std::to_string(filter.m_minY));
parameters.emplace_back(std::to_string(filter.m_maxY));
mp_filtered_topic->set_expression_parameters(parameters);
}
m_mutex.unlock();
}
void ShapeSubscriber::SubListener::on_requested_deadline_missed(
DataReader* reader,
const eprosima::fastrtps::RequestedDeadlineMissedStatus& status)
{
if (0 < status.total_count_change)
{
std::stringstream str;
str << "DataReader " << reader->guid() << " detects deadline missed";
parent_->m_mainWindow->addMessageToOutput(QString(str.str().c_str()));
}
}
void ShapeSubscriber::SubListener::on_liveliness_changed(
DataReader* reader,
const eprosima::fastrtps::LivelinessChangedStatus& status)
{
std::stringstream str;
if (0 < status.alive_count_change)
{
str << "DataReader " << reader->guid() << " detects liveliness recovered";
parent_->m_mainWindow->addMessageToOutput(QString(str.str().c_str()));
}
if (0 < status.not_alive_count_change)
{
str << "DataReader " << reader->guid() << " detects liveliness lost";
parent_->m_mainWindow->addMessageToOutput(QString(str.str().c_str()));
}
}
void ShapeSubscriber::SubListener::on_requested_incompatible_qos(
DataReader* reader,
const RequestedIncompatibleQosStatus& status)
{
if (0 < status.total_count_change)
{
std::stringstream str;
str << "DataReader " << reader->guid() << " detects incompatible QoS " << qos_policy_id_to_string(
status.last_policy_id);
parent_->m_mainWindow->addMessageToOutput(QString(str.str().c_str()));
}
}