1 #include <boost/algorithm/string.hpp> 26 this->doc(
"RTT Service for connecting the operations of "+owner->
getName()+
" to ROS service clients and servers.");
30 .
doc(
"Connects an RTT operation or operation caller to an associated ROS service server or client.")
31 .arg(
"operation_name",
"The RTT operation name (like \"some_provided_service.another.operation\").")
32 .arg(
"service_name",
"The ROS service name (like \"/my_robot/ns/some_service\").")
33 .arg(
"service_type",
"The ROS service type (like \"std_srvs/Empty\").");
35 .doc(
"Disconnects an RTT operation or operation caller from an associated ROS service server or client.")
36 .arg(
"service_name",
"The ROS service name (like \"/my_robot/ns/some_service\").");
38 .doc(
"Disconnects all RTT operations and operation callers from associated ROS service servers or clients.");
42 has_service_factory = rosservice_registry_->getOperation(
"hasServiceFactory");
43 get_service_factory = rosservice_registry_->getOperation(
"getServiceFactory");
55 std::vector<std::string> rtt_uri_tokens;
56 boost::split(rtt_uri_tokens, rtt_uri, boost::is_any_of(
"."));
59 if(rtt_uri_tokens.size() < 1) {
65 for(std::vector<std::string>::iterator it = rtt_uri_tokens.begin();
66 it+1 != rtt_uri_tokens.end();
69 if(required->requiresService(*it)) {
70 required = required->requires(*it);
77 return required->getOperationCaller(rtt_uri_tokens.back());
84 std::vector<std::string> rtt_uri_tokens;
85 boost::split(rtt_uri_tokens, rtt_uri, boost::is_any_of(
"."));
88 if(rtt_uri_tokens.size() < 1) {
94 for(std::vector<std::string>::iterator it = rtt_uri_tokens.begin();
95 it+1 != rtt_uri_tokens.end();
98 if(provided->hasService(*it)) {
99 provided = provided->provides(*it);
106 return provided->getOperation(rtt_uri_tokens.back());
113 const std::string &rtt_operation_name,
114 const std::string &ros_service_name,
115 const std::string &ros_service_type)
118 if(!this->has_service_factory(ros_service_type)) {
125 operation_caller = this->get_owner_operation_caller(rtt_operation_name);
127 if(operation_caller) {
129 if(client_proxies_.find(ros_service_name) == client_proxies_.end()) {
131 client_proxies_[ros_service_name] =
132 get_service_factory(ros_service_type)->create_client_proxy(ros_service_name);
136 if (!client_proxies_[ros_service_name]->connect(this->getOwner(), operation_caller)) {
137 RTT::log(
RTT::Error) <<
"Could not connect OperationCaller '" << rtt_operation_name <<
"' to ROS service client '" << ros_service_name <<
"'" <<
RTT::endlog();
145 operation = this->get_owner_operation(rtt_operation_name);
149 if(server_proxies_.find(ros_service_name) == server_proxies_.end()) {
151 server_proxies_[ros_service_name] =
152 get_service_factory(ros_service_type)->create_server_proxy(ros_service_name);
156 if (!server_proxies_[ros_service_name]->connect(this->getOwner(), operation)) {
157 RTT::log(
RTT::Error) <<
"Could not connect Operation '" << rtt_operation_name <<
"' to ROS service server '" << ros_service_name <<
"'" <<
RTT::endlog();
163 RTT::log(
RTT::Error) <<
"No such Operation or OperationCaller '" << rtt_operation_name <<
"' in '" << getOwner()->getName() <<
"'" <<
RTT::endlog();
172 std::map<std::string, ROSServiceServerProxyBase*>::iterator iter_s
173 = server_proxies_.find(ros_service_name);
174 if (iter_s != server_proxies_.end()) {
175 delete iter_s->second;
176 server_proxies_.erase(iter_s);
180 std::map<std::string, ROSServiceClientProxyBase*>::iterator iter_c
181 = client_proxies_.find(ros_service_name);
182 if (iter_c != client_proxies_.end())
184 delete iter_c->second;
185 client_proxies_.erase(iter_c);
195 std::map<std::string, ROSServiceServerProxyBase*>::iterator iter_s;
196 for(iter_s = server_proxies_.begin(); iter_s != server_proxies_.end(); iter_s = server_proxies_.begin())
198 delete iter_s->second;
199 server_proxies_.erase(iter_s);
202 std::map<std::string, ROSServiceClientProxyBase*>::iterator iter_c;
203 for(iter_c = client_proxies_.begin(); iter_c != client_proxies_.end(); iter_c = client_proxies_.begin())
205 delete iter_c->second;
206 client_proxies_.erase(iter_c);
std::map< std::string, ROSServiceServerProxyBase * > server_proxies_
RTT::Service::shared_ptr rosservice_registry_
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
RTT::OperationInterfacePart * get_owner_operation(const std::string rtt_uri)
Get an RTT operation from a string identifier.
RTT::OperationCaller< ROSServiceProxyFactoryBase *(const std::string &)> get_service_factory
static ROSServiceRegistryServicePtr Instance()
RTT::OperationCaller< bool(const std::string &)> has_service_factory
const std::string & doc() const
ROSServiceService(TaskContext *owner)
bool connect(const std::string &rtt_operation_name, const std::string &ros_service_name, const std::string &ros_service_type)
Connect an RTT operation or operation caller to a ROS service server or service client.
std::map< std::string, ROSServiceClientProxyBase * > client_proxies_
RTT::base::OperationCallerBaseInvoker * get_owner_operation_caller(const std::string rtt_uri)
Get an RTT operation caller from a string identifier.
bool disconnect(const std::string &ros_service_name)
static Logger::LogFunction endlog()
virtual const std::string & getName() const