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
|
#include "Ft8MtdDecodeScheduler.hpp"
#include <algorithm>
constexpr int Ft8MtdDecodeScheduler::BackoffPeriods[];
Ft8MtdDecodeScheduler::Decision Ft8MtdDecodeScheduler::request (
Stage stage, qint64 period, int earlyStageCount, bool decoderBusy,
bool finalAlreadyPending)
{
Decision result;
recoverOnFinalPublish_ = false;
if (Stage::None == stage) return result;
if (Stage::Final == stage)
{
if (decoderBusy)
{
auto const wasDegraded = degraded_;
auto const failedProbe = probePeriod_ == period
&& !probeComplete (period);
degrade (period, wasDegraded && failedProbe);
result.enteredDegraded = !wasDegraded;
result.action = finalAlreadyPending ? Action::ReplaceFinal
: Action::DeferFinal;
result.skippedPeriods = skippedPeriods ();
return result;
}
recoverOnFinalPublish_ = degraded_
&& (0 == earlyStageCount || probeComplete (period));
return result;
}
if (!degraded_)
{
if (!decoderBusy) return result;
degrade (period, false);
result.enteredDegraded = true;
result.action = Action::SkipEarly;
result.skippedPeriods = skippedPeriods ();
return result;
}
if (Stage::EarlyOne == stage)
{
if (period < nextProbePeriod_)
{
result.action = Action::SkipEarly;
result.skippedPeriods = skippedPeriods ();
return result;
}
if (decoderBusy)
{
degrade (period, true);
result.action = Action::SkipEarly;
result.skippedPeriods = skippedPeriods ();
return result;
}
probePeriod_ = period;
probeEarlyStageCount_ = std::max (earlyStageCount, 1);
completedEarlyStages_ = 0;
return result;
}
auto const precedingStageCompleted = probePeriod_ == period
&& completedEarlyStages_ >= 1;
if (!precedingStageCompleted || decoderBusy)
{
if (probePeriod_ == period) degrade (period, true);
result.action = Action::SkipEarly;
result.skippedPeriods = skippedPeriods ();
}
return result;
}
bool Ft8MtdDecodeScheduler::published (Stage stage, qint64)
{
if (Stage::Final != stage || !recoverOnFinalPublish_) return false;
degraded_ = false;
backoffIndex_ = 0;
nextProbePeriod_ = 0;
recoverOnFinalPublish_ = false;
clearProbe ();
return true;
}
void Ft8MtdDecodeScheduler::completed (Stage stage, qint64 period)
{
if (period != probePeriod_) return;
if (Stage::EarlyOne == stage)
{
completedEarlyStages_ = std::max (completedEarlyStages_, 1);
}
else if (Stage::EarlyTwo == stage && completedEarlyStages_ >= 1)
{
completedEarlyStages_ = std::max (completedEarlyStages_, 2);
}
}
void Ft8MtdDecodeScheduler::publicationFailed (Stage stage, qint64 period)
{
if (period == probePeriod_ && Stage::Final != stage)
{
degrade (period, true);
}
recoverOnFinalPublish_ = false;
}
void Ft8MtdDecodeScheduler::cancel ()
{
degraded_ = false;
backoffIndex_ = 0;
nextProbePeriod_ = 0;
recoverOnFinalPublish_ = false;
clearProbe ();
}
int Ft8MtdDecodeScheduler::skippedPeriods () const
{
return degraded_ ? BackoffPeriods[backoffIndex_] : 0;
}
void Ft8MtdDecodeScheduler::degrade (qint64 period, bool advance)
{
if (!degraded_)
{
degraded_ = true;
backoffIndex_ = 0;
}
else if (advance)
{
backoffIndex_ = std::min (backoffIndex_ + 1, BackoffCount - 1);
}
nextProbePeriod_ = period + BackoffPeriods[backoffIndex_] + 1;
recoverOnFinalPublish_ = false;
clearProbe ();
}
bool Ft8MtdDecodeScheduler::probeComplete (qint64 period) const
{
return probePeriod_ == period
&& completedEarlyStages_ >= probeEarlyStageCount_;
}
void Ft8MtdDecodeScheduler::clearProbe ()
{
probePeriod_ = -1;
probeEarlyStageCount_ = 0;
completedEarlyStages_ = 0;
}
|