1 #include "ace/OS_NS_time.h"
2 #include "ace/OS_NS_unistd.h"
3 #include "ace/Log_Msg.h"
5 #include "Thermometer.h"
6 #include "Temperature_Monitor.h"
10 Temperature_Monitor::Temperature_Monitor
11 (Temperature_Monitor_Options
&opt
,
12 Naming_Context
&naming_context
)
13 : opt_(opt
), naming_context_(naming_context
)
17 // Listing 31 code/ch21
18 void Temperature_Monitor::record_temperature (float temp
)
20 Name_Binding_Ptr current
21 (this->naming_context_
.fetch ("current"));
24 this->naming_context_
.rebind ("previous",
29 // Listing 32 code/ch21
30 this->naming_context_
.rebind ("current", temp
);
33 // Listing 33 code/ch21
34 this->naming_context_
.unbind ("lastReset");
35 this->naming_context_
.unbind ("resetCount");
39 // Listing 41 code/ch21
40 void Temperature_Monitor::record_failure ()
42 Name_Binding_Ptr lastReset
43 (this->naming_context_
.fetch ("lastReset"));
44 Name_Binding_Ptr resetCount
45 (this->naming_context_
.fetch ("resetCount"));
48 // Listing 42 code/ch21
49 int now
= (int) ACE_OS::time ();
53 lastResetTime
= lastReset
->int_value ();
57 this->naming_context_
.rebind ("lastReset", now
);
62 // Listing 43 code/ch21
63 if (now
- lastResetTime
> this->opt_
.reset_interval ())
65 this->reset_device (resetCount
);
70 // Listing 5 code/ch21
72 Temperature_Monitor::reset_device (Name_Binding_Ptr
&resetCount
)
74 int number_of_resets
= 1;
75 if (resetCount
.get ())
77 number_of_resets
= resetCount
->int_value () + 1;
78 if (number_of_resets
> this->opt_
.excessive_resets ())
84 ACE_OS::sprintf (message
,
87 this->thermometer_
->address(),
90 notification
.send (this->opt_
.admin_email (),
91 this->opt_
.email_from (),
92 "Excessive number of thermometer resets",
97 this->thermometer_
->reset ();
98 this->naming_context_
.rebind ("lastReset",
99 (int) ACE_OS::time ());
100 this->naming_context_
.rebind ("resetCount",
105 // Listing 2 code/ch21
106 void Temperature_Monitor::monitor ()
109 new Thermometer (this->opt_
.thermometer_address ());
113 float temp
= this->thermometer_
->temperature ();
114 ACE_DEBUG ((LM_INFO
, ACE_TEXT ("Read temperature %.2f\n"),
119 this->record_temperature (temp
);
123 this->record_failure ();
126 ACE_OS::sleep (this->opt_
.poll_interval ());
129 ACE_NOTREACHED (delete this->thermometer_
;)