blob: 816065388969f518af75c404102f0bf5f1549871 [file] [log] [blame]
James Kuszmaul0de4feb2022-04-15 12:16:59 -07001#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
9namespace 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.
15class FoxgloveWebsocketServer {
16 public:
17 FoxgloveWebsocketServer(aos::EventLoop *event_loop, uint32_t port);
18 ~FoxgloveWebsocketServer();
19
20 private:
21 typedef foxglove::websocket::ChannelId ChannelId;
22
23 struct FetcherState {
24 std::unique_ptr<aos::RawFetcher> fetcher;
25 // Whether the current message in the fetcher has been sent to the client.
26 // Starts as true because the fetcher starts with no data.
27 // This is necessary because we have to send all of our messages out
28 // in order, which we can only do once we know the timestamp of each
29 // message. And we can only know the timestamp after having called Fetch()
30 // on the fetcher. Once we get around to actually sending the data, we can
31 // set this to true so that we know it is safe to call FetchNext() again.
32 bool sent_current_message = true;
33 };
34
35 aos::EventLoop *event_loop_;
36 foxglove::websocket::Server server_;
37 // A map of fetchers for every single channel that could be subscribed to.
38 std::map<ChannelId, FetcherState> fetchers_;
39 // The set of channels that we have clients actively subscribed to.
40 std::set<ChannelId> active_channels_;
41};
42} // namespace aos
43#endif // AOS_UTIL_FOXGLOVE_WEBSOCKET_LIB_H_