📄 client.cpp
字号:
#include <OB/CORBA.h>#include <OB/CosNaming.h>#if defined(HAVE_STD_IOSTREAM)using namespace std;#endif#ifdef HAVE_FSTREAM# include <fstream>#else# include <fstream.h>#endif#include "CCS.h" // ORB-specific//----------------------------------------------------------------// 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){ for (CORBA::ULong 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;}//----------------------------------------------------------------// Change the temperature of a thermostat.static voidset_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() << endl; } catch (const CCS::Thermostat::BadTemp &bt) { cerr << "Setting of nominal temperature failed." << endl; cerr << bt.details << endl; // Overloaded << }}//----------------------------------------------------------------intmain(int argc, char * argv[]){ int status = 0; CORBA::ORB_var orb; try { // Initialize ORB orb = CORBA::ORB_init(argc, argv); // Get controller reference from Naming Service CORBA::Object_var obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContext_var inc = CosNaming::NamingContext::_narrow(obj); CosNaming::Name name; name.length(2); name[0].id = CORBA::string_dup("michi"); name[1].id = CORBA::string_dup("controller"); obj = inc->resolve(name); // 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 and try to delete them all, so // we start with a clean slate. CCS::Controller::ThermometerSeq_var list = ctrl->list(); CORBA::ULong i; for (i = 0; i < list->length(); ++i) list[i]->destroy(); // Create a few devices. CCS::Thermometer_var t = ctrl->create_thermometer(27, "Room 1"); CCS::Thermostat_var ts = ctrl->create_thermostat(28, "Room 2", 48); CCS::Thermostat_var ts2 = ctrl->create_thermostat(30, "Room 3", 48); CCS::Thermostat_var ts3 = ctrl->create_thermostat(32, "Room 3", 68); CCS::Thermostat_var ts4 = ctrl->create_thermostat(34, "Room 3", 68); CCS::Thermostat_var ts5 = ctrl->create_thermostat(36, "Room 3", 48); cout << t->location() << endl; cout << ts->location() << endl; cout << ts2->location() << endl; // Destroy a device and make sure that it's really gone. cout << "Destroying device " << t->asset_num() << endl; t->destroy(); bool got_exception = false; try { cout << t << endl; } catch (const CORBA::OBJECT_NOT_EXIST &) { cout << "Device destroyed" << endl << endl; got_exception = true; } if (!got_exception) throw "Destroyed device still exists!"; // Try to create a device a second time and check that we // get DuplicateAsset cout << "Creating duplicate device 28" << endl; got_exception = false; try { ts = ctrl->create_thermostat(28, "Room 2", 48); } catch (CCS::Controller::DuplicateAsset &) { cout << "Got DuplicateAsset exception, as expected" << endl << endl; got_exception = true; } // Show details for each device. list = ctrl->list(); for (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 (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); } // Look for device in Rooms Earth and HAL. This must // locate at least one device because we earlier changed // the location of the first device to Room Earth. 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::Controller::ThermostatSeq tss; CORBA::ULong len; for (i = 0; i < list->length(); ++i) { tmstat = CCS::Thermostat::_narrow(list[i]); if (CORBA::is_nil(tmstat)) continue; // Skip thermometers len = tss.length(); tss.length(len + 1); tss[len] = tmstat; } // 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 + -