blob: adfd7804dcd0be4067a52279eb563c085780f7fa [file] [log] [blame]
Brian Silvermanea2c95f2021-02-10 18:10:26 -08001#include <unistd.h>
2
3#include <iostream>
4
5#include "aos/aos_cli_utils.h"
6#include "aos/configuration.h"
7#include "aos/init.h"
8#include "aos/json_to_flatbuffer.h"
9#include "gflags/gflags.h"
10#include "glog/logging.h"
11
James Kuszmaul27ff5ac2022-01-21 11:43:48 -080012DEFINE_double(rate, -1, "Rate at which to send the message (-1 to send once).");
13
Brian Silvermanea2c95f2021-02-10 18:10:26 -080014int main(int argc, char **argv) {
15 gflags::SetUsageMessage(
16 "Sends messages on arbitrary channels.\n"
17 "Typical Usage: aos_send [--config path_to_config.json]"
18 " channel_name message_type '{\"foo\": \"bar\"}'\n"
19 "Example usage: aos_send /test aos.examples.Ping "
20 "'{\"value\": 1}'");
21 aos::InitGoogle(&argc, &argv);
22
23 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070024 if (cli_info.Initialize(
25 &argc, &argv,
26 [&cli_info](const aos::Channel *channel) {
27 return aos::configuration::ChannelIsSendableOnNode(
28 channel, cli_info.event_loop->node());
29 },
30 false)) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -080031 return 0;
32 }
33 if (cli_info.found_channels.size() > 1) {
34 LOG(FATAL) << "Matched multiple channels, but may only send on 1";
35 }
36
37 if (argc == 1) {
38 LOG(FATAL) << "Must specify a message to send";
39 }
40
41 const aos::Channel *const channel = cli_info.found_channels[0];
42 const std::unique_ptr<aos::RawSender> sender =
43 cli_info.event_loop->MakeRawSender(channel);
44 flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(),
45 sender->fbb_allocator());
Austin Schuha36cfa82021-03-20 21:56:03 -070046 fbb.ForceDefaults(true);
Brian Silvermanea2c95f2021-02-10 18:10:26 -080047 fbb.Finish(aos::JsonToFlatbuffer(std::string_view(argv[1]), channel->schema(),
48 &fbb));
James Kuszmaul27ff5ac2022-01-21 11:43:48 -080049
50 if (FLAGS_rate < 0) {
51 sender->CheckOk(sender->Send(fbb.GetSize()));
52 } else {
53 cli_info.event_loop
54 ->AddTimer([&fbb, &sender]() {
55 sender->CheckOk(sender->Send(fbb.GetBufferPointer(), fbb.GetSize()));
56 })
57 ->Setup(cli_info.event_loop->monotonic_now(),
58 std::chrono::duration_cast<std::chrono::nanoseconds>(
59 std::chrono::duration<double>(1.0 / FLAGS_rate)));
60 cli_info.event_loop->Run();
61 }
Brian Silvermanea2c95f2021-02-10 18:10:26 -080062
63 return 0;
64}