topic_handlers.h
Go to the documentation of this file.
1 
35 #ifndef ROSSERIAL_SERVER_TOPIC_HANDLERS_H
36 #define ROSSERIAL_SERVER_TOPIC_HANDLERS_H
37 
38 #include <ros/ros.h>
39 #include <rosserial_msgs/TopicInfo.h>
42 
43 namespace rosserial_server
44 {
45 
46 class Publisher {
47 public:
48  Publisher(ros::NodeHandle& nh, const rosserial_msgs::TopicInfo& topic_info) {
50  try
51  {
52  msginfo = lookupMessage(topic_info.message_type);
53  }
54  catch (const std::exception& e)
55  {
56  ROS_WARN_STREAM("Unable to look up message: " << e.what());
57  }
58 
59  if (!msginfo.md5sum.empty() && msginfo.md5sum != topic_info.md5sum)
60  {
61  ROS_WARN_STREAM("Message" << topic_info.message_type << "MD5 sum from client does not "
62  "match that in system. Will avoid using system's message definition.");
63  msginfo.full_text = "";
64  }
65  message_.morph(topic_info.md5sum, topic_info.message_type, msginfo.full_text, "false");
66  publisher_ = message_.advertise(nh, topic_info.topic_name, 1);
67  }
68 
72  }
73 
74  std::string get_topic() {
75  return publisher_.getTopic();
76  }
77 
78 private:
81 };
82 
84 
85 
86 class Subscriber {
87 public:
88  Subscriber(ros::NodeHandle& nh, rosserial_msgs::TopicInfo& topic_info,
89  boost::function<void(std::vector<uint8_t>& buffer)> write_fn)
90  : write_fn_(write_fn) {
93  topic_info.topic_name, 1, boost::bind(&Subscriber::handle, this, _1));
94  opts.md5sum = topic_info.md5sum;
95  opts.datatype = topic_info.message_type;
96  subscriber_ = nh.subscribe(opts);
97  }
98 
99  std::string get_topic() {
100  return subscriber_.getTopic();
101  }
102 
103 private:
105  size_t length = ros::serialization::serializationLength(*msg);
106  std::vector<uint8_t> buffer(length);
107 
108  ros::serialization::OStream ostream(&buffer[0], length);
110 
111  write_fn_(buffer);
112  }
113 
115  boost::function<void(std::vector<uint8_t>& buffer)> write_fn_;
116 };
117 
119 
121 public:
122  ServiceClient(ros::NodeHandle& nh, rosserial_msgs::TopicInfo& topic_info,
123  boost::function<void(std::vector<uint8_t>& buffer, const uint16_t topic_id)> write_fn)
124  : write_fn_(write_fn) {
125  topic_id_ = -1;
126 
129  rosserial_server::MsgInfo respinfo;
130  try
131  {
132  srvinfo = lookupMessage(topic_info.message_type, "srv");
133  reqinfo = lookupMessage(topic_info.message_type + "Request", "srv");
134  respinfo = lookupMessage(topic_info.message_type + "Response", "srv");
135  }
136  catch (const std::exception& e)
137  {
138  ROS_WARN_STREAM("Unable to look up service definition: " << e.what());
139  }
140  service_md5_ = srvinfo.md5sum;
141  request_message_md5_ = reqinfo.md5sum;
142  response_message_md5_ = respinfo.md5sum;
143 
145  opts.service = topic_info.topic_name;
146  opts.md5sum = srvinfo.md5sum;
147  opts.persistent = false; // always false for now
148  service_client_ = nh.serviceClient(opts);
149  }
150  void setTopicId(uint16_t topic_id) {
151  topic_id_ = topic_id;
152  }
153  std::string getServiceMD5() {
154  return service_md5_;
155  }
156  std::string getRequestMessageMD5() {
157  return request_message_md5_;
158  }
159  std::string getResponseMessageMD5() {
160  return response_message_md5_;
161  }
162 
164  // deserialize request message
166 
167  // perform service call
168  // note that at present, at least for rosserial-windows a service call returns nothing,
169  // so we discard the return value of this call() invocation.
170  service_client_.call(request_message_, response_message_, service_md5_);
171 
172  // write service response over the wire
173  size_t length = ros::serialization::serializationLength(response_message_);
174  std::vector<uint8_t> buffer(length);
175  ros::serialization::OStream ostream(&buffer[0], length);
177  write_fn_(buffer,topic_id_);
178  }
179 
180 private:
184  boost::function<void(std::vector<uint8_t>& buffer, const uint16_t topic_id)> write_fn_;
185  std::string service_md5_;
186  std::string request_message_md5_;
188  uint16_t topic_id_;
189 };
190 
192 
193 } // namespace
194 
195 #endif // ROSSERIAL_SERVER_TOPIC_HANDLERS_H
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
Subscriber(ros::NodeHandle &nh, rosserial_msgs::TopicInfo &topic_info, boost::function< void(std::vector< uint8_t > &buffer)> write_fn)
static void write(Stream &stream, typename boost::call_traits< T >::param_type t)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void morph(const std::string &md5sum, const std::string &datatype, const std::string &msg_def, const std::string &latching)
boost::shared_ptr< Publisher > PublisherPtr
ServiceClient(ros::NodeHandle &nh, rosserial_msgs::TopicInfo &topic_info, boost::function< void(std::vector< uint8_t > &buffer, const uint16_t topic_id)> write_fn)
ros::ServiceClient service_client_
boost::shared_ptr< Subscriber > SubscriberPtr
void handle(const boost::shared_ptr< topic_tools::ShapeShifter const > &msg)
Publisher(ros::NodeHandle &nh, const rosserial_msgs::TopicInfo &topic_info)
void publish(const boost::shared_ptr< M > &message) const
topic_tools::ShapeShifter request_message_
topic_tools::ShapeShifter response_message_
static void read(Stream &stream, typename boost::call_traits< T >::reference t)
void setTopicId(uint16_t topic_id)
#define ROS_WARN_STREAM(args)
boost::shared_ptr< ServiceClient > ServiceClientPtr
ros::Publisher advertise(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size_, bool latch=false, const ros::SubscriberStatusCallback &connect_cb=ros::SubscriberStatusCallback()) const
void handle(ros::serialization::IStream stream)
std::string getTopic() const
topic_tools::ShapeShifter message_
void init(const std::string &_topic, uint32_t _queue_size, const boost::function< void(const boost::shared_ptr< M const > &)> &_callback, const boost::function< boost::shared_ptr< M >(void)> &factory_fn=DefaultMessageCreator< M >())
uint32_t serializationLength(const T &t)
void handle(ros::serialization::IStream stream)
const MsgInfo lookupMessage(const std::string &message_type, const std::string submodule="msg")
Definition: msg_lookup.cpp:36


rosserial_server
Author(s): Mike Purvis
autogenerated on Mon Feb 28 2022 23:35:31