service_relay.h
Go to the documentation of this file.
1 
25 #ifndef MESSAGE_RELAY_RELAY_SERVICE_RELAY_H
26 #define MESSAGE_RELAY_RELAY_SERVICE_RELAY_H
27 
29 #include "ros/callback_queue.h"
30 #include "ros/ros.h"
31 
32 #include <string>
33 
34 namespace message_relay
35 {
36 
38 {
39  std::string type;
40  std::string service;
47 
49  : check_for_server_frequency(1.0)
50  { }
51 };
52 
54 {
55 public:
57 
58  virtual ~ServiceRelay()
59  { }
60 
61 protected:
63  { }
64 };
65 
66 template< typename ServiceType >
68 {
70 
71 private:
72  explicit ServiceRelayImpl(const ServiceRelayParams &params)
73  : origin_(params.origin), target_(params.target), frame_id_processor_(params.frame_id_processor),
74  time_processor_(params.time_processor), check_for_server_frequency_(params.check_for_server_frequency)
75  {
76  frame_id_processor_inverse_ = FrameIdProcessor::inverse(frame_id_processor_);
77  time_processor_inverse_ = TimeProcessor::inverse(time_processor_);
78 
79  server_options_ = ros::AdvertiseServiceOptions::create<ServiceType>(
80  params.service,
81  boost::bind(&ServiceRelayImpl<ServiceType>::serviceCb, this, _1, _2),
83  params.callback_queue.get());
84  client_ = origin_->serviceClient<ServiceType>(server_options_.service);
85  ROS_DEBUG_STREAM("Created service client at " << origin_->getNamespace() << "/" << server_options_.service
86  << ", waiting for connection...");
87 
88  ros::TimerOptions wait_timer_options(
89  ros::Duration(1.0),
90  boost::bind(&ServiceRelayImpl<ServiceType>::waitCb, this),
91  params.callback_queue.get());
92  wait_timer_ = origin_->createTimer(wait_timer_options);
93  }
94 
95  void waitCb()
96  {
97  ROS_INFO_STREAM("Searching for service server at " << origin_->getNamespace() << "/" << server_options_.service
98  << "...");
99  if (client_.waitForExistence(ros::Duration(0.1)))
100  {
101  ROS_INFO_STREAM("...found, creating relay server at " << target_->getNamespace() << "/"
102  << server_options_.service);
103  server_ = target_->advertiseService(server_options_);
104  wait_timer_.stop();
105  }
106  else
107  {
108  ROS_WARN_STREAM("...not found");
109  }
110  }
111 
112 
113  bool serviceCb(typename ServiceType::Request &req, typename ServiceType::Response &res)
114  {
115  if (frame_id_processor_inverse_)
116  {
117  ServiceProcessor<ServiceType, FrameIdProcessor>::processRequest(req, frame_id_processor_inverse_);
118  }
119  if (time_processor_inverse_)
120  {
122  }
123 
124  client_.call(req, res);
125 
126  if (frame_id_processor_)
127  {
129  }
130  if (time_processor_)
131  {
133  }
134  return true;
135  };
136 
142 
146 };
147 
148 } // namespace message_relay
149 
150 #endif // MESSAGE_RELAY_RELAY_SERVICE_RELAY_H
FrameIdProcessor::ConstPtr frame_id_processor_inverse_
boost::shared_ptr< ServiceRelay > Ptr
Definition: service_relay.h:56
boost::shared_ptr< void const > VoidConstPtr
TimeProcessor::ConstPtr time_processor_inverse_
static void processRequest(typename Service::Request &req, typename Processor::ConstPtr &processor)
ServiceRelayImpl(const ServiceRelayParams &params)
Definition: service_relay.h:72
boost::shared_ptr< ros::NodeHandle > origin
Definition: service_relay.h:41
static ConstPtr inverse(const ConstPtr &processor)
bool serviceCb(typename ServiceType::Request &req, typename ServiceType::Response &res)
boost::shared_ptr< ros::NodeHandle > target
Definition: service_relay.h:42
static ConstPtr inverse(const ConstPtr &processor)
TimeProcessor::ConstPtr time_processor
Definition: service_relay.h:44
boost::shared_ptr< ros::CallbackQueue > callback_queue
Definition: service_relay.h:46
static void processResponse(typename Service::Response &res, typename Processor::ConstPtr &processor)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
#define ROS_INFO_STREAM(args)
boost::shared_ptr< ros::NodeHandle > target_
ros::AdvertiseServiceOptions server_options_
ServiceRelay::Ptr createServiceRelay(const ServiceRelayParams &params)
FrameIdProcessor::ConstPtr frame_id_processor
Definition: service_relay.h:43


message_relay
Author(s):
autogenerated on Wed Jul 17 2019 03:27:53