F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
48  void TokenBucket ::
50  U32 replenishInterval
51  )
52  {
53  this->m_replenishInterval = replenishInterval;
54  }
55 
56  void TokenBucket ::
58  U32 maxTokens
59  )
60  {
61  this->m_maxTokens = maxTokens;
62  }
63 
64  void TokenBucket ::
66  U32 replenishRate
67  )
68  {
69  this->m_replenishRate = replenishRate;
70  }
71 
72  void TokenBucket ::
74  {
75  if (this->m_tokens < this->m_maxTokens) {
76  this->m_tokens = this->m_maxTokens;
77  }
78  }
79 
80  U32 TokenBucket ::
82  {
83  return this->m_replenishInterval;
84  }
85 
86  U32 TokenBucket ::
88  {
89  return this->m_maxTokens;
90  }
91 
92  U32 TokenBucket ::
94  {
95  return this->m_replenishRate;
96  }
97 
98  U32 TokenBucket ::
99  getTokens() const
100  {
101  return this->m_tokens;
102  }
103 
104  bool TokenBucket ::
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
void setReplenishInterval(U32 replenishInterval)
Definition: TokenBucket.cpp:49
U32 getReplenishRate() const
Definition: TokenBucket.cpp:93
#define MAX_TOKEN_BUCKET_TOKENS
Definition: TokenBucket.hpp:20
U32 getReplenishInterval() const
Definition: TokenBucket.cpp:81
TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
Definition: TokenBucket.cpp:19
bool trigger(const Fw::Time time)
U32 getMaxTokens() const
Definition: TokenBucket.cpp:87
void setReplenishRate(U32 replenishRate)
Definition: TokenBucket.cpp:65
void setMaxTokens(U32 maxTokens)
Definition: TokenBucket.cpp:57
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:92
U32 getTokens() const
Definition: TokenBucket.cpp:99
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:132
#define FW_ASSERT(...)
Definition: Assert.hpp:14