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, |
| 16 | std::string_view remote_name) { |
| 17 | CHECK(config->has_nodes()) << ": Config must have nodes to transfer."; |
| 18 | |
| 19 | flatbuffers::FlatBufferBuilder fbb; |
| 20 | fbb.ForceDefaults(true); |
| 21 | |
| 22 | flatbuffers::Offset<Node> node_offset = CopyFlatBuffer<Node>(my_node, &fbb); |
| 23 | const std::string_view node_name = my_node->name()->string_view(); |
| 24 | |
| 25 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 26 | for (const Channel *channel : *config->channels()) { |
| 27 | if (channel->has_destination_nodes()) { |
| 28 | for (const Connection *connection : *channel->destination_nodes()) { |
| 29 | if (connection->name()->string_view() == node_name && |
| 30 | channel->source_node()->string_view() == remote_name) { |
| 31 | channel_offsets.emplace_back(CopyFlatBuffer<Channel>(channel, &fbb)); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 38 | channels_offset = fbb.CreateVector(channel_offsets); |
| 39 | |
| 40 | Connect::Builder connect_builder(fbb); |
| 41 | connect_builder.add_channels_to_transfer(channels_offset); |
| 42 | connect_builder.add_node(node_offset); |
| 43 | fbb.Finish(connect_builder.Finish()); |
| 44 | |
| 45 | return fbb.Release(); |
| 46 | } |
| 47 | |
| 48 | } // namespace message_bridge |
| 49 | } // namespace aos |