Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 1 | #include "aos/events/shm_event_loop.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 2 | #include "aos/init.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 3 | #include "aos/network/web_proxy.h" |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 4 | #include "aos/seasocks/seasocks_logger.h" |
| 5 | #include "gflags/gflags.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 6 | |
| 7 | #include "internal/Embedded.h" |
| 8 | #include "seasocks/Server.h" |
| 9 | #include "seasocks/WebSocket.h" |
| 10 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 11 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
| 12 | DEFINE_string(data_dir, "www", "Directory to serve data files from"); |
| 13 | |
| 14 | void RunDataThread( |
| 15 | std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> *subscribers, |
| 16 | const aos::FlatbufferDetachedBuffer<aos::Configuration> &config) { |
| 17 | aos::ShmEventLoop event_loop(&config.message()); |
| 18 | |
| 19 | // TODO(alex): skip fetchers on the wrong node. |
| 20 | for (uint i = 0; i < config.message().channels()->size(); ++i) { |
| 21 | auto channel = config.message().channels()->Get(i); |
| 22 | auto fetcher = event_loop.MakeRawFetcher(channel); |
| 23 | subscribers->emplace_back( |
| 24 | std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i)); |
| 25 | } |
| 26 | |
| 27 | flatbuffers::FlatBufferBuilder fbb(1024); |
| 28 | |
| 29 | auto timer = event_loop.AddTimer([&]() { |
| 30 | for (auto &subscriber : *subscribers) { |
| 31 | subscriber->RunIteration(); |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | event_loop.OnRun([&]() { |
| 36 | timer->Setup(event_loop.monotonic_now(), std::chrono::milliseconds(100)); |
| 37 | }); |
| 38 | |
| 39 | event_loop.Run(); |
| 40 | } |
| 41 | |
| 42 | int main(int argc, char **argv) { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 43 | // Make sure to reference this to force the linker to include it. |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 44 | aos::InitGoogle(&argc, &argv); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 45 | findEmbeddedContent(""); |
| 46 | |
| 47 | aos::InitNRT(); |
| 48 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 49 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 50 | aos::configuration::ReadConfig(FLAGS_config); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 51 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 52 | std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers; |
| 53 | |
| 54 | std::thread data_thread{ |
| 55 | [&subscribers, &config]() { RunDataThread(&subscribers, config); }}; |
| 56 | |
| 57 | seasocks::Server server(std::shared_ptr<seasocks::Logger>( |
| 58 | new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info))); |
| 59 | |
| 60 | auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>( |
| 61 | &server, subscribers, config); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 62 | server.addWebSocketHandler("/ws", websocket_handler); |
| 63 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame^] | 64 | server.serve(FLAGS_data_dir.c_str(), 8080); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 65 | } |