Store UUIDs as 16 bytes of data

This makes them much more efficient to write over shared memory to solve
the boot UUID logging problem when we add them.

Change-Id: Idf361d6b096bfa52cbc98f555c90bf1f6b90d3e0
diff --git a/aos/network/BUILD b/aos/network/BUILD
index 1d4b634..8e7d476 100644
--- a/aos/network/BUILD
+++ b/aos/network/BUILD
@@ -148,6 +148,7 @@
         "//aos:configuration",
         "//aos:flatbuffer_merge",
         "//aos:flatbuffers",
+        "//aos/events/logging:uuid",
         "@com_github_google_flatbuffers//:flatbuffers",
     ],
 )
diff --git a/aos/network/message_bridge_client_lib.cc b/aos/network/message_bridge_client_lib.cc
index 8a0ec34..97058f1 100644
--- a/aos/network/message_bridge_client_lib.cc
+++ b/aos/network/message_bridge_client_lib.cc
@@ -98,9 +98,9 @@
     std::vector<SctpClientChannelState> *channels, int client_index,
     MessageBridgeClientStatus *client_status)
     : event_loop_(event_loop),
-      connect_message_(
-          MakeConnectMessage(event_loop->configuration(), my_node, remote_name,
-                             event_loop->boot_uuid().string_view())),
+      connect_message_(MakeConnectMessage(event_loop->configuration(), my_node,
+                                          remote_name,
+                                          event_loop->boot_uuid())),
       message_reception_reply_(MakeMessageHeaderReply()),
       remote_node_(CHECK_NOTNULL(
           configuration::GetNode(event_loop->configuration(), remote_name))),
diff --git a/aos/network/message_bridge_protocol.cc b/aos/network/message_bridge_protocol.cc
index 53102c2..87114ed 100644
--- a/aos/network/message_bridge_protocol.cc
+++ b/aos/network/message_bridge_protocol.cc
@@ -13,14 +13,14 @@
 
 aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage(
     const Configuration *config, const Node *my_node,
-    std::string_view remote_name, std::string_view boot_uuid) {
+    std::string_view remote_name, const UUID &boot_uuid) {
   CHECK(config->has_nodes()) << ": Config must have nodes to transfer.";
 
   flatbuffers::FlatBufferBuilder fbb;
   fbb.ForceDefaults(true);
 
   flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
-      fbb.CreateString(boot_uuid);
+      boot_uuid.PackString(&fbb);
 
   flatbuffers::Offset<Node> node_offset =
       RecursiveCopyFlatBuffer<Node>(my_node, &fbb);
diff --git a/aos/network/message_bridge_protocol.h b/aos/network/message_bridge_protocol.h
index 5759a29..fed859b 100644
--- a/aos/network/message_bridge_protocol.h
+++ b/aos/network/message_bridge_protocol.h
@@ -4,6 +4,7 @@
 #include <string_view>
 
 #include "aos/configuration.h"
+#include "aos/events/logging/uuid.h"
 #include "aos/network/connect_generated.h"
 
 namespace aos {
@@ -33,7 +34,7 @@
 // Builds up a subscription request for my_node to remote_name.
 aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage(
     const Configuration *config, const Node *my_node,
-    std::string_view remote_name, std::string_view boot_uuid);
+    std::string_view remote_name, const UUID &boot_uuid);
 
 }  // namespace message_bridge
 }  // namespace aos
diff --git a/aos/network/message_bridge_server_lib.cc b/aos/network/message_bridge_server_lib.cc
index a96d08e..7328f6f 100644
--- a/aos/network/message_bridge_server_lib.cc
+++ b/aos/network/message_bridge_server_lib.cc
@@ -113,8 +113,7 @@
             peer.timestamp_logger->MakeBuilder();
 
         flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
-            builder.fbb()->CreateString(
-                server_status.BootUUID(peer.node_index));
+            server_status.BootUUID(peer.node_index).PackString(builder.fbb());
 
         RemoteMessage::Builder message_header_builder =
             builder.MakeBuilder<RemoteMessage>();
@@ -279,7 +278,7 @@
                            configuration::GetNode(event_loop->configuration(),
                                                   destination_node_name),
                            event_loop->node()->name()->string_view(),
-                           UUID::Zero().string_view())
+                           UUID::Zero())
             .span()
             .size());
     VLOG(1) << "Connection to " << destination_node_name << " has size "
@@ -405,7 +404,7 @@
                    ->name()
                    ->string_view();
     server_status_.ResetFilter(node_index);
-    server_status_.SetBootUUID(node_index, "");
+    server_status_.ClearBootUUID(node_index);
   }
 }
 
@@ -480,7 +479,8 @@
       }
     }
     server_status_.ResetFilter(node_index);
-    server_status_.SetBootUUID(node_index, connect->boot_uuid()->string_view());
+    server_status_.SetBootUUID(
+        node_index, UUID::FromString(connect->boot_uuid()->string_view()));
     VLOG(1) << "Resetting filters for " << node_index << " "
             << event_loop_->configuration()
                    ->nodes()
diff --git a/aos/network/message_bridge_server_status.cc b/aos/network/message_bridge_server_status.cc
index 6c58dd1..7788f4a 100644
--- a/aos/network/message_bridge_server_status.cc
+++ b/aos/network/message_bridge_server_status.cc
@@ -85,11 +85,8 @@
       statistics_.message().connections()->size());
 
   filters_.resize(event_loop->configuration()->nodes()->size());
-  boot_uuids_.resize(event_loop->configuration()->nodes()->size());
-  for (std::string &boot_uuid : boot_uuids_) {
-    // Make sure the memory gets allocated.
-    boot_uuid.reserve(UUID::kSize);
-  }
+  boot_uuids_.resize(event_loop->configuration()->nodes()->size(), UUID::Zero());
+  has_boot_uuids_.resize(event_loop->configuration()->nodes()->size(), false);
   timestamp_fetchers_.resize(event_loop->configuration()->nodes()->size());
   server_connection_.resize(event_loop->configuration()->nodes()->size());
 
@@ -144,12 +141,19 @@
 }
 
 void MessageBridgeServerStatus::SetBootUUID(int node_index,
-                                            std::string_view boot_uuid) {
+                                            const UUID &boot_uuid) {
+  has_boot_uuids_[node_index] = true;
   boot_uuids_[node_index] = boot_uuid;
   SendStatistics();
   last_statistics_send_time_ = event_loop_->monotonic_now();
 }
 
+void MessageBridgeServerStatus::ClearBootUUID(int node_index) {
+  has_boot_uuids_[node_index] = false;
+  SendStatistics();
+  last_statistics_send_time_ = event_loop_->monotonic_now();
+}
+
 void MessageBridgeServerStatus::ResetFilter(int node_index) {
   filters_[node_index].Reset();
   server_connection_[node_index]->mutate_monotonic_offset(0);
@@ -175,9 +179,9 @@
     flatbuffers::Offset<Node> node_offset = node_builder.Finish();
 
     flatbuffers::Offset<flatbuffers::String> boot_uuid_offset;
-    if (!boot_uuids_[node_index].empty() &&
-        connection->state() == State::CONNECTED) {
-      boot_uuid_offset = builder.fbb()->CreateString(boot_uuids_[node_index]);
+    if (connection->state() == State::CONNECTED &&
+        has_boot_uuids_[node_index]) {
+      boot_uuid_offset = boot_uuids_[node_index].PackString(builder.fbb());
     }
 
     ServerConnection::Builder server_connection_builder =
diff --git a/aos/network/message_bridge_server_status.h b/aos/network/message_bridge_server_status.h
index 680c189..52fc174 100644
--- a/aos/network/message_bridge_server_status.h
+++ b/aos/network/message_bridge_server_status.h
@@ -41,13 +41,13 @@
   // Resets the filter and clears the entry from the server statistics.
   void ResetFilter(int node_index);
   // Sets the boot UUID for the provided node.
-  void SetBootUUID(int node_index, std::string_view boot_uuid);
+  void SetBootUUID(int node_index, const UUID &boot_uuid);
+  // Clears the boot UUID for the provided node.
+  void ClearBootUUID(int node_index);
 
   // Returns the boot UUID for a node, or an empty string_view if there isn't
   // one.
-  std::string_view BootUUID(int node_index) const {
-    return boot_uuids_[node_index];
-  }
+  const UUID &BootUUID(int node_index) const { return boot_uuids_[node_index]; }
 
   // Returns the ServerConnection message which is updated by the server.
   ServerConnection *FindServerConnection(std::string_view node_name);
@@ -93,7 +93,8 @@
   std::vector<ClippedAverageFilter> filters_;
 
   // List of UUIDs for each node.
-  std::vector<std::string> boot_uuids_;
+  std::vector<UUID> boot_uuids_;
+  std::vector<bool> has_boot_uuids_;
 
   // Sender for the timestamps that we are forwarding over the network.
   aos::Sender<Timestamp> timestamp_sender_;