Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
Alex Perry | 09648e0 | 2020-02-28 19:50:29 -0800 | [diff] [blame] | 2 | #include "aos/flatbuffer_merge.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 3 | #include "aos/init.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 4 | #include "aos/network/web_proxy.h" |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 5 | #include "aos/seasocks/seasocks_logger.h" |
| 6 | #include "gflags/gflags.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 7 | |
| 8 | #include "internal/Embedded.h" |
| 9 | #include "seasocks/Server.h" |
| 10 | #include "seasocks/WebSocket.h" |
| 11 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 12 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
| 13 | DEFINE_string(data_dir, "www", "Directory to serve data files from"); |
| 14 | |
| 15 | void RunDataThread( |
| 16 | std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> *subscribers, |
| 17 | const aos::FlatbufferDetachedBuffer<aos::Configuration> &config) { |
| 18 | aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 19 | const bool is_multi_node = |
| 20 | aos::configuration::MultiNode(event_loop.configuration()); |
| 21 | const aos::Node *self = |
| 22 | is_multi_node ? aos::configuration::GetMyNode(event_loop.configuration()) |
| 23 | : nullptr; |
| 24 | |
| 25 | LOG(INFO) << "My node is " << aos::FlatbufferToJson(self); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 26 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 27 | for (uint i = 0; i < config.message().channels()->size(); ++i) { |
| 28 | auto channel = config.message().channels()->Get(i); |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 29 | if (aos::configuration::ChannelIsReadableOnNode(channel, self)) { |
Alex Perry | 656dd5b | 2020-02-16 17:20:42 -0800 | [diff] [blame] | 30 | auto fetcher = event_loop.MakeRawFetcher(channel); |
| 31 | subscribers->emplace_back( |
| 32 | std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i)); |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 33 | } else { |
| 34 | subscribers->emplace_back(nullptr); |
Alex Perry | 656dd5b | 2020-02-16 17:20:42 -0800 | [diff] [blame] | 35 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | flatbuffers::FlatBufferBuilder fbb(1024); |
| 39 | |
| 40 | auto timer = event_loop.AddTimer([&]() { |
| 41 | for (auto &subscriber : *subscribers) { |
Austin Schuh | f6e7139 | 2020-02-26 23:10:15 -0800 | [diff] [blame] | 42 | if (subscriber != nullptr) { |
| 43 | subscriber->RunIteration(); |
| 44 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 45 | } |
| 46 | }); |
| 47 | |
| 48 | event_loop.OnRun([&]() { |
| 49 | timer->Setup(event_loop.monotonic_now(), std::chrono::milliseconds(100)); |
| 50 | }); |
| 51 | |
| 52 | event_loop.Run(); |
| 53 | } |
| 54 | |
| 55 | int main(int argc, char **argv) { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 56 | // Make sure to reference this to force the linker to include it. |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 57 | aos::InitGoogle(&argc, &argv); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 58 | findEmbeddedContent(""); |
| 59 | |
| 60 | aos::InitNRT(); |
| 61 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 62 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 63 | aos::configuration::ReadConfig(FLAGS_config); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 64 | |
Alex Perry | 09648e0 | 2020-02-28 19:50:29 -0800 | [diff] [blame] | 65 | for (size_t i = 0; i < config.message().channels()->size(); ++i) { |
| 66 | aos::Channel *channel = |
| 67 | config.mutable_message()->mutable_channels()->GetMutableObject(i); |
| 68 | channel->clear_schema(); |
| 69 | } |
| 70 | |
| 71 | config = aos::CopyFlatBuffer(&config.message()); |
| 72 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 73 | std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers; |
| 74 | |
| 75 | std::thread data_thread{ |
| 76 | [&subscribers, &config]() { RunDataThread(&subscribers, config); }}; |
| 77 | |
| 78 | seasocks::Server server(std::shared_ptr<seasocks::Logger>( |
| 79 | new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info))); |
| 80 | |
| 81 | auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>( |
| 82 | &server, subscribers, config); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 83 | server.addWebSocketHandler("/ws", websocket_handler); |
| 84 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 85 | server.serve(FLAGS_data_dir.c_str(), 8080); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 86 | } |