blob: 87114ed79b33f2e22640ff79d81529ed534648fc [file] [log] [blame]
Austin Schuh7bc59052020-02-16 23:48:33 -08001#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
11namespace aos {
12namespace message_bridge {
13
14aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage(
15 const Configuration *config, const Node *my_node,
Austin Schuh5e2bfb82021-03-13 22:46:55 -080016 std::string_view remote_name, const UUID &boot_uuid) {
Austin Schuh7bc59052020-02-16 23:48:33 -080017 CHECK(config->has_nodes()) << ": Config must have nodes to transfer.";
18
19 flatbuffers::FlatBufferBuilder fbb;
20 fbb.ForceDefaults(true);
21
Austin Schuh20ac95d2020-12-05 17:24:19 -080022 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -080023 boot_uuid.PackString(&fbb);
Austin Schuh20ac95d2020-12-05 17:24:19 -080024
Austin Schuha4fc60f2020-11-01 23:06:47 -080025 flatbuffers::Offset<Node> node_offset =
26 RecursiveCopyFlatBuffer<Node>(my_node, &fbb);
Austin Schuh7bc59052020-02-16 23:48:33 -080027 const std::string_view node_name = my_node->name()->string_view();
28
29 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
30 for (const Channel *channel : *config->channels()) {
31 if (channel->has_destination_nodes()) {
32 for (const Connection *connection : *channel->destination_nodes()) {
33 if (connection->name()->string_view() == node_name &&
34 channel->source_node()->string_view() == remote_name) {
Austin Schuha1250ce2020-03-15 22:48:15 -070035 // Remove the schema to save some space on the wire.
36 aos::FlatbufferDetachedBuffer<Channel> cleaned_channel =
Austin Schuha4fc60f2020-11-01 23:06:47 -080037 RecursiveCopyFlatBuffer<Channel>(channel);
Austin Schuha1250ce2020-03-15 22:48:15 -070038 cleaned_channel.mutable_message()->clear_schema();
39 channel_offsets.emplace_back(
40 CopyFlatBuffer<Channel>(&cleaned_channel.message(), &fbb));
Austin Schuh7bc59052020-02-16 23:48:33 -080041 }
42 }
43 }
44 }
45
46 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
47 channels_offset = fbb.CreateVector(channel_offsets);
48
49 Connect::Builder connect_builder(fbb);
50 connect_builder.add_channels_to_transfer(channels_offset);
51 connect_builder.add_node(node_offset);
Austin Schuh20ac95d2020-12-05 17:24:19 -080052 connect_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh7bc59052020-02-16 23:48:33 -080053 fbb.Finish(connect_builder.Finish());
54
55 return fbb.Release();
56}
57
58} // namespace message_bridge
59} // namespace aos