rapid
A ROS robotics library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
service_client.h
Go to the documentation of this file.
1 #ifndef _RAPID_ROS_SERVICE_CLIENT_H_
2 #define _RAPID_ROS_SERVICE_CLIENT_H_
3 
4 #include "ros/service_client.h"
5 
6 namespace rapid_ros {
7 // DECLARATIONS ---------------------------------------------------------------
8 template <class Service>
10  public:
12  virtual bool call(Service& srv) = 0;
13  virtual bool call(typename Service::Request& req,
14  typename Service::Response& res) = 0;
15 };
16 
17 // Wraps a real ros::ServiceClient.
18 template <class Service>
19 class ServiceClient : public ServiceClientInterface<Service> {
20  public:
21  explicit ServiceClient(const ros::ServiceClient& client);
22  virtual bool call(Service& srv);
23  virtual bool call(typename Service::Request& req,
24  typename Service::Response& res);
25 
26  private:
27  ServiceClient();
28  ros::ServiceClient client_;
29 };
30 
31 // A mock service client.
32 //
33 // Currently only works to test methods that call the service exactly once.
34 // A TODO would be to set different responses for different requests.
35 //
36 // All calls are successes unless set_success(false) is called.
37 template <class Service>
38 class MockServiceClient : public ServiceClientInterface<Service> {
39  public:
41  virtual bool call(Service& srv);
42  virtual bool call(typename Service::Request& req,
43  typename Service::Response& res);
44 
45  // Mock methods.
46  void set_success(bool success);
47  void set_response(const typename Service::Response& response);
48 
49  private:
50  bool success_;
51  typename Service::Response response_;
52 };
53 
54 // DEFINITIONS ----------------------------------------------------------------
55 // An interface for service clients.
56 template <class Service>
57 ServiceClient<Service>::ServiceClient(const ros::ServiceClient& client)
58  : client_(client) {}
59 
60 template <class Service>
61 bool ServiceClient<Service>::call(Service& srv) {
62  return client_.call(srv);
63 }
64 
65 template <class Service>
66 bool ServiceClient<Service>::call(typename Service::Request& req,
67  typename Service::Response& res) {
68  return client_.call(req, res);
69 }
70 
71 template <class Service>
73  : success_(true), response_() {}
74 
75 template <class Service>
77  return call(srv.request, srv.response);
78  // if (!success_) {
79  // return false;
80  //}
81  // srv.response = response_;
82  // return true;
83 }
84 
85 template <class Service>
86 bool MockServiceClient<Service>::call(typename Service::Request& req,
87  typename Service::Response& res) {
88  if (!success_) {
89  return false;
90  }
91  res = response_;
92  return true;
93 }
94 
95 template <class Service>
97  success_ = success;
98 }
99 
100 template <class Service>
102  const typename Service::Response& response) {
103  response_ = response;
104 }
105 } // namespace rapid_ros
106 
107 #endif // _RAPID_ROS_SERVICE_CLIENT_H_
void set_success(bool success)
virtual bool call(Service &srv)
virtual bool call(Service &srv)
void set_response(const typename Service::Response &response)
virtual bool call(Service &srv)=0