blob: 479320a6003a8179c26fa4d92e1167eb81adaec8 [file] [log] [blame]
Alex Perry5f474f22020-02-01 12:14:24 -08001#include "aos/events/shm_event_loop.h"
Alex Perryb3b50792020-01-18 16:13:45 -08002#include "aos/init.h"
Alex Perryb3b50792020-01-18 16:13:45 -08003#include "aos/network/web_proxy.h"
Alex Perry5f474f22020-02-01 12:14:24 -08004#include "aos/seasocks/seasocks_logger.h"
5#include "gflags/gflags.h"
Alex Perryb3b50792020-01-18 16:13:45 -08006
7#include "internal/Embedded.h"
8#include "seasocks/Server.h"
9#include "seasocks/WebSocket.h"
10
Alex Perry5f474f22020-02-01 12:14:24 -080011DEFINE_string(config, "./config.json", "File path of aos configuration");
12DEFINE_string(data_dir, "www", "Directory to serve data files from");
13
14void 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());
Alex Perry656dd5b2020-02-16 17:20:42 -080018 const aos::Node *self = aos::configuration::GetMyNode(&config.message());
Alex Perry5f474f22020-02-01 12:14:24 -080019
20 // TODO(alex): skip fetchers on the wrong node.
21 for (uint i = 0; i < config.message().channels()->size(); ++i) {
22 auto channel = config.message().channels()->Get(i);
Alex Perry656dd5b2020-02-16 17:20:42 -080023 if (!aos::configuration::ChannelIsReadableOnNode(channel, self)) {
24 subscribers->emplace_back(nullptr);
25 } else {
26 auto fetcher = event_loop.MakeRawFetcher(channel);
27 subscribers->emplace_back(
28 std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i));
29 }
Alex Perry5f474f22020-02-01 12:14:24 -080030 }
31
32 flatbuffers::FlatBufferBuilder fbb(1024);
33
34 auto timer = event_loop.AddTimer([&]() {
35 for (auto &subscriber : *subscribers) {
36 subscriber->RunIteration();
37 }
38 });
39
40 event_loop.OnRun([&]() {
41 timer->Setup(event_loop.monotonic_now(), std::chrono::milliseconds(100));
42 });
43
44 event_loop.Run();
45}
46
47int main(int argc, char **argv) {
Alex Perryb3b50792020-01-18 16:13:45 -080048 // Make sure to reference this to force the linker to include it.
Alex Perry5f474f22020-02-01 12:14:24 -080049 aos::InitGoogle(&argc, &argv);
Alex Perryb3b50792020-01-18 16:13:45 -080050 findEmbeddedContent("");
51
52 aos::InitNRT();
53
Alex Perry5f474f22020-02-01 12:14:24 -080054 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
55 aos::configuration::ReadConfig(FLAGS_config);
Alex Perryb3b50792020-01-18 16:13:45 -080056
Alex Perry5f474f22020-02-01 12:14:24 -080057 std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers;
58
59 std::thread data_thread{
60 [&subscribers, &config]() { RunDataThread(&subscribers, config); }};
61
62 seasocks::Server server(std::shared_ptr<seasocks::Logger>(
63 new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info)));
64
65 auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>(
66 &server, subscribers, config);
Alex Perryb3b50792020-01-18 16:13:45 -080067 server.addWebSocketHandler("/ws", websocket_handler);
68
Alex Perry5f474f22020-02-01 12:14:24 -080069 server.serve(FLAGS_data_dir.c_str(), 8080);
Alex Perryb3b50792020-01-18 16:13:45 -080070}