forked from embedded-software-laboratory/embeddedRTPS
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StatefulWriter.tpp
374 lines (325 loc) · 11.4 KB
/
StatefulWriter.tpp
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
363
364
365
366
367
368
369
370
371
372
373
374
/*
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/messages/MessageFactory.h"
#include <cstring>
#include <stdio.h>
using rtps::StatefulWriterT;
/*
static int line_cnt_ = 0;
static char tft_buffer_[150];
#define log(args...) if(true){ \
Lock lock{m_mutex}; \
snprintf(tft_buffer_, sizeof(tft_buffer_), args); \
TFT_PrintLine(line_cnt_, tft_buffer_); \
line_cnt_ = (line_cnt_+1)%5; \
}
*/
#define SFW_VERBOSE 0
#if SFW_VERBOSE
#include "rtps/utils/printutils.h"
#endif
template <class NetworkDriver>
StatefulWriterT<NetworkDriver>::~StatefulWriterT() {
m_running = false;
sys_msleep(10); // Required for tests/ Join currently not available
// if(sys_mutex_valid(&m_mutex)){
sys_mutex_free(&m_mutex);
//}
}
template <class NetworkDriver>
bool StatefulWriterT<NetworkDriver>::init(TopicData attributes,
TopicKind_t topicKind,
ThreadPool * /*threadPool*/,
NetworkDriver &driver) {
if (sys_mutex_new(&m_mutex) != ERR_OK) {
#if SFW_VERBOSE
log("StatefulWriter: Failed to create mutex.\n");
#endif
return false;
}
m_transport = &driver;
m_attributes = attributes;
m_topicKind = topicKind;
m_packetInfo.srcPort = attributes.unicastLocator.port;
if (m_attributes.endpointGuid.entityId ==
ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER) {
#ifdef MROS2_USE_EMBEDDEDRTPS
m_heartbeatThread = sys_thread_new("HBThreadPub", callHbPubFunc, this,
Config::HEARTBEAT_STACKSIZE,
Config::THREAD_POOL_WRITER_PRIO);
networkPubDriverPtr = this;
hbPubFuncPtr = hbFunctionJumppad;
#else
m_heartbeatThread = sys_thread_new("HBThreadPub", hbFunctionJumppad, this,
Config::HEARTBEAT_STACKSIZE,
Config::THREAD_POOL_WRITER_PRIO);
#endif
} else if (m_attributes.endpointGuid.entityId ==
ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER) {
#ifdef MROS2_USE_EMBEDDEDRTPS
m_heartbeatThread = sys_thread_new("HBThreadSub", callHbSubFunc, this,
Config::HEARTBEAT_STACKSIZE,
Config::THREAD_POOL_WRITER_PRIO);
networkSubDriverPtr = this;
hbSubFuncPtr = hbFunctionJumppad;
#else
m_heartbeatThread = sys_thread_new("HBThreadSub", hbFunctionJumppad, this,
Config::HEARTBEAT_STACKSIZE,
Config::THREAD_POOL_WRITER_PRIO);
#endif
} else {
m_heartbeatThread = sys_thread_new("HBThread", hbFunctionJumppad, this,
Config::HEARTBEAT_STACKSIZE,
Config::THREAD_POOL_WRITER_PRIO);
}
/*
if(hbFuncPointer == NULL)
{
hbFuncPointer = hbFunctionJumppad;
hogePointer = this;
}*/
m_is_initialized_ = true;
return true;
}
template <class NetworkDriver>
bool StatefulWriterT<NetworkDriver>::addNewMatchedReader(
const ReaderProxy &newProxy) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: New reader added with id: ",
&this->m_attributes.topicName[0]);
printGuid(newProxy.remoteReaderGuid);
log("\n");
#endif
return m_proxies.add(newProxy);
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::removeReader(const Guid &guid) {
auto isElementToRemove = [&](const ReaderProxy &proxy) {
return proxy.remoteReaderGuid == guid;
};
auto thunk = [](void *arg, const ReaderProxy &value) {
return (*static_cast<decltype(isElementToRemove) *>(arg))(value);
};
m_proxies.remove(thunk, &isElementToRemove);
}
template <class NetworkDriver>
const rtps::CacheChange *StatefulWriterT<NetworkDriver>::newChange(
ChangeKind_t kind, const uint8_t *data, DataSize_t size) {
if (isIrrelevant(kind)) {
return nullptr;
}
Lock lock{m_mutex};
if (m_history.isFull()) {
// Right now we drop elements anyway because we cannot detect non-responding
// readers yet. return nullptr;
SequenceNumber_t newMin = ++SequenceNumber_t(m_history.getSeqNumMin());
if (m_nextSequenceNumberToSend < newMin) {
m_nextSequenceNumberToSend =
newMin; // Make sure we have the correct sn to send
}
}
auto *result = m_history.addChange(data, size);
if (mp_threadPool != nullptr) {
mp_threadPool->addWorkload(this);
}
#if SFW_VERBOSE
log("StatefulWriter[%s]: Adding new data.\n", this->m_attributes.topicName);
#endif
return result;
}
template <class NetworkDriver> void StatefulWriterT<NetworkDriver>::progress() {
for (const auto &proxy : m_proxies) {
if (!sendData(proxy, m_nextSequenceNumberToSend)) {
continue;
}
}
++m_nextSequenceNumberToSend;
}
template <typename NetworkDriver>
bool StatefulWriterT<NetworkDriver>::isIrrelevant(ChangeKind_t kind) const {
// Right now we only allow alive changes
// return kind == ChangeKind_t::INVALID || (m_topicKind == TopicKind_t::NO_KEY
// && kind != ChangeKind_t::ALIVE);
return kind != ChangeKind_t::ALIVE;
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::setAllChangesToUnsent() {
Lock lock(m_mutex);
m_nextSequenceNumberToSend = m_history.getSeqNumMin();
if (mp_threadPool != nullptr) {
mp_threadPool->addWorkload(this);
}
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::onNewAckNack(
const SubmessageAckNack &msg, const GuidPrefix_t &sourceGuidPrefix) {
// Lock lock{m_mutex};
// Search for reader
ReaderProxy *reader = nullptr;
for (auto &proxy : m_proxies) {
if (proxy.remoteReaderGuid.prefix == sourceGuidPrefix &&
proxy.remoteReaderGuid.entityId == msg.readerId) {
reader = &proxy;
break;
}
}
if (reader == nullptr) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: No proxy found with id: ",
&this->m_attributes.topicName[0]);
printEntityId(msg.readerId);
log(" Dropping acknack.\n");
#endif
return;
}
uint8_t hash = 0;
for (int i = 0; i < sourceGuidPrefix.id.size(); i++) {
hash += sourceGuidPrefix.id.at(i);
}
char bfr[20];
size_t size = snprintf(bfr, sizeof(bfr), "%u <= %u", msg.count.value,
reader->ackNackCount.value);
if (!(size < sizeof(bfr))) {
while (1)
;
}
if (msg.count.value <= reader->ackNackCount.value) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: Count too small. Dropping acknack.\n",
&this->m_attributes.topicName[0]);
#endif
return;
}
reader->ackNackCount = msg.count;
// Send missing packets
SequenceNumber_t nextSN = msg.readerSNState.base;
#if SFW_VERBOSE
if (nextSN.low == 0 && nextSN.high == 0) {
log("StatefulWriter[%s]: Received preemptive acknack. Ignored.\n",
&this->m_attributes.topicName[0]);
} else {
log("StatefulWriter[%s]: Received non-preemptive acknack.\n",
&this->m_attributes.topicName[0]);
}
#endif
for (uint32_t i = 0; i < msg.readerSNState.numBits; ++i, ++nextSN) {
if (msg.readerSNState.isSet(i)) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: Send Packet on acknack.\n",
this->m_attributes.topicName);
#endif
sendData(*reader, nextSN);
}
}
// Check for sequence numbers after defined range
SequenceNumber_t maxSN;
{
Lock lock(m_mutex);
maxSN = m_history.getSeqNumMax();
}
while (nextSN <= maxSN) {
sendData(*reader, nextSN);
++nextSN;
}
}
template <class NetworkDriver>
bool StatefulWriterT<NetworkDriver>::sendData(
const ReaderProxy &reader, const SequenceNumber_t &snMissing) {
// TODO smarter packaging e.g. by creating MessageStruct and serialize after
// adjusting values Reusing the pbuf is not possible. See
// https://www.nongnu.org/lwip/2_0_x/raw_api.html (Zero-Copy MACs)
PacketInfo info;
info.srcPort = m_packetInfo.srcPort;
MessageFactory::addHeader(info.buffer, m_attributes.endpointGuid.prefix);
MessageFactory::addSubMessageTimeStamp(info.buffer);
// Just usable for IPv4
const Locator &locator = reader.remoteLocator;
info.destAddr = locator.getIp4Address();
info.destPort = (Ip4Port_t)locator.port;
{
Lock lock(m_mutex);
const CacheChange *next = m_history.getChangeBySN(snMissing);
if (next == nullptr) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: Couldn't get a CacheChange with SN (%i,%u)\n",
&this->m_attributes.topicName[0], snMissing.high, snMissing.low);
#endif
return false;
}
MessageFactory::addSubMessageData(
info.buffer, next->data, false, next->sequenceNumber,
m_attributes.endpointGuid.entityId, reader.remoteReaderGuid.entityId);
}
m_transport->sendPacket(info);
return true;
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::hbFunctionJumppad(void *thisPointer) {
auto *writer = static_cast<StatefulWriterT<NetworkDriver> *>(thisPointer);
writer->sendHeartBeatLoop();
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::sendHeartBeatLoop() {
while (m_running) {
sendHeartBeat();
sys_msleep(Config::SF_WRITER_HB_PERIOD_MS);
}
}
template <class NetworkDriver>
void StatefulWriterT<NetworkDriver>::sendHeartBeat() {
if (m_proxies.isEmpty()) {
#if SFW_VERBOSE
log("StatefulWriter[%s]: Skipping heartbeat. No proxies.\n",
this->m_attributes.topicName);
#endif
return;
}
for (auto &proxy : m_proxies) {
PacketInfo info;
info.srcPort = m_packetInfo.srcPort;
SequenceNumber_t firstSN;
SequenceNumber_t lastSN;
MessageFactory::addHeader(info.buffer, m_attributes.endpointGuid.prefix);
{
Lock lock(m_mutex);
firstSN = m_history.getSeqNumMin();
lastSN = m_history.getSeqNumMax();
}
if (firstSN == SEQUENCENUMBER_UNKNOWN || lastSN == SEQUENCENUMBER_UNKNOWN) {
#if SFW_VERBOSE
if (strlen(&this->m_attributes.typeName[0]) != 0) {
log("StatefulWriter[%s]: Skipping heartbeat. No data.\n",
this->m_attributes.topicName);
}
#endif
return;
}
MessageFactory::addHeartbeat(
info.buffer, m_attributes.endpointGuid.entityId,
proxy.remoteReaderGuid.entityId, firstSN, lastSN, m_hbCount);
info.destAddr = proxy.remoteLocator.getIp4Address();
info.destPort = proxy.remoteLocator.port;
m_transport->sendPacket(info);
}
m_hbCount.value++;
}
#undef SFW_VERBOSE