blob: 0e49760258b4447cf8f3d66497fcdb1fc6206e50 [file] [log] [blame]
Alex Perry5f474f22020-02-01 12:14:24 -08001#include "aos/events/shm_event_loop.h"
Alex Perry09648e02020-02-28 19:50:29 -08002#include "aos/flatbuffer_merge.h"
Alex Perryb3b50792020-01-18 16:13:45 -08003#include "aos/init.h"
Alex Perryb3b50792020-01-18 16:13:45 -08004#include "aos/network/web_proxy.h"
Alex Perry5f474f22020-02-01 12:14:24 -08005#include "aos/seasocks/seasocks_logger.h"
6#include "gflags/gflags.h"
Alex Perryb3b50792020-01-18 16:13:45 -08007
8#include "internal/Embedded.h"
9#include "seasocks/Server.h"
10#include "seasocks/WebSocket.h"
11
Alex Perry5f474f22020-02-01 12:14:24 -080012DEFINE_string(config, "./config.json", "File path of aos configuration");
13DEFINE_string(data_dir, "www", "Directory to serve data files from");
14
15void 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 Schuhf6e71392020-02-26 23:10:15 -080019 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 Perry5f474f22020-02-01 12:14:24 -080026
27 // TODO(alex): skip fetchers on the wrong node.
28 for (uint i = 0; i < config.message().channels()->size(); ++i) {
29 auto channel = config.message().channels()->Get(i);
Austin Schuhf6e71392020-02-26 23:10:15 -080030 if (aos::configuration::ChannelIsReadableOnNode(channel, self)) {
Alex Perry656dd5b2020-02-16 17:20:42 -080031 auto fetcher = event_loop.MakeRawFetcher(channel);
32 subscribers->emplace_back(
33 std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i));
Austin Schuhf6e71392020-02-26 23:10:15 -080034 } else {
35 subscribers->emplace_back(nullptr);
Alex Perry656dd5b2020-02-16 17:20:42 -080036 }
Alex Perry5f474f22020-02-01 12:14:24 -080037 }
38
39 flatbuffers::FlatBufferBuilder fbb(1024);
40
41 auto timer = event_loop.AddTimer([&]() {
42 for (auto &subscriber : *subscribers) {
Austin Schuhf6e71392020-02-26 23:10:15 -080043 if (subscriber != nullptr) {
44 subscriber->RunIteration();
45 }
Alex Perry5f474f22020-02-01 12:14:24 -080046 }
47 });
48
49 event_loop.OnRun([&]() {
50 timer->Setup(event_loop.monotonic_now(), std::chrono::milliseconds(100));
51 });
52
53 event_loop.Run();
54}
55
56int main(int argc, char **argv) {
Alex Perryb3b50792020-01-18 16:13:45 -080057 // Make sure to reference this to force the linker to include it.
Alex Perry5f474f22020-02-01 12:14:24 -080058 aos::InitGoogle(&argc, &argv);
Alex Perryb3b50792020-01-18 16:13:45 -080059 findEmbeddedContent("");
60
61 aos::InitNRT();
62
Alex Perry5f474f22020-02-01 12:14:24 -080063 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
64 aos::configuration::ReadConfig(FLAGS_config);
Alex Perryb3b50792020-01-18 16:13:45 -080065
Alex Perry09648e02020-02-28 19:50:29 -080066 for (size_t i = 0; i < config.message().channels()->size(); ++i) {
67 aos::Channel *channel =
68 config.mutable_message()->mutable_channels()->GetMutableObject(i);
69 channel->clear_schema();
70 }
71
72 config = aos::CopyFlatBuffer(&config.message());
73
Alex Perry5f474f22020-02-01 12:14:24 -080074 std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers;
75
76 std::thread data_thread{
77 [&subscribers, &config]() { RunDataThread(&subscribers, config); }};
78
79 seasocks::Server server(std::shared_ptr<seasocks::Logger>(
80 new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info)));
81
82 auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>(
83 &server, subscribers, config);
Alex Perryb3b50792020-01-18 16:13:45 -080084 server.addWebSocketHandler("/ws", websocket_handler);
85
Alex Perry5f474f22020-02-01 12:14:24 -080086 server.serve(FLAGS_data_dir.c_str(), 8080);
Alex Perryb3b50792020-01-18 16:13:45 -080087}