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 #include <ros/this_node.h>
34 
35 #include <boost/thread/mutex.hpp>
36 #include <boost/uuid/random_generator.hpp>
37 #include <boost/uuid/uuid_io.hpp>
38 
39 #include <map>
40 
41 namespace swri
42 {
43 template<class MReq, class MRes>
45 {
46 public:
47  typedef
48  boost::mutex request_lock_;
52 
54  std::string name_;
55  std::string service_name_;
56 
57  int sequence_;
58 
59  TopicServiceClientImpl() : 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, &TopicServiceClientImpl<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 private:
122 
124  {
125  ROS_DEBUG("Got response for %s with sequence %i",
126  message->srv_header.sender.c_str(), message->srv_header.sequence);
127 
128  if (message->srv_header.sender != name_)
129  {
130  ROS_DEBUG("Got response from another client, ignoring..");
131  return;
132  }
133 
134  if (message->srv_header.sequence != sequence_)
135  {
136  ROS_WARN("Got wrong sequence number, ignoring..");
137  ROS_DEBUG("message seq:%i vs current seq: %i", message->srv_header.sequence, sequence_);
138  return;
139  }
140 
141  response_ = message;
142  }
143 }; // class TopicServiceClientImpl
144 
145 template<class MReq>
147 {
149 
150 public:
151 
153  const std::string &service,
154  const std::string &client_name = "")
155  {
158 
159  impl_->initialize(nh, service, client_name);
160  }
161 
162  std::string getService()
163  {
164  return impl_->service_name_;
165  }
166 
167  bool exists()
168  {
169  return impl_->request_pub_.getNumSubscribers() > 0 && impl_->response_sub_.getNumPublishers() > 0;
170  }
171 
172  bool call(MReq& req)
173  {
174  return impl_->call(req.request, req.response);
175  }
176 }; // class TopicServiceClient
177 
178 
179 } // namespace swri
180 #endif // SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
boost::shared_ptr< MRes > response_
uint32_t getNumPublishers() const
ROSCPP_DECL const std::string & getName()
boost::shared_ptr< TopicServiceClientImpl< typename MReq::Request, typename MReq::Response > > impl_
#define ROS_WARN(...)
void publish(const boost::shared_ptr< M > &message) const
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
void response_callback(const boost::shared_ptr< MRes > &message)
std::string resolveName(const std::string &name, bool remap=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static Time now()
bool sleep() const
uint32_t getNumSubscribers() const
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
bool call(MReq &request, MRes &response)
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
#define ROS_DEBUG(...)


swri_roscpp
Author(s):
autogenerated on Sat Jan 21 2023 03:13:16