service_client.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * Copyright (C) 2009, Willow Garage, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef ROSCPP_SERVICE_CLIENT_H
30 #define ROSCPP_SERVICE_CLIENT_H
31 
32 #include "ros/forwards.h"
33 #include "ros/common.h"
34 #include "ros/service_traits.h"
35 #include "ros/serialization.h"
36 
37 namespace roswrap
38 {
39 
44 {
45 public:
47  ServiceClient(const std::string& service_name, bool persistent, const M_string& header_values, const std::string& service_md5sum);
48  ServiceClient(const ServiceClient& rhs);
49  ~ServiceClient();
50 
55  template<class MReq, class MRes>
56  bool call(MReq& req, MRes& res)
57  {
58  namespace st = service_traits;
59 
60  if (!isValid())
61  {
62  return false;
63  }
64 
65  if (strcmp(st::md5sum(req), st::md5sum(res)))
66  {
67  ROS_ERROR("The request and response parameters to the service "
68  "call must be autogenerated from the same "
69  "server definition file (.srv). your service call "
70  "for %s appeared to use request/response types "
71  "from different .srv files. (%s vs. %s)", impl_->name_.c_str(), st::md5sum(req), st::md5sum(res));
72  return false;
73  }
74 
75  return call(req, res, st::md5sum(req));
76  }
77 
81  template<class Service>
82  bool call(Service& service)
83  {
84  namespace st = service_traits;
85 
86  if (!isValid())
87  {
88  return false;
89  }
90 
91  return call(service.request, service.response, st::md5sum(service));
92  }
93 
97  template<typename MReq, typename MRes>
98  bool call(const MReq& req, MRes& resp, const std::string& service_md5sum)
99  {
100  namespace ser = serialization;
101  SerializedMessage ser_req = ser::serializeMessage(req);
102  SerializedMessage ser_resp;
103  bool ok = call(ser_req, ser_resp, service_md5sum);
104  if (!ok)
105  {
106  return false;
107  }
108 
109  try
110  {
111  ser::deserializeMessage(ser_resp, resp);
112  }
113  catch (std::exception& e)
114  {
115  deserializeFailed(e);
116  return false;
117  }
118 
119  return true;
120  }
121 
122  bool call(const SerializedMessage& req, SerializedMessage& resp, const std::string& service_md5sum);
123 
128  bool isValid() const;
129 
133  bool isPersistent() const;
134 
143  void shutdown();
144 
151  bool waitForExistence(ros::Duration timeout = ros::Duration(-1));
152 
157  bool exists();
158 
162  std::string getService();
163 
164  operator void*() const { return isValid() ? (void*)1 : (void*)0; }
165  bool operator<(const ServiceClient& rhs) const
166  {
167  return impl_ < rhs.impl_;
168  }
169 
170  bool operator==(const ServiceClient& rhs) const
171  {
172  return impl_ == rhs.impl_;
173  }
174 
175  bool operator!=(const ServiceClient& rhs) const
176  {
177  return impl_ != rhs.impl_;
178  }
179 
180 private:
181  // This works around a problem with the OSX linker that causes the static variable declared by
182  // ROS_ERROR to error with missing symbols when it's used directly in the templated call() method above
183  // This for some reason only showed up in the rxtools package
184  void deserializeFailed(const std::exception& e)
185  {
186  ROS_ERROR("Exception thrown while while deserializing service call: %s", e.what());
187  }
188 
189  struct Impl
190  {
191  Impl();
192  ~Impl();
193 
194  void shutdown();
195  bool isValid() const;
196 
198  std::string name_;
201  std::string service_md5sum_;
203  };
204  typedef std::shared_ptr<Impl> ImplPtr;
205  typedef std::weak_ptr<Impl> ImplWPtr;
206 
208 
209  friend class NodeHandle;
210  friend class NodeHandleBackingCollection;
211 };
212 typedef std::shared_ptr<ServiceClient> ServiceClientPtr;
213 
214 }
215 
216 #endif
roswrap::message_traits::md5sum
const char * md5sum()
returns MD5Sum<M>::value();
Definition: message_traits.h:227
roswrap::ServiceClient::ImplPtr
std::shared_ptr< Impl > ImplPtr
Definition: service_client.h:204
roswrap::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
exists
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
Checks if a service is both advertised and available.
roswrap::M_string
std::map< std::string, std::string > M_string
Definition: datatypes.h:46
roswrap::ServiceClient::Impl::server_link_
ServiceServerLinkPtr server_link_
Definition: service_client.h:197
roswrap::shutdown
ROSCPP_DECL void shutdown()
Disconnects everything and unregisters from the master. It is generally not necessary to call this fu...
Definition: rossimu.cpp:269
roswrap::ServiceClient::Impl::name_
std::string name_
Definition: service_client.h:198
roswrap::ServiceClient::call
bool call(const MReq &req, MRes &resp, const std::string &service_md5sum)
Mostly for internal use, the other templated versions of call() just call into this one.
Definition: service_client.h:98
roswrap::ServiceClient::Impl::service_md5sum_
std::string service_md5sum_
Definition: service_client.h:201
roswrap::ServiceClient::ServiceClient
ServiceClient()
Definition: service_client.h:46
roswrap::ServiceClient::Impl::header_values_
M_string header_values_
Definition: service_client.h:200
roswrap::SerializedMessage
Definition: serialized_message.h:40
roswrap::ServiceClient
Provides a handle-based interface to service client connections.
Definition: service_client.h:43
roswrap::ServiceClient::operator==
bool operator==(const ServiceClient &rhs) const
Definition: service_client.h:170
roswrap::ServiceClient::operator<
bool operator<(const ServiceClient &rhs) const
Definition: service_client.h:165
roswrap
Definition: param_modi.cpp:41
roswrap::ServiceClient::ImplWPtr
std::weak_ptr< Impl > ImplWPtr
Definition: service_client.h:205
roswrap::ServiceClient::impl_
ImplPtr impl_
Definition: service_client.h:207
roswrap::NodeHandle
roscpp's interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
ROS_ERROR
#define ROS_ERROR(...)
Definition: sick_scan_logging.h:127
roswrap::ServiceServerLinkPtr
std::shared_ptr< ServiceServerLink > ServiceServerLinkPtr
Definition: forwards.h:87
roswrap::ServiceClient::Impl::is_shutdown_
bool is_shutdown_
Definition: service_client.h:202
sick_scan_base.h
roswrap::ServiceClient::Impl
Definition: service_client.h:189
sick_generic_device_finder.timeout
timeout
Definition: sick_generic_device_finder.py:113
roswrap::ServiceClient::deserializeFailed
void deserializeFailed(const std::exception &e)
Definition: service_client.h:184
roswrap::ServiceClient::operator!=
bool operator!=(const ServiceClient &rhs) const
Definition: service_client.h:175
ROSCPP_DECL
#define ROSCPP_DECL
Definition: roswrap/src/cfgsimu/sick_scan/ros/common.h:63
roswrap::ok
ROSCPP_DECL bool ok()
Check whether it's time to exit.
Definition: rossimu.cpp:273
roswrap::service::call
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:66
ros::Duration
roswrap::ServiceClientPtr
std::shared_ptr< ServiceClient > ServiceClientPtr
Definition: service_client.h:212
roswrap::ServiceClient::Impl::persistent_
bool persistent_
Definition: service_client.h:199
roswrap::ServiceClient::call
bool call(Service &service)
Call the service aliased by this handle with the specified service request/response.
Definition: service_client.h:82


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:10