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

📄 server.cpp

📁 一套Orbacus4.X下有关Corba的学习例程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#ifdef _MSC_VER#pragma warning ( disable : 4786 )#endif#include <OB/CORBA.h>#include <OB/CosNaming.h>#ifdef HAVE_FSTREAM#   include <fstream>#else#   include <fstream.h>#endif#if defined(HAVE_STRSTREAM)#   include <strstream>#else#   if defined(HAVE_STRSTREA_H)#       include <strstrea.h>#   else#       include <strstream.h>#   endif#endif#include <algorithm>#include <signal.h>#if defined(HAVE_STD_IOSTREAM) || defined(HAVE_STD_STL)using namespace std;#endif#include "icp.h"#include "server.h"//----------------------------------------------------------------// Controller servant, set by controller constructorController_impl * Thermometer_impl::m_ctrl;//----------------------------------------------------------------// Helper function to convert asset numbers to object IDs.static PortableServer::ObjectId *make_oid(CCS::AssetType anum){    // Convert asset number to OID.    ostrstream ostr;    ostr << anum << ends;    char * anum_str = ostr.str();    PortableServer::ObjectId_var oid        = PortableServer::string_to_ObjectId(anum_str);    ostr.rdbuf()->freeze(0);    return oid._retn();}// Helper function to read the model string from a device.CCS::ModelTypeThermometer_impl::get_model(){    char buf[32];    if (ICP_get(m_anum, "model", buf, sizeof(buf)) != 0)        abort();    return CORBA::string_dup(buf);}// Helper function to read the temperature from a device.CCS::TempTypeThermometer_impl::get_temp(){    short temp;    if (ICP_get(m_anum, "temperature", &temp, sizeof(temp)) != 0)        abort();    return temp;}// Helper function to read the location from a device.CCS::LocTypeThermometer_impl::get_loc(){    char buf[32];    if (ICP_get(m_anum, "location", buf, sizeof(buf)) != 0)        abort();    return CORBA::string_dup(buf);}// Helper function to set the location of a device.voidThermometer_impl::set_loc(const char * loc){    if (ICP_set(m_anum, "location", loc) != 0)        abort();}// m_poa static memberPortableServer::POA_var Thermometer_impl::m_poa;// Constructor.Thermometer_impl::Thermometer_impl(CCS::AssetType anum) : m_anum(anum){    if (CORBA::is_nil(m_poa))        throw "Thermometer_impl::m_poa member not set";    m_ctrl->add_impl(anum, this);}// Destructor.Thermometer_impl::~Thermometer_impl(){    // Intentionally empty}// IDL model attribute.CCS::ModelTypeThermometer_impl::model() throw(CORBA::SystemException){    return get_model();}// IDL asset_num attribute.CCS::AssetTypeThermometer_impl::asset_num() throw(CORBA::SystemException){    return m_anum;}// IDL temperature attribute.CCS::TempTypeThermometer_impl::temperature() throw(CORBA::SystemException){    return get_temp();}// IDL location attribute accessor.CCS::LocTypeThermometer_impl::location() throw(CORBA::SystemException){    return get_loc();}// IDL location attribute modifier.voidThermometer_impl::location(const char *loc) throw(CORBA::SystemException){    set_loc(loc);}// IDL destroy operation.voidThermometer_impl::destroy() throw(CORBA::SystemException){    m_ctrl->remove_impl(m_anum);    if (ICP_offline(m_anum) != 0)        abort();    PortableServer::ObjectId_var oid = make_oid(m_anum);    PortableServer::POA_var poa = _default_POA();    poa->deactivate_object(oid);}//----------------------------------------------------------------// Helper function to get a thermostat's nominal temperature.CCS::TempTypeThermostat_impl::get_nominal_temp(){    short temp;    if (ICP_get(m_anum, "nominal_temp", &temp, sizeof(temp)) != 0)        abort();    return temp;}// Helper function to set a thermostat's nominal temperature.CCS::TempTypeThermostat_impl::set_nominal_temp(CCS::TempType new_temp)throw(CCS::Thermostat::BadTemp){    short old_temp;    // We need to return the previous nominal temperature,    // so we first read the current nominal temperature before    // changing it.    if (ICP_get(        m_anum, "nominal_temp", &old_temp, sizeof(old_temp)       ) != 0) {        abort();    }    // Now set the nominal temperature to the new value.    if (ICP_set(m_anum, "nominal_temp", &new_temp) != 0) {        // If ICP_set() failed, read this thermostat's minimum        // and maximum so we can initialize the BadTemp exception.        CCS::Thermostat::BtData btd;        ICP_get(            m_anum, "MIN_TEMP",            &btd.min_permitted, sizeof(btd.min_permitted)        );        ICP_get(            m_anum, "MAX_TEMP",            &btd.max_permitted, sizeof(btd.max_permitted)        );        btd.requested = new_temp;        btd.error_msg = CORBA::string_dup(            new_temp > btd.max_permitted ? "Too hot" : "Too cold"        );        throw CCS::Thermostat::BadTemp(btd);    }    return old_temp;}// m_poa static memberPortableServer::POA_var Thermostat_impl::m_poa;// Constructor.Thermostat_impl::Thermostat_impl(CCS::AssetType anum) : Thermometer_impl(anum){    if (CORBA::is_nil(m_poa))        throw "Thermostat_impl::m_poa member not set";}// Destructor.Thermostat_impl::~Thermostat_impl(){    // Intentionally empty.}// IDL get_nominal operation.CCS::TempTypeThermostat_impl::get_nominal() throw(CORBA::SystemException){    return get_nominal_temp();}// IDL set_nominal operation.CCS::TempTypeThermostat_impl::set_nominal(CCS::TempType new_temp)throw(CORBA::SystemException, CCS::Thermostat::BadTemp){    return set_nominal_temp(new_temp);}//----------------------------------------------------------------// Helper function to add an entry to the asset map.voidController_impl::add_impl(CCS::AssetType anum, Thermometer_impl * tip){    m_assets[anum] = tip;}// Helper function to remove an entry from the asset map.voidController_impl::remove_impl(CCS::AssetType anum){    m_assets.erase(anum);}// Helper function to locate a servant in the asset map.boolController_impl::exists(CCS::AssetType anum){    return m_assets.find(anum) != m_assets.end();}// m_poa static memberPortableServer::POA_var Controller_impl::m_poa;// ConstructorController_impl::Controller_impl(    const char *            asset_file) throw(int) : m_asset_file(asset_file){    if (CORBA::is_nil(m_poa))        throw "Controller_impl::m_poa member not set";    // Set static member in Thermometer_impl class.    Thermometer_impl::m_ctrl = this;    // Instantiate one device for each entry in the database.    fstream afile(m_asset_file, ios::in|ios::out#if defined(_MSC_VER)    |ios::app#elif defined(__sun)    |ios::app, 0666#else    , 0666#endif    );    if (!afile) {        cerr << "Cannot open " << m_asset_file << endl;        throw 0;    }    CCS::AssetType anum;    while (afile >> anum) {        // Get model.        char buf[32];        if (ICP_get(anum, "model", buf, sizeof(buf)) != 0)            abort();        // Instantiate and activate servant.        Thermometer_impl * servant;        PortableServer::ObjectId_var oid = make_oid(anum);        if (strcmp(buf, "Sens-A-Temp") == 0) {            servant = new Thermometer_impl(anum);            Thermometer_impl::poa()->activate_object_with_id(oid, servant);        } else {            servant = new Thermostat_impl(anum);            Thermostat_impl::poa()->activate_object_with_id(oid, servant);        }        servant->_remove_ref();    }    afile.close();#if !defined(__GNUC__) && !defined(_MSC_VER)    if (!afile) {        cerr << "Cannot close " << m_asset_file << endl;        throw 0;    }#endif}// DestructorController_impl::~Controller_impl(){    // Write out the current set of asset numbers    // and clean up all servant instances.    ofstream afile(m_asset_file);    if (!afile) {        cerr << "Cannot open " << m_asset_file << endl;        abort();    }    AssetMap::iterator i;    for (i = m_assets.begin(); i != m_assets.end(); ++i) {        afile << i->first << endl;        if (!afile) {            cerr << "Cannot update " << m_asset_file << endl;            abort();        }    }    afile.close();#if !defined(__GNUC__) && !defined(_MSC_VER)    if (!afile) {        cerr << "Cannot close " << m_asset_file << endl;        abort();    }

⌨️ 快捷键说明

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