My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
PeriodicTimer.h
Go to the documentation of this file.
1 // $Id: PeriodicTimer.h,v 1.1 2010/02/23 17:19:58 eml Exp $
2 
3 #ifndef GCP_UTIL_PERIODICTIMER_H
4 #define GCP_UTIL_PERIODICTIMER_H
5 
15 #include <iostream>
16 
17 #include "gcp/util/common/GenericTask.h"
18 #include "gcp/util/common/GenericTaskMsg.h"
19 #include "gcp/util/common/SpawnableTask.h"
20 #include "gcp/util/common/TimeOut.h"
21 
22 #define PERIODIC_TIMER_HANDLER(fn) void (fn)(void* args)
23 
24 namespace gcp {
25  namespace util {
26 
27  //------------------------------------------------------------
28  // A utility class for sending messages to the ModemPager task
29  //------------------------------------------------------------
30 
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  // A utility class used to store handlers
65  //------------------------------------------------------------
66 
67  class Handler {
68  public:
69  PERIODIC_TIMER_HANDLER(*fn_);
70  void* args_;
71  };
72 
73  //------------------------------------------------------------
74  // Main class definition
75  //------------------------------------------------------------
76 
77  class PeriodicTimer :
78  public SpawnableTask<PeriodicTimerMsg> {
79  public:
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 gcp
136 
137 
138 
139 #endif // End #ifndef GCP_UTIL_PERIODICTIMER_H
Definition: GenericTaskMsg.h:31
PeriodicTimer()
Definition: PeriodicTimer.cc:12
Definition: PeriodicTimer.h:31
Definition: SpawnableTask.h:31
Definition: HorizonsCommunicator.h:80
Definition: TimeOut.h:20
virtual ~PeriodicTimer()
Definition: PeriodicTimer.cc:21
Definition: PeriodicTimer.h:77
void addHandler(PERIODIC_TIMER_HANDLER(*handler), void *args=0)
Definition: PeriodicTimer.cc:97