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 
102 ROSCPP_DECL bool waitForService(const std::string& service_name, int32_t timeout);
103 
111 ROSCPP_DECL bool waitForService(const std::string& service_name, ros::Duration timeout = ros::Duration(-1));
112 
120 ROSCPP_DECL bool exists(const std::string& service_name, bool print_failure_reason);
121 
132 template<class MReq, class MRes>
133 ServiceClient createClient(const std::string& service_name, bool persistent = false, const M_string& header_values = M_string())
134 {
135  NodeHandle nh;
136  ServiceClient client = nh.template serviceClient<MReq, MRes>(ros::names::resolve(service_name), persistent, header_values);
137  return client;
138 }
139 
150 template<class Service>
151 ServiceClient createClient(const std::string& service_name, bool persistent = false, const M_string& header_values = M_string())
152 {
153  NodeHandle nh;
154  ServiceClient client = nh.template serviceClient<Service>(ros::names::resolve(service_name), persistent, header_values);
155  return client;
156 }
157 
158 }
159 
160 }
161 
162 #endif // ROSCPP_SERVICE_H
163 
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
Encapsulates all options available for creating a ServiceClient.
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:65
bool call(MReq &req, MRes &res)
Call the service aliased by this handle with the specified request/response messages.
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
std::map< std::string, std::string > M_string
roscpp&#39;s interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
boost::shared_ptr< ServiceServerLink > ServiceServerLinkPtr
Definition: forwards.h:85
Provides a handle-based interface to service client connections.
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
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:128
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:133


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54