Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / examples / Advanced / ch_21 / icp.cpp
blob8a66d24aba3d1f824c629209bfc2fecdc20cbee6
1 //=============================================================================
2 /**
3 * @file icp.cpp
5 * @author Source code used in TAO has been modified and adapted from the codeprovided in the book
6 * @author "Advanced CORBA Programming with C++" by MichiHenning and Steve Vinoski. Copyright 1999. Addison-Wesley
7 * @author Reading
8 * @author MA.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
9 */
10 //=============================================================================
15 #include <string>
16 #include <map>
17 #include <algorithm>
18 #include <stdlib.h>
19 #include <iostream>
20 #include "icp.h"
22 using namespace std;
24 //----------------------------------------------------------------
26 enum DeviceType { thermometer, thermostat };
28 struct DeviceState { // State for a device
29 DeviceType type;
30 const char * model;
31 string location;
32 short nominal_temp; // For thermostats only
34 typedef map<unsigned long, DeviceState> StateMap;
36 //----------------------------------------------------------------
38 const size_t MAXSTR = 32; // Max len of string including NUL
40 const short MIN_TEMP = 40; // 40 F == 4.44 C
41 const short MAX_TEMP = 90; // 90 F == 32.22 C
42 const short DFLT_TEMP = 68; // 68 F == 20.00 C
44 static StateMap dstate; // Map of known devices
46 //----------------------------------------------------------------
48 // ICP_online() simulates adding a new device to the network by
49 // adding it to the dstate map.
51 // For this simple simulation, devices with odd asset numbers
52 // are thermometers and devices with even asset numbers
53 // are thermostats.
55 // Thermostats get an initial nominal temperature of DFLT_TEMP.
56 // The location string is intentionally left blank because it
57 // must be programmed by the controller after putting the device
58 // on-line (as should be the nominal temperature).
60 // If a device with the specified ID is on-line already, the
61 // return value is -1. A zero return value indicates success.
63 extern "C"
64 int
65 ICP_online(unsigned long id)
67 // Look for id in state map.
68 StateMap::iterator pos = dstate.find(id);
69 if (pos != dstate.end())
70 return -1; // Already exists
72 // Fill in state.
73 DeviceState ds;
74 ds.type = (id % 2) ? thermometer : thermostat;
75 ds.model = (ds.type == thermometer)
76 ? "Sens-A-Temp" : "Select-A-Temp";
77 ds.nominal_temp = DFLT_TEMP;
79 // Insert new device into map
80 dstate[id] = ds;
82 return 0;
85 //----------------------------------------------------------------
87 // ICP_offline() simulates removing a device from the network by
88 // removing it from the dstate map. If the device isn't known, the
89 // return value is -1. A zero return value indicates success.
91 extern "C"
92 int
93 ICP_offline(unsigned long id)
95 // Look for id in state map
96 StateMap::iterator pos = dstate.find(id);
97 if (pos == dstate.end())
98 return -1; // No such device
99 dstate.erase(id);
100 return 0;
103 //----------------------------------------------------------------
105 // vary_temp() simulates the variation in actual temperature
106 // around a thermostat.The function randomly varies the
107 // temperature as a percentage of calls as follows:
109 // 3 degrees too cold: 5%
110 // 3 degrees too hot: 5%
111 // 2 degrees too cold: 10%
112 // 2 degrees too hot: 10%
113 // 1 degree too cold: 15%
114 // 1 degree too hot: 15%
115 // exact temperature: 40%
117 static
118 long
119 vary_temp(long temp)
121 long r = ACE_OS::rand() % 50;
122 long delta;
123 if (r < 5)
124 delta = 3;
125 else if (r < 15)
126 delta = 2;
127 else if (r < 30)
128 delta = 1;
129 else
130 delta = 0;
131 if (ACE_OS::rand() % 2)
132 delta = -delta;
133 return temp + delta;
136 //----------------------------------------------------------------
138 // Function object. Locates a thermostat that is in the same room
139 // as the device at position pos.
141 class ThermostatInSameRoom {
142 public:
143 ThermostatInSameRoom(
144 const StateMap::iterator & pos
145 ) : m_pos(pos) {}
146 bool operator()(
147 pair<const unsigned long, DeviceState> & p) const
149 return(
150 p.second.type == thermostat
151 && p.second.location
152 == m_pos->second.location
155 private:
156 const StateMap::iterator & m_pos;
159 //----------------------------------------------------------------
161 // actual_temp() is a helper function to determine the actual
162 // temperature returned by a particular thermometer or thermostat.
163 // The pos argument indicates the device.
165 // The function locates all thermostats that are in the same room
166 // as the device denoted by pos and computes the average of all
167 // the thermostats' nominal temperatures. (If no thermostats are
168 // in the same room as the device, the function assumes that the
169 // average of the nominal temperatures is DFLT_TEMP.)
171 // The returned temperature varies from the average as
172 // determined by vary_temp().
174 static
175 long
176 actual_temp(const StateMap::iterator & pos)
178 long sum = 0;
179 long count = 0;
180 StateMap::iterator where = std::find_if(
181 dstate.begin(), dstate.end(),
182 ThermostatInSameRoom(pos)
184 while (where != dstate.end()) {
185 count++;
186 sum += where->second.nominal_temp;
187 where = std::find_if(
188 ++where, dstate.end(),
189 ThermostatInSameRoom(pos)
192 return vary_temp(count == 0 ? DFLT_TEMP : sum / count);
195 //---------------------------------------------------------------
197 // ICP_get() returns an attribute value of the device with the
198 // given id. The attribute is named by the attr parameter. The
199 // value is copied into the buffer pointed to by the value
200 // pointer. The len parameter is the size of the passed buffer,
201 // so ICP_get can avoid overrunning the buffer.
203 // By default, thermometers report a temperature that varies
204 // somewhat around DFLT_TEMP. However, if there is another
205 // thermostat in the same room as the the thermometer, the
206 // thermometer reports a temperature that varies around that
207 // thermostat's temperature. For several thermostats that are in
208 // the same room, the thermometer reports around the average
209 // nominal temperature of all the thermostats.
211 // Attempts to read from a non-existent device or to read a
212 // non-existent attribute return -1. A return value of zero
213 // indicates success. If the supplied buffer is too short to hold
214 // a value, ICP_get() silently truncates the value and
215 // returns success.
217 extern "C"
219 ICP_get(
220 unsigned long id,
221 const char * attr,
222 void * value,
223 size_t len)
225 // Look for id in state map
226 StateMap::iterator pos = dstate.find(id);
227 if (pos == dstate.end())
228 return -1; // No such device
230 // Depending on the attribute, return the
231 // corresponding piece of state.
232 if (ACE_OS::strcmp(attr, "model") == 0) {
233 ACE_OS::strncpy((char *)value, pos->second.model, len);
234 } else if (ACE_OS::strcmp(attr, "location") == 0) {
235 ACE_OS::strncpy((char *)value, pos->second.location.c_str(), len);
236 } else if (ACE_OS::strcmp(attr, "nominal_temp") == 0) {
237 if (pos->second.type != thermostat)
238 return -1; // Must be thermostat
239 ACE_OS::memcpy(
240 value, &pos->second.nominal_temp,
241 ace_min(len, sizeof(pos->second.nominal_temp))
243 } else if (ACE_OS::strcmp(attr, "temperature") == 0) {
244 long temp = actual_temp(pos);
245 ACE_OS::memcpy(value, &temp, ace_min(len, sizeof(temp)));
246 } else if (ACE_OS::strcmp(attr, "MIN_TEMP") == 0) {
247 ACE_OS::memcpy(value, &MIN_TEMP, ace_min(len, sizeof(MIN_TEMP)));
248 } else if (ACE_OS::strcmp(attr, "MAX_TEMP") == 0) {
249 ACE_OS::memcpy(value, &MAX_TEMP, ace_min(len, sizeof(MAX_TEMP)));
250 } else {
251 return -1; // No such attribute
253 return 0; // OK
256 //----------------------------------------------------------------
258 // ICP_set() sets the the attributed specified by attr to the
259 // value specified by value for the device with ID id. Attempts to
260 // write a string longer than MAXSTR bytes (including the
261 // terminating NUL) result in silent truncation of the string.
262 // Attempts to access a non-existent device or attribute
263 // return -1. Attempts to set a nominal temperature outside the
264 // legal range also return -1. A zero return value
265 // indicates success.
267 extern "C"
269 ICP_set(unsigned long id, const char * attr, const void * value)
271 // Look for id in state map
272 StateMap::iterator pos = dstate.find(id);
273 if (pos == dstate.end())
274 return -1; // No such device
276 // Change either location or nominal temp, depending on attr.
277 if (ACE_OS::strcmp(attr, "location") == 0) {
278 pos->second.location.assign(
279 (const char *)value, MAXSTR - 1
281 } else if (ACE_OS::strcmp(attr, "nominal_temp") == 0) {
282 if (pos->second.type != thermostat)
283 return -1; // Must be thermostat
284 short temp;
285 ACE_OS::memcpy(&temp, value, sizeof(temp));
286 if (temp < MIN_TEMP || temp > MAX_TEMP)
287 return -1;
288 pos->second.nominal_temp = temp;
289 } else {
290 return -1; // No such attribute
292 return 0; // OK
295 //----------------------------------------------------------------
297 #include <fstream>
299 class ICP_Persist {
300 public:
301 ICP_Persist(const char * file);
302 ~ICP_Persist();
303 private:
304 string m_filename;
307 // Read device state from a file and initialize the dstate map.
309 ICP_Persist::
310 ICP_Persist(const char * file) : m_filename(file)
312 // Open input file, creating it if necessary.
313 std::ifstream db(m_filename.c_str(), std::ios::in|std::ios::out);//, 0666);
314 if (!db) {
315 std::cerr << "Error opening " << m_filename << std::endl;
316 ACE_OS::exit(1);
319 // Read device details, one attribute per line.
320 DeviceState ds;
321 unsigned long id;
322 while (db >> id) {
323 // Read device type and set model string accordingly.
324 int dtype;
325 db >> dtype;
326 ds.type = dtype == thermometer
327 ? thermometer : thermostat;
328 ds.model = dtype == thermometer
329 ? "Sens-A-Temp" : "Select-A-Temp";
330 char loc[MAXSTR];
331 db.get(loc[0]); // Skip newline
332 db.getline(loc, sizeof(loc)); // Read location
333 ds.location = loc;
334 if (ds.type == thermostat)
335 db >> ds.nominal_temp; // Read temperature
336 dstate[id] = ds; // Add entry to map
339 // db.close();
340 // if (!db) {
341 // cerr << "Error closing " << m_filename << endl;
342 // ACE_OS::exit(1);
343 // }
346 // Write device state to the file.
348 ICP_Persist::
349 ~ICP_Persist()
351 // Open input file, truncating it.
352 std::ofstream db(m_filename.c_str());
353 if (!db) {
354 std::cerr << "Error opening " << m_filename << std::endl;
355 ACE_OS::exit(1);
358 // Write the state details for each device.
359 StateMap::iterator i;
360 for (i = dstate.begin(); i != dstate.end(); i++) {
361 db << i->first << std::endl;
362 db << (unsigned long)(i->second.type) << std::endl;
363 db << i->second.location << std::endl;
364 if (i->second.type == thermostat)
365 db << i->second.nominal_temp << std::endl;
367 if (!db) {
368 std::cerr << "Error writing " << m_filename << std::endl;
369 ACE_OS::exit(1);
372 // db.close();
373 // if (!db) {
374 // cerr << "Error closing " << m_filename << endl;
375 // exit(1);
376 // }
379 // Instantiate a single global instance of the class.
380 static ICP_Persist mydb("/tmp/CCS_DB");