2 //=============================================================================
6 * @author Source code used in TAO has been modified and adapted from thecode provided in the book
7 * @author "Advanced CORBA Programming with C++"by Michi Henning and Steve Vinoski. Copyright1999. Addison-Wesley
9 * @author MA. Used with permission ofAddison-Wesley.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
11 //=============================================================================
14 #include "CCSC.h" // ORB-specific
15 #include <ace/streams.h>
19 // Generic ostream inserter for exceptions. Inserts the exception
20 // name, if available, and the repository ID otherwise.
22 // This inserter may or may not be needed for your ORB.
25 operator<< (ostream
&os
, const CORBA::Exception
&e
)
30 CORBA::TypeCode_var tc
= tmp
.type ();
31 const char * p
= tc
->name ();
40 #if !defined (GEN_OSTREAM_OPS)
42 // Show the details for a thermometer or thermostat.
45 operator<< (std::ostream
&os
, CCS::Thermometer_ptr t
)
48 if (CORBA::is_nil (t
))
50 os
<< "Cannot show state for nil reference." << std::endl
;
54 // Try to narrow and print what kind of device it is.
55 CCS::Thermostat_var tmstat
= CCS::Thermostat::_narrow (t
);
56 os
<< (CORBA::is_nil (tmstat
.in ()) ? "Thermometer:" : "Thermostat:")
59 // Show attribute values.
60 CCS::ModelType_var model
= t
->model ();
61 CCS::LocType_var location
= t
->location ();
62 os
<< "\tAsset number: " << t
->asset_num () << std::endl
;
63 os
<< "\tModel : " << model
.in () << std::endl
;
64 os
<< "\tLocation : " << location
.in () << std::endl
;
65 os
<< "\tTemperature : " << t
->temperature () << std::endl
;
67 // If device is a thermostat, show nominal temperature.
68 if (!CORBA::is_nil (tmstat
.in ()))
69 os
<< "\tNominal temp: " << tmstat
->get_nominal () << std::endl
;
73 // Show the information in a BtData struct.
76 operator<< (std::ostream
&os
, const CCS::Thermostat::BtData
&btd
)
78 os
<< "CCS::Thermostat::BtData details:" << std::endl
;
79 os
<< "\trequested : " << btd
.requested
<< std::endl
;
80 os
<< "\tmin_permitted: " << btd
.min_permitted
<< std::endl
;
81 os
<< "\tmax_permitted: " << btd
.max_permitted
<< std::endl
;
82 os
<< "\terror_msg : " << btd
.error_msg
<< std::endl
;
86 // Loop over the sequence of records in an EChange exception and show
87 // the details of each record.
90 operator<< (std::ostream
&os
, const CCS::Controller::EChange
&ec
)
92 for (CORBA::ULong i
= 0; i
< ec
.errors
.length (); i
++)
94 os
<< "Change failed:" << std::endl
;
95 os
<< ec
.errors
[i
].tmstat_ref
.in (); // Overloaded <<
96 os
<< ec
.errors
[i
].info
<< std::endl
; // Overloaded <<
103 // Change the temperature of a thermostat.
105 set_temp (CCS::Thermostat_ptr tmstat
, CCS::TempType new_temp
)
107 if (CORBA::is_nil (tmstat
)) // Don't call via nil reference
110 CCS::AssetType anum
= tmstat
->asset_num ();
113 std::cout
<< "Setting thermostat " << anum
114 << " to " << new_temp
<< " degrees." << std::endl
;
115 CCS::TempType old_nominal
= tmstat
->set_nominal (new_temp
);
116 std::cout
<< "Old nominal temperature was: "
117 << old_nominal
<< std::endl
;
118 std::cout
<< "New nominal temperature is: "
119 << tmstat
->get_nominal () << std::endl
;
121 catch (const CCS::Thermostat::BadTemp
&bt
)
123 std::cerr
<< "Setting of nominal temperature failed." << std::endl
;
124 std::cerr
<< bt
.details
<< std::endl
; // Overloaded <<
129 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
134 // Initialize the ORB
135 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
140 std::cerr
<< "Usage: client IOR_string" << std::endl
;
144 // Get controller reference from argv
145 // and convert to object.
146 CORBA::Object_var obj
= orb
->string_to_object (argv
[1]);
147 if (CORBA::is_nil (obj
.in ()))
149 std::cerr
<< "Nil controller reference" << std::endl
;
153 // Try to narrow to CCS::Controller.
154 CCS::Controller_var ctrl
;
157 ctrl
= CCS::Controller::_narrow (obj
.in ());
159 catch (const CORBA::SystemException
&se
)
161 std::cerr
<< "Cannot narrow controller reference: "
167 if (CORBA::is_nil (ctrl
.in ()))
169 std::cerr
<< "Wrong type for controller ref." << std::endl
;
173 // Get list of devices
174 CCS::Controller::ThermometerSeq_var list
= ctrl
->list ();
176 // Show number of devices.
177 CORBA::ULong len
= list
->length ();
178 std::cout
<< "Controller has " << len
<< " device";
181 std::cout
<< "." << std::endl
;
183 CCS::Thermometer_var t
= ctrl
->create_thermometer (27, "Room 1");
184 CCS::Thermostat_var ts
= ctrl
->create_thermostat (28, "Room 2", 48);
185 CCS::Thermostat_var ts2
= ctrl
->create_thermostat (30, "Room 3", 48);
186 CCS::Thermostat_var ts3
= ctrl
->create_thermostat (32, "Room 3", 68);
187 CCS::Thermostat_var ts4
= ctrl
->create_thermostat (34, "Room 3", 68);
188 CCS::Thermostat_var ts5
= ctrl
->create_thermostat (36, "Room 3", 48);
189 std::cout
<< t
->location () << std::endl
;
190 std::cout
<< ts
->location () << std::endl
;
191 std::cout
<< ts2
->location () << std::endl
;
194 list
= ctrl
->list ();
196 // Show details for each device.
197 for ( i
= 0; i
< list
->length (); i
++)
199 CCS::Thermometer_ptr ti
= list
[i
];
203 std::cout
<< std::endl
;
205 // Change the location of first device in the list
206 CCS::AssetType anum
= list
[0u]->asset_num ();
207 std::cout
<< "Changing location of device "
208 << anum
<< "." << std::endl
;
209 list
[0u]->location ("Earth");
210 // Check that the location was updated
211 std::cout
<< "New details for device "
212 << anum
<< " are:" << std::endl
;
213 CCS::Thermometer_ptr tx
= list
[0u];
214 std::cout
<< tx
<< std::endl
;
216 // Find first thermostat in list.
217 CCS::Thermostat_var tmstat
;
219 i
< list
->length () && CORBA::is_nil (tmstat
.in ());
222 tmstat
= CCS::Thermostat::_narrow (list
[i
]);
225 // Check that we found a thermostat on the list.
226 if (CORBA::is_nil (tmstat
.in ()))
228 std::cout
<< "No thermostat devices in list." << std::endl
;
232 // Set temperature of thermostat to
233 // 50 degrees (should work).
234 set_temp (tmstat
.inout (), 50);
235 std::cout
<< std::endl
;
237 // Set temperature of thermostat to
238 // -10 degrees (should fail).
239 set_temp (tmstat
.inout (), -10);
242 // Look for device in Rooms Earth and HAL. This must locate at
243 // least one device because we earlier changed the location of
244 // the first device to Room Earth.
245 std::cout
<< "Looking for devices in Earth and HAL." << std::endl
;
246 CCS::Controller::SearchSeq ss
;
248 ss
[0].key
.loc (CORBA::string_dup ("Earth"));
249 ss
[1].key
.loc (CORBA::string_dup ("HAL"));
252 // Show the devices found in that room.
253 for ( i
= 0; i
< ss
.length (); i
++)
254 std::cout
<< ss
[i
].device
.in (); // Overloaded <<
256 std::cout
<< std::endl
;
258 // Increase the temperature of all thermostats by 40
259 // degrees. First, make a new list (tss) containing only
261 std::cout
<< "Increasing thermostats by 40 degrees." << std::endl
;
262 CCS::Controller::ThermostatSeq tss
;
264 for ( i
= 0; i
< list
->length (); i
++)
266 tmstat
= CCS::Thermostat::_narrow (list
[i
]);
267 if (CORBA::is_nil (tmstat
.in ()))
268 continue; // Skip thermometers
270 tss
.length (len
+ 1);
274 // Try to change all thermostats.
277 ctrl
->change (tss
, 40);
279 catch (const CCS::Controller::EChange
&ec
)
281 std::cerr
<< ec
; // Overloaded <<
284 catch (const CORBA::Exception
& e
)
286 std::cerr
<< "Uncaught CORBA exception: "