Open Lighting Architecture
0.9.5
Main Page
Modules
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
include
ola
util
Backoff.h
1
/*
2
* This library is free software; you can redistribute it and/or
3
* modify it under the terms of the GNU Lesser General Public
4
* License as published by the Free Software Foundation; either
5
* version 2.1 of the License, or (at your option) any later version.
6
*
7
* This library is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
* Lesser General Public License for more details.
11
*
12
* You should have received a copy of the GNU Lesser General Public
13
* License along with this library; if not, write to the Free Software
14
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
*
16
* Backoff.h
17
* Copyright (C) 2013 Simon Newton
18
*/
19
20
#ifndef INCLUDE_OLA_UTIL_BACKOFF_H_
21
#define INCLUDE_OLA_UTIL_BACKOFF_H_
22
23
#include <math.h>
24
#include <ola/Clock.h>
25
#include <memory>
26
27
namespace
ola {
28
33
class
BackOffPolicy
{
34
public
:
35
BackOffPolicy
() {}
36
virtual
~
BackOffPolicy
() {}
37
44
virtual
TimeInterval
BackOffTime
(
unsigned
int
failed_attempts)
const
= 0;
45
};
46
47
52
class
ConstantBackoffPolicy
:
public
BackOffPolicy
{
53
public
:
54
explicit
ConstantBackoffPolicy
(
const
TimeInterval
&duration)
55
: m_duration(duration) {
56
}
57
58
TimeInterval
BackOffTime
(
unsigned
int
)
const
{
59
return
m_duration;
60
}
61
62
private
:
63
const
TimeInterval
m_duration;
64
};
65
66
73
class
LinearBackoffPolicy
:
public
BackOffPolicy
{
74
public
:
75
LinearBackoffPolicy
(
const
TimeInterval
&duration,
const
TimeInterval
&max)
76
: m_duration(duration),
77
m_max(max) {
78
}
79
80
TimeInterval
BackOffTime
(
unsigned
int
failed_attempts)
const
{
81
TimeInterval
interval = m_duration * failed_attempts;
82
if
(interval > m_max)
83
interval = m_max;
84
return
interval;
85
}
86
87
private
:
88
const
TimeInterval
m_duration;
89
const
TimeInterval
m_max;
90
};
91
92
98
class
ExponentialBackoffPolicy
:
public
BackOffPolicy
{
99
public
:
100
ExponentialBackoffPolicy
(
const
TimeInterval
&initial,
101
const
TimeInterval
&max)
102
: m_initial(initial),
103
m_max(max) {
104
}
105
106
TimeInterval
BackOffTime
(
unsigned
int
failed_attempts)
const
{
107
TimeInterval
interval = (
108
m_initial *
static_cast<
int
>
(::pow(2, failed_attempts - 1)));
109
if
(interval > m_max)
110
interval = m_max;
111
return
interval;
112
}
113
114
private
:
115
const
TimeInterval
m_initial;
116
const
TimeInterval
m_max;
117
};
118
119
120
// TODO(simon): add an ExponentialJitterBackoffPolicy
121
122
// Generates backoff times.
123
class
BackoffGenerator
{
124
public
:
125
explicit
BackoffGenerator
(
const
BackOffPolicy
*policy)
126
: m_policy(policy),
127
m_failures(0) {
128
}
129
130
TimeInterval
Next() {
131
return
m_policy->BackOffTime(++m_failures);
132
}
133
134
void
Reset() {
135
m_failures = 0;
136
}
137
138
private
:
139
std::auto_ptr<const BackOffPolicy> m_policy;
140
unsigned
int
m_failures;
141
};
142
}
// namespace ola
143
#endif // INCLUDE_OLA_UTIL_BACKOFF_H_
Generated on Sun Mar 1 2015 13:02:40 for Open Lighting Architecture by
1.8.1.2