Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / examples / Advanced / ch_8_and_10 / client.cpp
blobb051a9506d3b172c795d9c24f522ee8046872a90
1 //=============================================================================
2 /**
3 * @file client.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 //=============================================================================
13 #include "CCSC.h" // ORB-specific
14 #include <iostream>
16 using namespace std;
18 // This inserter may or may not be needed for your ORB.
19 #if !defined (GEN_OSTREAM_OPS)
21 // Show the details for a thermometer or thermostat.
22 static std::ostream &
23 operator<<(std::ostream & os, CCS::Thermometer_ptr t)
25 // Check for nil.
26 if (CORBA::is_nil(t)) {
27 os << "Cannot show state for nil reference." << endl;
28 return os;
31 // Try to narrow and print what kind of device it is.
32 CCS::Thermostat_var tmstat = CCS::Thermostat::_narrow(t);
33 os << (CORBA::is_nil(tmstat.in()) ? "Thermometer:" : "Thermostat:")
34 << endl;
36 // Show attribute values.
37 CCS::ModelType_var model = t->model();
38 CCS::LocType_var location = t->location();
39 os << "\tAsset number: " << t->asset_num() << endl;
40 os << "\tModel : " << model.in() << endl;
42 os << "\tLocation : " << location.in() << endl;
43 os << "\tTemperature : " << t->temperature() << endl;
45 // If device is a thermostat, show nominal temperature.
46 if (!CORBA::is_nil(tmstat.in()))
47 os << "\tNominal temp: " << tmstat->get_nominal() << endl;
48 return os;
51 //----------------------------------------------------------------
53 // Show the information in a BtData struct.
55 static std::ostream &
56 operator<<(std::ostream & os, const CCS::Thermostat::BtData & btd)
58 os << "CCS::Thermostat::BtData details:" << endl;
59 os << "\trequested : " << btd.requested << endl;
60 os << "\tmin_permitted: " << btd.min_permitted << endl;
61 os << "\tmax_permitted: " << btd.max_permitted << endl;
62 os << "\terror_msg : " << btd.error_msg << endl;
63 return os;
66 //----------------------------------------------------------------
68 // Loop over the sequence of records in an EChange exception and
69 // show the details of each record.
71 static std::ostream &
72 operator<<(std::ostream & os, const CCS::Controller::EChange & ec)
74 for (CORBA::ULong i = 0; i < ec.errors.length(); i++) {
75 os << "Change failed:" << endl;
76 os << ec.errors[i].tmstat_ref.in(); // Overloaded <<
77 os << ec.errors[i].info << endl; // Overloaded <<
79 return os;
82 //----------------------------------------------------------------
84 // Generic ostream inserter for exceptions. Inserts the exception
85 // name, if available, and the repository ID otherwise.
86 #if 0
88 static std::ostream &
89 operator<<(std::ostream & os, const CORBA::Exception & e)
91 CORBA::Any tmp;
92 tmp <<= e;
94 CORBA::TypeCode_var tc = tmp.type();
95 const char * p = tc->name();
96 if (*p != '\0')
97 os << p;
98 else
99 os << tc->id();
100 return os;
102 #endif
104 #endif
106 //----------------------------------------------------------------
108 // Change the temperature of a thermostat.
110 static void
111 set_temp(CCS::Thermostat_ptr tmstat, CCS::TempType new_temp)
113 if (CORBA::is_nil(tmstat)) // Don't call via nil reference
114 return;
116 CCS::AssetType anum = tmstat->asset_num();
119 std::cout << "Setting thermostat " << anum
120 << " to " << new_temp << " degrees." << std::endl;
121 CCS::TempType old_nominal = tmstat->set_nominal(new_temp);
122 std::cout << "Old nominal temperature was: "
123 << old_nominal << std::endl;
124 std::cout << "New nominal temperature is: "
125 << tmstat->get_nominal() << std::endl;
127 catch (const CCS::Thermostat::BadTemp & bt)
129 std::cerr << "Setting of nominal temperature failed." << std::endl;
130 std::cerr << bt.details << std::endl; // Overloaded <<
134 //----------------------------------------------------------------
137 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
141 // Initialize the ORB
142 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
144 // Check arguments
145 if (argc != 2)
147 std::cerr << "Usage: client IOR_string" << std::endl;
148 throw 0;
151 // Get controller reference from argv
152 // and convert to object.
153 CORBA::Object_var obj = orb->string_to_object(argv[1]);
154 if (CORBA::is_nil(obj.in()))
156 std::cerr << "Nil controller reference" << std::endl;
157 throw 0;
160 // Try to narrow to CCS::Controller.
161 CCS::Controller_var ctrl;
164 ctrl = CCS::Controller::_narrow(obj.in());
166 catch (const CORBA::SystemException & se)
168 std::cerr << "Cannot narrow controller reference: "
169 << se
170 << std::endl;
171 throw 0;
173 if (CORBA::is_nil(ctrl.in()))
175 std::cerr << "Wrong type for controller ref." << std::endl;
176 throw 0;
179 // Get list of devices
180 CCS::Controller::ThermometerSeq_var list = ctrl->list();
182 // Show number of devices.
183 CORBA::ULong len = list->length();
184 std::cout << "Controller has " << len << " device";
185 if (len != 1)
186 std::cout << "s";
187 std::cout << "." << std::endl;
189 // If there are no devices at all, we are finished.
190 if (len == 0)
191 return 0;
193 // Show details for each device.
194 for (CORBA::ULong i = 0; i < list->length(); i++)
195 std::cout << list[i].in();
196 std::cout << std::endl;
198 // Change the location of first device in the list
199 CCS::AssetType anum = list[(CORBA::ULong) 0]->asset_num();
200 std::cout << "Changing location of device "
201 << anum << "." << std::endl;
202 list[(CORBA::ULong) 0]->location("Earth");
203 // Check that the location was updated
204 std::cout << "New details for device "
205 << anum << " are:" << std::endl;
206 CCS::Thermometer_ptr tx = list[0u];
207 std::cout << tx << std::endl;
209 // Find first thermostat in list.
210 CCS::Thermostat_var tmstat;
211 for ( CORBA::ULong j = 0;
212 j < list->length() && CORBA::is_nil(tmstat.in());
213 j++)
215 tmstat = CCS::Thermostat::_narrow(list[j]);
218 // Check that we found a thermostat on the list.
219 if (CORBA::is_nil(tmstat.in()))
221 std::cout << "No thermostat devices in list." << std::endl;
223 else
225 // Set temperature of thermostat to
226 // 50 degrees (should work).
227 set_temp(tmstat.inout(), 50);
228 std::cout << std::endl;
230 // Set temperature of thermostat to
231 // -10 degrees (should fail).
232 set_temp(tmstat.inout(), -10);
235 // Look for device in Rooms Earth and HAL. This must
236 // locate at least one device because we earlier changed
237 // the location of the first device to Room Earth.
238 std::cout << "Looking for devices in Earth and HAL." << std::endl;
239 CCS::Controller::SearchSeq ss;
240 ss.length(2);
241 ss[0].key.loc(CORBA::string_dup("Earth"));
242 ss[1].key.loc(CORBA::string_dup("HAL"));
243 ctrl->find(ss);
245 // Show the devices found in that room.
246 for (CORBA::ULong k = 0; k < ss.length(); k++)
247 std::cout << ss[k].device.in(); // Overloaded <<
248 std::cout << std::endl;
250 // Increase the temperature of all thermostats
251 // by 40 degrees. First, make a new list (tss)
252 // containing only thermostats.
253 std::cout << "Increasing thermostats by 40 degrees." << std::endl;
254 CCS::Controller::ThermostatSeq tss;
255 for (CORBA::ULong l = 0; l < list->length(); l++)
257 tmstat = CCS::Thermostat::_narrow(list[l]);
258 if (CORBA::is_nil(tmstat.in()))
259 continue; // Skip thermometers
260 len = tss.length();
261 tss.length(len + 1);
262 tss[len] = tmstat;
265 // Try to change all thermostats.
268 ctrl->change(tss, 40);
270 catch (const CCS::Controller::EChange & ec)
272 std::cerr << ec; // Overloaded <<
275 catch (const CORBA::Exception & e)
277 std::cerr << "Uncaught CORBA exception: "
278 << e
279 << std::endl;
280 return 1;
282 catch (...)
284 return 1;
286 return 0;