Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 1 | #include <poll.h> |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 6 | #include "gflags/gflags.h" |
| 7 | #include "glog/logging.h" |
| 8 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 9 | #include "aos/aos_cli_utils.h" |
| 10 | #include "aos/configuration.h" |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/json_to_flatbuffer.h" |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 13 | |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 14 | DEFINE_double(rate, -1, "Rate at which to send the message (-1 to send once)."); |
| 15 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 16 | int main(int argc, char **argv) { |
| 17 | gflags::SetUsageMessage( |
| 18 | "Sends messages on arbitrary channels.\n" |
| 19 | "Typical Usage: aos_send [--config path_to_config.json]" |
| 20 | " channel_name message_type '{\"foo\": \"bar\"}'\n" |
| 21 | "Example usage: aos_send /test aos.examples.Ping " |
Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 22 | "'{\"value\": 1}'\n" |
| 23 | "Pipe usage: cat ping.json | aos_send /test/ aos.examples.Ping -"); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 24 | aos::InitGoogle(&argc, &argv); |
| 25 | |
| 26 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 27 | if (cli_info.Initialize( |
| 28 | &argc, &argv, |
| 29 | [&cli_info](const aos::Channel *channel) { |
| 30 | return aos::configuration::ChannelIsSendableOnNode( |
| 31 | channel, cli_info.event_loop->node()); |
| 32 | }, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 33 | "channel is sendable on node", false)) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | if (cli_info.found_channels.size() > 1) { |
| 37 | LOG(FATAL) << "Matched multiple channels, but may only send on 1"; |
| 38 | } |
| 39 | |
| 40 | if (argc == 1) { |
| 41 | LOG(FATAL) << "Must specify a message to send"; |
| 42 | } |
| 43 | |
Eric Schmiedeberg | 7055b7e | 2022-07-19 10:04:58 -0600 | [diff] [blame] | 44 | // Check if the user wants to use stdin (denoted by '-') or the argument |
| 45 | // present in argv for the message to send. CliUtilInfo will ensure the |
| 46 | // message data will be in argv[1] |
| 47 | std::string_view message_to_send{argv[1]}; |
| 48 | std::string stdin_data; |
| 49 | if (message_to_send == "-") { |
| 50 | // Read in everything from stdin, blocks when there's no data on stdin |
| 51 | stdin_data = std::string(std::istreambuf_iterator(std::cin), {}); |
| 52 | message_to_send = stdin_data; |
| 53 | } |
| 54 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 55 | const aos::Channel *const channel = cli_info.found_channels[0]; |
| 56 | const std::unique_ptr<aos::RawSender> sender = |
| 57 | cli_info.event_loop->MakeRawSender(channel); |
| 58 | flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(), |
| 59 | sender->fbb_allocator()); |
Austin Schuh | a36cfa8 | 2021-03-20 21:56:03 -0700 | [diff] [blame] | 60 | fbb.ForceDefaults(true); |
Austin Schuh | e54988c | 2024-04-30 12:14:02 -0700 | [diff] [blame] | 61 | flatbuffers::Offset<flatbuffers::Table> msg_offset = |
| 62 | aos::JsonToFlatbuffer(message_to_send, channel->schema(), &fbb); |
| 63 | |
| 64 | if (msg_offset.IsNull()) { |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | fbb.Finish(msg_offset); |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 69 | |
| 70 | if (FLAGS_rate < 0) { |
| 71 | sender->CheckOk(sender->Send(fbb.GetSize())); |
| 72 | } else { |
| 73 | cli_info.event_loop |
| 74 | ->AddTimer([&fbb, &sender]() { |
| 75 | sender->CheckOk(sender->Send(fbb.GetBufferPointer(), fbb.GetSize())); |
| 76 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 77 | ->Schedule(cli_info.event_loop->monotonic_now(), |
| 78 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 79 | std::chrono::duration<double>(1.0 / FLAGS_rate))); |
James Kuszmaul | 27ff5ac | 2022-01-21 11:43:48 -0800 | [diff] [blame] | 80 | cli_info.event_loop->Run(); |
| 81 | } |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |