service.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008, Morgan Quigley and Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ROSCPP_SERVICE_H
29 #define ROSCPP_SERVICE_H
30 
31 #include <string>
32 #include "ros/common.h"
33 #include "ros/message.h"
34 #include "ros/forwards.h"
35 #include "ros/node_handle.h"
36 #include "ros/service_traits.h"
37 #include "ros/names.h"
38 
39 #include <boost/shared_ptr.hpp>
40 
41 namespace ros
42 {
43 
44 class ServiceServerLink;
46 
50 namespace service
51 {
52 
64 template<class MReq, class MRes>
65 bool call(const std::string& service_name, MReq& req, MRes& res)
66 {
67  namespace st = service_traits;
68  NodeHandle nh;
69  ServiceClientOptions ops(ros::names::resolve(service_name), st::md5sum(req), false, M_string());
70  ServiceClient client = nh.serviceClient(ops);
71  return client.call(req, res);
72 }
73 
84 template<class Service>
85 bool call(const std::string& service_name, Service& service)
86 {
87  namespace st = service_traits;
88 
89  NodeHandle nh;
90  ServiceClientOptions ops(ros::names::resolve(service_name), st::md5sum(service), false, M_string());
91  ServiceClient client = nh.serviceClient(ops);
92  return client.call(service.request, service.response);
93 }
94 
103 ROSCPP_DECL bool waitForService(const std::string& service_name, int32_t timeout);
104 
112 ROSCPP_DECL bool waitForService(const std::string& service_name, ros::Duration timeout = ros::Duration(-1));
113 
121 ROSCPP_DECL bool exists(const std::string& service_name, bool print_failure_reason);
122 
133 template<class MReq, class MRes>
134 ServiceClient createClient(const std::string& service_name, bool persistent = false, const M_string& header_values = M_string())
135 {
136  NodeHandle nh;
137  ServiceClient client = nh.template serviceClient<MReq, MRes>(ros::names::resolve(service_name), persistent, header_values);
138  return client;
139 }
140 
151 template<class Service>
152 ServiceClient createClient(const std::string& service_name, bool persistent = false, const M_string& header_values = M_string())
153 {
154  NodeHandle nh;
155  ServiceClient client = nh.template serviceClient<Service>(ros::names::resolve(service_name), persistent, header_values);
156  return client;
157 }
158 
159 }
160 
161 }
162 
163 #endif // ROSCPP_SERVICE_H
164 
ros::service::exists
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
Checks if a service is both advertised and available.
Definition: service.cpp:41
ros::service::call
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:65
boost::shared_ptr< ServiceServerLink >
forwards.h
ros
ros::service::createClient
ServiceClient createClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
Create a client for a service.
Definition: service.h:134
ros::NodeHandle::serviceClient
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
Create a client for a service, version templated on two message types.
Definition: node_handle.h:1240
ros::names::resolve
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
Resolve a graph resource name into a fully qualified graph resource name.
Definition: names.cpp:136
message.h
ros::ServiceClientOptions
Encapsulates all options available for creating a ServiceClient.
Definition: service_client_options.h:41
ros::ServiceServerLinkPtr
boost::shared_ptr< ServiceServerLink > ServiceServerLinkPtr
Definition: forwards.h:85
ros::ServiceClient
Provides a handle-based interface to service client connections.
Definition: service_client.h:42
ros::ServiceClient::call
bool call(MReq &req, MRes &res)
Call the service aliased by this handle with the specified request/response messages.
Definition: service_client.h:56
ros::NodeHandle
roscpp's interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
ros::service::waitForService
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)
Wait for a service to be advertised and available. Blocks until it is.
Definition: service.cpp:129
names.h
ros::Duration
node_handle.h
ros::M_string
std::map< std::string, std::string > M_string
service_traits.h


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:44