Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Reactor / Timer_State_Data.cpp
blob8cd424fdf2fbc6bae3ff0bcc2ba054f0216c7a9d
1 /**
2 * Reactor examples
4 * Timers & state data
5 */
7 #include "ace/OS_NS_time.h"
8 #include "ace/Log_Msg.h"
9 #include "ace/Reactor.h"
10 #include "ace/Event_Handler.h"
12 // Listing 0 code/ch07
13 class TemperatureSensor
15 public:
16 TemperatureSensor (const char *location)
17 : location_(location),
18 count_(0),
19 temperature_(0.0)
20 // ...
21 { }
23 const char *location () const
25 return this->location_;
28 int querySensor ()
30 // ...
31 return ++this->count_;
34 float temperature () const
36 return this->temperature_;
39 private:
40 const char *location_;
41 int count_;
42 float temperature_;
43 // ...
45 // Listing 0
47 // Listing 1 code/ch07
48 class TemperatureQueryHandler : public ACE_Event_Handler
50 public:
51 TemperatureQueryHandler ()
52 : ACE_Event_Handler(),
53 counter_(0),
54 averageTemperature_(0.0)
55 // ...
59 int handle_timeout (const ACE_Time_Value &current_time,
60 const void *arg)
62 time_t epoch = ((timespec_t)current_time).tv_sec;
64 const TemperatureSensor *const_sensor =
65 reinterpret_cast<const TemperatureSensor *> (arg);
66 TemperatureSensor *sensor =
67 const_cast<TemperatureSensor *> (const_sensor);
69 int queryCount = sensor->querySensor ();
70 this->updateAverageTemperature (sensor);
72 ACE_DEBUG ((LM_INFO,
73 ACE_TEXT ("%s\t")
74 ACE_TEXT ("%d/%d\t")
75 ACE_TEXT ("%.2f/%.2f\t")
76 ACE_TEXT ("%s\n"),
77 sensor->location (),
78 ++this->counter_,
79 queryCount,
80 this->averageTemperature_,
81 sensor->temperature (),
82 ACE_OS::ctime(&epoch)));
83 return 0;
86 private:
87 void updateAverageTemperature (TemperatureSensor *)
89 // ...
92 int counter_;
93 float averageTemperature_;
95 // Listing 1
97 // Create a SIGINT handler so that we can exit
98 // the program politely
99 class SigintHandler : public ACE_Event_Handler
101 public:
102 int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0)
104 if (signum == SIGINT)
106 ACE_Reactor::instance ()->end_reactor_event_loop ();
108 return 0;
112 int ACE_TMAIN (int, ACE_TCHAR *[])
114 // Listing 2 code/ch07
115 TemperatureQueryHandler *temperatureMonitor =
116 new TemperatureQueryHandler ();
117 // Listing 2
119 ACE_Time_Value initialDelay (3);
120 ACE_Time_Value intervalOne (5);
121 // Listing 3 code/ch07
122 TemperatureSensor *sensorOne =
123 new TemperatureSensor ("Kitchen");
125 ACE_Reactor::instance ()->schedule_timer (temperatureMonitor,
126 sensorOne,
127 initialDelay,
128 intervalOne);
129 // Listing 3
131 ACE_Time_Value intervalTwo (4);
132 // Listing 4 code/ch07
133 TemperatureSensor *sensorTwo =
134 new TemperatureSensor ("Foyer");
136 ACE_Reactor::instance ()->schedule_timer (temperatureMonitor,
137 sensorTwo,
138 initialDelay,
139 intervalTwo);
140 // Listing 4
142 SigintHandler *handleExit = new SigintHandler ();
144 ACE_Reactor::instance ()->register_handler (SIGINT,
145 handleExit);
147 ACE_Reactor::instance ()->run_reactor_event_loop ();
149 return 0;