2 //=============================================================================
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
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>
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)
29 operator<<(ostream
& os
, const CORBA::Exception
& e
)
34 CORBA::TypeCode_var tc
= tmp
.type();
35 const char * p
= tc
->name();
45 //----------------------------------------------------------------
47 // Show the details for a thermometer or thermostat.
50 operator<<(std::ostream
&os
, CCS::Thermometer_ptr t
)
53 if (CORBA::is_nil(t
)) {
54 os
<< "Cannot show state for nil reference." << std::endl
;
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:")
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
;
77 //----------------------------------------------------------------
79 // Show the information in a BtData struct.
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
;
92 //----------------------------------------------------------------
94 // Loop over the sequence of records in an EChange exception and
95 // show the details of each record.
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 <<
110 //----------------------------------------------------------------
112 // Change the temperature of a thermostat.
115 set_temp(CCS::Thermostat_ptr tmstat
, CCS::TempType new_temp
)
117 if (CORBA::is_nil(tmstat
)) // Don't call via nil reference
120 CCS::AssetType anum
= tmstat
->asset_num();
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
[])
142 // Initialize the ORB
143 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())) {
155 std::cerr
<< "Nil controller reference" << std::endl
;
159 // Try to narrow to CCS::Controller.
160 CCS::Controller_var ctrl
;
162 ctrl
= CCS::Controller::_narrow(obj
.in());
163 } catch (const CORBA::SystemException
&se
) {
164 std::cerr
<< "Cannot narrow controller reference: "
169 if (CORBA::is_nil(ctrl
.in())) {
170 std::cerr
<< "Wrong type for controller ref." << std::endl
;
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";
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
;
196 // Show details for each device.
197 for ( i
= 0; i
< list
->length(); i
++)
199 CCS::Thermometer_ptr ti
= list
[i
];
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
;
218 i
< list
->length() && CORBA::is_nil(tmstat
.in());
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
;
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
;
243 ss
[0].key
.loc(CORBA::string_dup("Earth"));
244 ss
[1].key
.loc(CORBA::string_dup("HAL"));
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
266 // Try to change all thermostats.
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: "