Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Naming / Temperature_Monitor2.cpp
blob5dc3df0dd1a6071e3e2ac2cdd71747a6cf786c18
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_Monitor2.h"
7 #include "EMail.h"
9 // Listing 1 code/ch21
10 void Temperature_Monitor2::record_temperature (float temp)
12 Name_Binding_Ptr current
13 (this->naming_context_.fetch ("current"));
14 if (current.get ())
16 this->naming_context_.rebind ("previous",
17 current->value ());
20 this->record_history (temp);
22 this->naming_context_.unbind ("lastFailure");
23 this->naming_context_.unbind ("lastReset");
24 this->naming_context_.unbind ("resetCount");
26 // Listing 1
28 // Listing 2 code/ch21
29 void Temperature_Monitor2::record_history (float temp)
31 int now = (int)ACE_OS::time ();
32 this->shared_context_.rebind ("lastUpdate", now);
34 Name_Binding_Ptr counter
35 (this->shared_context_.fetch ("counter"));
36 int counterValue = counter.get () ? counter->int_value () : 0;
38 char name[BUFSIZ];
39 ACE_OS::sprintf (name, "history[%d]", counterValue);
41 char value[BUFSIZ];
42 ACE_OS::sprintf (value, "%d|%.2f", now, temp);
44 this->shared_context_.rebind (name, value);
46 ++counterValue;
47 counterValue %= this->opt_.history_size ();
48 this->shared_context_.rebind ("counter", counterValue);
50 // Listing 2
52 void Temperature_Monitor2::reset_device (Name_Binding_Ptr &resetCount)
54 int number_of_resets = 1;
56 if (resetCount.get ())
58 number_of_resets = resetCount->int_value () + 1;
60 if (number_of_resets > this->opt_.excessive_resets ())
62 EMail notification;
64 char message[BUFSIZ];
65 ACE_OS::sprintf (message,
66 "Thermometer: %s\n"
67 "Reset Count: %d\n",
68 this->thermometer_->address (),
69 number_of_resets);
71 notification.send (this->opt_.admin_email (),
72 this->opt_.email_from (),
73 "Excessive number of thermometer resets",
74 message);
78 this->thermometer_->reset ();
80 this->naming_context_.rebind ("lastReset", (int)ACE_OS::time ());
81 this->naming_context_.rebind ("resetCount", number_of_resets);
84 void Temperature_Monitor2::record_failure ()
86 Name_Binding_Ptr lastFailure (this->naming_context_.fetch ("lastFailure"));
87 Name_Binding_Ptr lastReset (this->naming_context_.fetch ("lastReset"));
88 Name_Binding_Ptr resetCount (this->naming_context_.fetch ("resetCount"));
90 int now = (int) ACE_OS::time ();
92 int lastFailureTime;
93 int lastResetTime = 0;
95 if (lastFailure.get ())
97 lastFailureTime = lastFailure->int_value ();
99 else
101 this->naming_context_.rebind ("firstFailure", now);
102 this->naming_context_.rebind ("lastReset", now);
103 lastFailureTime = now;
104 lastResetTime = now;
107 if (lastReset.get ())
109 lastResetTime = lastReset->int_value ();
112 if (now - lastResetTime > this->opt_.reset_interval ())
114 this->reset_device (resetCount);
117 this->naming_context_.rebind ("lastFailure", lastFailureTime);
120 void Temperature_Monitor2::monitor ()
122 this->thermometer_ = new Thermometer (this->opt_.thermometer_address ());
124 for (;;)
126 float temp = this->thermometer_->temperature ();
128 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Read temperature %.2f\n"), temp));
130 if (temp >= 0)
132 this->record_temperature (temp);
134 else
136 this->record_failure ();
139 ACE_OS::sleep (this->opt_.poll_interval ());
142 ACE_NOTREACHED (delete this->thermometer_;)