F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.hpp>
18 #include <Fw/Time/Time.hpp>
19 
20 #define MAX_TOKEN_BUCKET_TOKENS 1000
21 
22 namespace Utils {
23 
25  {
26 
27  public:
28 
29  // Full constructor
30  //
31  // replenishInterval is in microseconds
32  //
33  TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime);
34 
35  // replenishRate=1, startTokens=maxTokens, startTime=0
36  TokenBucket(U32 replenishInterval, U32 maxTokens);
37 
38  public:
39 
40  // Adjust settings at runtime
41  void setMaxTokens(U32 maxTokens);
42  void setReplenishInterval(U32 replenishInterval);
43  void setReplenishRate(U32 replenishRate);
44 
45  U32 getMaxTokens() const;
46  U32 getReplenishInterval() const;
47  U32 getReplenishRate() const;
48  U32 getTokens() const;
49 
50  // Manual replenish
51  void replenish();
52 
53  // Main point of entry
54  //
55  // Evaluates time since last trigger to determine number of tokens to
56  // replenish. If time moved backwards, always returns false.
57  //
58  // If number of tokens is not zero, consumes one and returns true.
59  // Otherwise, returns false.
60  //
61  bool trigger(const Fw::Time time);
62 
63  private:
64 
65  // parameters
66  U32 m_replenishInterval;
67  U32 m_maxTokens;
68  U32 m_replenishRate;
69 
70  // state
71  U32 m_tokens;
72  Fw::Time m_time;
73  };
74 
75 } // end namespace Utils
76 
77 #endif
C++-compatible configuration header for fprime configuration.
Definition: Time.hpp:9
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