F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TokenBucket.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TokenBucket.hpp
3 // \author vwong
4 // \brief hpp 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 #ifndef TokenBucket_HPP
15 #define TokenBucket_HPP
16 
17 #include <Fw/FPrimeBasicTypes.hpp>
18 #include <Fw/Time/Time.hpp>
19 
20 #define MAX_TOKEN_BUCKET_TOKENS 1000
21 
22 namespace Utils {
23 
24 class TokenBucket {
25  public:
26  // Full constructor
27  //
28  // replenishInterval is in microseconds
29  //
30  TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime);
31 
32  // replenishRate=1, startTokens=maxTokens, startTime=0
33  TokenBucket(U32 replenishInterval, U32 maxTokens);
34 
35  public:
36  // Adjust settings at runtime
37  void setMaxTokens(U32 maxTokens);
38  void setReplenishInterval(U32 replenishInterval);
39  void setReplenishRate(U32 replenishRate);
40 
41  U32 getMaxTokens() const;
42  U32 getReplenishInterval() const;
43  U32 getReplenishRate() const;
44  U32 getTokens() const;
45 
46  // Manual replenish
47  void replenish();
48 
49  // Main point of entry
50  //
51  // Evaluates time since last trigger to determine number of tokens to
52  // replenish. If time moved backwards, always returns false.
53  //
54  // If number of tokens is not zero, consumes one and returns true.
55  // Otherwise, returns false.
56  //
57  bool trigger(const Fw::Time time);
58 
59  private:
60  // parameters
61  U32 m_replenishInterval;
62  U32 m_maxTokens;
63  U32 m_replenishRate;
64 
65  // state
66  U32 m_tokens;
67  Fw::Time m_time;
68 };
69 
70 } // end namespace Utils
71 
72 #endif
void setReplenishInterval(U32 replenishInterval)
Definition: TokenBucket.cpp:34
U32 getReplenishRate() const
Definition: TokenBucket.cpp:60
U32 getReplenishInterval() const
Definition: TokenBucket.cpp:52
TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
Definition: TokenBucket.cpp:18
bool trigger(const Fw::Time time)
Definition: TokenBucket.cpp:68
U32 getMaxTokens() const
Definition: TokenBucket.cpp:56
void setReplenishRate(U32 replenishRate)
Definition: TokenBucket.cpp:42
void setMaxTokens(U32 maxTokens)
Definition: TokenBucket.cpp:38
U32 getTokens() const
Definition: TokenBucket.cpp:64