F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RateLimiter.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title RateLimiter.hpp
3 // \author vwong
4 // \brief hpp file for a rate limiter utility class
5 //
6 // \copyright
7 // Copyright (C) 2009-2020 California Institute of Technology.
8 //
9 // ALL RIGHTS RESERVED. United States Government Sponsorship
10 // acknowledged.
11 // ======================================================================
12 
13 #ifndef RateLimiter_HPP
14 #define RateLimiter_HPP
15 
16 #include <Fw/Types/BasicTypes.hpp>
17 #include <Fw/Time/Time.hpp>
18 
19 namespace Utils {
20 
22  {
23 
24  public:
25 
26  // Construct with defined cycles
27  RateLimiter(U32 counterCycle, U32 timeCycle);
28 
29  // Construct with cycles set to 0
30  RateLimiter();
31 
32  public:
33 
34  // Adjust cycles at run-time
35  void setCounterCycle(U32 counterCycle);
36  void setTimeCycle(U32 timeCycle);
37 
38  // Main point of entry
39  //
40  // It will only factor in counter or time, whichever one has a cycle defined
41  //
42  // If both are defined, then satisfying _either_ one will work
43  // e.g. I want to trigger only once every X times or once every Y
44  // seconds, whichever comes first
45  //
46  // The argument-less version is a shorthand for counter-only RateLimiters
47  // If a time cycle is defined but the argument-less version is called,
48  // RateLimiter assumes the client forgot to supply a time, and asserts
49  //
50  bool trigger(Fw::Time time);
51  bool trigger();
52 
53  // Manual state adjustments, if necessary
54  void reset();
55  void resetCounter();
56  void resetTime();
57  void setCounter(U32);
58  void setTime(Fw::Time time);
59 
60  private:
61 
62  // Helper functions to update each independently
63  bool shouldCounterTrigger();
64  bool shouldTimeTrigger(Fw::Time time);
65  void updateCounter(bool triggered);
66  void updateTime(bool triggered, Fw::Time time);
67 
68  private:
69 
70  // parameters
71  U32 m_counterCycle;
72  U32 m_timeCycle;
73 
74  // state
75  U32 m_counter;
76  Fw::Time m_time;
77  bool m_timeAtNegativeInfinity;
78  };
79 
80 } // end namespace Utils
81 
82 #endif
Fw::Time
Definition: Time.hpp:10
Utils::RateLimiter::reset
void reset()
Definition: RateLimiter.cpp:52
Utils::RateLimiter::trigger
bool trigger()
Definition: RateLimiter.cpp:128
Utils
Definition: CRCChecker.cpp:20
Utils::RateLimiter::resetCounter
void resetCounter()
Definition: RateLimiter.cpp:59
Utils::RateLimiter::setCounterCycle
void setCounterCycle(U32 counterCycle)
Definition: RateLimiter.cpp:36
Utils::RateLimiter::setTime
void setTime(Fw::Time time)
Definition: RateLimiter.cpp:80
Utils::RateLimiter::setCounter
void setCounter(U32)
Definition: RateLimiter.cpp:72
Utils::RateLimiter::setTimeCycle
void setTimeCycle(U32 timeCycle)
Definition: RateLimiter.cpp:44
Utils::RateLimiter::resetTime
void resetTime()
Definition: RateLimiter.cpp:65
Utils::RateLimiter::RateLimiter
RateLimiter()
Definition: RateLimiter.cpp:28
BasicTypes.hpp
Declares ISF basic types.
Time.hpp
Utils::RateLimiter
Definition: RateLimiter.hpp:21