CARMA C++
PeriodicTimer.h
1 // $Id: PeriodicTimer.h,v 1.2 2011/11/14 22:42:03 eml Exp $
2 
3 #ifndef SZA_UTIL_PERIODICTIMER_H
4 #define SZA_UTIL_PERIODICTIMER_H
5 
15 #include <iostream>
16 
20 #include "carma/szautil/TimeOut.h"
21 
22 #define PERIODIC_TIMER_HANDLER(fn) void (fn)(void* args)
23 
24 namespace sza {
25  namespace util {
26 
27  //------------------------------------------------------------
28  // A utility class for sending messages to the ModemPager task
29  //------------------------------------------------------------
30 
31  class PeriodicTimerMsg : public GenericTaskMsg {
32  public:
33 
34  enum MsgType {
35  TIMER,
36  ADD_HANDLER,
37  REM_HANDLER
38  };
39 
40  union {
41 
42  struct {
43  bool enable;
44  unsigned intervalInSeconds;
45  } timer;
46 
47  struct {
48  PERIODIC_TIMER_HANDLER(*fn);
49  void* args;
50  } addHandler;
51 
52  struct {
53  PERIODIC_TIMER_HANDLER(*fn);
54  } remHandler;
55 
56  } body;
57 
58  // A type for this message
59 
60  MsgType type;
61  };
62 
63  //------------------------------------------------------------
64  // Main class definition
65  //------------------------------------------------------------
66 
67  class PeriodicTimer :
68  public SpawnableTask<PeriodicTimerMsg> {
69  public:
70 
71  //------------------------------------------------------------
72  // A utility class used to store handlers
73  //------------------------------------------------------------
74 
75  class Handler {
76  public:
77  PERIODIC_TIMER_HANDLER(*fn_);
78  void* args_;
79  };
80 
84  PeriodicTimer();
85 
89  virtual ~PeriodicTimer();
90 
91  // Add a callback function to be called whenever the timer expires
92 
93  void addHandler(PERIODIC_TIMER_HANDLER(*handler), void* args=0);
94 
95  // Remove a callback function from the list to be called
96 
97  void removeHandler(PERIODIC_TIMER_HANDLER(*handler));
98 
99  // Enable/Disable the timer
100 
101  void enableTimer(bool enable, unsigned intervalInSeconds=0);
102 
103  private:
104 
105  TimeOut timeOut_;
106 
107  // A list of handlers to be called when the ephemeris is updated
108 
109  std::vector<Handler> handlers_;
110 
111  //-----------------------------------------------------------------------
112  // Methods called in response to messages received on our message queue
113  //-----------------------------------------------------------------------
114 
115  void executeEnableTimer(bool enable, unsigned intervalInSeconds);
116  void executeAddHandler(PERIODIC_TIMER_HANDLER(*handler), void* args=0);
117  void executeRemoveHandler(PERIODIC_TIMER_HANDLER(*handler));
118 
119  //-----------------------------------------------------------------------
120  // Run methods used by this class
121  //-----------------------------------------------------------------------
122 
123  void serviceMsgQ();
124  void processMsg(PeriodicTimerMsg* msg);
125 
126  // React to a timeout in select
127 
128  void registerTimeOut();
129 
130  void callHandlers();
131 
132  }; // End class PeriodicTimer
133 
134  } // End namespace util
135 } // End namespace sza
136 
137 
138 
139 #endif // End #ifndef SZA_UTIL_PERIODICTIMER_H
Tagged: Fri Nov 14 12:39:33 UTC 2003.
virtual void addHandler(PeriodicTimerMsg *msg)
Respond to a message to add/remove a handler.
virtual void enableTimer(PeriodicTimerMsg *msg)
Respond to a message to enable/disable a timer.
Tagged: Fri Nov 14 12:39:34 UTC 2003.
Tagged: Tue May 2 16:31:46 PDT 2006.
Tagged: Fri Jan 26 16:49:57 NZDT 2007.