ros2_foxglove_bridge.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <chrono>
5 #include <memory>
6 #include <regex>
7 #include <thread>
8 
9 #include <rclcpp/rclcpp.hpp>
10 #include <rosgraph_msgs/msg/clock.hpp>
11 #include <websocketpp/common/connection_hdl.hpp>
12 
16 #include <foxglove_bridge/param_utils.hpp>
21 
22 namespace foxglove_bridge {
23 
24 using ConnectionHandle = websocketpp::connection_hdl;
26 using Subscription = rclcpp::GenericSubscription::SharedPtr;
27 using SubscriptionsByClient = std::map<ConnectionHandle, Subscription, std::owner_less<>>;
28 using Publication = rclcpp::GenericPublisher::SharedPtr;
29 using ClientPublications = std::unordered_map<foxglove::ClientChannelId, Publication>;
30 using PublicationsByClient = std::map<ConnectionHandle, ClientPublications, std::owner_less<>>;
31 
32 class FoxgloveBridge : public rclcpp::Node {
33 public:
34  using TopicAndDatatype = std::pair<std::string, std::string>;
35 
36  FoxgloveBridge(const rclcpp::NodeOptions& options = rclcpp::NodeOptions());
37 
39 
40  void rosgraphPollThread();
41 
43  const std::map<std::string, std::vector<std::string>>& topicNamesAndTypes);
44 
46 
48  const std::map<std::string, std::vector<std::string>>& topicNamesAndTypes);
49 
50 private:
51  struct PairHash {
52  template <class T1, class T2>
53  std::size_t operator()(const std::pair<T1, T2>& pair) const {
54  return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
55  }
56  };
57 
58  std::unique_ptr<foxglove::ServerInterface<ConnectionHandle>> _server;
60  std::vector<std::regex> _topicWhitelistPatterns;
61  std::vector<std::regex> _serviceWhitelistPatterns;
62  std::shared_ptr<ParameterInterface> _paramInterface;
63  std::unordered_map<foxglove::ChannelId, foxglove::ChannelWithoutId> _advertisedTopics;
64  std::unordered_map<foxglove::ServiceId, foxglove::ServiceWithoutId> _advertisedServices;
65  std::unordered_map<foxglove::ChannelId, SubscriptionsByClient> _subscriptions;
67  std::unordered_map<foxglove::ServiceId, GenericClient::SharedPtr> _serviceClients;
68  rclcpp::CallbackGroup::SharedPtr _subscriptionCallbackGroup;
69  rclcpp::CallbackGroup::SharedPtr _clientPublishCallbackGroup;
70  rclcpp::CallbackGroup::SharedPtr _servicesCallbackGroup;
71  std::mutex _subscriptionsMutex;
73  std::mutex _servicesMutex;
74  std::unique_ptr<std::thread> _rosgraphPollThread;
77  std::shared_ptr<rclcpp::Subscription<rosgraph_msgs::msg::Clock>> _clockSubscription;
78  bool _useSimTime = false;
79  std::vector<std::string> _capabilities;
80  std::atomic<bool> _subscribeGraphUpdates = false;
81  bool _includeHidden = false;
82 
84 
85  void subscribe(foxglove::ChannelId channelId, ConnectionHandle clientHandle);
86 
87  void unsubscribe(foxglove::ChannelId channelId, ConnectionHandle clientHandle);
88 
89  void clientAdvertise(const foxglove::ClientAdvertisement& advertisement, ConnectionHandle hdl);
90 
92 
93  void clientMessage(const foxglove::ClientMessage& message, ConnectionHandle hdl);
94 
95  void setParameters(const std::vector<foxglove::Parameter>& parameters,
96  const std::optional<std::string>& requestId, ConnectionHandle hdl);
97 
98  void getParameters(const std::vector<std::string>& parameters,
99  const std::optional<std::string>& requestId, ConnectionHandle hdl);
100 
101  void subscribeParameters(const std::vector<std::string>& parameters,
103 
104  void parameterUpdates(const std::vector<foxglove::Parameter>& parameters);
105 
106  void logHandler(LogLevel level, char const* msg);
107 
108  void rosMessageHandler(const foxglove::ChannelId& channelId, ConnectionHandle clientHandle,
109  std::shared_ptr<rclcpp::SerializedMessage> msg);
110 
111  void serviceRequest(const foxglove::ServiceRequest& request, ConnectionHandle clientHandle);
112 
113  bool hasCapability(const std::string& capability);
114 };
115 
116 } // namespace foxglove_bridge
std::unique_ptr< std::thread > _rosgraphPollThread
rclcpp::GenericPublisher::SharedPtr Publication
std::unordered_map< foxglove::ServiceId, foxglove::ServiceWithoutId > _advertisedServices
foxglove::MessageDefinitionCache _messageDefinitionCache
std::vector< std::regex > _serviceWhitelistPatterns
void getParameters(const std::vector< std::string > &parameters, const std::optional< std::string > &requestId, ConnectionHandle hdl)
void unsubscribe(foxglove::ChannelId channelId, ConnectionHandle clientHandle)
std::size_t operator()(const std::pair< T1, T2 > &pair) const
void subscribe(foxglove::ChannelId channelId, ConnectionHandle clientHandle)
uint32_t ChannelId
Definition: common.hpp:25
bool hasCapability(const std::string &capability)
std::pair< std::string, std::string > TopicAndDatatype
void subscribeParameters(const std::vector< std::string > &parameters, foxglove::ParameterSubscriptionOperation op, ConnectionHandle)
rclcpp::CallbackGroup::SharedPtr _clientPublishCallbackGroup
rclcpp::CallbackGroup::SharedPtr _subscriptionCallbackGroup
void setParameters(const std::vector< foxglove::Parameter > &parameters, const std::optional< std::string > &requestId, ConnectionHandle hdl)
std::unordered_map< foxglove::ChannelId, SubscriptionsByClient > _subscriptions
void subscribeConnectionGraph(bool subscribe)
void logHandler(foxglove::WebSocketLogLevel level, char const *msg)
void clientMessage(const foxglove::ClientMessage &clientMsg, ConnectionHandle clientHandle)
void updateConnectionGraph(const std::map< std::string, std::vector< std::string >> &topicNamesAndTypes)
std::map< ConnectionHandle, ClientPublications, std::owner_less<> > PublicationsByClient
std::map< ConnectionHandle, ros::Subscriber, std::owner_less<> > SubscriptionsByClient
void parameterUpdates(XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
rclcpp::CallbackGroup::SharedPtr _servicesCallbackGroup
ParameterSubscriptionOperation
Definition: parameter.hpp:11
void serviceRequest(const foxglove::ServiceRequest &request, ConnectionHandle clientHandle)
rclcpp::GenericSubscription::SharedPtr Subscription
websocketpp::connection_hdl ConnectionHandle
std::shared_ptr< rclcpp::Subscription< rosgraph_msgs::msg::Clock > > _clockSubscription
std::unordered_map< foxglove::ChannelId, foxglove::ChannelWithoutId > _advertisedTopics
std::unordered_map< foxglove::ClientChannelId, ros::Publisher > ClientPublications
std::unique_ptr< foxglove::ServerInterface< ConnectionHandle > > _server
void rosMessageHandler(const foxglove::ChannelId channelId, ConnectionHandle clientHandle, const ros::MessageEvent< ros_babel_fish::BabelFishMessage const > &msgEvent)
void clientUnadvertise(foxglove::ClientChannelId channelId, ConnectionHandle clientHandle)
std::shared_ptr< ParameterInterface > _paramInterface
void clientAdvertise(const foxglove::ClientAdvertisement &channel, ConnectionHandle clientHandle)
WebSocketLogLevel
Definition: common.hpp:41
std::unordered_map< foxglove::ServiceId, GenericClient::SharedPtr > _serviceClients


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22