Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Advanced CORBA Programming wit C++ - M. Henning, S. Vinoski.pdf
Скачиваний:
65
Добавлен:
24.05.2014
Размер:
5 Mб
Скачать

IT-SC book: Advanced CORBA® Programming with C++

min_permitted: 40 max_permitted: 90 error_msg : Too cold

Looking for devices in Earth and HAL.

Thermometer:

 

1027

Asset number:

Model

:

Sens-A-Temp

Location

:

Earth

Temperature :

67

Thermostat:

 

8042

Asset number:

Model

:

Select-A-Temp

Location

:

HAL

Temperature :

38

Nominal temp:

40

Thermometer:

 

8053

Asset number:

Model

:

Sens-A-Temp

Location

:

HAL

Temperature :

69

Increasing thermostats by 40 degrees.

Change failed:

Thermostat:

 

4026

Asset number:

Model

:

Select-A-Temp

Location

:

ENIAC

Temperature :

62

Nominal temp:

60

CCS::Thermostat::BtData details:

requested

:

100

min_permitted:

40

max_permitted:

90

error_msg

:

Too hot

8.7 The Complete Client Code

For your reference, we reproduce the entire client code here.

#include

<iostream.h>

// ORB-specific

#include

"CCS.hh"

//----------------------------------------------------------------

 

 

// Show the details for a thermometer or thermostat. static ostream &

operator<(ostream & os, CCS::Thermometer_ptr t)

{

// Check for nil.

if (CORBA::is_nil(t)) {

os < "Cannot show state for nil reference." < endl; return os;

}

// Try to narrow and print what kind of device it is. CCS::Thermostat_var tmstat = CCS::Thermostat::_narrow(t);

306

IT-SC book: Advanced CORBA® Programming with C++

os < (CORBA::is_nil(tmstat) ? "Thermometer:" : "Thermostat:")

<endl;

//Show attribute values. CCS::ModelType_var model = t->model(); CCS::LocType_var location = t->location();

os < "\tAsset number: " < t->asset_num() < endl;

os < "\tModel

: " < model < endl;

os < "\tLocation

: " < location < endl;

os < "\tTemperature

: " < t->temperature() < endl;

// If device is a thermostat, show nominal temperature. if (!CORBA::is_nil(tmstat))

os < "\tNominal temp: " < tmstat->get_nominal() < endl; return os;

}

//----------------------------------------------------------------

// Show the information in a BtData struct.

static ostream &

operator<(ostream & os, const CCS::Thermostat::BtDat a & btd)

{

os < "CCS::Thermostat::BtData details:" < endl;

os < "\trequested

: "

<

btd.requested < endl;

os < "\tmin_permitted: "

<

btd.min_permitted < endl;

os < "\tmax_permitted: "

<

btd.max_permitted < endl;

os < "\terror_msg

: " < btd.error_msg < endl;

return os;

 

 

 

}

//----------------------------------------------------------------

//Loop over the sequence of records in an EChange exception and

//show the details of each record.

static ostream &

operator<(ostream & os, const CCS::Controller::EChange & ec)

{

for (CORBA::ULong i = 0; i < ec.errors.length(); i++) {

os < "Change failed:" < endl;

// Overloaded <

os < ec.errors[i].tmstat_ref;

os < ec.errors[i].info < endl;

// Overloaded <

}

return os;

}

//----------------------------------------------------------------

//Generic ostream inserter for exceptions. Inserts the exception

//name, if available, and the repository ID otherwise.

static ostream &

operator<(ostream & os, const CORBA::Exception & e)

{

CORBA::Any tmp; tmp <= e;

CORBA::TypeCode_var tc = tmp.type(); const char * p = tc->name();

307

IT-SC book: Advanced CORBA® Programming with C++

if (*p != '\0') os < p;

else

os < tc->id(); return os;

}

//----------------------------------------------------------------

// Change the temperature of a thermostat.

static void

set_temp(CCS::Thermostat_ptr tmstat, CCS::TempType new_temp)

{

if (CORBA::is_nil(tmstat)) // Don't call via nil reference return;

CCS::AssetType anum = tmstat->asset_num(); try {

cout < "Setting thermostat " < anum

<" to " < new_temp < " degrees." < endl; CCS::TempType old_nominal = tmstat->set_nominal(new_temp); cout < "Old nominal temperature was: "

<old_nominal < endl;

cout < "New nominal temperature is: "

<tmstat->get_nominal() <

}catch (const CCS::Thermostat::BadTemp & bt) {

cerr < "Setting of nominal temperature failed." < endl;

cerr < bt.details < endl;

// Overloaded <

}

 

}

 

//----------------------------------------------------------------

 

int

main(int argc, char * argv[])

{

try {

// Initialize the ORB

CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

//Check arguments if (argc != 2) {

cerr < "Usage: client IOR_string" < endl; throw 0;

}

//Get controller reference from argv

//and convert to object.

CORBA::Object_var obj = orb->string_to_object (argv[1]); if (CORBA::is_nil(obj)) {

cerr < "Nil controller reference" < endl; throw 0;

}

// Try to narrow to CCS::Controller. CCS::Controller_var ctrl;

try {

ctrl = CCS::Controller::_narrow(obj);

} catch (const CORBA::SystemException & se) { cerr < "Cannot narrow controller reference: "

308

IT-SC book: Advanced CORBA® Programming with C++

< se < endl; throw 0;

}

if (CORBA::is_nil(ctrl)) {

cerr < "Wrong type for controller ref." < endl; throw 0;

}

// Get list of devices CCS::Controller::ThermometerSeq_var list = ctrl->list();

//Show number of devices. CORBA::ULong len = list->length();

cout < "Controller has " < len < " device"; if (len != 1)

cout < "s"; cout < "." < endl;

//If there are no devices at all, we are finished. if (len == 0)

return 0;

//Show details for each device.

for (CORBA::ULong i = 0; i < list->length(); i++) cout < list[i];

cout < endl;

// Change the location of first device in the list CCS::AssetType anum = list[0]->asset_num();

cout < "Changing location of device "

<anum < "." < endl; list[0]->location("Earth");

//Check that the location was updated cout < "New details for device "

<anum < " are:" < endl;

cout < list[0] < endl;

// Find first thermostat in list. CCS::Thermostat_var tmstat;

for ( CORBA::ULong i = 0;

i < list->length() && CORBA::is_nil(tmstat); i++) {

tmstat = CCS::Thermostat::_narrow(list[i]);

}

// Check that we found a thermostat on the list. if (CORBA::is_nil(tmstat)) {

cout < "No thermostat devices in list." < endl;

}else {

//Set temperature of thermostat to

//50 degrees (should work).

set_temp(tmstat, 50); cout < endl;

//Set temperature of thermostat to

//-10 degrees (should fail).

set_temp(tmstat, -10);

}

309