Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Naming / Temperature_Monitor.cpp
blob0dc015cd2df7091bb1c012099c5e174702b41a7a
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"
7 #include "EMail.h"
9 // Listing 1 code/ch21
10 Temperature_Monitor::Temperature_Monitor
11 (Temperature_Monitor_Options &opt,
12 Naming_Context &naming_context)
13 : opt_(opt), naming_context_(naming_context)
14 { }
15 // Listing 1
17 // Listing 31 code/ch21
18 void Temperature_Monitor::record_temperature (float temp)
20 Name_Binding_Ptr current
21 (this->naming_context_.fetch ("current"));
22 if (current.get())
24 this->naming_context_.rebind ("previous",
25 current->value ());
27 // Listing 31
29 // Listing 32 code/ch21
30 this->naming_context_.rebind ("current", temp);
31 // Listing 32
33 // Listing 33 code/ch21
34 this->naming_context_.unbind ("lastReset");
35 this->naming_context_.unbind ("resetCount");
36 // Listing 33
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"));
46 // Listing 41
48 // Listing 42 code/ch21
49 int now = (int) ACE_OS::time ();
50 int lastResetTime;
51 if (lastReset.get ())
53 lastResetTime = lastReset->int_value ();
55 else
57 this->naming_context_.rebind ("lastReset", now);
58 lastResetTime = now;
60 // Listing 42
62 // Listing 43 code/ch21
63 if (now - lastResetTime > this->opt_.reset_interval ())
65 this->reset_device (resetCount);
67 // Listing 43
70 // Listing 5 code/ch21
71 void
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 ())
80 // Exclude 5
81 EMail notification;
83 char message[BUFSIZ];
84 ACE_OS::sprintf (message,
85 "Thermometer: %s\n"
86 "Reset Count: %d\n",
87 this->thermometer_->address(),
88 number_of_resets);
90 notification.send (this->opt_.admin_email (),
91 this->opt_.email_from (),
92 "Excessive number of thermometer resets",
93 message);
94 // Exclude 5
97 this->thermometer_->reset ();
98 this->naming_context_.rebind ("lastReset",
99 (int) ACE_OS::time ());
100 this->naming_context_.rebind ("resetCount",
101 number_of_resets);
103 // Listing 5
105 // Listing 2 code/ch21
106 void Temperature_Monitor::monitor ()
108 this->thermometer_ =
109 new Thermometer (this->opt_.thermometer_address ());
111 for(;;)
113 float temp = this->thermometer_->temperature ();
114 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Read temperature %.2f\n"),
115 temp));
117 if (temp >= 0)
119 this->record_temperature (temp);
121 else
123 this->record_failure ();
126 ACE_OS::sleep (this->opt_.poll_interval ());
129 ACE_NOTREACHED (delete this->thermometer_;)
131 // Listing 2