Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Advanced / ch_21 / client.cpp
blobb57c2a900b2554447e387bfb8507002a2f4aca9a
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * @author Source code used in TAO has been modified and adapted from the code provided in the book
7 * @author "Advanced CORBA Programming with C++" by MichiHenning and Steve Vinoski. Copyright 1999. Addison-Wesley
8 * @author Reading
9 * @author MA.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
11 //=============================================================================
13 #include "CCSC.h" // ORB-specific
14 #include <ace/streams.h>
16 using namespace std;
18 //----------------------------------------------------------------
20 // Generic ostream inserter for exceptions. Inserts the exception
21 // name, if available, and the repository ID otherwise.
23 // This inserter may or may not be needed for your ORB.
24 #if !defined (GEN_OSTREAM_OPS)
26 #if 0
28 static ostream &
29 operator<<(ostream & os, const CORBA::Exception & e)
31 CORBA::Any tmp;
32 tmp <<= e;
34 CORBA::TypeCode_var tc = tmp.type();
35 const char * p = tc->name();
36 if (*p != '\0')
37 os << p;
38 else
39 os << tc->id();
40 return os;
43 #endif
45 //----------------------------------------------------------------
47 // Show the details for a thermometer or thermostat.
49 static std::ostream &
50 operator<<(std::ostream &os, CCS::Thermometer_ptr t)
52 // Check for nil.
53 if (CORBA::is_nil(t)) {
54 os << "Cannot show state for nil reference." << std::endl;
55 return os;
58 // Try to narrow and print what kind of device it is.
59 CCS::Thermostat_var tmstat = CCS::Thermostat::_narrow(t);
60 os << (CORBA::is_nil(tmstat.in()) ? "Thermometer:" : "Thermostat:")
61 << std::endl;
63 // Show attribute values.
64 CCS::ModelType_var model = t->model();
65 CCS::LocType_var location = t->location();
66 os << "\tAsset number: " << t->asset_num() << std::endl;
67 os << "\tModel : " << model.in() << std::endl;
68 os << "\tLocation : " << location.in() << std::endl;
69 os << "\tTemperature : " << t->temperature() << std::endl;
71 // If device is a thermostat, show nominal temperature.
72 if (!CORBA::is_nil(tmstat.in()))
73 os << "\tNominal temp: " << tmstat->get_nominal() << std::endl;
74 return os;
77 //----------------------------------------------------------------
79 // Show the information in a BtData struct.
81 static std::ostream &
82 operator<<(std::ostream &os, const CCS::Thermostat::BtData &btd)
84 os << "CCS::Thermostat::BtData details:" << std::endl;
85 os << "\trequested : " << btd.requested << std::endl;
86 os << "\tmin_permitted: " << btd.min_permitted << std::endl;
87 os << "\tmax_permitted: " << btd.max_permitted << std::endl;
88 os << "\terror_msg : " << btd.error_msg << std::endl;
89 return os;
92 //----------------------------------------------------------------
94 // Loop over the sequence of records in an EChange exception and
95 // show the details of each record.
97 static std::ostream &
98 operator<<(std::ostream &os, const CCS::Controller::EChange &ec)
100 for (CORBA::ULong i = 0; i < ec.errors.length(); i++) {
101 os << "Change failed:" << std::endl;
102 os << ec.errors[i].tmstat_ref.in(); // Overloaded <<
103 os << ec.errors[i].info << std::endl; // Overloaded <<
105 return os;
108 #endif
110 //----------------------------------------------------------------
112 // Change the temperature of a thermostat.
114 static void
115 set_temp(CCS::Thermostat_ptr tmstat, CCS::TempType new_temp)
117 if (CORBA::is_nil(tmstat)) // Don't call via nil reference
118 return;
120 CCS::AssetType anum = tmstat->asset_num();
121 try {
122 std::cout << "Setting thermostat " << anum
123 << " to " << new_temp << " degrees." << std::endl;
124 CCS::TempType old_nominal = tmstat->set_nominal(new_temp);
125 std::cout << "Old nominal temperature was: "
126 << old_nominal << std::endl;
127 std::cout << "New nominal temperature is: "
128 << tmstat->get_nominal() << std::endl;
129 } catch (const CCS::Thermostat::BadTemp &bt) {
130 std::cerr << "Setting of nominal temperature failed." << std::endl;
131 std::cerr << bt.details << std::endl; // Overloaded <<
135 //----------------------------------------------------------------
138 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
140 CORBA::ULong i = 0;
141 try {
142 // Initialize the ORB
143 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
145 // Check arguments
146 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())) {
155 std::cerr << "Nil controller reference" << std::endl;
156 throw 0;
159 // Try to narrow to CCS::Controller.
160 CCS::Controller_var ctrl;
161 try {
162 ctrl = CCS::Controller::_narrow(obj.in());
163 } catch (const CORBA::SystemException &se) {
164 std::cerr << "Cannot narrow controller reference: "
165 << se
166 << std::endl;
167 throw 0;
169 if (CORBA::is_nil(ctrl.in())) {
170 std::cerr << "Wrong type for controller ref." << std::endl;
171 throw 0;
174 // Get list of devices
175 CCS::Controller::ThermometerSeq_var list = ctrl->list();
177 // Show number of devices.
178 CORBA::ULong len = list->length();
179 std::cout << "Controller has " << len << " device";
180 if (len != 1)
181 std::cout << "s";
182 std::cout << "." << std::endl;
184 CCS::Thermometer_var t = ctrl->create_thermometer(27, "Room 1");
185 CCS::Thermostat_var ts = ctrl->create_thermostat(28, "Room 2", 48);
186 CCS::Thermostat_var ts2 = ctrl->create_thermostat(30, "Room 3", 48);
187 CCS::Thermostat_var ts3 = ctrl->create_thermostat(32, "Room 3", 68);
188 CCS::Thermostat_var ts4 = ctrl->create_thermostat(34, "Room 3", 68);
189 CCS::Thermostat_var ts5 = ctrl->create_thermostat(36, "Room 3", 48);
190 std::cout << t->location() << std::endl;
191 std::cout << ts->location() << std::endl;
192 std::cout << ts2->location() << std::endl;
193 t->remove();
195 list = ctrl->list();
196 // Show details for each device.
197 for ( i = 0; i < list->length(); i++)
199 CCS::Thermometer_ptr ti = list[i];
200 std::cout << ti;
202 std::cout << std::endl;
204 // Change the location of first device in the list
205 CCS::AssetType anum = list[0U]->asset_num();
206 std::cout << "Changing location of device "
207 << anum << "." << std::endl;
208 list[0U]->location("Earth");
209 // Check that the location was updated
210 std::cout << "New details for device "
211 << anum << " are:" << std::endl;
212 CCS::Thermometer_ptr tx = list[0u];
213 std::cout << tx << std::endl;
215 // Find first thermostat in list.
216 CCS::Thermostat_var tmstat;
217 for ( i = 0;
218 i < list->length() && CORBA::is_nil(tmstat.in());
219 i++) {
220 tmstat = CCS::Thermostat::_narrow(list[i]);
223 // Check that we found a thermostat on the list.
224 if (CORBA::is_nil(tmstat.in())) {
225 std::cout << "No thermostat devices in list." << std::endl;
226 } else {
227 // Set temperature of thermostat to
228 // 50 degrees (should work).
229 set_temp(tmstat.inout(), 50);
230 std::cout << std::endl;
232 // Set temperature of thermostat to
233 // -10 degrees (should fail).
234 set_temp(tmstat.inout(), -10);
237 // Look for device in Rooms Earth and HAL. This must
238 // locate at least one device because we earlier changed
239 // the location of the first device to Room Earth.
240 std::cout << "Looking for devices in Earth and HAL." << std::endl;
241 CCS::Controller::SearchSeq ss;
242 ss.length(2);
243 ss[0].key.loc(CORBA::string_dup("Earth"));
244 ss[1].key.loc(CORBA::string_dup("HAL"));
245 ctrl->find(ss);
247 // Show the devices found in that room.
248 for ( i = 0; i < ss.length(); i++)
249 std::cout << ss[i].device.in(); // Overloaded <<
250 std::cout << std::endl;
252 // Increase the temperature of all thermostats
253 // by 40 degrees. First, make a new list (tss)
254 // containing only thermostats.
255 std::cout << "Increasing thermostats by 40 degrees." << std::endl;
256 CCS::Controller::ThermostatSeq tss;
257 for ( i = 0; i < list->length(); i++) {
258 tmstat = CCS::Thermostat::_narrow(list[i]);
259 if (CORBA::is_nil(tmstat.in()))
260 continue; // Skip thermometers
261 len = tss.length();
262 tss.length(len + 1);
263 tss[len] = tmstat;
266 // Try to change all thermostats.
267 try {
268 ctrl->change(tss, 40);
269 } catch (const CCS::Controller::EChange &ec) {
270 std::cerr << ec; // Overloaded <<
272 } catch (const CORBA::Exception & e) {
273 std::cerr << "Uncaught CORBA exception: "
274 << e
275 << std::endl;
276 return 1;
277 } catch (...) {
278 return 1;
280 return 0;