-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectiveReader.tpp
More file actions
199 lines (168 loc) · 6.56 KB
/
Copy pathSelectiveReader.tpp
File metadata and controls
199 lines (168 loc) · 6.56 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
/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
This file is part of embeddedRTPS.
Author: i11 - Embedded Software, RWTH Aachen University
*/
#include "rtps/discovery/FRDPAgent.h"
#include "rtps/utils/Lock.h"
#include "rtps/utils/Log.h"
#include "rtps/sft/sft_config.h"
#include <ucdr/microcdr.h>
#if SFT_FIELDMASK_MODE == 1
#include "rtps/sft/core/AccessTracking.hpp"
#endif
#if SLR_VERBOSE && RTPS_GLOBAL_VERBOSE
#include "rtps/utils/printutils.h"
#define SLR_LOG(...) \
if (true) { \
printf("[SelectiveReader %s] ", &this->m_attributes.topicName[0]); \
printf(__VA_ARGS__); \
printf("\n"); \
}
#else
#define SLR_LOG(...) //
#endif
namespace rtps {
template <class SftMessageType>
void SelectiveReader<SftMessageType>::init(const TopicData &attributes,
FRDPAgent *frdpAgent) {
m_attributes = attributes;
m_frdp_agent = frdpAgent;
m_is_initialized_ = true;
if (mutex_new(&m_mutex) != EMB_ERR_OK) {
SLR_LOG("Failed to create mutex.\n");
}
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::newChange(const ReaderCacheChange &cacheChange) {
SLR_LOG("newChange received, size=%u\n", cacheChange.size);
if (m_sftCallback != nullptr) {
// Copy data for deserialization
if (cacheChange.size > MAX_BUFFER_SIZE) {
SLR_LOG("Message too large for buffer: size=%u max=%u\n",
cacheChange.size, (unsigned)MAX_BUFFER_SIZE);
return;
}
if (!cacheChange.copyInto(m_deserializeBuffer, MAX_BUFFER_SIZE)) {
SLR_LOG("Failed to copy data\n");
return;
}
SLR_LOG("SFT newChange: size=%u fieldMask=0x%08X\n",
cacheChange.size, cacheChange.fieldMask);
ucdrBuffer rb;
ucdr_init_buffer(&rb, m_deserializeBuffer, cacheChange.size);
m_message.deserialize_partial(&rb, cacheChange.fieldMask);
m_message.reset_access_counts();
m_sftCallback(m_callee, &m_message);
// After callback, update field requirements based on accessed fields
updateFieldRequirements();
} else if (m_rawCallback != nullptr) {
// Fallback to raw callback for compatibility
m_rawCallback(m_callee, cacheChange);
} else {
SLR_LOG("No callback registered\n");
}
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::updateFieldRequirements() {
uint32_t requiredMask = 0;
#if SFT_FIELDMASK_MODE == 0
requiredMask = m_message.generate_field_mask();
SLR_LOG("Runtime field mask computed: 0x%08X\n", requiredMask);
#elif SFT_FIELDMASK_MODE == 1
requiredMask =
sft::access_tracking::get_compile_time_mask<SftMessageType>();
SLR_LOG("Compile-time field mask: 0x%08X\n", requiredMask);
#else
#error "Invalid SFT_FIELDMASK_MODE value. Must be 0 or 1."
#endif
if (requiredMask != m_lastAnnouncedMask) {
SLR_LOG("Field mask changed: 0x%08X -> 0x%08X\n",
m_lastAnnouncedMask, requiredMask);
m_lastAnnouncedMask = requiredMask;
if (m_frdp_agent != nullptr) {
m_frdp_agent->announceFieldRequirements(m_attributes.endpointGuid, requiredMask);
}
}
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::setFieldRequirements(uint32_t fieldMask) {
if (fieldMask == m_lastAnnouncedMask) {
return;
}
SLR_LOG("Field mask set explicitly: 0x%08X -> 0x%08X\n",
m_lastAnnouncedMask, fieldMask);
m_lastAnnouncedMask = fieldMask;
if (m_frdp_agent != nullptr) {
m_frdp_agent->announceFieldRequirements(m_attributes.endpointGuid, fieldMask);
}
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::registerSftCallback(sftCallback_fp cb,
void *callee) {
if (cb != nullptr) {
m_sftCallback = cb;
m_callee = callee;
}
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::registerCallback(ddsReaderCallback_fp cb,
void *callee) {
if (cb != nullptr) {
m_rawCallback = cb;
m_callee = callee;
}
}
template <class SftMessageType>
bool SelectiveReader<SftMessageType>::addNewMatchedWriter(
const WriterProxy &newProxy) {
SLR_LOG("Adding WriterProxy\n");
return m_proxies.add(newProxy);
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::removeWriter(const Guid_t &guid) {
Lock lock(m_mutex);
auto isElementToRemove = [&](const WriterProxy &proxy) {
return proxy.remoteWriterGuid == guid;
};
auto thunk = [](void *arg, const WriterProxy &value) {
return (*static_cast<decltype(isElementToRemove) *>(arg))(value);
};
m_proxies.remove(thunk, &isElementToRemove);
}
template <class SftMessageType>
void SelectiveReader<SftMessageType>::removeWriterOfParticipant(
const GuidPrefix_t &guidPrefix) {
Lock lock(m_mutex);
auto isElementToRemove = [&](const WriterProxy &proxy) {
return proxy.remoteWriterGuid.prefix == guidPrefix;
};
auto thunk = [](void *arg, const WriterProxy &value) {
return (*static_cast<decltype(isElementToRemove) *>(arg))(value);
};
m_proxies.remove(thunk, &isElementToRemove);
}
template <class SftMessageType>
bool SelectiveReader<SftMessageType>::onNewHeartbeat(const SubmessageHeartbeat &,
const GuidPrefix_t &) {
// Nothing to do for best-effort reader
return true;
}
} // namespace rtps
#undef SLR_LOG