blob: 7ea3ff41f112f058414ac37e01d8fcd937abdcff [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());
Austin Schuhf6e71392020-02-26 23:10:15 -080018 const bool is_multi_node =
19 aos::configuration::MultiNode(event_loop.configuration());
20 const aos::Node *self =
21 is_multi_node ? aos::configuration::GetMyNode(event_loop.configuration())
22 : nullptr;
23
24 LOG(INFO) << "My node is " << aos::FlatbufferToJson(self);
Alex Perry5f474f22020-02-01 12:14:24 -080025
26 // TODO(alex): skip fetchers on the wrong node.
27 for (uint i = 0; i < config.message().channels()->size(); ++i) {
28 auto channel = config.message().channels()->Get(i);
Austin Schuhf6e71392020-02-26 23:10:15 -080029 if (aos::configuration::ChannelIsReadableOnNode(channel, self)) {
Alex Perry656dd5b2020-02-16 17:20:42 -080030 auto fetcher = event_loop.MakeRawFetcher(channel);
31 subscribers->emplace_back(
32 std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i));
Austin Schuhf6e71392020-02-26 23:10:15 -080033 } else {
34 subscribers->emplace_back(nullptr);
Alex Perry656dd5b2020-02-16 17:20:42 -080035 }
Alex Perry5f474f22020-02-01 12:14:24 -080036 }
37
38 flatbuffers::FlatBufferBuilder fbb(1024);
39
40 auto timer = event_loop.AddTimer([&]() {
41 for (auto &subscriber : *subscribers) {
Austin Schuhf6e71392020-02-26 23:10:15 -080042 if (subscriber != nullptr) {
43 subscriber->RunIteration();
44 }
Alex Perry5f474f22020-02-01 12:14:24 -080045 }
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
55int main(int argc, char **argv) {
Alex Perryb3b50792020-01-18 16:13:45 -080056 // Make sure to reference this to force the linker to include it.
Alex Perry5f474f22020-02-01 12:14:24 -080057 aos::InitGoogle(&argc, &argv);
Alex Perryb3b50792020-01-18 16:13:45 -080058 findEmbeddedContent("");
59
60 aos::InitNRT();
61
Alex Perry5f474f22020-02-01 12:14:24 -080062 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
63 aos::configuration::ReadConfig(FLAGS_config);
Alex Perryb3b50792020-01-18 16:13:45 -080064
Alex Perry5f474f22020-02-01 12:14:24 -080065 std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers;
66
67 std::thread data_thread{
68 [&subscribers, &config]() { RunDataThread(&subscribers, config); }};
69
70 seasocks::Server server(std::shared_ptr<seasocks::Logger>(
71 new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info)));
72
73 auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>(
74 &server, subscribers, config);
Alex Perryb3b50792020-01-18 16:13:45 -080075 server.addWebSocketHandler("/ws", websocket_handler);
76
Alex Perry5f474f22020-02-01 12:14:24 -080077 server.serve(FLAGS_data_dir.c_str(), 8080);
Alex Perryb3b50792020-01-18 16:13:45 -080078}