Go to the documentation of this file.00001
00019 #include <rtm/ConfigAdmin.h>
00020 #include <algorithm>
00021 #include <assert.h>
00022 #include <string.h>
00023
00024 namespace RTC
00025 {
00032 ConfigAdmin::ConfigAdmin(coil::Properties& configsets)
00033 : m_configsets(configsets), m_activeId("default"),
00034 m_active(true), m_changed(false)
00035 {
00036 }
00037
00045 ConfigAdmin::~ConfigAdmin(void)
00046 {
00047 for (int i(0), len(m_params.size()); i < len; ++i)
00048 {
00049 if (m_params[i] != NULL) { delete m_params[i]; }
00050 }
00051 m_params.clear();
00052 }
00053
00054
00064 void ConfigAdmin::update(void)
00065 {
00066 if (m_changed && m_active)
00067 {
00068 update(m_activeId.c_str());
00069 m_changed = false;
00070 }
00071 return;
00072 }
00073
00074
00082 void ConfigAdmin::update(const char* config_set)
00083 {
00084 if (m_configsets.hasKey(config_set) == NULL) { return; }
00085
00086 coil::Properties& prop(m_configsets.getNode(config_set));
00087
00088 for (int i(0), len(m_params.size()); i < len; ++i)
00089 {
00090 if (prop.hasKey(m_params[i]->name) != NULL)
00091 {
00092 m_params[i]->update(prop[m_params[i]->name].c_str());
00093 onUpdate(config_set);
00094 }
00095 }
00096 }
00097
00098
00106 void ConfigAdmin::update(const char* config_set, const char* config_param)
00107 {
00108 if ((config_set == 0) || (config_param == 0)) { return; }
00109
00110 std::string key(config_set);
00111 key += "."; key += config_param;
00112
00113 std::vector<ConfigBase*>::iterator it;
00114 it = std::find_if(m_params.begin(), m_params.end(),
00115 find_conf(config_param));
00116 if (it != m_params.end())
00117 {
00118 (*it)->update(m_configsets[key].c_str());
00119 onUpdateParam(config_set, config_param);
00120 return;
00121 }
00122 }
00123
00124
00132 bool ConfigAdmin::isExist(const char* param_name)
00133 {
00134 std::vector<ConfigBase*>::iterator it;
00135 it = std::find_if(m_params.begin(), m_params.end(),
00136 find_conf(param_name));
00137 if (it != m_params.end())
00138 {
00139 return true;
00140 }
00141 return false;
00142 }
00143
00144
00152 const std::vector<coil::Properties*>& ConfigAdmin::getConfigurationSets(void)
00153 {
00154 return m_configsets.getLeaf();
00155 }
00156
00164 const coil::Properties&
00165 ConfigAdmin::getConfigurationSet(const char* config_id)
00166 {
00167 coil::Properties* p(m_configsets.findNode(config_id));
00168 if (p == 0) { return m_emptyconf; }
00169 return *p;
00170 }
00171
00179 bool
00180 ConfigAdmin::setConfigurationSetValues(const coil::Properties& config_set)
00181 {
00182 std::string node(config_set.getName());
00183 if (node.empty()) { return false; }
00184
00185 coil::Properties& p(m_configsets.getNode(config_set.getName()));
00186
00187 p << config_set;
00188 m_changed = true;
00189 m_active = false;
00190 onSetConfigurationSet(config_set);
00191 return true;
00192 }
00193
00201 const coil::Properties& ConfigAdmin::getActiveConfigurationSet(void)
00202 {
00203 coil::Properties& p(m_configsets.getNode(m_activeId));
00204
00205 return p;
00206 }
00207
00215 bool ConfigAdmin::addConfigurationSet(const coil::Properties& config_set)
00216 {
00217 std::string node(config_set.getName());
00218 if (node.empty()) { return false; }
00219 if (m_configsets.hasKey(node.c_str()) != 0) { return false; }
00220
00221 coil::Properties& p(m_configsets.getNode(node));
00222 p << config_set;
00223 m_newConfig.push_back(node);
00224
00225 m_changed = true;
00226 m_active = false;
00227 onAddConfigurationSet(config_set);
00228 return true;
00229 }
00230
00238 bool ConfigAdmin::removeConfigurationSet(const char* config_id)
00239 {
00240 if (strcmp(config_id, "default") == 0) return false;
00241 if (m_activeId == config_id) return false;
00242
00243
00244 std::vector<std::string>::iterator it;
00245 it = std::find(m_newConfig.begin(), m_newConfig.end(), config_id);
00246 if (it == m_newConfig.end()) { return false; }
00247
00248 coil::Properties* p(m_configsets.removeNode(config_id));
00249 if (p != NULL) { delete p; }
00250 m_newConfig.erase(it);
00251
00252 m_changed = true;
00253 m_active = false;
00254 onRemoveConfigurationSet(config_id);
00255 return true;
00256 }
00257
00265 bool ConfigAdmin::activateConfigurationSet(const char* config_id)
00266 {
00267 if (config_id == NULL) { return false; }
00268
00269 if (config_id[0] == '_') { return false; }
00270
00271 if (m_configsets.hasKey(config_id) == 0) { return false; }
00272 m_activeId = config_id;
00273 m_active = true;
00274 m_changed = true;
00275 onActivateSet(config_id);
00276 return true;
00277 }
00278
00279
00280
00281
00282 void ConfigAdmin::setOnUpdate(OnUpdateCallback* cb)
00283 {
00284 std::cerr << "setOnUpdate function is obsolete." << std::endl;
00285 std::cerr << "Use addConfigurationSetNameListener instead." << std::endl;
00286 m_listeners.configsetname_[ON_UPDATE_CONFIG_SET].addListener(cb, false);
00287 }
00288
00289 void ConfigAdmin::setOnUpdateParam(OnUpdateParamCallback* cb)
00290 {
00291 std::cerr << "setOnUpdateParam function is obsolete." << std::endl;
00292 std::cerr << "Use addConfigurationParamListener instead." << std::endl;
00293 m_listeners.configparam_[ON_UPDATE_CONFIG_PARAM].addListener(cb, false);
00294 }
00295
00296 void ConfigAdmin::setOnSetConfigurationSet(OnSetConfigurationSetCallback* cb)
00297 {
00298 std::cerr << "setOnSetConfigurationSet function is obsolete." << std::endl;
00299 std::cerr << "Use addConfigurationSetListener instead." << std::endl;
00300 m_listeners.configset_[ON_SET_CONFIG_SET].addListener(cb, false);
00301 }
00302
00303 void ConfigAdmin::setOnAddConfigurationSet(OnAddConfigurationAddCallback* cb)
00304 {
00305 std::cerr << "setOnAddConfigurationSet function is obsolete." << std::endl;
00306 std::cerr << "Use addConfigurationSetListener instead." << std::endl;
00307 m_listeners.configset_[ON_ADD_CONFIG_SET].addListener(cb, false);
00308 }
00309
00310 void
00311 ConfigAdmin::setOnRemoveConfigurationSet(OnRemoveConfigurationSetCallback* cb)
00312 {
00313 std::cerr << "setOnRemoveConfigurationSet function is obsolete."<<std::endl;
00314 std::cerr << "Use addConfigurationSetNameListener instead." << std::endl;
00315 m_listeners.configsetname_[ON_REMOVE_CONFIG_SET].addListener(cb, false);
00316 }
00317
00318 void ConfigAdmin::setOnActivateSet(OnActivateSetCallback* cb)
00319 {
00320 std::cerr << "setOnActivateSet function is obsolete." << std::endl;
00321 std::cerr << "Use addConfigurationSetNameListener instead." << std::endl;
00322 m_listeners.configsetname_[ON_ACTIVATE_CONFIG_SET].addListener(cb, false);
00323 }
00324
00325
00326
00327
00335 void ConfigAdmin::
00336 addConfigurationParamListener(ConfigurationParamListenerType type,
00337 ConfigurationParamListener* listener,
00338 bool autoclean)
00339 {
00340 m_listeners.configparam_[type].addListener(listener, autoclean);
00341 }
00342
00350 void ConfigAdmin::
00351 removeConfigurationParamListener(ConfigurationParamListenerType type,
00352 ConfigurationParamListener* listener)
00353 {
00354 m_listeners.configparam_[type].removeListener(listener);
00355 }
00356
00364 void ConfigAdmin::
00365 addConfigurationSetListener(ConfigurationSetListenerType type,
00366 ConfigurationSetListener* listener,
00367 bool autoclean)
00368 {
00369 m_listeners.configset_[type].addListener(listener, autoclean);
00370 }
00371
00379 void ConfigAdmin::
00380 removeConfigurationSetListener(ConfigurationSetListenerType type,
00381 ConfigurationSetListener* listener)
00382 {
00383 m_listeners.configset_[type].removeListener(listener);
00384 }
00385
00393 void ConfigAdmin::
00394 addConfigurationSetNameListener(ConfigurationSetNameListenerType type,
00395 ConfigurationSetNameListener* listener,
00396 bool autoclean)
00397 {
00398 m_listeners.configsetname_[type].addListener(listener, autoclean);
00399 }
00400
00408 void ConfigAdmin::
00409 removeConfigurationSetNameListener(ConfigurationSetNameListenerType type,
00410 ConfigurationSetNameListener* listener)
00411 {
00412 m_listeners.configsetname_[type].removeListener(listener);
00413 }
00414
00415
00416
00424 void ConfigAdmin::onUpdate(const char* config_set)
00425 {
00426 m_listeners.configsetname_[ON_UPDATE_CONFIG_SET].notify(config_set);
00427 }
00428
00436 void
00437 ConfigAdmin::onUpdateParam(const char* config_set, const char* config_param)
00438 {
00439 m_listeners.configparam_[ON_UPDATE_CONFIG_PARAM].notify(config_set,
00440 config_param);
00441 }
00442
00450 void ConfigAdmin::onSetConfigurationSet(const coil::Properties& config_set)
00451 {
00452 m_listeners.configset_[ON_SET_CONFIG_SET].notify(config_set);
00453 }
00454
00462 void ConfigAdmin::onAddConfigurationSet(const coil::Properties& config_set)
00463 {
00464 m_listeners.configset_[ON_ADD_CONFIG_SET].notify(config_set);
00465 }
00466
00474 void ConfigAdmin::onRemoveConfigurationSet(const char* config_id)
00475 {
00476 m_listeners.configsetname_[ON_REMOVE_CONFIG_SET].notify(config_id);
00477 }
00478
00486 void ConfigAdmin::onActivateSet(const char* config_id)
00487 {
00488 m_listeners.configsetname_[ON_ACTIVATE_CONFIG_SET].notify(config_id);
00489 }
00490
00491
00492 };