topic_relay.h
Go to the documentation of this file.
1 
25 #ifndef MESSAGE_RELAY_RELAY_TOPIC_RELAY_H
26 #define MESSAGE_RELAY_RELAY_TOPIC_RELAY_H
27 
29 #include "ros/callback_queue.h"
30 #include "ros/ros.h"
31 #include "ros/transport_hints.h"
32 
33 #include <string>
34 
35 namespace message_relay
36 {
37 
39 {
40  std::string type;
41  std::string topic;
46  bool latch;
50  bool unreliable;
51 
53  : type(), topic(), origin(), target(), frame_id_processor(), time_processor(), latch(false),
54  throttle_frequency(0.0), queue_size(100), callback_queue(), unreliable(false)
55  { }
56 };
57 
59 {
60 public:
62 
63  virtual ~TopicRelay()
64  { }
65 
66 protected:
68  { }
69 };
70 
71 template<typename MessageType>
72 class TopicRelayImpl : public TopicRelay
73 {
75 
76 private:
77  explicit TopicRelayImpl(const TopicRelayParams &params)
78  : origin_(params.origin), target_(params.target), frame_id_processor_(params.frame_id_processor),
79  time_processor_(params.time_processor), throttle_duration_(0.0)
80  {
81  if (params.throttle_frequency > 0.0)
82  {
83  throttle_duration_ = ros::Duration(1.0 / params.throttle_frequency);
84  }
85 
86  pub_options_ = ros::AdvertiseOptions::create<MessageType>(
87  params.topic,
88  params.queue_size,
92  params.callback_queue.get());
93  pub_options_.latch = params.latch;
94 
95  sub_options_ = ros::SubscribeOptions::create<MessageType>(
96  pub_options_.topic,
97  pub_options_.queue_size,
98  boost::bind(&TopicRelayImpl<MessageType>::topicCb, this, _1),
100  NULL);
101  sub_options_.transport_hints = params.unreliable? ros::TransportHints().unreliable() : ros::TransportHints();
102 
103  sub_ = boost::make_shared<ros::Subscriber>(origin_->subscribe(sub_options_));
104  pub_ = boost::make_shared<ros::Publisher>(target_->advertise(pub_options_));
105  }
106 
107  void topicCb(const typename MessageType::ConstPtr &input)
108  {
109  if (!throttle_duration_.isZero())
110  {
111  // Check if throttle duration has been met since the last relay callback
112  if (ros::Time::now() > (last_relay_ + throttle_duration_))
113  {
114  last_relay_ = ros::Time::now();
115  }
116  else
117  {
118  // Otherwise skip this callback
119  return;
120  }
121  }
122 
123  typename MessageType::ConstPtr output;
124 
125  if (frame_id_processor_ || time_processor_)
126  {
127  typename MessageType::Ptr msg = boost::make_shared<MessageType>(*input);
128  if (frame_id_processor_)
129  {
131  }
132  if (time_processor_)
133  {
135  }
136  output = boost::static_pointer_cast<MessageType const>(msg);
137  }
138  else
139  {
140  output = input;
141  }
142  pub_->publish(output);
143  };
144 
148 
151 
156 };
157 
158 } // namespace message_relay
159 
160 #endif // MESSAGE_RELAY_RELAY_TOPIC_RELAY_H
boost::shared_ptr< ros::CallbackQueue > callback_queue
Definition: topic_relay.h:49
TopicRelayImpl(const TopicRelayParams &params)
Definition: topic_relay.h:77
msg
boost::shared_ptr< void const > VoidConstPtr
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
ros::SubscribeOptions sub_options_
Definition: topic_relay.h:155
boost::shared_ptr< ros::NodeHandle > target
Definition: topic_relay.h:43
TimeProcessor::ConstPtr time_processor
Definition: topic_relay.h:45
TimeProcessor::ConstPtr time_processor_
Definition: topic_relay.h:147
boost::shared_ptr< TopicRelay > Ptr
Definition: topic_relay.h:61
FrameIdProcessor::ConstPtr frame_id_processor_
Definition: topic_relay.h:146
boost::shared_ptr< ros::Subscriber > sub_
Definition: topic_relay.h:152
FrameIdProcessor::ConstPtr frame_id_processor
Definition: topic_relay.h:44
TransportHints & unreliable()
ros::Duration throttle_duration_
Definition: topic_relay.h:149
boost::shared_ptr< ros::Publisher > pub_
Definition: topic_relay.h:153
TopicRelay::Ptr createTopicRelay(const TopicRelayParams &params)
ros::AdvertiseOptions pub_options_
Definition: topic_relay.h:154
boost::shared_ptr< ros::NodeHandle > target_
Definition: topic_relay.h:143
static Time now()
static void processMessage(typename Message::Ptr &msg, typename Processor::ConstPtr &processor)
void topicCb(const typename MessageType::ConstPtr &input)
Definition: topic_relay.h:107
boost::shared_ptr< ros::NodeHandle > origin
Definition: topic_relay.h:42


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