forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveResolveAttempts.cpp
335 lines (283 loc) · 8.77 KB
/
ActiveResolveAttempts.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
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
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/
#include "ActiveResolveAttempts.h"
#include <lib/support/logging/CHIPLogging.h>
using namespace chip;
namespace mdns {
namespace Minimal {
constexpr chip::System::Clock::Timeout ActiveResolveAttempts::kMaxRetryDelay;
void ActiveResolveAttempts::Reset()
{
for (auto & item : mRetryQueue)
{
item.attempt.Clear();
}
}
void ActiveResolveAttempts::Complete(const PeerId & peerId)
{
for (auto & item : mRetryQueue)
{
if (item.attempt.Matches(peerId))
{
item.attempt.Clear();
return;
}
}
#if CHIP_MINMDNS_HIGH_VERBOSITY
// This may happen during boot time adverisements: nodes come online
// and advertise their IP without any explicit queries for them
ChipLogProgress(Discovery, "Discovered node without a pending query");
#endif
}
void ActiveResolveAttempts::CompleteCommissioner(const chip::Dnssd::DiscoveredNodeData & data)
{
for (auto & item : mRetryQueue)
{
if (item.attempt.Matches(data, chip::Dnssd::DiscoveryType::kCommissionerNode))
{
item.attempt.Clear();
return;
}
}
}
void ActiveResolveAttempts::CompleteCommissionable(const chip::Dnssd::DiscoveredNodeData & data)
{
for (auto & item : mRetryQueue)
{
if (item.attempt.Matches(data, chip::Dnssd::DiscoveryType::kCommissionableNode))
{
item.attempt.Clear();
return;
}
}
}
bool ActiveResolveAttempts::HasBrowseFor(chip::Dnssd::DiscoveryType type) const
{
for (auto & item : mRetryQueue)
{
if (!item.attempt.IsBrowse())
{
continue;
}
if (item.attempt.BrowseData().type == type)
{
return true;
}
}
return false;
}
void ActiveResolveAttempts::CompleteIpResolution(SerializedQNameIterator targetHostName)
{
for (auto & item : mRetryQueue)
{
if (item.attempt.MatchesIpResolve(targetHostName))
{
item.attempt.Clear();
return;
}
}
}
CHIP_ERROR ActiveResolveAttempts::CompleteAllBrowses()
{
for (auto & item : mRetryQueue)
{
if (item.attempt.IsBrowse())
{
item.attempt.Clear();
}
}
return CHIP_NO_ERROR;
}
void ActiveResolveAttempts::NodeIdResolutionNoLongerNeeded(const PeerId & peerId)
{
for (auto & item : mRetryQueue)
{
if (item.attempt.Matches(peerId))
{
item.attempt.ConsumerRemoved();
return;
}
}
}
void ActiveResolveAttempts::MarkPending(const chip::PeerId & peerId)
{
MarkPending(ScheduledAttempt(peerId, /* firstSend */ true));
}
void ActiveResolveAttempts::MarkPending(const chip::Dnssd::DiscoveryFilter & filter, const chip::Dnssd::DiscoveryType type)
{
MarkPending(ScheduledAttempt(filter, type, /* firstSend */ true));
}
void ActiveResolveAttempts::MarkPending(ScheduledAttempt::IpResolve && resolve)
{
MarkPending(ScheduledAttempt(std::move(resolve), /* firstSend */ true));
}
void ActiveResolveAttempts::MarkPending(ScheduledAttempt && attempt)
{
// Strategy when picking the peer id to use:
// 1 if a matching peer id is already found, use that one
// 2 if an 'unused' entry is found, use that
// 3 otherwise expire the one with the largest nextRetryDelay
// or if equal nextRetryDelay, pick the one with the oldest
// queryDueTime
RetryEntry * entryToUse = &mRetryQueue[0];
for (size_t i = 1; i < kRetryQueueSize; i++)
{
if (entryToUse->attempt.Matches(attempt))
{
break; // best match possible
}
RetryEntry * entry = mRetryQueue + i;
// Rule 1: attempt match always matches
if (entry->attempt.Matches(attempt))
{
entryToUse = entry;
continue;
}
// Rule 2: select unused entries
if (!entryToUse->attempt.IsEmpty() && entry->attempt.IsEmpty())
{
entryToUse = entry;
continue;
}
if (entryToUse->attempt.IsEmpty())
{
continue;
}
// Rule 3: both choices are used (have a defined node id):
// - try to find the one with the largest next delay (oldest request)
// - on same delay, use queryDueTime to determine the oldest request
// (the one with the smallest due time was issued the longest time
// ago)
if (entry->nextRetryDelay > entryToUse->nextRetryDelay)
{
entryToUse = entry;
}
else if ((entry->nextRetryDelay == entryToUse->nextRetryDelay) && (entry->queryDueTime < entryToUse->queryDueTime))
{
entryToUse = entry;
}
}
if ((!entryToUse->attempt.IsEmpty()) && (!entryToUse->attempt.Matches(attempt)))
{
// TODO: node was evicted here, if/when resolution failures are
// supported this could be a place for error callbacks
//
// Note however that this is NOT an actual 'timeout' it is showing
// a burst of lookups for which we cannot maintain state. A reply may
// still be received for this peer id (query was already sent on the
// network)
ChipLogError(Discovery, "Re-using pending resolve entry before reply was received.");
}
attempt.WillCoalesceWith(entryToUse->attempt);
entryToUse->attempt = attempt;
entryToUse->queryDueTime = mClock->GetMonotonicTimestamp();
entryToUse->nextRetryDelay = System::Clock::Seconds16(1);
}
Optional<System::Clock::Timeout> ActiveResolveAttempts::GetTimeUntilNextExpectedResponse() const
{
Optional<System::Clock::Timeout> minDelay = Optional<System::Clock::Timeout>::Missing();
chip::System::Clock::Timestamp now = mClock->GetMonotonicTimestamp();
for (auto & entry : mRetryQueue)
{
if (entry.attempt.IsEmpty())
{
continue;
}
if (now >= entry.queryDueTime)
{
// found an entry that needs processing right now
return Optional<System::Clock::Timeout>::Value(0);
}
System::Clock::Timeout entryDelay = entry.queryDueTime - now;
if (!minDelay.HasValue() || (minDelay.Value() > entryDelay))
{
minDelay.SetValue(entryDelay);
}
}
return minDelay;
}
Optional<ActiveResolveAttempts::ScheduledAttempt> ActiveResolveAttempts::NextScheduled()
{
chip::System::Clock::Timestamp now = mClock->GetMonotonicTimestamp();
for (auto & entry : mRetryQueue)
{
if (entry.attempt.IsEmpty())
{
continue; // not a pending item
}
if (entry.queryDueTime > now)
{
continue; // not yet due
}
if (entry.nextRetryDelay > kMaxRetryDelay)
{
ChipLogError(Discovery, "Timeout waiting for mDNS resolution.");
entry.attempt.Clear();
continue;
}
entry.queryDueTime = now + entry.nextRetryDelay;
entry.nextRetryDelay *= 2;
Optional<ScheduledAttempt> attempt = MakeOptional(entry.attempt);
entry.attempt.firstSend = false;
return attempt;
}
return Optional<ScheduledAttempt>::Missing();
}
bool ActiveResolveAttempts::ShouldResolveIpAddress(PeerId peerId) const
{
for (auto & item : mRetryQueue)
{
if (item.attempt.IsEmpty())
{
continue;
}
if (item.attempt.IsBrowse())
{
return true;
}
if (item.attempt.IsResolve())
{
auto & data = item.attempt.ResolveData();
if (data.peerId == peerId)
{
return true;
}
}
}
return false;
}
bool ActiveResolveAttempts::IsWaitingForIpResolutionFor(SerializedQNameIterator hostName) const
{
for (auto & entry : mRetryQueue)
{
if (entry.attempt.IsEmpty())
{
continue; // not a pending item
}
if (!entry.attempt.IsIpResolve())
{
continue;
}
if (hostName == entry.attempt.IpResolveData().hostName.Content())
{
return true;
}
}
return false;
}
} // namespace Minimal
} // namespace mdns