📄 server.cpp
字号:
#endif}CCS::Thermometer_ptrController_impl::create_thermometer(CCS::AssetType anum, const char * loc)throw(CORBA::SystemException, CCS::Controller::DuplicateAsset){ if (exists(anum)) throw CCS::Controller::DuplicateAsset(); if (ICP_online(anum) != 0) abort(); if (ICP_set(anum, "location", loc) != 0) abort(); Thermometer_impl * t = new Thermometer_impl(anum); PortableServer::ObjectId_var oid = make_oid(anum); Thermometer_impl::poa()->activate_object_with_id(oid, t); t->_remove_ref(); return t->_this();}CCS::Thermostat_ptrController_impl::create_thermostat( CCS::AssetType anum, const char* loc, CCS::TempType temp)throw( CORBA::SystemException, CCS::Controller::DuplicateAsset, CCS::Thermostat::BadTemp){ if (exists(anum)) throw CCS::Controller::DuplicateAsset(); if (ICP_online(anum) != 0) abort(); if (ICP_set(anum, "location", loc) != 0) abort(); // Set the nominal temperature. if (ICP_set(anum, "nominal_temp", &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( anum, "MIN_TEMP", &btd.min_permitted, sizeof(btd.min_permitted) ); ICP_get( anum, "MAX_TEMP", &btd.max_permitted, sizeof(btd.max_permitted) ); btd.requested = temp; btd.error_msg = CORBA::string_dup( temp > btd.max_permitted ? "Too hot" : "Too cold" ); ICP_offline(anum); throw CCS::Thermostat::BadTemp(btd); } Thermostat_impl * t = new Thermostat_impl(anum); PortableServer::ObjectId_var oid = make_oid(anum); Thermostat_impl::poa()->activate_object_with_id(oid, t); t->_remove_ref(); return t->_this();}// IDL list operation.CCS::Controller::ThermometerSeq *Controller_impl::list() throw(CORBA::SystemException){ // Create a new thermometer sequence. Because we know // the number of elements we will put onto the sequence, // we use the maximum constructor. CCS::Controller::ThermometerSeq_var listv = new CCS::Controller::ThermometerSeq(m_assets.size()); listv->length(m_assets.size()); // Loop over the m_assets set and create a // reference for each device. CORBA::ULong count = 0; AssetMap::iterator i; for (i = m_assets.begin(); i != m_assets.end(); ++i) listv[count++] = i->second->_this(); return listv._retn();}// IDL change operation.voidController_impl::change( const CCS::Controller::ThermostatSeq & tlist, CORBA::Short delta) throw(CORBA::SystemException, CCS::Controller::EChange){ CCS::Controller::EChange ec; // Just in case we need it // We cannot add a delta value to a thermostat's temperature // directly, so for each thermostat, we read the nominal // temperature, add the delta value to it, and write // it back again. CORBA::ULong i; for (i = 0; i < tlist.length(); ++i) { if (CORBA::is_nil(tlist[i])) continue; // Skip nil references // Read nominal temp and update it. CCS::TempType tnom = tlist[i]->get_nominal(); tnom += delta; try { tlist[i]->set_nominal(tnom); } catch (const CCS::Thermostat::BadTemp &bt) { // If the update failed because the temperature // is out of range, we add the thermostat's info // to the errors sequence. CORBA::ULong len = ec.errors.length(); ec.errors.length(len + 1); ec.errors[len].tmstat_ref = tlist[i]; ec.errors[len].info = bt.details; } } // If we encountered errors in the above loop, // we will have added elements to the errors sequence. if (ec.errors.length() != 0) throw ec;}// IDL find operationvoidController_impl::find(CCS::Controller::SearchSeq & slist)throw(CORBA::SystemException){ // Loop over input list and lookup each device. CORBA::ULong listlen = slist.length(); CORBA::ULong i; for (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 = where->second->_this(); } 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 = where->second->_this(); } 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 = where->second->_this(); } ++num_found; // Find next matching device with this key. where = find_if( ++where, m_assets.end(), StrFinder(sc, search_str) ); } } }}//----------------------------------------------------------------static PortableServer::POA_ptrcreate_persistent_POA( const char * name, PortableServer::POA_ptr parent){ // Create policy list for simple persistence CORBA::PolicyList pl; CORBA::ULong len = pl.length(); pl.length(len + 1); pl[len++] = parent->create_lifespan_policy( PortableServer::PERSISTENT ); pl.length(len + 1); pl[len++] = parent->create_id_assignment_policy( PortableServer::USER_ID ); pl.length(len + 1); pl[len++] = parent->create_thread_policy( PortableServer::SINGLE_THREAD_MODEL ); pl.length(len + 1); pl[len++] = parent->create_implicit_activation_policy( PortableServer::NO_IMPLICIT_ACTIVATION ); // Get parent POA's POA manager PortableServer::POAManager_var pmanager = parent->the_POAManager(); // Create new POA PortableServer::POA_var poa = parent->create_POA(name, pmanager, pl); // Clean up for (CORBA::ULong i = 0; i < len; ++i) pl[i]->destroy(); return poa._retn();}//----------------------------------------------------------------static voidrun(CORBA::ORB_ptr orb){ // Get reference to Root POA. CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj); // Create POAs for controller, thermometers, and thermostats. // The controller POA becomes the parent of the thermometer and // thermostat POAs. All POAs use the Root POA's POA manager. PortableServer::POA_var poa; poa = create_persistent_POA("Controller", root_poa); Controller_impl::poa(poa); poa = create_persistent_POA("Thermometers", Controller_impl::poa()); Thermometer_impl::poa(poa); poa = create_persistent_POA("Thermostats", Controller_impl::poa()); Thermostat_impl::poa(poa); // Create a controller. Controller_impl * ctrl_servant = new Controller_impl("CCS_assets"); // Create a reference for the controller and // create the corresponding CORBA object. PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("the_controller"); Controller_impl::poa()->activate_object_with_id(oid, ctrl_servant); // Servant is reference-counted and is now in the AOM (which calls // _add_ref), so we can drop the reference count. ctrl_servant->_remove_ref(); // MISSING, step 4 // Get Root POA manager and activate all POAs PortableServer::POAManager_var mgr = root_poa->the_POAManager(); mgr->activate(); // Accept requests orb->run();}//----------------------------------------------------------------static CORBA::ORB_var orb; // Global, so handler can see it.//----------------------------------------------------------------#ifdef WIN32BOOLhandler(DWORD){ // Inform JTC of presence of new thread JTCAdoptCurrentThread adopt; // Terminate event loop try { if (!CORBA::is_nil(orb)) orb->shutdown(false); } catch (...) { // Can't throw here... } return TRUE;}#elseextern "C"voidhandler(int){ // Ignore further signals struct sigaction ignore; ignore.sa_handler = SIG_IGN; sigemptyset(&ignore.sa_mask); ignore.sa_flags = 0; if (sigaction(SIGINT, &ignore, (struct sigaction *)0) == -1) abort(); if (sigaction(SIGTERM, &ignore, (struct sigaction *)0) == -1) abort(); if (sigaction(SIGHUP, &ignore, (struct sigaction *)0) == -1) abort(); // Terminate event loop try { if (!CORBA::is_nil(orb)) orb->shutdown(false); } catch (...) { // Can't throw here... }}#endif//----------------------------------------------------------------intmain(int argc, char* argv[]){ // Install signal handler for cleanup#ifdef WIN32 BOOL rc = SetConsoleCtrlHandler((PHANDLER_ROUTINE)handler, TRUE); if (!rc) abort();#else struct sigaction sa; // New signal state sa.sa_handler = handler; // Set handler function sigfillset(&sa.sa_mask); // Mask all other signals // while handler runs sa.sa_flags = 0 | SA_RESTART; // Restart interrupted syscalls if (sigaction(SIGINT, &sa, (struct sigaction *)0) == -1) abort(); if (sigaction(SIGHUP, &sa, (struct sigaction *)0) == -1) abort(); if (sigaction(SIGTERM, &sa, (struct sigaction *)0) == -1) abort();#endif // Initialize the ORB and start working... int status = 0; try { orb = CORBA::ORB_init(argc, argv); run(orb); } catch (const CORBA::Exception & ex) { cerr << "Uncaught CORBA exception: " << ex << endl; status = 1; } catch (const char * & msg) { cerr << msg << endl; status = 1; } catch (...) { cerr << "Uncaught non-CORBA exception" << endl; status = 1; } // Destroy the ORB. if (!CORBA::is_nil(orb)) { try { orb->destroy(); } catch (const CORBA::Exception & ex) { cerr << "Cannot destroy ORB: " << ex << endl; status = 1; } } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -