-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathPDPStatelessWriter.cpp
More file actions
179 lines (157 loc) · 5.09 KB
/
PDPStatelessWriter.cpp
File metadata and controls
179 lines (157 loc) · 5.09 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
// 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.
/**
* @file PDPStatelessWriter.cpp
*/
#include <rtps/builtin/discovery/participant/simple/PDPStatelessWriter.hpp>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <mutex>
#include <set>
#include <vector>
#include <fastrtps/rtps/history/WriterHistory.h>
#include <rtps/participant/RTPSParticipantImpl.h>
namespace eprosima {
namespace fastrtps {
namespace rtps {
PDPStatelessWriter::PDPStatelessWriter(
RTPSParticipantImpl* participant,
const GUID_t& guid,
const WriterAttributes& attributes,
fastdds::rtps::FlowController* flow_controller,
WriterHistory* history,
WriterListener* listener)
: StatelessWriter(participant, guid, attributes, flow_controller, history, listener)
, interested_readers_(participant->getRTPSParticipantAttributes().allocation.participants)
{
}
bool PDPStatelessWriter::matched_reader_add(
const ReaderProxyData& data)
{
bool ret = StatelessWriter::matched_reader_add(data);
if (ret)
{
// Mark new reader as interested
add_interested_reader(data.guid());
// Send announcement to new reader
reschedule_all_samples();
}
return ret;
}
bool PDPStatelessWriter::matched_reader_remove(
const GUID_t& reader_guid)
{
bool ret = StatelessWriter::matched_reader_remove(reader_guid);
if (ret)
{
// Mark reader as not interested
remove_interested_reader(reader_guid);
}
return ret;
}
void PDPStatelessWriter::unsent_change_added_to_history(
CacheChange_t* change,
const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time)
{
mark_all_readers_interested();
StatelessWriter::unsent_change_added_to_history(change, max_blocking_time);
}
void PDPStatelessWriter::set_initial_peers(
const fastdds::rtps::LocatorList& locator_list)
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
initial_peers_.push_back(locator_list);
mp_RTPSParticipant->createSenderResources(initial_peers_);
}
void PDPStatelessWriter::send_periodic_announcement()
{
mark_all_readers_interested();
reschedule_all_samples();
}
bool PDPStatelessWriter::send_to_fixed_locators(
CDRMessage_t* message,
std::chrono::steady_clock::time_point& max_blocking_time_point) const
{
bool ret = true;
if (should_reach_all_destinations_)
{
ret = initial_peers_.empty() ||
mp_RTPSParticipant->sendSync(message, m_guid,
Locators(initial_peers_.begin()), Locators(initial_peers_.end()),
max_blocking_time_point);
if (ret)
{
fixed_locators_.clear();
should_reach_all_destinations_ = false;
}
}
else
{
interested_readers_.clear();
}
return ret;
}
bool PDPStatelessWriter::is_relevant(
const CacheChange_t& /* change */,
const GUID_t& reader_guid) const
{
return interested_readers_.end() !=
std::find(interested_readers_.begin(), interested_readers_.end(), reader_guid);
}
void PDPStatelessWriter::mark_all_readers_interested()
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
should_reach_all_destinations_ = true;
interested_readers_.clear();
fixed_locators_.clear();
fixed_locators_.push_back(initial_peers_);
reader_data_filter(nullptr);
}
void PDPStatelessWriter::add_interested_reader(
const GUID_t& reader_guid)
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
if (!should_reach_all_destinations_)
{
auto it = std::find(interested_readers_.begin(), interested_readers_.end(), reader_guid);
if (it == interested_readers_.end())
{
interested_readers_.emplace_back(reader_guid);
reader_data_filter(this);
}
}
}
void PDPStatelessWriter::remove_interested_reader(
const GUID_t& reader_guid)
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
interested_readers_.remove(reader_guid);
}
void PDPStatelessWriter::reschedule_all_samples()
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
size_t n_samples = mp_history->getHistorySize();
if (0 < n_samples)
{
assert(1 == n_samples);
auto it = mp_history->changesBegin();
CacheChange_t* change = *it;
flow_controller_->add_new_sample(this, change, std::chrono::steady_clock::now() + std::chrono::hours(24));
}
}
} // namespace rtps
} // namespace fastdds
} // namespace eprosima