NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
bridge_publisher.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_PUBLISHER_H_
20 #define COMMS_BRIDGE_BRIDGE_PUBLISHER_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 <queue>
36 #include <chrono>
37 #include <thread>
38 #include <condition_variable>
39 #include <string>
40 #include <vector>
41 #include <utility>
42 
43 typedef std::shared_ptr<ros::Subscriber> SubscriberPtr;
44 typedef std::shared_ptr<ros::Publisher> PublisherPtr;
45 typedef std::chrono::time_point<std::chrono::steady_clock> timepoint_t;
46 
48  public:
49  virtual ~BridgePublisher();
50 
51  // 0 => none, > 0 => progressively more
52  void setVerbosity(unsigned int verbosity);
53 
54  protected:
55  // a yet-unknown message type will have null string values
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 
71  class ContentInfo {
72  public:
73  // the md5sum from the original advertisement
74  std::string type_md5_sum;
75 
76  // opaque serialized message data
77  std::vector<uint8_t> data;
78  };
79 
81  public:
83 
84  ros::Publisher publisher; // our subscriber on this input topic
85  std::string out_topic; // topic name to republish on
86  bool advertised; // whether we have sent type info to other side
87  unsigned int relay_seqnum; // counter of messages relayed on this topic
88 
89  // Will be filled in only on receipt of advertisement from our
90  // bridger subscriber counterpart
92 
93  std::queue<ContentInfo>
94  waiting_msgs; // unpublished messages because type yet unknown or we're still delaying after advertising
95  timepoint_t delay_until; // time to wait until relaying any data, after having advertised
96  };
97 
98  /**************************************************************************/
99 
100  // This base class should only be extended, not itself instantiated
101  explicit BridgePublisher(double ad2pub_delay);
102 
103  // Should be called by implementing class when it has received a topic's
104  // AdvertisementInfo
105  // Must be called with mutex held
106  bool advertiseTopic(const std::string& output_topic, const AdvertisementInfo& ad_info);
107 
108  // Should be called by an implementing class when it receives a serialized
109  // message over its particular conduit
110  // Must be called with mutex held
111  bool relayMessage(RelayTopicInfo& topic_info, const ContentInfo& content_info);
112 
113  // worker thread spawned at construction
114  void drainThread();
115 
116  // does actual work of publishing a message (eg called by relayMessage)
117  // Must be called with mutex held
118  bool relayContent(RelayTopicInfo& topic_info, const ContentInfo& content_info);
119 
120  // Publishes messages that were enqueued for a given topic
121  // Must be called with mutex held
122  void drainWaitingQueue(RelayTopicInfo& topic_info);
123  void drainWaitingQueue(std::string const& output_topic);
124 
125  unsigned int m_verbose_;
126 
127  double m_ad2pub_delay_; // seconds
128 
129  std::mutex m_mutex_; // serializes access to below data structures
130  std::shared_ptr<std::thread> worker_thread_;
131  std::map<std::string, RelayTopicInfo> m_relay_topics_; // keyed by input topic
132  std::priority_queue<std::pair<timepoint_t, std::string>> m_drain_queue_;
133  std::condition_variable m_drain_cv_;
134  // stats:
135  unsigned int m_n_relayed_;
136 };
137 
138 #endif // COMMS_BRIDGE_BRIDGE_PUBLISHER_H_
SubscriberPtr
std::shared_ptr< ros::Subscriber > SubscriberPtr
Definition: bridge_publisher.h:43
BridgePublisher
Definition: bridge_publisher.h:47
BridgePublisher::RelayTopicInfo::relay_seqnum
unsigned int relay_seqnum
Definition: bridge_publisher.h:87
BridgePublisher::drainWaitingQueue
void drainWaitingQueue(RelayTopicInfo &topic_info)
Definition: bridge_publisher.cpp:227
BridgePublisher::ContentInfo
Definition: bridge_publisher.h:71
BridgePublisher::RelayTopicInfo::delay_until
timepoint_t delay_until
Definition: bridge_publisher.h:95
BridgePublisher::ContentInfo::type_md5_sum
std::string type_md5_sum
Definition: bridge_publisher.h:74
BridgePublisher::m_verbose_
unsigned int m_verbose_
Definition: bridge_publisher.h:125
BridgePublisher::relayMessage
bool relayMessage(RelayTopicInfo &topic_info, const ContentInfo &content_info)
Definition: bridge_publisher.cpp:146
BridgePublisher::~BridgePublisher
virtual ~BridgePublisher()
Definition: bridge_publisher.cpp:45
BridgePublisher::m_mutex_
std::mutex m_mutex_
Definition: bridge_publisher.h:129
BridgePublisher::AdvertisementInfo
Definition: bridge_publisher.h:56
BridgePublisher::RelayTopicInfo::ad_info
AdvertisementInfo ad_info
Definition: bridge_publisher.h:91
BridgePublisher::BridgePublisher
BridgePublisher(double ad2pub_delay)
Definition: bridge_publisher.cpp:38
BridgePublisher::worker_thread_
std::shared_ptr< std::thread > worker_thread_
Definition: bridge_publisher.h:130
BridgePublisher::AdvertisementInfo::data_type
std::string data_type
Definition: bridge_publisher.h:62
BridgePublisher::m_n_relayed_
unsigned int m_n_relayed_
Definition: bridge_publisher.h:135
BridgePublisher::m_drain_cv_
std::condition_variable m_drain_cv_
Definition: bridge_publisher.h:133
BridgePublisher::RelayTopicInfo::waiting_msgs
std::queue< ContentInfo > waiting_msgs
Definition: bridge_publisher.h:94
PublisherPtr
std::shared_ptr< ros::Publisher > PublisherPtr
Definition: bridge_publisher.h:44
BridgePublisher::m_ad2pub_delay_
double m_ad2pub_delay_
Definition: bridge_publisher.h:127
BridgePublisher::AdvertisementInfo::latching
bool latching
Definition: bridge_publisher.h:59
BridgePublisher::RelayTopicInfo
Definition: bridge_publisher.h:80
BridgePublisher::RelayTopicInfo::out_topic
std::string out_topic
Definition: bridge_publisher.h:85
BridgePublisher::RelayTopicInfo::advertised
bool advertised
Definition: bridge_publisher.h:86
BridgePublisher::RelayTopicInfo::RelayTopicInfo
RelayTopicInfo()
Definition: bridge_publisher.h:82
BridgePublisher::m_drain_queue_
std::priority_queue< std::pair< timepoint_t, std::string > > m_drain_queue_
Definition: bridge_publisher.h:132
BridgePublisher::advertiseTopic
bool advertiseTopic(const std::string &output_topic, const AdvertisementInfo &ad_info)
Definition: bridge_publisher.cpp:51
BridgePublisher::relayContent
bool relayContent(RelayTopicInfo &topic_info, const ContentInfo &content_info)
Definition: bridge_publisher.cpp:107
BridgePublisher::drainThread
void drainThread()
Definition: bridge_publisher.cpp:194
timepoint_t
std::chrono::time_point< std::chrono::steady_clock > timepoint_t
Definition: bridge_publisher.h:45
BridgePublisher::RelayTopicInfo::publisher
ros::Publisher publisher
Definition: bridge_publisher.h:84
BridgePublisher::m_relay_topics_
std::map< std::string, RelayTopicInfo > m_relay_topics_
Definition: bridge_publisher.h:131
BridgePublisher::ContentInfo::data
std::vector< uint8_t > data
Definition: bridge_publisher.h:77
BridgePublisher::setVerbosity
void setVerbosity(unsigned int verbosity)
Definition: bridge_publisher.cpp:49
BridgePublisher::AdvertisementInfo::md5_sum
std::string md5_sum
Definition: bridge_publisher.h:65
BridgePublisher::AdvertisementInfo::definition
std::string definition
Definition: bridge_publisher.h:68