NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
bridge_subscriber.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef COMMS_BRIDGE_BRIDGE_SUBSCRIBER_H_
20 #define COMMS_BRIDGE_BRIDGE_SUBSCRIBER_H_
21 
22 /*
23  Virtual base class for the subscriber (input side) of a generic ROS bridge
24  Actual implementation depends on an inheriting class
25  */
26 
27 #include <stdint.h>
28 
29 #include <ros/ros.h>
30 #include <topic_tools/shape_shifter.h>
31 
32 #include <mutex>
33 #include <memory>
34 #include <map>
35 #include <string>
36 
37 typedef std::shared_ptr<ros::Subscriber> SubscriberPtr;
38 typedef std::shared_ptr<ros::Publisher> PublisherPtr;
39 
41  public:
43  virtual ~BridgeSubscriber();
44 
45  // Add a relay between this input topic on the subscriber side and this
46  // output topic on the republisher side
47  // All relays must have a unique input topic and unique output topic
48  // Other topologies (merging topics or duplicating topics) should be
49  // implemented by some other node on top of this one
50  bool addTopic(std::string const& in_topic, std::string const& out_topic);
51 
52  // 0 => none, > 0 => progressively more
53  void setVerbosity(unsigned int verbosity);
54 
55  protected:
57  public:
58  // whehter republisher should advertise this as a latching ROS topic
59  bool latching;
60 
61  // ROS message data type name, eg "std_msgs/Int32"
62  std::string data_type;
63 
64  // ROS message md5sum of type, eg "da5909fbe378aeaf85e547e830cc1bb7"
65  std::string md5_sum;
66 
67  // ROS message definition, eg as one finds in a .msg definition file
68  std::string definition;
69  };
70 
72  public:
74 
75  SubscriberPtr sub; // our subscriber on this input topic
76  std::string out_topic; // topic name to republish on
77  bool advertised; // whether we have sent type info to other side
78  unsigned int relay_seqnum; // counter of messages relayed on this topic
79 
80  // not filled in until advertiseTopic() is called
82  };
83 
84  class ContentInfo {
85  public:
86  // the md5sum from the original advertisement
87  std::string type_md5_sum;
88 
89  // length in bytes of the serialized data
90  size_t data_size;
91 
92  // opaque serialized message data
93  const uint8_t* data;
94  };
95 
96  // Called via addTopic()
97  // Notifies implementation that a bridge between these two topics is needed
98  // No information about the message type etc. is yet available
99  // Called with the mutex held
100  virtual void subscribeTopic(std::string const& in_topic, const RelayTopicInfo& info) = 0;
101 
102  // Called upon receipt of the first message on a subscribed topic
103  // Notifies implementation to advertise the topic with this type info
104  // Called with the mutex held
105  virtual void advertiseTopic(const RelayTopicInfo& info) = 0;
106 
107  // Called on receipt of any message on a subscribed topic
108  // Notifies implementation to relay this message content to the output topic
109  // Note any pointers within info struct are valid only during this call
110  // Called with the mutex held
111  virtual void relayMessage(const RelayTopicInfo& topic_info, ContentInfo const& content_info) = 0;
112 
113  /**************************************************************************/
114 
115  SubscriberPtr rosSubscribe(std::string const& topic);
116 
117  void handleRelayedMessage(const ros::MessageEvent<topic_tools::ShapeShifter const>& msg_event,
118  std::string const& topic, SubscriberPtr sub);
119 
120  unsigned int m_verbose_;
121 
122  std::mutex m_mutex_; // serializes access to below data structures
123  uint8_t* m_msgbuffer_; // data serialization scratch
124  std::map<std::string, RelayTopicInfo> m_relay_topics_; // keyed by input topic
125  // stats:
126  unsigned int m_n_relayed_;
127 };
128 
129 #endif // COMMS_BRIDGE_BRIDGE_SUBSCRIBER_H_
SubscriberPtr
std::shared_ptr< ros::Subscriber > SubscriberPtr
Definition: bridge_publisher.h:43
BridgeSubscriber::AdvertisementInfo::definition
std::string definition
Definition: bridge_subscriber.h:68
BridgeSubscriber::BridgeSubscriber
BridgeSubscriber()
Definition: bridge_subscriber.cpp:34
BridgeSubscriber::advertiseTopic
virtual void advertiseTopic(const RelayTopicInfo &info)=0
BridgeSubscriber::RelayTopicInfo
Definition: bridge_subscriber.h:71
BridgeSubscriber::relayMessage
virtual void relayMessage(const RelayTopicInfo &topic_info, ContentInfo const &content_info)=0
BridgeSubscriber::handleRelayedMessage
void handleRelayedMessage(const ros::MessageEvent< topic_tools::ShapeShifter const > &msg_event, std::string const &topic, SubscriberPtr sub)
Definition: bridge_subscriber.cpp:45
BridgeSubscriber::AdvertisementInfo
Definition: bridge_subscriber.h:56
BridgeSubscriber::RelayTopicInfo::ad_info
AdvertisementInfo ad_info
Definition: bridge_subscriber.h:81
BridgeSubscriber::RelayTopicInfo::relay_seqnum
unsigned int relay_seqnum
Definition: bridge_subscriber.h:78
BridgeSubscriber::addTopic
bool addTopic(std::string const &in_topic, std::string const &out_topic)
Definition: bridge_subscriber.cpp:152
SubscriberPtr
std::shared_ptr< ros::Subscriber > SubscriberPtr
Definition: bridge_subscriber.h:37
BridgeSubscriber::m_relay_topics_
std::map< std::string, RelayTopicInfo > m_relay_topics_
Definition: bridge_subscriber.h:124
BridgeSubscriber::ContentInfo
Definition: bridge_subscriber.h:84
BridgeSubscriber::~BridgeSubscriber
virtual ~BridgeSubscriber()
Definition: bridge_subscriber.cpp:41
BridgeSubscriber::ContentInfo::data_size
size_t data_size
Definition: bridge_subscriber.h:90
BridgeSubscriber::setVerbosity
void setVerbosity(unsigned int verbosity)
Definition: bridge_subscriber.cpp:43
BridgeSubscriber::m_mutex_
std::mutex m_mutex_
Definition: bridge_subscriber.h:122
BridgeSubscriber::RelayTopicInfo::advertised
bool advertised
Definition: bridge_subscriber.h:77
BridgeSubscriber::RelayTopicInfo::sub
SubscriberPtr sub
Definition: bridge_subscriber.h:75
BridgeSubscriber::AdvertisementInfo::data_type
std::string data_type
Definition: bridge_subscriber.h:62
BridgeSubscriber::AdvertisementInfo::md5_sum
std::string md5_sum
Definition: bridge_subscriber.h:65
BridgeSubscriber::RelayTopicInfo::RelayTopicInfo
RelayTopicInfo()
Definition: bridge_subscriber.h:73
BridgeSubscriber::m_msgbuffer_
uint8_t * m_msgbuffer_
Definition: bridge_subscriber.h:123
BridgeSubscriber::RelayTopicInfo::out_topic
std::string out_topic
Definition: bridge_subscriber.h:76
PublisherPtr
std::shared_ptr< ros::Publisher > PublisherPtr
Definition: bridge_subscriber.h:38
BridgeSubscriber::m_verbose_
unsigned int m_verbose_
Definition: bridge_subscriber.h:120
BridgeSubscriber::m_n_relayed_
unsigned int m_n_relayed_
Definition: bridge_subscriber.h:126
BridgeSubscriber::AdvertisementInfo::latching
bool latching
Definition: bridge_subscriber.h:59
BridgeSubscriber::ContentInfo::data
const uint8_t * data
Definition: bridge_subscriber.h:93
BridgeSubscriber::rosSubscribe
SubscriberPtr rosSubscribe(std::string const &topic)
Definition: bridge_subscriber.cpp:131
BridgeSubscriber
Definition: bridge_subscriber.h:40
BridgeSubscriber::ContentInfo::type_md5_sum
std::string type_md5_sum
Definition: bridge_subscriber.h:87
BridgeSubscriber::subscribeTopic
virtual void subscribeTopic(std::string const &in_topic, const RelayTopicInfo &info)=0