blob: 21b6f9c6141e34182fe9c9204d044f44b078c7cf [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());
18
19 // TODO(alex): skip fetchers on the wrong node.
20 for (uint i = 0; i < config.message().channels()->size(); ++i) {
21 auto channel = config.message().channels()->Get(i);
22 auto fetcher = event_loop.MakeRawFetcher(channel);
23 subscribers->emplace_back(
24 std::make_unique<aos::web_proxy::Subscriber>(std::move(fetcher), i));
25 }
26
27 flatbuffers::FlatBufferBuilder fbb(1024);
28
29 auto timer = event_loop.AddTimer([&]() {
30 for (auto &subscriber : *subscribers) {
31 subscriber->RunIteration();
32 }
33 });
34
35 event_loop.OnRun([&]() {
36 timer->Setup(event_loop.monotonic_now(), std::chrono::milliseconds(100));
37 });
38
39 event_loop.Run();
40}
41
42int main(int argc, char **argv) {
Alex Perryb3b50792020-01-18 16:13:45 -080043 // Make sure to reference this to force the linker to include it.
Alex Perry5f474f22020-02-01 12:14:24 -080044 aos::InitGoogle(&argc, &argv);
Alex Perryb3b50792020-01-18 16:13:45 -080045 findEmbeddedContent("");
46
47 aos::InitNRT();
48
Alex Perry5f474f22020-02-01 12:14:24 -080049 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
50 aos::configuration::ReadConfig(FLAGS_config);
Alex Perryb3b50792020-01-18 16:13:45 -080051
Alex Perry5f474f22020-02-01 12:14:24 -080052 std::vector<std::unique_ptr<aos::web_proxy::Subscriber>> subscribers;
53
54 std::thread data_thread{
55 [&subscribers, &config]() { RunDataThread(&subscribers, config); }};
56
57 seasocks::Server server(std::shared_ptr<seasocks::Logger>(
58 new aos::seasocks::SeasocksLogger(seasocks::Logger::Level::Info)));
59
60 auto websocket_handler = std::make_shared<aos::web_proxy::WebsocketHandler>(
61 &server, subscribers, config);
Alex Perryb3b50792020-01-18 16:13:45 -080062 server.addWebSocketHandler("/ws", websocket_handler);
63
Alex Perry5f474f22020-02-01 12:14:24 -080064 server.serve(FLAGS_data_dir.c_str(), 8080);
Alex Perryb3b50792020-01-18 16:13:45 -080065}