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
16 TemperatureSensor (const char *location
)
17 : location_(location
),
23 const char *location () const
25 return this->location_
;
31 return ++this->count_
;
34 float temperature () const
36 return this->temperature_
;
40 const char *location_
;
47 // Listing 1 code/ch07
48 class TemperatureQueryHandler
: public ACE_Event_Handler
51 TemperatureQueryHandler ()
52 : ACE_Event_Handler(),
54 averageTemperature_(0.0)
59 int handle_timeout (const ACE_Time_Value
¤t_time
,
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
);
75 ACE_TEXT ("%.2f/%.2f\t")
80 this->averageTemperature_
,
81 sensor
->temperature (),
82 ACE_OS::ctime(&epoch
)));
87 void updateAverageTemperature (TemperatureSensor
*)
93 float averageTemperature_
;
97 // Create a SIGINT handler so that we can exit
98 // the program politely
99 class SigintHandler
: public ACE_Event_Handler
102 int handle_signal (int signum
, siginfo_t
* = 0, ucontext_t
* = 0)
104 if (signum
== SIGINT
)
106 ACE_Reactor::instance ()->end_reactor_event_loop ();
112 int ACE_TMAIN (int, ACE_TCHAR
*[])
114 // Listing 2 code/ch07
115 TemperatureQueryHandler
*temperatureMonitor
=
116 new TemperatureQueryHandler ();
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
,
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
,
142 SigintHandler
*handleExit
= new SigintHandler ();
144 ACE_Reactor::instance ()->register_handler (SIGINT
,
147 ACE_Reactor::instance ()->run_reactor_event_loop ();