topic_service_client.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2018, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 #ifndef SWRI_ROSCPP_TOPIC_SERVICE_CLIENT_H_
30 #define SWRI_ROSCPP_TOPIC_SERVICE_CLIENT_H_
31 
32 #include <ros/node_handle.h>
33 
34 #include <boost/thread/mutex.hpp>
35 #include <boost/uuid/random_generator.hpp>
36 #include <boost/uuid/uuid_io.hpp>
37 
38 #include <map>
39 
40 namespace swri
41 {
42 template<class MReq, class MRes>
44 {
45 private:
46  typedef
47  boost::mutex request_lock_;
51 
53  std::string name_;
54  std::string service_name_;
55 
56  int sequence_;
57 
58 public:
59  TopicServiceClientRaw() : sequence_(0), timeout_(ros::Duration(4.0))
60  {
61 
62  }
63 
65  const std::string &service,
66  const std::string &client_name = "")
67  {
68  //Converts using string stream instead of to_string so non C++ 11 nodes won't fail
69  boost::uuids::random_generator gen;
70  boost::uuids::uuid u = gen();
71  std::string random_str = boost::uuids::to_string(u);
72  name_ = client_name.length() ? client_name : (ros::this_node::getName() + random_str);
73  std::string rservice = nh.resolveName(service);
74  service_name_ = rservice;
75 
76  request_pub_ = nh.advertise<MReq>(rservice + "/request", 10);
77  response_sub_ = nh.subscribe(rservice + "/response", 10, &TopicServiceClientRaw<MReq, MRes>::response_callback, this);
78  }
79 
80  bool call(MReq& request, MRes& response)
81  {
82  boost::mutex::scoped_lock scoped_lock(request_lock_);
83 
84  // block for response
85  request.srv_header.stamp = ros::Time::now();
86  request.srv_header.sequence = sequence_;
87  request.srv_header.sender = name_;
88 
89  // Wait until we get a subscriber and publisher
90  while (request_pub_.getNumSubscribers() == 0 || response_sub_.getNumPublishers() == 0)
91  {
92  ros::Duration(0.002).sleep();
93  ros::spinOnce();
94 
95  if (ros::Time::now() - request.srv_header.stamp > timeout_)
96  {
97  ROS_ERROR("Topic service timeout exceeded");
98  return false;
99  }
100  }
101  response_.reset();
102  request_pub_.publish(request);
103 
104  // Wait until we get a response
105  while (!response_ && ros::Time::now() - request.srv_header.stamp < timeout_)
106  {
107  ros::Duration(0.002).sleep();
108  ros::spinOnce();
109  }
110 
111  sequence_++;
112  if (response_)
113  {
114  response = *response_;
115  response_.reset();
116  return response.srv_header.result;
117  }
118  return false;
119  }
120 
121  std::string getService()
122  {
123  return service_name_;
124  }
125 
126  bool exists()
127  {
128  return request_pub_.getNumSubscribers() > 0 && response_sub_.getNumPublishers() > 0;
129  }
130 
131  // The service server can output a console log message when the
132  // service is called if desired.
133  void setLogCalls(bool enable);
134  bool logCalls() const;
135 private:
136 
138  {
139  ROS_DEBUG("Got response for %s with sequence %i",
140  message->srv_header.sender.c_str(), message->srv_header.sequence);
141 
142  if (message->srv_header.sender != name_)
143  {
144  ROS_DEBUG("Got response from another client, ignoring..");
145  return;
146  }
147 
148  if (message->srv_header.sequence != sequence_)
149  {
150  ROS_WARN("Got wrong sequence number, ignoring..");
151  ROS_DEBUG("message seq:%i vs current seq: %i", message->srv_header.sequence, sequence_);
152  return;
153  }
154 
155  response_ = message;
156  }
157 }; // class TopicServiceClientRaw
158 
159 template<class MReq>
160 class TopicServiceClient: public TopicServiceClientRaw<typename MReq:: Request, typename MReq:: Response>
161 {
162 public:
163  bool call(MReq& req)
164  {
166  }
167 
168 }; // class TopicServiceClient
169 
170 
171 } // namespace swri
172 #endif // SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_
void publish(const boost::shared_ptr< M > &message) const
void response_callback(const boost::shared_ptr< MRes > &message)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool call(MReq &request, MRes &response)
std::string resolveName(const std::string &name, bool remap=true) const
bool sleep() const
ROSCPP_DECL const std::string & getName()
#define ROS_WARN(...)
boost::shared_ptr< MRes > response_
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
uint32_t getNumPublishers() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint32_t getNumSubscribers() const
void setLogCalls(bool enable)
static Time now()
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


swri_roscpp
Author(s):
autogenerated on Tue Apr 6 2021 02:50:35