James Kuszmaul | 0de4feb | 2022-04-15 12:16:59 -0700 | [diff] [blame] | 1 | #ifndef AOS_UTIL_FOXGLOVE_WEBSOCKET_LIB_H_ |
| 2 | #define AOS_UTIL_FOXGLOVE_WEBSOCKET_LIB_H_ |
| 3 | #include <map> |
| 4 | #include <set> |
| 5 | |
| 6 | #include "aos/events/event_loop.h" |
| 7 | #include "foxglove/websocket/server.hpp" |
| 8 | |
| 9 | namespace aos { |
| 10 | // This class implements a live AOS -> Foxglove Websocket Protocol connection, |
| 11 | // making use of the implementation at |
| 12 | // https://github.com/foxglove/ws-protocol/tree/main/cpp to send JSON messages |
| 13 | // to a foxglove studio client. |
| 14 | // See foxglove_websocket.cc for some usage notes. |
| 15 | class FoxgloveWebsocketServer { |
| 16 | public: |
James Kuszmaul | f1dbaff | 2023-02-08 21:17:32 -0800 | [diff] [blame^] | 17 | // Whether to serialize the messages into the MCAP file as JSON or |
| 18 | // flatbuffers. |
| 19 | enum class Serialization { |
| 20 | kJson, |
| 21 | kFlatbuffer, |
| 22 | }; |
| 23 | enum class FetchPinnedChannels { |
| 24 | kYes, |
| 25 | kNo, |
| 26 | }; |
| 27 | FoxgloveWebsocketServer(aos::EventLoop *event_loop, uint32_t port, |
| 28 | Serialization serialization, |
| 29 | FetchPinnedChannels fetch_pinned_channels); |
James Kuszmaul | 0de4feb | 2022-04-15 12:16:59 -0700 | [diff] [blame] | 30 | ~FoxgloveWebsocketServer(); |
| 31 | |
| 32 | private: |
| 33 | typedef foxglove::websocket::ChannelId ChannelId; |
| 34 | |
| 35 | struct FetcherState { |
| 36 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 37 | // Whether the current message in the fetcher has been sent to the client. |
| 38 | // Starts as true because the fetcher starts with no data. |
| 39 | // This is necessary because we have to send all of our messages out |
| 40 | // in order, which we can only do once we know the timestamp of each |
| 41 | // message. And we can only know the timestamp after having called Fetch() |
| 42 | // on the fetcher. Once we get around to actually sending the data, we can |
| 43 | // set this to true so that we know it is safe to call FetchNext() again. |
| 44 | bool sent_current_message = true; |
| 45 | }; |
| 46 | |
| 47 | aos::EventLoop *event_loop_; |
James Kuszmaul | f1dbaff | 2023-02-08 21:17:32 -0800 | [diff] [blame^] | 48 | const Serialization serialization_; |
| 49 | const FetchPinnedChannels fetch_pinned_channels_; |
James Kuszmaul | 0de4feb | 2022-04-15 12:16:59 -0700 | [diff] [blame] | 50 | foxglove::websocket::Server server_; |
| 51 | // A map of fetchers for every single channel that could be subscribed to. |
| 52 | std::map<ChannelId, FetcherState> fetchers_; |
| 53 | // The set of channels that we have clients actively subscribed to. |
| 54 | std::set<ChannelId> active_channels_; |
| 55 | }; |
| 56 | } // namespace aos |
| 57 | #endif // AOS_UTIL_FOXGLOVE_WEBSOCKET_LIB_H_ |