plugin_manager.cpp
Go to the documentation of this file.
00001 #include <vigir_pluginlib/plugin_manager.h>
00002 
00003 #include <ros/console.h>
00004 
00005 
00006 
00007 namespace vigir_pluginlib
00008 {
00009 using namespace vigir_generic_params;
00010 
00011 PluginManager::Ptr PluginManager::singelton_ = PluginManager::Ptr();
00012 
00013 PluginManager::PluginManager()
00014 {
00015 }
00016 
00017 PluginManager::Ptr PluginManager::Instance()
00018 {
00019    if (!singelton_)
00020       singelton_.reset(new PluginManager());
00021    return singelton_;
00022 }
00023 
00024 PluginManager::~PluginManager()
00025 {
00026   boost::unique_lock<boost::shared_mutex> lock(plugins_mutex_);
00027 
00028   // prevents warning when ClassLoaders get destroyed
00029   plugins_by_name_.clear();
00030 
00031   for (PluginLoaderBase* loader : class_loader_)
00032     delete loader;
00033   class_loader_.clear();
00034 }
00035 
00036 void PluginManager::initialize(ros::NodeHandle& nh)
00037 {
00038   Instance()->nh_ = nh;
00039 
00040   // subscribe topics
00041   Instance()->add_plugin_sub_ = nh.subscribe("plugin_manager/add_plugin", 1, &PluginManager::addPlugin, Instance().get());
00042   Instance()->remove_plugin_sub_ = nh.subscribe("plugin_manager/remove_plugin", 1, &PluginManager::removePlugin, Instance().get());
00043 
00044   // publish topics
00045   Instance()->plugin_states_pub_ = nh.advertise<msgs::PluginStates>("plugin_manager/plugin_states_update", 1);
00046 
00047   // start own services
00048   Instance()->get_plugin_descriptions_srv_ = nh.advertiseService("plugin_manager/get_plugin_descriptions", &PluginManager::getPluginDescriptionsService, Instance().get());
00049   Instance()->get_plugin_states_srv_ = nh.advertiseService("plugin_manager/get_plugin_states", &PluginManager::getPluginStatesService, Instance().get());
00050   Instance()->add_plugin_srv_ = nh.advertiseService("plugin_manager/add_plugin", &PluginManager::addPluginService, Instance().get());
00051   Instance()->remove_plugin_srv_ = nh.advertiseService("plugin_manager/remove_plugin", &PluginManager::removePluginService, Instance().get());
00052   Instance()->load_plugin_set_srv_ = nh.advertiseService("plugin_manager/load_plugin_set", &PluginManager::loadPluginSetService, Instance().get());
00053 
00054   // init action servers
00055   Instance()->get_plugin_descriptions_as_.reset(new GetPluginDescriptionsActionServer(nh, "plugin_manager/get_plugin_descriptions", boost::bind(&PluginManager::getPluginDescriptionsAction, Instance().get(), _1), false));
00056   Instance()->get_plugin_states_as_.reset(new GetPluginStatesActionServer(nh, "plugin_manager/get_plugin_states", boost::bind(&PluginManager::getPluginStatesAction, Instance().get(), _1), false));
00057   Instance()->add_plugin_as_.reset(new PluginManagementActionServer(nh, "plugin_manager/add_plugin", boost::bind(&PluginManager::addPluginAction, Instance().get(), _1), false));
00058   Instance()->remove_plugin_as_.reset(new PluginManagementActionServer(nh, "plugin_manager/remove_plugin", boost::bind(&PluginManager::removePluginAction, Instance().get(), _1), false));
00059   Instance()->load_plugin_set_as_.reset(new PluginManagementActionServer(nh, "plugin_manager/load_plugin_set", boost::bind(&PluginManager::loadPluginSetAction, Instance().get(), _1), false));
00060 
00061   // start action servers
00062   Instance()->get_plugin_descriptions_as_->start();
00063   Instance()->get_plugin_states_as_->start();
00064   Instance()->add_plugin_as_->start();
00065   Instance()->remove_plugin_as_->start();
00066   Instance()->load_plugin_set_as_->start();
00067 }
00068 
00069 bool PluginManager::autocompletePluginDescriptionByName(const std::string& name, msgs::PluginDescription& plugin_description)
00070 {
00071   try
00072   {
00073     ros::NodeHandle plugin_nh(Instance()->nh_, name);
00074 
00075     std::string type_class_package;
00076     if (!plugin_nh.getParam("type_class_package", type_class_package))
00077       return false;
00078 
00079     std::string type_class;
00080     if (!plugin_nh.getParam("type_class", type_class))
00081       return false;
00082 
00083     if ((plugin_description.type_class_package.data.empty() || plugin_description.type_class_package.data == type_class_package) &&
00084         (plugin_description.type_class.data.empty() || plugin_description.type_class.data == type_class))
00085     {
00086       plugin_description.type_class_package.data = type_class_package;
00087       plugin_description.type_class.data = type_class;
00088 
00089       if (plugin_description.base_class_package.data.empty())
00090         plugin_nh.param("base_class_package", plugin_description.base_class_package.data, std::string());
00091       if (plugin_description.base_class.data.empty())
00092         plugin_nh.param("base_class", plugin_description.base_class.data, std::string());
00093 
00094       if (plugin_description.public_param_ns.data.empty())
00095         plugin_description.public_param_ns.data = name;
00096 
00097       return true;
00098     }
00099   }
00100   catch (std::exception& e)
00101   {
00102     ROS_ERROR("Exception while extracting plugin description with name '%s':\n%s", name.c_str(), e.what());
00103     return false;
00104   }
00105 }
00106 
00107 bool PluginManager::addPlugins(const std::vector<msgs::PluginDescription>& plugin_descriptions, bool initialize)
00108 {
00109   bool success = true;
00110 
00111   std::list<Plugin::Ptr> plugins;
00112 
00113   // add plugins
00114   for (const msgs::PluginDescription& description : plugin_descriptions)
00115   {
00116     if (addPlugin(description, false))
00117       plugins.push_back(getPluginByName(description.name.data));
00118     else
00119       success = false;
00120   }
00121 
00122   // (post) intialize plugins
00123   if (initialize)
00124   {
00125     for (Plugin::Ptr plugin : plugins)
00126       success &= plugin->initialize(ParameterManager::getActive());
00127     for (Plugin::Ptr plugin : plugins)
00128       success &= plugin->postInitialize(ParameterManager::getActive());
00129   }
00130 
00131   return success;
00132 }
00133 
00134 bool PluginManager::addPlugin(const msgs::PluginDescription& plugin_description, bool initialize)
00135 {
00136   // best effort handling for incomplete plugin description
00137   msgs::PluginDescription description = plugin_description;
00138   if (description.type_class_package.data.empty() || description.type_class.data.empty())
00139   {
00140     if (description.name.data.empty())
00141     {
00142       ROS_ERROR("[PluginManager] addPlugin: Call without name (%s) or type class package (%s) and type class (%s)!", description.name.data.c_str(), description.type_class_package.data.c_str(), description.type_class.data.c_str());
00143       return false;
00144     }
00145     else if (!autocompletePluginDescriptionByName(description.name.data, description))
00146     {
00147       ROS_ERROR("[PluginManager] addPlugin: Can't autocomplete plugin description for (%s)!", description.name.data.c_str());
00148       return false;
00149     }
00150   }
00151 
00152   // create plugin
00153   Plugin::Ptr p;
00154 
00155   try
00156   {
00157     boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00158 
00159     std::string _base_class = description.base_class.data;
00160 
00161     // search for appropriate ClassLoader
00162     for (PluginLoaderBase* loader : Instance()->class_loader_)
00163     {
00164       if (loader->isClassAvailable(description.type_class.data) && (description.base_class.data.empty() || description.base_class.data == loader->getBaseClassType()))
00165       {
00166         if (description.type_class_package.data != loader->getClassPackage(description.type_class.data))
00167           continue;
00168         if (!description.base_class_package.data.empty() && description.base_class_package.data != loader->getBaseClassPackage())
00169           continue;
00170         if (!description.base_class.data.empty() && description.base_class.data != loader->getBaseClassType())
00171           continue;
00172 
00173         if (!p)
00174         {
00175           _base_class = loader->getBaseClassType().c_str();
00176           p = loader->createPluginInstance(description.type_class.data);
00177 
00178           if (description.name.data.empty())
00179             description.name.data = p->getName();
00180 
00181           p->updateDescription(description);
00182         }
00183         else
00184           ROS_WARN("[PluginManager] Duplicate source for plugin '%s' found in ClassLoader '%s'!\nPlugin was already instanciated from ClassLoader '%s'", description.type_class.data.c_str(), loader->getBaseClassType().c_str(), _base_class.c_str());
00185       }
00186     }
00187     if (!p)
00188     {
00189       ROS_ERROR_STREAM("[PluginManager] Plugin with following description is unknown! Check if ClassLoader has been initialized and the plugin has been properly registered!\n" << description);
00190       return false;
00191     }
00192   }
00193   catch (pluginlib::PluginlibException& e)
00194   {
00195     ROS_ERROR("[PluginManager] Plugin (%s) of type_class '%s' failed to load for some reason. Error message: \n %s", description.name.data.c_str(), description.type_class.data.c_str(), e.what());
00196     return false;
00197   }
00198 
00199   PluginManager::addPlugin(p, initialize);
00200   return true;
00201 }
00202 
00203 bool PluginManager::addPluginByName(const std::string& name, bool initialize)
00204 {
00205   msgs::PluginDescription description;
00206   description.name.data = name;
00207   return addPlugin(description, initialize);
00208 }
00209 
00210 void PluginManager::addPlugin(Plugin* plugin, bool initialize)
00211 {
00212   Plugin::Ptr plugin_ptr(plugin);
00213   addPlugin(plugin_ptr, initialize);
00214 }
00215 
00216 void PluginManager::addPlugin(Plugin::Ptr plugin, bool initialize)
00217 {
00218   if (!plugin)
00219   {
00220     ROS_ERROR("[PluginManager] addPlugin: Got NULL pointer as plugin. Fix it immediatly!");
00221     return;
00222   }
00223 
00224   boost::upgrade_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00225 
00226   std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.find(plugin->getName());
00227   Plugin::Ptr unique_plugin;
00228 
00229   if (itr != Instance()->plugins_by_name_.end()) // replace by name
00230   {
00231     // skip duplicated plugin
00232     if (isDescriptionMatching(plugin->getDescription(), itr->second->getDescription()))
00233     {
00234       ROS_INFO("[PluginManager] addPlugin: Plugin '%s' with type_class '%s' is already added. Skipped.", plugin->getName().c_str(), plugin->getTypeClass().c_str());
00235       return;
00236     }
00237     else
00238       ROS_INFO("[PluginManager] addPlugin: Plugin '%s' with type_class '%s' is replaced by '%s' with type_class '%s'!", itr->second->getName().c_str(), itr->second->getTypeClass().c_str(), plugin->getName().c_str(), plugin->getTypeClass().c_str());
00239   }
00240   else if (plugin->isUnique() && getUniquePluginByTypeClass(plugin->getTypeClass(), unique_plugin)) // replace due to uniqueness
00241   {
00242     ROS_INFO("[PluginManager] addPlugin: Unique plugin '%s' with type_class '%s' is replaced by '%s'!", unique_plugin->getName().c_str(), unique_plugin->getTypeClass().c_str(), plugin->getName().c_str());
00243     boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00244     Instance()->plugins_by_name_.erase(Instance()->plugins_by_name_.find(unique_plugin->getName())); // prevent outputs by removePlugin call
00245   }
00246   else
00247     ROS_INFO("[PluginManager] addPlugin: Added new plugin '%s' with type_class '%s'", plugin->getName().c_str(), plugin->getTypeClass().c_str());
00248 
00249   {
00250     boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00251     Instance()->plugins_by_name_[plugin->getName()] = plugin;
00252 
00253     plugin->setup(Instance()->nh_, ParameterManager::getActive());
00254 
00255     if (initialize && !(plugin->initialize(ParameterManager::getActive()) && plugin->postInitialize(ParameterManager::getActive())))
00256       ROS_ERROR("[PluginManager] addPlugin: Initialization of Plugin '%s' with type_class '%s' failed!", plugin->getName().c_str(), plugin->getTypeClass().c_str());
00257 
00258     Instance()->loaded_plugin_set_.clear();
00259   }
00260 
00261   // publish update
00262   Instance()->publishPluginStateUpdate();
00263 }
00264 
00265 Plugin::Ptr PluginManager::getPluginByName(const std::string& name)
00266 {
00267   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00268 
00269   std::map<std::string, Plugin::Ptr>::const_iterator itr = Instance()->plugins_by_name_.find(name);
00270   if (itr == Instance()->plugins_by_name_.end())
00271     return Plugin::Ptr();
00272 
00273   return itr->second;
00274 }
00275 
00276 bool PluginManager::getPluginByName(const std::string& name, Plugin::Ptr& plugin)
00277 {
00278   plugin = getPluginByName(name);
00279   return plugin.get() != nullptr;
00280 }
00281 
00282 bool PluginManager::getPluginsByTypeClass(const std::string& type_class, std::vector<Plugin::Ptr>& plugins)
00283 {
00284   plugins.clear();
00285 
00286   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00287 
00288   for (std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00289   {
00290     if (itr->second->getTypeClass() == type_class)
00291       plugins.push_back(itr->second);
00292   }
00293 
00294   return !plugins.empty();
00295 }
00296 
00297 bool PluginManager::getUniquePluginByTypeClass(const std::string& type_class, Plugin::Ptr& plugin)
00298 {
00299   plugin.reset();
00300 
00301   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00302 
00303   for (std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00304   {
00305     if (itr->second->isUnique() && itr->second->getTypeClass() == type_class)
00306     {
00307       plugin = itr->second;
00308       return true;
00309     }
00310   }
00311 
00312   return false;
00313 }
00314 
00315 void PluginManager::getPluginDescriptions(std::vector<msgs::PluginDescription>& descriptions, msgs::PluginDescription filter)
00316 {
00317   descriptions.clear();
00318 
00319   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00320 
00321   for (PluginLoaderBase* loader : Instance()->class_loader_)
00322   {
00323     msgs::PluginDescription description;
00324     description.base_class_package.data = loader->getBaseClassPackage();
00325     description.base_class.data = loader->getBaseClassType();
00326 
00327     if (!filter.base_class_package.data.empty() && filter.base_class_package.data != description.base_class_package.data)
00328       continue;
00329     if (!filter.base_class.data.empty() && filter.base_class.data != description.base_class.data)
00330       continue;
00331 
00332     for (std::string type_class : loader->getDeclaredClasses())
00333     {
00334       description.type_class_package.data = loader->getClassPackage(type_class);
00335       description.type_class.data = type_class;
00336 
00337       if (!filter.type_class_package.data.empty() && filter.type_class_package.data != description.type_class_package.data)
00338         continue;
00339       if (!filter.type_class.data.empty() && filter.type_class.data != description.type_class.data)
00340         continue;
00341 
00342       descriptions.push_back(description);
00343     }
00344   }
00345 }
00346 
00347 void PluginManager::getPluginStates(std::vector<msgs::PluginState>& plugin_states, msgs::PluginDescription filter)
00348 {
00349   plugin_states.clear();
00350 
00351   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00352 
00353   for (std::map<std::string, Plugin::Ptr>::const_iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00354   {
00355     const Plugin::Ptr& plugin = itr->second;
00356 
00357     msgs::PluginState state;
00358 
00359     state.header.stamp = ros::Time::now();
00360     state.description = plugin->getDescription();
00361 
00362     if (!vigir_pluginlib::isDescriptionMatching(state.description, filter))
00363       continue;
00364 
00365     plugin_states.push_back(state);
00366   }
00367 }
00368 
00369 bool PluginManager::removePlugins(const std::vector<msgs::PluginDescription>& plugin_descriptions)
00370 {
00371   bool success = true;
00372   for (const msgs::PluginDescription& description : plugin_descriptions)
00373   {
00374     if (!removePlugin(description))
00375       success = false;
00376   }
00377   return success;
00378 }
00379 
00380 bool PluginManager::removePlugin(const msgs::PluginDescription& plugin_description)
00381 {
00382   if (!plugin_description.name.data.empty())
00383     return removePluginByName(plugin_description.name.data);
00384 //  else if(!plugin_description.type_class.data.empty())
00385 //    addPlugin(plugin_description.type_class.data, plugin_description.base_class.data);
00386   else
00387     ROS_ERROR("[PluginManager] removePlugin: Call without name!");
00388 
00389   return false;
00390 }
00391 
00392 bool PluginManager::removePluginByName(const std::string& name)
00393 {
00394   boost::upgrade_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00395 
00396   std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.find(name);
00397   if (itr == Instance()->plugins_by_name_.end())
00398     return false;
00399 
00400   {
00401     ROS_INFO("[PluginManager] Removed plugin '%s' with type_class '%s'", itr->second->getName().c_str(), itr->second->getTypeClass().c_str());
00402     boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
00403     Instance()->plugins_by_name_.erase(itr);
00404 
00405     Instance()->loaded_plugin_set_.clear();
00406   }
00407 
00408   // publish update
00409   Instance()->publishPluginStateUpdate();
00410 
00411   return true;
00412 }
00413 
00414 void PluginManager::removePlugin(Plugin::Ptr& plugin)
00415 {
00416   removePluginByName(plugin->getName());
00417 }
00418 
00419 void PluginManager::removePluginsByTypeClass(const std::string& type_class)
00420 {
00421   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00422 
00423   for (std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end();)
00424   {
00425     if (itr->second->getTypeClass() == type_class)
00426     {
00427       lock.unlock();
00428       removePluginByName(itr++->first);
00429       lock.lock();
00430     }
00431     else
00432       itr++;
00433   }
00434 }
00435 
00436 bool PluginManager::loadPluginSet(const std::vector<msgs::PluginDescription>& plugin_descriptions)
00437 {
00438   bool success = true;
00439 
00440   // get list of active plugins
00441   std::vector<msgs::PluginDescription> active_plugins;
00442   {
00443     boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00444     for (std::map<std::string, Plugin::Ptr>::const_iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00445       active_plugins.push_back(itr->second->getDescription());
00446   }
00447 
00448   // remove all plugins which are not existing in list
00449   std::vector<msgs::PluginDescription> remove_plugin_list = filterDescriptionList(active_plugins, plugin_descriptions, true);
00450   if (!removePlugins(remove_plugin_list))
00451     success = false;
00452 
00453   // update param namespace of remaining plugins, if changed
00454   std::list<Plugin::Ptr> updated_plugins;
00455   std::vector<msgs::PluginDescription> updated_plugin_list = filterDescriptionList(plugin_descriptions, active_plugins);
00456   for (msgs::PluginDescription description : updated_plugin_list)
00457   {
00458     boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00459     Plugin::Ptr& plugin = Instance()->plugins_by_name_[description.name.data];
00460 
00461     plugin->updateDescription(description);
00462     plugin->setup(Instance()->nh_, ParameterManager::getActive()); // force reload parameters from param server
00463     updated_plugins.push_back(plugin);
00464   }
00465 
00466   // add new plugins
00467   std::vector<msgs::PluginDescription> add_plugin_list = filterDescriptionList(plugin_descriptions, active_plugins, true);
00468   if (!addPlugins(add_plugin_list))
00469     success = false;
00470 
00471   // reinitialize plugins which has been updated before
00472   for (Plugin::Ptr plugin : updated_plugins)
00473     success &= plugin->initialize(ParameterManager::getActive());
00474   for (Plugin::Ptr plugin : updated_plugins)
00475     success &= plugin->postInitialize(ParameterManager::getActive());
00476 
00477   return success;
00478 }
00479 
00480 bool PluginManager::loadPluginSet(const std::string& name)
00481 {
00482   std::string prefix = "plugin_sets/" + name;
00483 
00484   if (!Instance()->nh_.hasParam(prefix))
00485   {
00486     ROS_ERROR("[PluginManager] loadPluginSet: Couldn't find plugin set '%s' at parameter server.", name.c_str());
00487     return false;
00488   }
00489 
00490   if (Instance()->loaded_plugin_set_ == name)
00491   {
00492     ROS_INFO("[PluginManager] loadPluginSet: Plugin set '%s' is already loaded.", name.c_str());
00493     return true;
00494   }
00495 
00496   Instance()->loaded_plugin_set_.clear();
00497   ROS_INFO("[PluginManager] loadPluginSet: Loading plugin set '%s'...", name.c_str());
00498 
00499   // grab all plugin descriptions in the subtree
00500   std::vector<msgs::PluginDescription> plugin_descriptions;
00501   XmlRpc::XmlRpcValue val;
00502   Instance()->nh_.getParam(prefix, val);
00503 
00504   if (val.getType() != XmlRpc::XmlRpcValue::TypeStruct)
00505     return false;
00506 
00507   for (const auto& kv : val)
00508   {
00509     msgs::PluginDescription description;
00510     description.name.data = kv.first;
00511 
00512     XmlRpc::XmlRpcValue d = kv.second;
00513     if (d.getType() == XmlRpc::XmlRpcValue::TypeStruct)
00514     {
00515       if (d.hasMember("type_class_package"))
00516         description.type_class_package.data = static_cast<std::string>(d["type_class_package"]);
00517       if (d.hasMember("type_class"))
00518         description.type_class.data = static_cast<std::string>(d["type_class"]);
00519       if (d.hasMember("base_class_package"))
00520         description.base_class_package.data = static_cast<std::string>(d["base_class_package"]);
00521       if (d.hasMember("base_class"))
00522         description.base_class.data = static_cast<std::string>(d["base_class"]);
00523       description.private_param_ns.data = prefix + std::string("/") + description.name.data;
00524 
00525       std::string import_name;
00526       if (d.hasMember("import"))
00527       {
00528         import_name = static_cast<std::string>(d["import"]);
00529         // auto complete remaining empty description fields by global definition of 'imported_name' plugin if exists
00530         autocompletePluginDescriptionByName(import_name, description);
00531       }
00532     }
00533     // lookup remaining empty description fields by global definition if exists
00534     autocompletePluginDescriptionByName(description.name.data, description);
00535 
00536     plugin_descriptions.push_back(description);
00537   }
00538 
00539   if (loadPluginSet(plugin_descriptions))
00540   {
00541     Instance()->loaded_plugin_set_ = name;
00542     ROS_INFO("[PluginManager] loadPluginSet: Loaded plugin set '%s' successfully.", name.c_str());
00543     return true;
00544   }
00545   else
00546   {
00547     ROS_ERROR("[PluginManager] loadPluginSet: Loaded plugin set '%s' failed!", name.c_str());
00548     return false;
00549   }
00550 }
00551 
00552 bool PluginManager::hasPlugin(Plugin::Ptr& plugin)
00553 {
00554   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00555   std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.find(plugin->getName());
00556   return (itr != Instance()->plugins_by_name_.end() && itr->second->getTypeClass() == plugin->getTypeClass());
00557 }
00558 
00559 bool PluginManager::hasPluginByName(const std::string& name)
00560 {
00561   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00562   return Instance()->plugins_by_name_.find(name) != Instance()->plugins_by_name_.end();
00563 }
00564 
00565 bool PluginManager::hasPluginsByTypeClass(const std::string& type_class)
00566 {
00567   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00568   for (std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00569   {
00570     if (itr->second->getTypeClass() == type_class)
00571       return true;
00572   }
00573 }
00574 
00575 void PluginManager::loadParams(const vigir_generic_params::ParameterSet& params)
00576 {
00577   boost::shared_lock<boost::shared_mutex> lock(Instance()->plugins_mutex_);
00578   for (std::map<std::string, Plugin::Ptr>::iterator itr = Instance()->plugins_by_name_.begin(); itr != Instance()->plugins_by_name_.end(); itr++)
00579     itr->second->loadParams(params);
00580 }
00581 
00582 bool PluginManager::getPluginDescription(const std::string& key, msgs::PluginDescription& description)
00583 {
00584   description = msgs::PluginDescription();
00585 
00586   if (!Instance()->nh_.hasParam(key))
00587   {
00588     ROS_ERROR("[PluginManager] getPluginDescription: Couldn't retrieve plugin description at '%s' from parameter server.", key.c_str());
00589     return false;
00590   }
00591 
00592   Instance()->nh_.getParam(key + "/name", description.name.data);
00593   Instance()->nh_.getParam(key + "/type_class", description.type_class.data);
00594   Instance()->nh_.getParam(key + "/type_class_package", description.type_class_package.data);
00595   Instance()->nh_.getParam(key + "/base_class", description.base_class.data);
00596   Instance()->nh_.getParam(key + "/base_class_package", description.base_class_package.data);
00597 
00598   return !description.name.data.empty() || !description.type_class.data.empty();
00599 }
00600 
00601 void PluginManager::publishPluginStateUpdate()
00602 {
00603   msgs::PluginStates plugin_states;
00604   getPluginStates(plugin_states.states);
00605   plugin_states_pub_.publish(plugin_states);
00606 }
00607 
00608 // --- Subscriber calls ---
00609 
00610 void PluginManager::addPlugin(const msgs::PluginDescriptionConstPtr plugin_description)
00611 {
00612   addPlugin(*plugin_description);
00613 }
00614 
00615 void PluginManager::removePlugin(const msgs::PluginDescriptionConstPtr plugin_description)
00616 {
00617   removePlugin(*plugin_description);
00618 }
00619 
00620 // --- Service calls ---
00621 
00622 bool PluginManager::getPluginDescriptionsService(msgs::GetPluginDescriptionsService::Request& req, msgs::GetPluginDescriptionsService::Response& resp)
00623 {
00624   getPluginDescriptions(resp.descriptions, req.filter);
00625   return true;
00626 }
00627 
00628 bool PluginManager::getPluginStatesService(msgs::GetPluginStatesService::Request& req, msgs::GetPluginStatesService::Response& resp)
00629 {
00630   getPluginStates(resp.states, req.filter);
00631   return true;
00632 }
00633 
00634 bool PluginManager::addPluginService(msgs::PluginManagementService::Request& req, msgs::PluginManagementService::Response& /*resp*/)
00635 {
00636   return addPlugins(req.descriptions);
00637 }
00638 
00639 bool PluginManager::removePluginService(msgs::PluginManagementService::Request& req, msgs::PluginManagementService::Response& /*resp*/)
00640 {
00641   return removePlugins(req.descriptions);
00642 }
00643 
00644 bool PluginManager::loadPluginSetService(msgs::PluginManagementService::Request& req, msgs::PluginManagementService::Response& /*resp*/)
00645 {
00646   if (!req.name.data.empty())
00647     return loadPluginSet(req.name.data);
00648   else
00649     return loadPluginSet(req.descriptions);
00650 }
00651 
00652 // --- Action Server calls ---
00653 
00654 void PluginManager::getPluginDescriptionsAction(const msgs::GetPluginDescriptionsGoalConstPtr goal)
00655 {
00656   // check if new goal was preempted in the meantime
00657   if (get_plugin_descriptions_as_->isPreemptRequested())
00658   {
00659     get_plugin_descriptions_as_->setPreempted();
00660     return;
00661   }
00662 
00663   msgs::GetPluginDescriptionsResult result;
00664   getPluginDescriptions(result.descriptions, goal->filter);
00665 
00666   get_plugin_descriptions_as_->setSucceeded(result);
00667 }
00668 
00669 void PluginManager::getPluginStatesAction(const msgs::GetPluginStatesGoalConstPtr goal)
00670 {
00671   // check if new goal was preempted in the meantime
00672   if (get_plugin_states_as_->isPreemptRequested())
00673   {
00674     get_plugin_states_as_->setPreempted();
00675     return;
00676   }
00677 
00678   msgs::GetPluginStatesResult result;
00679   getPluginStates(result.states, goal->filter);
00680 
00681   get_plugin_states_as_->setSucceeded(result);
00682 }
00683 
00684 void PluginManager::addPluginAction(const msgs::PluginManagementGoalConstPtr goal)
00685 {
00686   // check if new goal was preempted in the meantime
00687   if (add_plugin_as_->isPreemptRequested())
00688   {
00689     add_plugin_as_->setPreempted();
00690     return;
00691   }
00692 
00693   msgs::PluginManagementResult result;
00694   result.success.data = addPlugins(goal->descriptions);
00695 
00696   if (result.success.data)
00697     add_plugin_as_->setSucceeded(result);
00698   else
00699     add_plugin_as_->setAborted(result);
00700 }
00701 
00702 void PluginManager::removePluginAction(const msgs::PluginManagementGoalConstPtr goal)
00703 {
00704   // check if new goal was preempted in the meantime
00705   if (remove_plugin_as_->isPreemptRequested())
00706   {
00707     remove_plugin_as_->setPreempted();
00708     return;
00709   }
00710 
00711   msgs::PluginManagementResult result;
00712   result.success.data = removePlugins(goal->descriptions);
00713 
00714   if (result.success.data)
00715     remove_plugin_as_->setSucceeded(result);
00716   else
00717     remove_plugin_as_->setAborted(result);
00718 }
00719 
00720 void PluginManager::loadPluginSetAction(const msgs::PluginManagementGoalConstPtr goal)
00721 {
00722   // check if new goal was preempted in the meantime
00723   if (load_plugin_set_as_->isPreemptRequested())
00724   {
00725     load_plugin_set_as_->setPreempted();
00726     return;
00727   }
00728 
00729   msgs::PluginManagementResult result;
00730   if (!goal->name.data.empty())
00731     result.success.data = loadPluginSet(goal->name.data);
00732   else
00733     result.success.data = loadPluginSet(goal->descriptions);
00734 
00735   if (result.success.data)
00736     load_plugin_set_as_->setSucceeded(result);
00737   else
00738     load_plugin_set_as_->setAborted(result);
00739 }
00740 } // namespace


vigir_pluginlib
Author(s): Alexander Stumpf
autogenerated on Tue Sep 13 2016 03:47:30