Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 1 | #include "aos/network/message_bridge_protocol.h" |
| 2 | |
| 3 | #include <string_view> |
| 4 | |
| 5 | #include "aos/configuration.h" |
| 6 | #include "aos/flatbuffer_merge.h" |
| 7 | #include "aos/flatbuffers.h" |
| 8 | #include "aos/network/connect_generated.h" |
| 9 | #include "flatbuffers/flatbuffers.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | namespace message_bridge { |
| 13 | |
| 14 | aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage( |
| 15 | const Configuration *config, const Node *my_node, |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame^] | 16 | std::string_view remote_name, const UUID &boot_uuid, |
| 17 | std::string_view config_sha256) { |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 18 | CHECK(config->has_nodes()) << ": Config must have nodes to transfer."; |
| 19 | |
| 20 | flatbuffers::FlatBufferBuilder fbb; |
| 21 | fbb.ForceDefaults(true); |
| 22 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 23 | flatbuffers::Offset<flatbuffers::String> boot_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 24 | boot_uuid.PackString(&fbb); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 25 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 26 | flatbuffers::Offset<Node> node_offset = |
| 27 | RecursiveCopyFlatBuffer<Node>(my_node, &fbb); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 28 | const std::string_view node_name = my_node->name()->string_view(); |
| 29 | |
| 30 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 31 | for (const Channel *channel : *config->channels()) { |
| 32 | if (channel->has_destination_nodes()) { |
| 33 | for (const Connection *connection : *channel->destination_nodes()) { |
| 34 | if (connection->name()->string_view() == node_name && |
| 35 | channel->source_node()->string_view() == remote_name) { |
Austin Schuh | a1250ce | 2020-03-15 22:48:15 -0700 | [diff] [blame] | 36 | // Remove the schema to save some space on the wire. |
| 37 | aos::FlatbufferDetachedBuffer<Channel> cleaned_channel = |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 38 | RecursiveCopyFlatBuffer<Channel>(channel); |
Austin Schuh | a1250ce | 2020-03-15 22:48:15 -0700 | [diff] [blame] | 39 | cleaned_channel.mutable_message()->clear_schema(); |
| 40 | channel_offsets.emplace_back( |
| 41 | CopyFlatBuffer<Channel>(&cleaned_channel.message(), &fbb)); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 48 | channels_offset = fbb.CreateVector(channel_offsets); |
| 49 | |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame^] | 50 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset = |
| 51 | fbb.CreateString(config_sha256); |
| 52 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 53 | Connect::Builder connect_builder(fbb); |
| 54 | connect_builder.add_channels_to_transfer(channels_offset); |
| 55 | connect_builder.add_node(node_offset); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 56 | connect_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame^] | 57 | connect_builder.add_config_sha256(config_sha256_offset); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 58 | fbb.Finish(connect_builder.Finish()); |
| 59 | |
| 60 | return fbb.Release(); |
| 61 | } |
| 62 | |
| 63 | } // namespace message_bridge |
| 64 | } // namespace aos |