-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathDataReaderInstance.hpp
More file actions
362 lines (311 loc) · 12.4 KB
/
DataReaderInstance.hpp
File metadata and controls
362 lines (311 loc) · 12.4 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
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
356
357
358
359
360
361
362
// Copyright 2021 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.
/**
* @file DataReaderInstance.hpp
*/
#ifndef _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERINSTANCE_HPP_
#define _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERINSTANCE_HPP_
#include <chrono>
#include <cstdint>
#include <fastdds/dds/subscriber/InstanceState.hpp>
#include <fastdds/dds/subscriber/ViewState.hpp>
#include <fastrtps/utils/collections/ResourceLimitedVector.hpp>
#include "DataReaderCacheChange.hpp"
#include "DataReaderHistoryCounters.hpp"
namespace eprosima {
namespace fastdds {
namespace dds {
namespace detail {
/// Book-keeping information for an instance
struct DataReaderInstance
{
using ChangeCollection = eprosima::fastrtps::ResourceLimitedVector<DataReaderCacheChange, std::true_type>;
using WriterOwnership = std::pair<fastrtps::rtps::GUID_t, uint32_t>;
using WriterCollection = eprosima::fastrtps::ResourceLimitedVector<WriterOwnership, std::false_type>;
//! A vector of DataReader changes belonging to the same instance
ChangeCollection cache_changes;
//! The list of alive writers for this instance
WriterCollection alive_writers;
//! GUID and strength of the current maximum strength writer
WriterOwnership current_owner{ {}, (std::numeric_limits<uint32_t>::max)() };
//! The time when the group will miss the deadline
std::chrono::steady_clock::time_point next_deadline_us;
//! Current view state of the instance
ViewStateKind view_state = ViewStateKind::NEW_VIEW_STATE;
//! Current instance state of the instance
InstanceStateKind instance_state = InstanceStateKind::ALIVE_INSTANCE_STATE;
//! Current disposed generation of the instance
int32_t disposed_generation_count = 0;
//! Current no_writers generation of the instance
int32_t no_writers_generation_count = 0;
//! Whether the instance has a state notification sample available
bool has_state_notification_sample = false;
DataReaderInstance(
const eprosima::fastrtps::ResourceLimitedContainerConfig& changes_allocation,
const eprosima::fastrtps::ResourceLimitedContainerConfig& writers_allocation)
: cache_changes(changes_allocation)
, alive_writers(writers_allocation)
{
}
void writer_update_its_ownership_strength(
const fastrtps::rtps::GUID_t& writer_guid,
const uint32_t ownership_strength)
{
if (writer_guid == current_owner.first)
{
// Check it is an "alive" writer.
auto writer_it = std::find_if(alive_writers.begin(), alive_writers.end(),
[&writer_guid](const WriterOwnership& item)
{
return item.first == writer_guid;
});
assert(alive_writers.end() != writer_it);
// Update writer info
(*writer_it).second = ownership_strength;
current_owner.second = ownership_strength;
update_owner();
}
return;
}
bool update_state(
DataReaderHistoryCounters& counters,
const fastrtps::rtps::ChangeKind_t change_kind,
const fastrtps::rtps::GUID_t& writer_guid,
const uint32_t ownership_strength)
{
bool ret_val = false;
if (!has_been_accounted_)
{
has_been_accounted_ = true;
assert(ViewStateKind::NEW_VIEW_STATE == view_state);
++counters.instances_new;
assert(InstanceStateKind::ALIVE_INSTANCE_STATE == instance_state);
++counters.instances_alive;
}
switch (change_kind)
{
case fastrtps::rtps::ALIVE:
ret_val = writer_alive(counters, writer_guid, ownership_strength);
break;
case fastrtps::rtps::NOT_ALIVE_DISPOSED:
ret_val = writer_dispose(counters, writer_guid, ownership_strength);
break;
case fastrtps::rtps::NOT_ALIVE_DISPOSED_UNREGISTERED:
ret_val = writer_dispose(counters, writer_guid, ownership_strength);
ret_val |= writer_unregister(counters, writer_guid);
break;
case fastrtps::rtps::NOT_ALIVE_UNREGISTERED:
ret_val = writer_unregister(counters, writer_guid);
break;
default:
// TODO (Miguel C): log error / assert
break;
}
has_state_notification_sample = false;
return ret_val;
}
bool writer_removed(
DataReaderHistoryCounters& counters,
const fastrtps::rtps::GUID_t& writer_guid)
{
return has_been_accounted_ && writer_unregister(counters, writer_guid);
}
void deadline_missed()
{
if (fastrtps::rtps::c_Guid_Unknown != current_owner.first)
{
if (alive_writers.remove_if([&](const WriterOwnership& item)
{
return item.first == current_owner.first;
}))
{
current_owner.second = 0;
current_owner.first = fastrtps::rtps::c_Guid_Unknown;
if (alive_writers.empty() && (InstanceStateKind::ALIVE_INSTANCE_STATE == instance_state))
{
instance_state = InstanceStateKind::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE;
has_state_notification_sample = true;
}
if (ALIVE_INSTANCE_STATE == instance_state)
{
update_owner();
}
}
}
}
private:
//! Whether this instance has ever been included in the history counters
bool has_been_accounted_ = false;
bool writer_alive(
DataReaderHistoryCounters& counters,
const fastrtps::rtps::GUID_t& writer_guid,
const uint32_t ownership_strength)
{
bool ret_val = false;
if (writer_guid == current_owner.first) // Accept sample of current owner.
{
current_owner.second = ownership_strength;
ret_val = true;
}
else if (ownership_strength > current_owner.second) // Accept sample of greater strength writer
{
current_owner.first = writer_guid;
current_owner.second = ownership_strength;
ret_val = true;
}
else if (ownership_strength == current_owner.second &&
writer_guid < current_owner.first) // Check if new writer has lower GUID.
{
current_owner.first = writer_guid;
ret_val = true;
}
else if ((std::numeric_limits<uint32_t>::max)() == ownership_strength) // uint32_t::max indicates we are in SHARED_OWNERSHIP_QOS.
{
assert(eprosima::fastrtps::rtps::c_Guid_Unknown == current_owner.first);
assert((std::numeric_limits<uint32_t>::max)() == current_owner.second);
ret_val = true;
}
else if (eprosima::fastrtps::rtps::c_Guid_Unknown == current_owner.first) // Without owner.
{
current_owner.first = writer_guid;
current_owner.second = ownership_strength;
ret_val = true;
}
if (ret_val)
{
if (InstanceStateKind::NOT_ALIVE_DISPOSED_INSTANCE_STATE == instance_state)
{
counters_update(counters.instances_disposed, counters.instances_alive, counters, true);
++disposed_generation_count;
alive_writers.clear();
view_state = ViewStateKind::NEW_VIEW_STATE;
}
else if (InstanceStateKind::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE == instance_state)
{
counters_update(counters.instances_no_writers, counters.instances_alive, counters, true);
++no_writers_generation_count;
assert(0 == alive_writers.size());
view_state = ViewStateKind::NEW_VIEW_STATE;
}
instance_state = InstanceStateKind::ALIVE_INSTANCE_STATE;
}
writer_set(writer_guid, ownership_strength);
return ret_val;
}
bool writer_dispose(
DataReaderHistoryCounters& counters,
const fastrtps::rtps::GUID_t& writer_guid,
const uint32_t ownership_strength)
{
bool ret_val = false;
if (ownership_strength >= current_owner.second ||
(ownership_strength == current_owner.second &&
writer_guid < current_owner.first)
)
{
if ((std::numeric_limits<uint32_t>::max)() != ownership_strength) // Not SHARED_OWNERSHIP_QOS
{
current_owner.first = writer_guid;
current_owner.second = ownership_strength;
}
if (InstanceStateKind::ALIVE_INSTANCE_STATE == instance_state)
{
ret_val = true;
instance_state = InstanceStateKind::NOT_ALIVE_DISPOSED_INSTANCE_STATE;
counters_update(counters.instances_alive, counters.instances_disposed, counters, false);
}
}
writer_set(writer_guid, ownership_strength);
return ret_val;
}
bool writer_unregister(
DataReaderHistoryCounters& counters,
const fastrtps::rtps::GUID_t& writer_guid)
{
bool ret_val = false;
if (alive_writers.remove_if([&writer_guid](const WriterOwnership& item)
{
return item.first == writer_guid;
}))
{
if (writer_guid == current_owner.first)
{
current_owner.second = 0;
current_owner.first = fastrtps::rtps::c_Guid_Unknown;
if (ALIVE_INSTANCE_STATE == instance_state)
{
update_owner();
}
}
if (alive_writers.empty() && (InstanceStateKind::ALIVE_INSTANCE_STATE == instance_state))
{
instance_state = InstanceStateKind::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE;
counters_update(counters.instances_alive, counters.instances_no_writers, counters, false);
has_state_notification_sample = true;
}
ret_val = true;
}
return ret_val;
}
void writer_set(
const fastrtps::rtps::GUID_t& writer_guid,
const uint32_t ownership_strength)
{
auto it = std::find_if(alive_writers.begin(), alive_writers.end(), [&writer_guid](const WriterOwnership& item)
{
return item.first == writer_guid;
});
if (it == alive_writers.end())
{
alive_writers.emplace_back(writer_guid, ownership_strength);
}
else
{
it->second = ownership_strength;
}
}
void counters_update(
uint64_t& decremented_counter,
uint64_t& incremented_counter,
DataReaderHistoryCounters& counters,
bool set_as_new_view_state)
{
--decremented_counter;
++incremented_counter;
if (set_as_new_view_state && (ViewStateKind::NEW_VIEW_STATE != view_state))
{
++counters.instances_new;
--counters.instances_not_new;
}
}
void update_owner()
{
std::for_each(alive_writers.begin(), alive_writers.end(),
[&](const WriterOwnership& item)
{
if (item.second > current_owner.second ||
(item.second == current_owner.second &&
item.first < current_owner.first)
)
{
current_owner = item;
}
});
}
};
} /* namespace detail */
} /* namespace dds */
} /* namespace fastdds */
} /* namespace eprosima */
#endif // _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERCACHECHANGE_HPP_