void Controller_impl::find (CCS::Controller::SearchSeq & slist) { // Loop over input list and lookup each device. CORBA::ULong listlen = slist.length (); for (CORBA::ULong i = 0; i < listlen; i++) { AssetMap::iterator where; // Iterator for asset set int num_found = 0; // Num matched per iteration // Assume we will not find a matching device. slist[i].device = CCS::Thermometer::_nil (); // Work out whether we are searching by asset, // model, or location. CCS::Controller::SearchCriterion sc = slist[i].key._d (); if (sc == CCS::Controller::ASSET) { // Search for matching asset number. where = m_assets.find (slist[i].key.asset_num ()); if (where != m_assets.end ()) slist[i].device = make_dref (m_poa.in (), where->first); } else { // Search for model or location string. const char *search_str; if (sc == CCS::Controller::LOCATION) search_str = slist[i].key.loc (); else search_str = slist[i].key.model_desc (); // Find first matching device (if any). where = find_if ( m_assets.begin (), m_assets.end (), StrFinder (sc, search_str) ); // While there are matches... while (where != m_assets.end ()) { if (num_found == 0) { // First match overwrites reference // in search record. slist[i].device = make_dref (m_poa.in (), where->first); } else { // Further matches each append a new // element to the search sequence. CORBA::ULong len = slist.length (); slist.length (len + 1); slist[len].key = slist[i].key; slist[len].device = make_dref (m_poa.in (), where->first); } num_found++; // Find next matching device with this key. where = find_if ( ++where, m_assets.end (), StrFinder (sc, search_str) ); } } } }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { CORBA::ULong i = 0; try { // Initialize the ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // Check arguments if (argc != 2) { std::cerr << "Usage: client IOR_string" << std::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.in())) { std::cerr << "Nil controller reference" << std::endl; throw 0; } // Try to narrow to CCS::Controller. CCS::Controller_var ctrl; try { ctrl = CCS::Controller::_narrow(obj.in()); } catch (const CORBA::SystemException &se) { std::cerr << "Cannot narrow controller reference: " << se << std::endl; throw 0; } if (CORBA::is_nil(ctrl.in())) { std::cerr << "Wrong type for controller ref." << std::endl; throw 0; } // Get list of devices CCS::Controller::ThermometerSeq_var list = ctrl->list(); // Show number of devices. CORBA::ULong len = list->length(); std::cout << "Controller has " << len << " device"; if (len != 1) std::cout << "s"; std::cout << "." << std::endl; 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); std::cout << t->location() << std::endl; std::cout << ts->location() << std::endl; std::cout << ts2->location() << std::endl; t->remove(); list = ctrl->list(); // Show details for each device. for ( i = 0; i < list->length(); i++) { CCS::Thermometer_ptr ti = list[i]; std::cout << ti; } std::cout << std::endl; // Change the location of first device in the list CCS::AssetType anum = list[0U]->asset_num(); std::cout << "Changing location of device " << anum << "." << std::endl; list[0U]->location("Earth"); // Check that the location was updated std::cout << "New details for device " << anum << " are:" << std::endl; CCS::Thermometer_ptr tx = list[0u]; std::cout << tx << std::endl; // Find first thermostat in list. CCS::Thermostat_var tmstat; for ( i = 0; i < list->length() && CORBA::is_nil(tmstat.in()); i++) { tmstat = CCS::Thermostat::_narrow(list[i]); } // Check that we found a thermostat on the list. if (CORBA::is_nil(tmstat.in())) { std::cout << "No thermostat devices in list." << std::endl; } else { // Set temperature of thermostat to // 50 degrees (should work). set_temp(tmstat.inout(), 50); std::cout << std::endl; // Set temperature of thermostat to // -10 degrees (should fail). set_temp(tmstat.inout(), -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. std::cout << "Looking for devices in Earth and HAL." << std::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++) std::cout << ss[i].device.in(); // Overloaded << std::cout << std::endl; // Increase the temperature of all thermostats // by 40 degrees. First, make a new list (tss) // containing only thermostats. std::cout << "Increasing thermostats by 40 degrees." << std::endl; CCS::Controller::ThermostatSeq tss; for ( i = 0; i < list->length(); i++) { tmstat = CCS::Thermostat::_narrow(list[i]); if (CORBA::is_nil(tmstat.in())) 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) { std::cerr << ec; // Overloaded << } } catch (const CORBA::Exception & e) { std::cerr << "Uncaught CORBA exception: " << e << std::endl; return 1; } catch (...) { return 1; } return 0; }