SdoConfiguration.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <coil/UUID.h>
00020 #include "rtm/SdoConfiguration.h"
00021 #include <rtm/CORBA_SeqUtil.h>
00022 #include <rtm/NVUtil.h>
00023 #include <rtm/ExecutionContextBase.h>
00024 #include <memory>
00025 #include <iostream>
00026 // ACE
00027 
00028 namespace SDOPackage
00029 {
00051   void 
00052   toProperties(coil::Properties& prop, const SDOPackage::ConfigurationSet& conf)
00053   {
00054     NVUtil::copyToProperties(prop, conf.configuration_data);
00055   }
00056   
00078   void
00079   toConfigurationSet(SDOPackage::ConfigurationSet& conf,
00080                      const coil::Properties& prop)
00081   {
00082 #ifndef ORB_IS_RTORB
00083     conf.description = CORBA::string_dup(prop["description"].c_str());
00084     conf.id = CORBA::string_dup(prop.getName());
00085 #else // ORB_IS_RTORB
00086     conf.description = (char *)prop["description"].c_str();
00087     conf.id = (char *)prop.getName();
00088 #endif // ORB_IS_RTORB
00089     NVUtil::copyFromProperties(conf.configuration_data, prop);
00090   }
00091   
00092   //============================================================
00093   // Ctor and Dtor
00094   //============================================================
00095   
00096   /* @if jp
00097    * @brief コンストラクタ
00098    * @else
00099    * @brief Constructor
00100    * @endif
00101    */
00102   Configuration_impl::Configuration_impl(RTC::ConfigAdmin& configsets,
00103                                          RTC::SdoServiceAdmin& sdoServiceAdmin)
00104     : rtclog("sdo_config"), m_configsets(configsets),
00105       m_sdoservice(sdoServiceAdmin)
00106   {
00107     m_objref = this->_this();
00108   }
00109   
00110   /* @if jp
00111    * @brief 仮想デストラクタ
00112    * @else
00113    * @brief Virtual destructor
00114    * @endif
00115    */
00116   Configuration_impl::~Configuration_impl()
00117   {
00118   }
00119 
00120   
00121   //============================================================
00122   // Basic Configuration 
00123   //============================================================
00124   /* @if jp
00125    * @brief [CORBA interface] SDO の DeviceProfile をセットする
00126    * @else
00127    * @brief [CORBA interface] Set DeviceProfile of SDO
00128    * @endif
00129    */
00130   CORBA::Boolean
00131   Configuration_impl::set_device_profile(const DeviceProfile& dProfile)
00132     throw (CORBA::SystemException,
00133            InvalidParameter, NotAvailable, InternalError)
00134   {
00135     RTC_TRACE(("set_device_profile()"));
00136     try
00137       {
00138         Guard gurad(m_dprofile_mutex);
00139         m_deviceProfile = dProfile;
00140       }
00141     catch (...)
00142       {
00143         throw InternalError("Unknown Error");
00144         // never reach here
00145         return false;
00146       }
00147     return true;
00148   }
00149   
00157   CORBA::Boolean
00158   Configuration_impl::add_service_profile(const ServiceProfile& sProfile)
00159     throw (CORBA::SystemException,
00160            InvalidParameter, NotAvailable, InternalError)
00161   {
00162     RTC_TRACE(("add_service_profile()"));
00163     // SDO specification defines that InvalidParameter() exception
00164     // is thrown when sProfile is null.
00165     // But sProfile is reference and it becomes never null.
00166     // So this operation does not throw InvalidParameter exception.
00167     //    if (CORBA::is_nil(sProfile.service)) throw InvalidParameter();
00168     try
00169       {
00170         return m_sdoservice.addSdoServiceConsumer(sProfile);
00171       }
00172     catch (...)
00173       {
00174         throw InternalError("Configuration::set_service_profile");
00175       }
00176     return false;
00177   }
00178   
00186   CORBA::Boolean
00187   Configuration_impl::add_organization(Organization_ptr org)
00188     throw (CORBA::SystemException,
00189            InvalidParameter, NotAvailable, InternalError)
00190   {
00191     RTC_TRACE(("add_organization()"));
00192     try
00193       {
00194         CORBA_SeqUtil::push_back(m_organizations,
00195                                  ::SDOPackage::Organization::_duplicate(org));
00196       }
00197     catch (...)
00198       {
00199         throw InternalError("Configuration::set_service_profile");
00200         // never reach here
00201         return false;
00202       }
00203     return true;
00204   }
00205   
00213   CORBA::Boolean
00214   Configuration_impl::remove_service_profile(const char* id)
00215     throw (CORBA::SystemException,
00216            InvalidParameter, NotAvailable, InternalError)
00217   {
00218     RTC_TRACE(("remove_service_profile(%s)", id));
00219     try
00220       {
00221         return m_sdoservice.removeSdoServiceConsumer(id);
00222       }
00223     catch (...)
00224       {
00225         throw InternalError("Configuration::remove_service_profile");
00226       }
00227     return false;
00228   }
00229   
00237   CORBA::Boolean
00238   Configuration_impl::remove_organization(const char* organization_id)
00239     throw (CORBA::SystemException,
00240            InvalidParameter, NotAvailable, InternalError)
00241   {
00242     RTC_TRACE(("remove_organization(%s)", organization_id));
00243     try
00244       {
00245         Guard gurad(m_org_mutex);
00246         CORBA_SeqUtil::erase_if(m_organizations, org_id(organization_id));
00247       }
00248     catch (...)
00249       {
00250         throw InternalError("Configuration::remove_organization");
00251         // never reach here
00252         return false;
00253       }
00254     return true;
00255   }
00256   
00257   //============================================================
00258   // Configuration Parameter manipulation
00259   //============================================================
00267   ParameterList*
00268   Configuration_impl::get_configuration_parameters()
00269     throw (CORBA::SystemException,
00270            NotAvailable, InternalError)
00271   {
00272     RTC_TRACE(("get_configuration_parameters()"));
00273     try
00274       {
00275         Guard gaurd(m_params_mutex);
00276         ParameterList_var param;
00277         param = new ParameterList(m_parameters);
00278         return param._retn();
00279       }
00280     catch (...)
00281       {
00282         throw InternalError("Configuration::get_configuration_parameters()");
00283         //never reach here
00284       }
00285     // never reach here
00286     return new ParameterList(0);
00287   }
00288   
00296   NVList*
00297   Configuration_impl::get_configuration_parameter_values()
00298     throw (CORBA::SystemException,
00299            NotAvailable, InternalError)
00300   {
00301     RTC_TRACE(("get_configuration_parameter_values()"));
00302     Guard guard(m_config_mutex);
00303     NVList_var nvlist;
00304     nvlist = new NVList((CORBA::ULong)0);
00305     
00306     /*
00307       CORBA::Long index;
00308       index = getActiveConfigIndex();
00309       if (index >= 0)
00310       {
00311       nvlist = new NVList(m_configurations[index].configuration_data);
00312       }
00313     */
00314     return nvlist._retn();
00315   }
00316   
00324   CORBA::Any*
00325   Configuration_impl::get_configuration_parameter_value(const char* name)
00326     throw (CORBA::SystemException,
00327            InvalidParameter, NotAvailable, InternalError)
00328   {
00329     RTC_TRACE(("get_configuration_parameter_value(%s)", name));
00330     if (std::string(name).empty()) throw InvalidParameter("Name is empty.");
00331     
00332     //    CORBA::Long index;
00333     CORBA::Any_var value;
00334     value = new CORBA::Any();
00335     /*
00336       index = getActiveConfigIndex();
00337       if (index < 0) throw InternalError("No active configuration.");
00338       
00339       CORBA::Long item;
00340       item = CORBA_SeqUtil::find(m_configurations[index].configuration_data,
00341       nv_name(name));
00342       if (item < 0) throw InvalidParameter("No such name."); 
00343       
00344       value = new CORBA::Any(m_configurations[index].configuration_data[item].value);
00345     */
00346     return value._retn();
00347   }
00348   
00356   CORBA::Boolean
00357   Configuration_impl::set_configuration_parameter(const char* name,
00358                                                   const CORBA::Any& value)
00359     throw (CORBA::SystemException,
00360            InvalidParameter, NotAvailable, InternalError)
00361   {
00362     RTC_TRACE(("set_configuration_parameter(%s, value)", name));
00363     /*
00364       if (name == "") throw InvalidParameter("Name is empty.");
00365       
00366       CORBA::Long index(getActiveConfigIndex());
00367       if (index < 0) throw InternalError("No active config.");
00368       
00369       CORBA::Long item;
00370       item = CORBA_SeqUtil::find(m_configurations[index].configuration_data,
00371       nv_name(name));
00372       if (item < 0) throw InvalidParameter("No such name.");
00373       
00374       m_configurations[index].configuration_data[item].value = value;
00375     */
00376     return true;
00377   }
00378   
00386   ConfigurationSetList*
00387   Configuration_impl::get_configuration_sets()
00388     throw (CORBA::SystemException,
00389            NotAvailable, InternalError)
00390   {
00391     RTC_TRACE(("get_configuration_sets()"));
00392     try
00393       {
00394         Guard guard(m_config_mutex);
00395         
00396         std::vector<coil::Properties*> cf(m_configsets.getConfigurationSets());
00397         ConfigurationSetList_var config_sets = 
00398           new ConfigurationSetList((CORBA::ULong)cf.size());
00399         // Ctor's first arg is max length. Actual length has to be set.
00400         config_sets->length((CORBA::ULong)cf.size());
00401 
00402         for (CORBA::ULong i(0), len(cf.size()); i < len; ++i)
00403           {
00404             toConfigurationSet(config_sets[i], *(cf[i]));
00405           }
00406         
00407         return config_sets._retn();
00408       }
00409     catch (CORBA::SystemException& e)
00410       {
00411 #ifndef ORB_IS_RTORB
00412         RTC_ERROR(("CORBA::SystemException cought: %s", e._name()));
00413 #else
00414         RTC_ERROR(("CORBA::SystemException cought."));
00415 #endif
00416         throw InternalError("Configuration::get_configuration_sets()");
00417 
00418       }
00419     catch (...)
00420       {
00421         RTC_ERROR(("Unknown exception cought."));
00422         throw InternalError("Configuration::get_configuration_sets()");
00423       }
00424     // never reach here
00425     return new ConfigurationSetList(0);
00426   }
00427   
00435   ConfigurationSet*
00436   Configuration_impl::get_configuration_set(const char* id)
00437     throw (CORBA::SystemException,
00438            NotAvailable, InternalError)
00439   {
00440     RTC_TRACE(("get_configuration_set(%s)", id));
00441     if (std::string(id).empty()) throw InternalError("ID is empty");
00442     // Originally getConfigurationSet raises InvalidParameter according to the 
00443     // SDO specification. However, SDO's IDL lacks InvalidParameter.
00444     
00445     Guard guard(m_config_mutex);
00446     
00447     try
00448       {
00449         if (!m_configsets.haveConfig(id))
00450           {
00451             RTC_ERROR(("No such ConfigurationSet"));
00452             throw InvalidParameter("No such ConfigurationSet");
00453           }
00454       }
00455     catch(...)
00456       {
00457         RTC_ERROR(("Unknown exception"));
00458         throw InternalError("Unknown exception");
00459       }
00460     
00461     const coil::Properties& configset(m_configsets.getConfigurationSet(id));
00462     
00463     try
00464       {
00465         ConfigurationSet_var config;
00466         config = new ConfigurationSet();
00467         toConfigurationSet(config, configset);
00468 
00469         return config._retn();
00470       }
00471     catch (...)
00472       {
00473         throw InternalError("Configuration::get_configuration_set()");
00474       }
00475 
00476     // never reach here
00477     RTC_FATAL(("never reach here"));
00478     return new ConfigurationSet();
00479   }
00480   
00488   CORBA::Boolean
00489   Configuration_impl::
00490   set_configuration_set_values(const ConfigurationSet& configuration_set)
00491     throw (CORBA::SystemException,
00492            InvalidParameter, NotAvailable, InternalError)
00493   {
00494     RTC_TRACE(("set_configuration_set_values()"));
00495     std::string id(configuration_set.id);
00496     if (id.empty()) throw InvalidParameter("ID is empty.");
00497     
00498     try
00499       {
00500         coil::Properties conf(id.c_str());
00501         toProperties(conf, configuration_set);
00502         
00503         //------------------------------------------------------------
00504         // Because the format of port-name had been changed from
00505         // <port_name> to <instance_name>.<port_name>, the following
00506         // processing was added.  (since r1648)
00507         if (conf.findNode("exported_ports") != 0)
00508           {
00509             coil::vstring
00510               exported_ports(coil::split(conf["exported_ports"], ","));
00511             std::string exported_ports_str("");
00512             for (size_t i(0), len(exported_ports.size()); i < len; ++i)
00513               {
00514                 coil::vstring keyval(coil::split(exported_ports[i], "."));
00515                 if (keyval.size() > 2)
00516                   {
00517                     exported_ports_str += (keyval[0] + "." + keyval.back());
00518                   }
00519                 else
00520                   {
00521                     exported_ports_str += exported_ports[i];
00522                   }
00523                 if (i != exported_ports.size() - 1)
00524                   {
00525                     exported_ports_str += ",";
00526                   }
00527               }
00528             conf["exported_ports"] = exported_ports_str;
00529           }
00530         //------------------------------------------------------------
00531 
00532         return m_configsets.setConfigurationSetValues(conf);
00533       }
00534     catch (...)
00535       {
00536         throw InternalError("Configuration::set_configuration_set_values()");
00537       }
00538     return true;
00539   }
00540   
00548   ConfigurationSet*
00549   Configuration_impl::get_active_configuration_set()
00550     throw (CORBA::SystemException,
00551            NotAvailable, InternalError)
00552   {
00553     RTC_TRACE(("get_active_configuration_set()"));
00554     // activeなConfigurationSetは無い
00555     if (!m_configsets.isActive()) throw NotAvailable();    
00556     
00557     try
00558       {
00559         Guard gurad(m_config_mutex);
00560         // activeなConfigurationSetを返す
00561         ConfigurationSet_var config;
00562         config = new ConfigurationSet();
00563         toConfigurationSet(config, m_configsets.getActiveConfigurationSet());
00564         return config._retn();
00565       }
00566     catch (...)
00567       {
00568         throw InternalError("Configuration::get_active_configuration_set()");
00569       }
00570     // never reach here
00571     return new ConfigurationSet();
00572   }
00573   
00581   CORBA::Boolean
00582   Configuration_impl::
00583   add_configuration_set(const ConfigurationSet& configuration_set)
00584     throw (CORBA::SystemException,
00585            InvalidParameter, NotAvailable, InternalError)
00586   {
00587     RTC_TRACE(("add_configuration_set()"));
00588     try
00589       {
00590         Guard gurad(m_config_mutex);
00591         const char* config_id(configuration_set.id);
00592 //      const char* config_value(configuration_set.description);
00593 //      RTC::Properties config(config_id);
00594         coil::Properties config(config_id);
00595 //      coil::Properties config(config_id,config_value);
00596         toProperties(config, configuration_set);
00597 //        config["description"] = configuration_set.description;
00598         return m_configsets.addConfigurationSet(config);
00599       }
00600     catch (...)
00601       {
00602         throw InternalError("Configuration::add_configuration_set()");
00603         return false;
00604       }
00605     return true;
00606   }
00607   
00615   CORBA::Boolean
00616   Configuration_impl::remove_configuration_set(const char* id)
00617     throw (CORBA::SystemException,
00618            InvalidParameter, NotAvailable, InternalError)
00619   {
00620     RTC_TRACE(("remove_configuration_set(%s)", id));
00621     if (std::string(id).empty())
00622       throw InvalidParameter("ID is empty.");
00623     
00624     try
00625       {
00626         Guard guard(m_config_mutex);
00627         return m_configsets.removeConfigurationSet(id);
00628       }
00629     catch (...)
00630       {
00631         throw InternalError("Configuration::remove_configuration_set()");
00632         return false;
00633       }
00634     return false;
00635   }
00636   
00644   CORBA::Boolean
00645   Configuration_impl::activate_configuration_set(const char* id)
00646     throw (CORBA::SystemException,
00647            InvalidParameter, NotAvailable, InternalError)
00648   {
00649     RTC_TRACE(("activate_configuration_set(%s)", id));
00650     if (std::string(id).empty())
00651       throw InvalidParameter("ID is empty.");
00652     
00653     if(m_configsets.activateConfigurationSet(id))
00654       {
00655         return true;
00656       }
00657     else
00658       {
00659         throw InvalidParameter("Configuration::activate_configuration_set()");
00660       }
00661 /*
00662     try
00663       {
00664         return m_configsets.activateConfigurationSet(id);
00665       }
00666     catch (...)
00667       {
00668         throw InternalError("Configuration::activate_configuration_set()");
00669         return false;
00670       }
00671 */
00672     return false;
00673   }
00674   
00675   //============================================================
00676   // Local interfaces
00677   //============================================================
00678   
00686   Configuration_ptr Configuration_impl::getObjRef()
00687   {
00688     return m_objref;
00689   }
00690   
00698   const DeviceProfile Configuration_impl::getDeviceProfile()
00699   {
00700     return m_deviceProfile;
00701   }
00702   
00710   const OrganizationList Configuration_impl::getOrganizations()
00711   {
00712     return m_organizations;
00713   }
00714   
00722   const std::string Configuration_impl::getUUID() const
00723   {
00724     coil::UUID_Generator uugen;
00725     uugen.init();
00726     std::auto_ptr<coil::UUID> uuid(uugen.generateUUID(2,0x01));
00727     
00728     return (const char*) uuid->to_string();
00729   }
00730 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:07