Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 1 | #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 | |
| 12 | int main(int argc, char **argv) { |
| 13 | gflags::SetUsageMessage( |
| 14 | "Sends messages on arbitrary channels.\n" |
| 15 | "Typical Usage: aos_send [--config path_to_config.json]" |
| 16 | " channel_name message_type '{\"foo\": \"bar\"}'\n" |
| 17 | "Example usage: aos_send /test aos.examples.Ping " |
| 18 | "'{\"value\": 1}'"); |
| 19 | aos::InitGoogle(&argc, &argv); |
| 20 | |
| 21 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 22 | if (cli_info.Initialize( |
| 23 | &argc, &argv, |
| 24 | [&cli_info](const aos::Channel *channel) { |
| 25 | return aos::configuration::ChannelIsSendableOnNode( |
| 26 | channel, cli_info.event_loop->node()); |
| 27 | }, |
| 28 | false)) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | if (cli_info.found_channels.size() > 1) { |
| 32 | LOG(FATAL) << "Matched multiple channels, but may only send on 1"; |
| 33 | } |
| 34 | |
| 35 | if (argc == 1) { |
| 36 | LOG(FATAL) << "Must specify a message to send"; |
| 37 | } |
| 38 | |
| 39 | const aos::Channel *const channel = cli_info.found_channels[0]; |
| 40 | const std::unique_ptr<aos::RawSender> sender = |
| 41 | cli_info.event_loop->MakeRawSender(channel); |
| 42 | flatbuffers::FlatBufferBuilder fbb(sender->fbb_allocator()->size(), |
| 43 | sender->fbb_allocator()); |
Austin Schuh | a36cfa8 | 2021-03-20 21:56:03 -0700 | [diff] [blame] | 44 | fbb.ForceDefaults(true); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 45 | fbb.Finish(aos::JsonToFlatbuffer(std::string_view(argv[1]), channel->schema(), |
| 46 | &fbb)); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 47 | sender->CheckOk(sender->Send(fbb.GetSize())); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |