rtt_rosservice_registry_service.cpp
Go to the documentation of this file.
00001 
00002 #include <rtt/RTT.hpp>
00003 #include <rtt/plugin/ServicePlugin.hpp>
00004 #include <rtt/internal/GlobalService.hpp>
00005 
00006 #include <rtt_roscomm/rtt_rosservice_registry_service.h>
00007 #include <rtt_roscomm/rtt_rosservice_proxy.h>
00008 
00009 ROSServiceRegistryServicePtr ROSServiceRegistryService::s_instance_;
00010 
00011 ROSServiceRegistryServicePtr ROSServiceRegistryService::Instance()
00012 {
00013   if (!s_instance_) {
00014     s_instance_.reset(new ROSServiceRegistryService(0));
00015     RTT::internal::GlobalService::Instance()->addService(s_instance_);
00016   }
00017   return s_instance_;
00018 }
00019 
00020 void ROSServiceRegistryService::Release()
00021 {
00022   s_instance_.reset();
00023 }
00024 
00025 ROSServiceRegistryService::ROSServiceRegistryService(RTT::TaskContext* owner)
00026   : Service("rosservice_registry", owner)
00027 {
00028   this->doc("Global RTT Service for registering ROS service types.");
00029   this->addOperation("registerServiceFactory", &ROSServiceRegistryService::registerServiceFactory, this, RTT::ClientThread);
00030   this->addOperation("hasServiceFactory", &ROSServiceRegistryService::hasServiceFactory, this, RTT::ClientThread);
00031   this->addOperation("getServiceFactory", &ROSServiceRegistryService::getServiceFactory, this, RTT::ClientThread);
00032   this->addOperation("listSrvs", &ROSServiceRegistryService::listSrvs, this, RTT::ClientThread);
00033 }
00034 
00040 bool ROSServiceRegistryService::registerServiceFactory(ROSServiceProxyFactoryBase* factory)
00041 {
00042   RTT::os::MutexLock lock(factory_lock_);
00043   if(factory == NULL) {
00044     RTT::log(RTT::Error) << "Failed to register ROS service factory: NULL pointer given." << RTT::endlog();
00045     return false;
00046   }
00047 
00048   const std::string &ros_service_type = factory->getType();
00049 
00050   // Check if the factory type has yet to be registered
00051   if(factories_.find(ros_service_type) == factories_.end()) {
00052     // Store a new factory
00053     factories_[ros_service_type] = boost::shared_ptr<ROSServiceProxyFactoryBase>(factory);
00054   } else {
00055     // Reset the existing factory
00056     factories_[ros_service_type].reset(factory);
00057   }
00058 
00059   RTT::log(RTT::Info) << "Successfully registered ROS service factory for \"" << ros_service_type << "\"." << RTT::endlog();
00060 
00061   return true;
00062 }
00063 
00064 bool ROSServiceRegistryService::hasServiceFactory(const std::string &service_type)
00065 {
00066   RTT::os::MutexLock lock(factory_lock_);
00067   return factories_.find(service_type) != factories_.end();
00068 }
00069 
00070 ROSServiceProxyFactoryBase* ROSServiceRegistryService::getServiceFactory(const std::string &service_type)
00071 {
00072   RTT::os::MutexLock lock(factory_lock_);
00073   if(factories_.find(service_type) != factories_.end()) {
00074     return factories_[service_type].get();
00075   }
00076 
00077   RTT::log(RTT::Error)<<"Service type \""<<service_type<<"\" has not been registered with the rosservice_registry service."<<RTT::endlog();
00078 
00079   return NULL;
00080 }
00081 
00082 void ROSServiceRegistryService::listSrvs()
00083 {
00084   RTT::os::MutexLock lock(factory_lock_);
00085 
00086   RTT::log(RTT::Info) << "Available ROS .srv types:" << RTT::endlog();
00087   for(std::map<std::string, boost::shared_ptr<ROSServiceProxyFactoryBase> >::const_iterator it = factories_.begin();
00088       it != factories_.end();
00089       ++it)
00090   {
00091     RTT::log(RTT::Info) << " -- " << it->first << RTT::endlog();
00092   }
00093 }
00094 
00095 std::map<std::string, boost::shared_ptr<ROSServiceProxyFactoryBase> > ROSServiceRegistryService::factories_;
00096 RTT::os::MutexRecursive ROSServiceRegistryService::factory_lock_;
00097 
00098 void loadROSServiceRegistryService()
00099 {
00100   ROSServiceRegistryService::Instance();
00101 }
00102 
00103 using namespace RTT;
00104 extern "C" {
00105   bool loadRTTPlugin(RTT::TaskContext* c){
00106     if (c != 0) return false;
00107     loadROSServiceRegistryService();
00108     return true;
00109   }
00110   std::string getRTTPluginName (){
00111     return "rosservice_registry";
00112   }
00113   std::string getRTTTargetName (){
00114     return OROCOS_TARGET_NAME;
00115   }
00116 }


rtt_roscomm
Author(s): Ruben Smits, Jonathan Bohren
autogenerated on Mon Apr 3 2017 03:34:43