From a0414dd9aa6db92cdf4bf63ec1031118a5c165a4 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Wed, 18 May 2022 14:22:10 +0000
Subject: [PATCH] #40 Remove msgpack from uuid header

---
 include/ftl/uuid.hpp      |  8 ++------
 src/peer.cpp              |  5 +++--
 src/protocol.hpp          |  3 ++-
 src/self.cpp              |  4 ++--
 src/streams/netstream.cpp |  8 +++++---
 src/uuidMSGPACK.hpp       | 21 +++++++++++++++++++++
 test/mocks/connection.cpp |  3 ++-
 test/netstream_unit.cpp   |  3 ++-
 8 files changed, 39 insertions(+), 16 deletions(-)
 create mode 100644 src/uuidMSGPACK.hpp

diff --git a/include/ftl/uuid.hpp b/include/ftl/uuid.hpp
index e542074..06a6adf 100644
--- a/include/ftl/uuid.hpp
+++ b/include/ftl/uuid.hpp
@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <cstring>
 #include <ftl/lib/loguru.hpp>
 
 #ifndef WIN32
@@ -19,8 +20,6 @@
 #include <string>
 #include <functional>
 
-#include <msgpack.hpp>
-
 namespace ftl {
 /**
  * C++ Wrapper for libuuid. The default constructor generates a new UUID.
@@ -128,10 +127,7 @@ class UUID {
 */
     }
 
-    /* Allow the UUID to be packed into an RPC message. */
-    MSGPACK_DEFINE(uuid_);
-
- private:
+ protected:
 #ifdef WIN32
     union {
         _GUID guid_;
diff --git a/src/peer.cpp b/src/peer.cpp
index 040f709..3aac02d 100644
--- a/src/peer.cpp
+++ b/src/peer.cpp
@@ -21,6 +21,7 @@
 #include <ftl/uri.hpp>
 #include <ftl/time.hpp>
 #include "peer.hpp"
+#include "uuidMSGPACK.hpp"
 
 #include "protocol/connection.hpp"
 
@@ -77,7 +78,7 @@ void Peer::_send_handshake() {
             << " peer) handshake sent, status: "
             << (isConnected() ? "connected" : "connecting");
 
-    send("__handshake__", ftl::net::kMagic, ftl::net::kVersion, net_->id());
+    send("__handshake__", ftl::net::kMagic, ftl::net::kVersion, ftl::UUIDMSGPACK(net_->id()));
 }
 
 void Peer::_process_handshake(uint64_t magic, uint32_t version, const UUID &pid) {
@@ -113,7 +114,7 @@ void Peer::_process_handshake(uint64_t magic, uint32_t version, const UUID &pid)
 
 void Peer::_bind_rpc() {
     // Install return handshake handler.
-    bind("__handshake__", [this](uint64_t magic, uint32_t version, const UUID &pid) {
+    bind("__handshake__", [this](uint64_t magic, uint32_t version, const ftl::UUIDMSGPACK &pid) {
         _process_handshake(magic, version, pid);
     });
 
diff --git a/src/protocol.hpp b/src/protocol.hpp
index 0806544..c9facb7 100644
--- a/src/protocol.hpp
+++ b/src/protocol.hpp
@@ -11,11 +11,12 @@
 #include <ftl/protocol/config.h>
 #include <tuple>
 #include <ftl/uuid.hpp>
+#include "uuidMSGPACK.hpp"
 
 namespace ftl {
 namespace net {
 
-typedef std::tuple<uint64_t, uint32_t, ftl::UUID> Handshake;
+typedef std::tuple<uint64_t, uint32_t, ftl::UUIDMSGPACK> Handshake;
 
 static const uint64_t kMagic = 0x0009340053640912;
 static const uint32_t kVersion = (FTL_VERSION_MAJOR << 16) +
diff --git a/src/self.cpp b/src/self.cpp
index 1c4692a..f7a21b8 100644
--- a/src/self.cpp
+++ b/src/self.cpp
@@ -9,7 +9,7 @@
 #include "./streams/netstream.hpp"
 #include "./streams/filestream.hpp"
 #include <ftl/lib/nlohmann/json.hpp>
-#include <ftl/uuid.hpp>
+#include "uuidMSGPACK.hpp"
 
 using ftl::protocol::Self;
 using ftl::protocol::FrameID;
@@ -141,7 +141,7 @@ std::vector<std::string> Self::getStreams() {
 }
 
 std::shared_ptr<ftl::protocol::Node> Self::locateStream(const std::string &uri) {
-    auto p = universe_->findOne<ftl::UUID>("find_stream", uri);
+    auto p = universe_->findOne<ftl::UUIDMSGPACK>("find_stream", uri);
 
     if (!p) return nullptr;
     auto peer = universe_->getPeer(*p);
diff --git a/src/streams/netstream.cpp b/src/streams/netstream.cpp
index 1097676..a0442ae 100644
--- a/src/streams/netstream.cpp
+++ b/src/streams/netstream.cpp
@@ -8,6 +8,7 @@
 #include <string>
 #include "netstream.hpp"
 #include <ftl/time.hpp>
+#include "../uuidMSGPACK.hpp"
 #include "packetMsgpack.hpp"
 
 #define LOGURU_REPLACE_GLOG 1
@@ -48,7 +49,7 @@ static std::list<std::string> net_streams;
 static SHARED_MUTEX stream_mutex;
 
 void Net::installRPC(ftl::net::Universe *net) {
-    net->bind("find_stream", [net](const std::string &uri) -> optional<ftl::UUID> {
+    net->bind("find_stream", [net](const std::string &uri) -> optional<ftl::UUIDMSGPACK> {
         DLOG(INFO) << "Request for stream: " << uri;
 
         ftl::URI u1(uri);
@@ -59,7 +60,8 @@ void Net::installRPC(ftl::net::Universe *net) {
             ftl::URI u2(s);
             // Don't compare query string components.
             if (base == u2.getBaseURI()) {
-                return std::reference_wrapper(net->id());
+                ftl::UUIDMSGPACK mpuuid(net->id());
+                return std::reference_wrapper(mpuuid);
             }
         }
         return {};
@@ -321,7 +323,7 @@ bool Net::_enable(FrameID id) {
 
     // not hosting, try to find peer now
     // First find non-proxy version, then check for proxy version if no match
-    auto p = net_->findOne<ftl::UUID>("find_stream", uri_);
+    auto p = net_->findOne<ftl::UUIDMSGPACK>("find_stream", uri_);
 
     if (p) {
         peer_ = *p;
diff --git a/src/uuidMSGPACK.hpp b/src/uuidMSGPACK.hpp
new file mode 100644
index 0000000..3bed536
--- /dev/null
+++ b/src/uuidMSGPACK.hpp
@@ -0,0 +1,21 @@
+/**
+ * @file uuidMSGPACK.hpp
+ * @copyright Copyright (c) 2022 University of Turku, MIT License
+ * @author Nicolas Pope
+ */
+
+#pragma once
+
+#include <ftl/uuid.hpp>
+#include <msgpack.hpp>
+
+namespace ftl {
+
+class UUIDMSGPACK : public ftl::UUID {
+ public:
+    UUIDMSGPACK() : ftl::UUID() {}
+    explicit UUIDMSGPACK(const ftl::UUID &u) : ftl::UUID(u) {}
+    MSGPACK_DEFINE(uuid_);
+};
+
+}
diff --git a/test/mocks/connection.cpp b/test/mocks/connection.cpp
index c47bc76..98de3ef 100644
--- a/test/mocks/connection.cpp
+++ b/test/mocks/connection.cpp
@@ -3,6 +3,7 @@
 #include "../../src/socket.hpp"
 #include "../../src/universe.hpp"
 #include "../../src/protocol/connection.hpp"
+#include "../../src/uuidMSGPACK.hpp"
 #include <ftl/protocol/self.hpp>
 #include <chrono>
 
@@ -77,7 +78,7 @@ ftl::net::PeerPtr createMockPeer(int c) {
 
 void send_handshake(ftl::net::Peer &p) {
 	ftl::UUID id;
-	p.send("__handshake__", ftl::net::kMagic, ((8 << 16) + (5 << 8) + 2), id);
+	p.send("__handshake__", ftl::net::kMagic, ((8 << 16) + (5 << 8) + 2), ftl::UUIDMSGPACK(id));
 }
 
 void provideResponses(const ftl::net::PeerPtr &p, int c, const std::vector<std::tuple<bool,std::string,msgpack::object>> &responses) {
diff --git a/test/netstream_unit.cpp b/test/netstream_unit.cpp
index 168b754..71701b0 100644
--- a/test/netstream_unit.cpp
+++ b/test/netstream_unit.cpp
@@ -10,6 +10,7 @@
 
 #include "../src/streams/netstream.hpp"
 #include "../src/streams/packetMsgpack.hpp"
+#include "../src/uuidMSGPACK.hpp"
 #include "mocks/connection.hpp"
 
 using ftl::protocol::FrameID;
@@ -100,7 +101,7 @@ TEST_CASE("Net stream sending requests") {
 		std::thread thr([&p]() {
             auto z = std::make_unique<msgpack::zone>();
             provideResponses(p, 0, {
-                {false, "find_stream", packResponse(*z, p->id())},
+                {false, "find_stream", packResponse(*z, ftl::UUIDMSGPACK(p->id()))},
                 {true, "enable_stream", {}},
             });
 		});
-- 
GitLab