⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 一套Orbacus4.X下有关Corba的学习例程
💻 CPP
字号:
#include <OB/CORBA.h>#include <assert.h>#if defined(HAVE_STD_IOSTREAM)using namespace std;#endif#include    "CCS.h"//----------------------------------------------------------------// 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);    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::BtData & 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){    CORBA::ULong i;    for (i = 0; i < ec.errors.length(); ++i) {        os << "Change failed:" << endl;        os << ec.errors[i].tmstat_ref.in(); // Overloaded <<        os << ec.errors[i].info << endl;    // Overloaded <<    }    return os;}//----------------------------------------------------------------intmain(int argc, char * argv[]){    int status = 0;    CORBA::ORB_var orb;    try {        // Initialize ORB and check arguments.        orb = CORBA::ORB_init(argc, argv);        if (argc != 3) {            cerr << "Usage: client IOR IOR" << endl;            throw 0;        }        // Get thermostat reference from argv[1]        // and convert to object.        CORBA::Object_var obj = orb->string_to_object(argv[1]);        if (CORBA::is_nil(obj)) {            cerr << "Nil thermostat reference" << endl;            throw 0;        }        // Try to narrow to CCS::Thermostat.        CCS::Thermostat_var tmstat;        try {            tmstat = CCS::Thermostat::_narrow(obj);        } catch (const CORBA::SystemException & se) {            cerr << "Cannot narrow thermostat reference: "                 << se << endl;            throw 0;        }        if (CORBA::is_nil(tmstat)) {            cerr << "Wrong type for thermostat ref." << endl;            throw 0;        }        // Show details of thermostat        cout << tmstat << endl;        // Change the temperature of the thermostat to a valid        // temperature.        cout << "Changing nominal temperature" << endl;        CCS::TempType old_temp = tmstat->set_nominal(60);        cout << "Nominal temperature is now 60, previously "             << old_temp << endl << endl;        cout << "Retrieving new nominal temperature" << endl;        cout << "Nominal temperature is now "             << tmstat->get_nominal() << endl << endl;                // Change the temperature to an illegal value and         // show the details of the exception that is thrown.        cout << "Setting nominal temperature out of range" << endl;        bool got_exception = false;        try {            tmstat->set_nominal(10000);        } catch (const CCS::Thermostat::BadTemp & e) {            cout << "Got BadTemp exception: " << endl;            cout << e.details << endl;        }        if (!got_exception)            assert("Did not get exception");        // Get controller reference from argv[2]        // and convert to object.        obj = orb->string_to_object(argv[2]);        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: "                 << 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;        // Show details for each device.        CORBA::ULong i;        for (i = 0; i < list->length(); ++i)            cout << list[i].in();        cout << endl;                    // Look for device in Rooms Earth and HAL.        cout << "Looking for devices in Earth and HAL." << endl;        CCS::Controller::SearchSeq ss;        ss.length(2);        ss[0].key.loc(CORBA::string_dup("Earth"));        ss[1].key.loc(CORBA::string_dup("HAL"));        ctrl->find(ss);        // Show the devices found in that room.        for (i = 0; i < ss.length(); ++i)            cout << ss[i].device.in();      // Overloaded <<        cout << endl;                // Increase the temperature of all thermostats        // by 40 degrees. First, make a new list (tss)        // containing only thermostats.        cout << "Increasing thermostats by 40 degrees." << endl;        CCS::Thermostat_var ts;        CCS::Controller::ThermostatSeq tss;        for (i = 0; i < list->length(); ++i) {            ts = CCS::Thermostat::_narrow(list[i]);            if (CORBA::is_nil(ts))                continue;                   // Skip thermometers            len = tss.length();            tss.length(len + 1);            tss[len] = ts;        }        // Try to change all thermostats.        try {            ctrl->change(tss, 40);        } catch (const CCS::Controller::EChange & ec) {            cerr << ec;                     // Overloaded <<        }    }    catch (const CORBA::Exception& ex) {        cerr << "Uncaught CORBA exception: " << ex << endl;        status = 1;    } catch (...) {        status = 1;    }    if (!CORBA::is_nil(orb)) {        try {            orb -> destroy();        } catch (const CORBA::Exception& ex) {            cerr << ex << endl;            status = 1;        }    }        return status;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -