1 //=============================================================================
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
8 * @author MA.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
10 //=============================================================================
13 #include "CCSC.h" // ORB-specific
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.
23 operator<<(std::ostream
& os
, CCS::Thermometer_ptr t
)
26 if (CORBA::is_nil(t
)) {
27 os
<< "Cannot show state for nil reference." << endl
;
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:")
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
;
51 //----------------------------------------------------------------
53 // Show the information in a BtData struct.
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
;
66 //----------------------------------------------------------------
68 // Loop over the sequence of records in an EChange exception and
69 // show the details of each record.
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 <<
82 //----------------------------------------------------------------
84 // Generic ostream inserter for exceptions. Inserts the exception
85 // name, if available, and the repository ID otherwise.
89 operator<<(std::ostream
& os
, const CORBA::Exception
& e
)
94 CORBA::TypeCode_var tc
= tmp
.type();
95 const char * p
= tc
->name();
106 //----------------------------------------------------------------
108 // Change the temperature of a thermostat.
111 set_temp(CCS::Thermostat_ptr tmstat
, CCS::TempType new_temp
)
113 if (CORBA::is_nil(tmstat
)) // Don't call via nil reference
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
);
147 std::cerr
<< "Usage: client IOR_string" << std::endl
;
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
;
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: "
173 if (CORBA::is_nil(ctrl
.in()))
175 std::cerr
<< "Wrong type for controller ref." << std::endl
;
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";
187 std::cout
<< "." << std::endl
;
189 // If there are no devices at all, we are finished.
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());
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
;
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
;
241 ss
[0].key
.loc(CORBA::string_dup("Earth"));
242 ss
[1].key
.loc(CORBA::string_dup("HAL"));
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
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: "