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
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_
response
const std::string response
node_handle.h
ros::Publisher
boost::shared_ptr< MRes >
swri::TopicServiceClientImpl::request_pub_
ros::Publisher request_pub_
Definition: topic_service_client.h:50
ros
swri::TopicServiceClient::call
bool call(MReq &req)
Definition: topic_service_client.h:172
swri::TopicServiceClientImpl
Definition: topic_service_client.h:44
ros::spinOnce
ROSCPP_DECL void spinOnce()
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
swri::TopicServiceClient::exists
bool exists()
Definition: topic_service_client.h:167
ros::Subscriber::getNumPublishers
uint32_t getNumPublishers() const
swri::TopicServiceClientImpl::TopicServiceClientImpl
TopicServiceClientImpl()
Definition: topic_service_client.h:59
swri::TopicServiceClientImpl::name_
std::string name_
Definition: topic_service_client.h:54
swri::TopicServiceClientImpl::initialize
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
Definition: topic_service_client.h:64
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
ROS_DEBUG
#define ROS_DEBUG(...)
ROS_WARN
#define ROS_WARN(...)
swri::TopicServiceClientImpl::timeout_
ros::Duration timeout_
Definition: topic_service_client.h:53
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::this_node::getName
const ROSCPP_DECL std::string & getName()
swri::TopicServiceClient
Definition: topic_service_client.h:146
swri::TopicServiceClientImpl::response_
boost::shared_ptr< MRes > response_
Definition: topic_service_client.h:51
this_node.h
swri::TopicServiceClient::getService
std::string getService()
Definition: topic_service_client.h:162
swri::TopicServiceClientImpl::response_callback
void response_callback(const boost::shared_ptr< MRes > &message)
Definition: topic_service_client.h:123
swri::TopicServiceClientImpl::response_sub_
ros::Subscriber response_sub_
Definition: topic_service_client.h:49
swri::TopicServiceClientImpl::call
bool call(MReq &request, MRes &response)
Definition: topic_service_client.h:80
swri::TopicServiceClient::impl_
boost::shared_ptr< TopicServiceClientImpl< typename MReq::Request, typename MReq::Response > > impl_
Definition: topic_service_client.h:148
ROS_ERROR
#define ROS_ERROR(...)
swri
Definition: dynamic_parameters.h:51
swri::TopicServiceClientImpl::service_name_
std::string service_name_
Definition: topic_service_client.h:55
swri::TopicServiceClient::initialize
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
Definition: topic_service_client.h:152
ros::Duration::sleep
bool sleep() const
ros::Publisher::getNumSubscribers
uint32_t getNumSubscribers() const
swri::TopicServiceClientImpl::request_lock_
boost::mutex request_lock_
Definition: topic_service_client.h:48
ros::Duration
ros::NodeHandle
ros::Subscriber
swri::TopicServiceClientImpl::sequence_
int sequence_
Definition: topic_service_client.h:57
ros::Time::now
static Time now()


swri_roscpp
Author(s): P. J. Reed
autogenerated on Thu Jun 6 2024 02:33:09