F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
TokenBucket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TokenBucket.cpp
3 // \author vwong
4 // \brief cpp file for a rate limiter utility class
5 //
6 // \copyright
7 //
8 // Copyright (C) 2009-2020 California Institute of Technology.
9 //
10 // ALL RIGHTS RESERVED. United States Government Sponsorship
11 // acknowledged.
12 // ======================================================================
13 
14 #include <Utils/TokenBucket.hpp>
15 
16 namespace Utils {
17 
20  U32 replenishInterval,
21  U32 maxTokens,
22  U32 replenishRate,
23  U32 startTokens,
24  Fw::Time startTime
25  ) :
26  m_replenishInterval(replenishInterval),
27  m_maxTokens(maxTokens),
28  m_replenishRate(replenishRate),
29  m_tokens(startTokens),
30  m_time(startTime)
31  {
32  }
33 
36  U32 replenishInterval,
37  U32 maxTokens
38  ) :
39  m_replenishInterval(replenishInterval),
40  m_maxTokens(maxTokens),
41  m_replenishRate(1),
42  m_tokens(maxTokens),
43  m_time(0, 0)
44  {
45  FW_ASSERT(this->m_maxTokens <= MAX_TOKEN_BUCKET_TOKENS, static_cast<FwAssertArgType>(this->m_maxTokens));
46  }
47 
50  U32 replenishInterval
51  )
52  {
53  this->m_replenishInterval = replenishInterval;
54  }
55 
58  U32 maxTokens
59  )
60  {
61  this->m_maxTokens = maxTokens;
62  }
63 
66  U32 replenishRate
67  )
68  {
69  this->m_replenishRate = replenishRate;
70  }
71 
73  replenish()
74  {
75  if (this->m_tokens < this->m_maxTokens) {
76  this->m_tokens = this->m_maxTokens;
77  }
78  }
79 
82  {
83  return this->m_replenishInterval;
84  }
85 
87  getMaxTokens() const
88  {
89  return this->m_maxTokens;
90  }
91 
93  getReplenishRate() const
94  {
95  return this->m_replenishRate;
96  }
97 
99  getTokens() const
100  {
101  return this->m_tokens;
102  }
103 
105  trigger(
106  const Fw::Time time
107  )
108  {
109  // attempt replenishing
110  if (this->m_replenishRate > 0) {
111  Fw::Time replenishInterval = Fw::Time(this->m_replenishInterval / 1000000, this->m_replenishInterval % 1000000);
112  Fw::Time nextTime = Fw::Time::add(this->m_time, replenishInterval);
113  while (this->m_tokens < this->m_maxTokens && nextTime <= time) {
114  // replenish by replenish rate, or up to maxTokens
115  this->m_tokens += FW_MIN(this->m_replenishRate, this->m_maxTokens - this->m_tokens);
116  this->m_time = nextTime;
117  nextTime = Fw::Time::add(this->m_time, replenishInterval);
118  }
119  if (this->m_tokens >= this->m_maxTokens && this->m_time < time) {
120  this->m_time = time;
121  }
122  }
123 
124  // attempt consuming token
125  if (this->m_tokens > 0) {
126  this->m_tokens--;
127  return true;
128 
129  } else {
130  return false;
131  }
132  }
133 
134 } // end namespace Utils
#define FW_ASSERT(...)
Definition: Assert.hpp:14
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:68
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
#define MAX_TOKEN_BUCKET_TOKENS
Definition: TokenBucket.hpp:20
Definition: Time.hpp:9
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:193
void setMaxTokens(U32 maxTokens)
Definition: TokenBucket.cpp:57
U32 getMaxTokens() const
Definition: TokenBucket.cpp:87
TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
Definition: TokenBucket.cpp:19
void setReplenishRate(U32 replenishRate)
Definition: TokenBucket.cpp:65
U32 getReplenishInterval() const
Definition: TokenBucket.cpp:81
bool trigger(const Fw::Time time)
U32 getTokens() const
Definition: TokenBucket.cpp:99
U32 getReplenishRate() const
Definition: TokenBucket.cpp:93
void setReplenishInterval(U32 replenishInterval)
Definition: TokenBucket.cpp:49