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
15 #include <orbsvcs/CosNamingC.h>
16 #include <ace/streams.h>
17 // ----------------------------------------------------------------
23 resolve_init(CORBA::ORB_ptr orb
, const char * id
)
25 CORBA::Object_var obj
;
27 obj
= orb
->resolve_initial_references(id
);
29 catch (const CORBA::ORB::InvalidName
&) {
32 catch (const CORBA::Exception
& e
) {
33 std::cerr
<< "Cannot get initial reference for "
39 assert(!CORBA::is_nil(obj
.in()));
41 typename
T::_var_type ref
;
43 ref
= T::_narrow(obj
.in());
45 catch (const CORBA::Exception
& e
) {
46 std::cerr
<< "Cannot narrow reference for "
52 if (CORBA::is_nil(ref
.in())) {
53 std::cerr
<< "Incorrect type of reference for "
60 //----------------------------------------------------------------
65 CosNaming::NamingContext_ptr nc
,
66 const CosNaming::Name
& name
)
68 CORBA::Object_var obj
;
70 obj
= nc
->resolve(name
);
72 catch (const CosNaming::NamingContext::NotFound
&) {
75 catch (const CORBA::Exception
& e
) {
76 std::cerr
<< "Cannot resolve binding: "
81 if (CORBA::is_nil(obj
.in())) {
82 std::cerr
<< "Nil binding in Naming Service" << std::endl
;
86 typename
T::_var_type ref
;
88 ref
= T::_narrow(obj
.in());
90 catch (const CORBA::Exception
& e
) {
91 std::cerr
<< "Cannot narrow reference: "
96 if (CORBA::is_nil(ref
.in())) {
97 std::cerr
<< "Reference has incorrect type" << std::endl
;
103 //----------------------------------------------------------------
105 // Generic ostream inserter for exceptions. Inserts the exception
106 // name, if available, and the repository ID otherwise.
108 #if 0 // This inserter is not be needed for TAO.
111 operator<<(ostream
& os
, const CORBA::Exception
& e
)
116 CORBA::TypeCode_var tc
= tmp
.type();
117 const char * p
= tc
->name();
127 //----------------------------------------------------------------
129 // Show the details for a thermometer or thermostat.
131 // This inserter may or may not be needed for your ORB.
132 #if !defined (GEN_OSTREAM_OPS)
134 static std::ostream
&
135 operator<<(std::ostream
&os
, CCS::Thermometer_ptr t
)
138 if (CORBA::is_nil(t
)) {
139 os
<< "Cannot show state for nil reference." << std::endl
;
143 // Try to narrow and print what kind of device it is.
144 CCS::Thermostat_var tmstat
= CCS::Thermostat::_narrow(t
);
145 os
<< (CORBA::is_nil(tmstat
.in()) ? "Thermometer:" : "Thermostat:")
148 // Show attribute values.
149 CCS::ModelType_var model
= t
->model();
150 CCS::LocType_var location
= t
->location();
151 os
<< "\tAsset number: " << t
->asset_num() << std::endl
;
152 os
<< "\tModel : " << model
.in() << std::endl
;
153 os
<< "\tLocation : " << location
.in() << std::endl
;
154 os
<< "\tTemperature : " << t
->temperature() << std::endl
;
156 // If device is a thermostat, show nominal temperature.
157 if (!CORBA::is_nil(tmstat
.in()))
158 os
<< "\tNominal temp: " << tmstat
->get_nominal() << std::endl
;
162 //----------------------------------------------------------------
164 // Show the information in a BtData struct.
166 static std::ostream
&
167 operator<<(std::ostream
&os
, const CCS::Thermostat::BtData
&btd
)
169 os
<< "CCS::Thermostat::BtData details:" << std::endl
;
170 os
<< "\trequested : " << btd
.requested
<< std::endl
;
171 os
<< "\tmin_permitted: " << btd
.min_permitted
<< std::endl
;
172 os
<< "\tmax_permitted: " << btd
.max_permitted
<< std::endl
;
173 os
<< "\terror_msg : " << btd
.error_msg
<< std::endl
;
177 //----------------------------------------------------------------
179 // Loop over the sequence of records in an EChange exception and
180 // show the details of each record.
182 static std::ostream
&
183 operator<<(std::ostream
&os
, const CCS::Controller::EChange
&ec
)
185 for (CORBA::ULong i
= 0; i
< ec
.errors
.length(); i
++) {
186 os
<< "Change failed:" << std::endl
;
187 os
<< ec
.errors
[i
].tmstat_ref
.in(); // Overloaded <<
188 os
<< ec
.errors
[i
].info
<< std::endl
; // Overloaded <<
195 //----------------------------------------------------------------
197 // Change the temperature of a thermostat.
200 set_temp(CCS::Thermostat_ptr tmstat
, CCS::TempType new_temp
)
202 if (CORBA::is_nil(tmstat
)) // Don't call via nil reference
205 CCS::AssetType anum
= tmstat
->asset_num();
207 std::cout
<< "Setting thermostat " << anum
208 << " to " << new_temp
<< " degrees." << std::endl
;
209 CCS::TempType old_nominal
= tmstat
->set_nominal(new_temp
);
210 std::cout
<< "Old nominal temperature was: "
211 << old_nominal
<< std::endl
;
212 std::cout
<< "New nominal temperature is: "
213 << tmstat
->get_nominal() << std::endl
;
214 } catch (const CCS::Thermostat::BadTemp
&bt
) {
215 std::cerr
<< "Setting of nominal temperature failed." << std::endl
;
216 std::cerr
<< bt
.details
<< std::endl
; // Overloaded <<
220 //----------------------------------------------------------------
223 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
228 // Initialize the ORB
229 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
233 std::cerr
<< "Usage: client" << std::endl
;
237 // Get reference to initial naming context.
238 CosNaming::NamingContext_var inc
239 = resolve_init
<CosNaming::NamingContext
>(orb
.in(), "NameService");
241 // Look for controller in the Naming Service.
244 n
[0].id
= CORBA::string_dup("CCS");
245 n
[1].id
= CORBA::string_dup("Controller");
246 CCS::Controller_var ctrl
;
248 ctrl
= resolve_name
<CCS::Controller
>(inc
.in(), n
);
249 } catch (const CosNaming::NamingContext::NotFound
&) {
250 std::cerr
<< "No controller in Naming Service" << std::endl
;
254 // Get list of devices
255 CCS::Controller::ThermometerSeq_var list
= ctrl
->list();
257 // Show number of devices.
258 CORBA::ULong len
= list
->length();
259 std::cout
<< "Controller has " << len
<< " device";
262 std::cout
<< "." << std::endl
;
264 CCS::Thermometer_var t
= ctrl
->create_thermometer(27, "Room 1");
265 CCS::Thermostat_var ts
= ctrl
->create_thermostat(28, "Room 2", 48);
266 CCS::Thermostat_var ts2
= ctrl
->create_thermostat(30, "Room 3", 48);
267 CCS::Thermostat_var ts3
= ctrl
->create_thermostat(32, "Room 3", 68);
268 CCS::Thermostat_var ts4
= ctrl
->create_thermostat(34, "Room 3", 68);
269 CCS::Thermostat_var ts5
= ctrl
->create_thermostat(36, "Room 3", 48);
270 std::cout
<< t
->location() << std::endl
;
271 std::cout
<< ts
->location() << std::endl
;
272 std::cout
<< ts2
->location() << std::endl
;
276 // Show details for each device.
277 for ( i
= 0; i
< list
->length(); i
++)
279 CCS::Thermometer_ptr ti
= list
[i
];
282 std::cout
<< std::endl
;
284 // Change the location of first device in the list
285 CCS::AssetType anum
= list
[0u]->asset_num();
286 std::cout
<< "Changing location of device "
287 << anum
<< "." << std::endl
;
288 list
[0u]->location("Earth");
289 // Check that the location was updated
290 std::cout
<< "New details for device "
291 << anum
<< " are:" << std::endl
;
292 CCS::Thermometer_ptr tx
= list
[0u];
293 std::cout
<< tx
<< std::endl
;
295 // Find first thermostat in list.
296 CCS::Thermostat_var tmstat
;
298 i
< list
->length() && CORBA::is_nil(tmstat
.in());
300 tmstat
= CCS::Thermostat::_narrow(list
[i
]);
303 // Check that we found a thermostat on the list.
304 if (CORBA::is_nil(tmstat
.in())) {
305 std::cout
<< "No thermostat devices in list." << std::endl
;
307 // Set temperature of thermostat to
308 // 50 degrees (should work).
309 set_temp(tmstat
.inout(), 50);
310 std::cout
<< std::endl
;
312 // Set temperature of thermostat to
313 // -10 degrees (should fail).
314 set_temp(tmstat
.inout(), -10);
317 // Look for device in Rooms Earth and HAL. This must
318 // locate at least one device because we earlier changed
319 // the location of the first device to Room Earth.
320 std::cout
<< "Looking for devices in Earth and HAL." << std::endl
;
321 CCS::Controller::SearchSeq ss
;
323 ss
[0].key
.loc(CORBA::string_dup("Earth"));
324 ss
[1].key
.loc(CORBA::string_dup("HAL"));
327 // Show the devices found in that room.
328 for ( i
= 0; i
< ss
.length(); i
++)
329 std::cout
<< ss
[i
].device
.in(); // Overloaded <<
330 std::cout
<< std::endl
;
332 // Increase the temperature of all thermostats
333 // by 40 degrees. First, make a new list (tss)
334 // containing only thermostats.
335 std::cout
<< "Increasing thermostats by 40 degrees." << std::endl
;
336 CCS::Controller::ThermostatSeq tss
;
337 for ( i
= 0; i
< list
->length(); i
++) {
338 tmstat
= CCS::Thermostat::_narrow(list
[i
]);
339 if (CORBA::is_nil(tmstat
.in()))
340 continue; // Skip thermometers
346 // Try to change all thermostats.
348 ctrl
->change(tss
, 40);
349 } catch (const CCS::Controller::EChange
&ec
) {
350 std::cerr
<< ec
; // Overloaded <<
352 } catch (const CORBA::Exception
& e
) {
353 std::cerr
<< "Uncaught CORBA exception: "