blob: c674520aaa2c51193452edb4f08d1b7d467276a6 [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
Alex Perry5f474f22020-02-01 12:14:24 -080027 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 Perry09648e02020-02-28 19:50:29 -080065 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 Perry5f474f22020-02-01 12:14:24 -080073 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 Perryb3b50792020-01-18 16:13:45 -080083 server.addWebSocketHandler("/ws", websocket_handler);
84
Alex Perry5f474f22020-02-01 12:14:24 -080085 server.serve(FLAGS_data_dir.c_str(), 8080);
Alex Perryb3b50792020-01-18 16:13:45 -080086}