From 7b84b34c20a9badfe867466e8ef9ac8862179bf2 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Wed, 20 Feb 2019 19:39:37 +0200
Subject: [PATCH] Start p2p net implementation

---
 p2p-rm/include/ftl/p2p-rm/protocol.hpp | 15 +++++++++++++
 p2p-rm/src/blob.cpp                    | 29 --------------------------
 p2p-rm/src/p2prm.cpp                   | 27 ++++++++++++++++++++++++
 p2p-rm/test/p2p-rm.cpp                 | 11 ++++++++++
 4 files changed, 53 insertions(+), 29 deletions(-)
 create mode 100644 p2p-rm/include/ftl/p2p-rm/protocol.hpp

diff --git a/p2p-rm/include/ftl/p2p-rm/protocol.hpp b/p2p-rm/include/ftl/p2p-rm/protocol.hpp
new file mode 100644
index 000000000..888106b88
--- /dev/null
+++ b/p2p-rm/include/ftl/p2p-rm/protocol.hpp
@@ -0,0 +1,15 @@
+#ifndef _FTL_P2P_RM_PROTOCOL_HPP_
+#define _FTL_P2P_RM_PROTOCOL_HPP_
+
+/* To get the service space for p2p */
+#include <ftl/net/protocol.hpp>
+
+#define P2P_SYNC				(FTL_PROTOCOL_P2P + 1)
+#define P2P_REQUESTOWNERSHIP	(FTL_PROTOCOL_P2P + 2)
+#define P2P_FINDOWNER			(FTL_PROTOCOL_P2P + 3)
+#define P2P_NOTIFYOWNERSHIP		(FTL_PROTOCOL_P2P + 4)
+#define P2P_URISEARCH			(FTL_PROTOCOL_P2P + 5)
+#define P2P_PEERSEARCH			(FTL_PROTOCOL_P2P + 6)
+
+#endif // _FTL_P2P_RM_PROTOCOL_HPP_
+
diff --git a/p2p-rm/src/blob.cpp b/p2p-rm/src/blob.cpp
index 3b8b1beef..440832de1 100644
--- a/p2p-rm/src/blob.cpp
+++ b/p2p-rm/src/blob.cpp
@@ -1,33 +1,4 @@
 #include <memory.h>
-#include <ftl/net.hpp>
-
-#include "ftl/p2p-rm/blob.hpp"
-
-#define MEMORY_SYNC		0x1000
-
-struct Header {
-	uint32_t blobid;
-	uint32_t offset;
-	uint32_t size;
-};
-
-void ftl::rm::Blob::sync(size_t offset, size_t size) {
-	// Sanity check
-	if (offset + size > size_) throw -1;
-
-	// TODO Delay send to collate many write operations?
-
-	if (sockets_.size() > 0) {
-		Header header{blobid_,static_cast<uint32_t>(offset),static_cast<uint32_t>(size)};
-	
-		// If local, write direct to data_, otherwise send over network
-		for (auto s : sockets_) {
-			// Send over network
-			s->send2(MEMORY_SYNC, std::string((const char*)&header,sizeof(header)),
-				std::string(&data_[offset],size));
-		}
-	}
-}
 
 /*void ftl::rm::Blob::write(size_t offset, const char *data, size_t size) {
 	// Sanity check
diff --git a/p2p-rm/src/p2prm.cpp b/p2p-rm/src/p2prm.cpp
index 1281c51ea..6f91b1da4 100644
--- a/p2p-rm/src/p2prm.cpp
+++ b/p2p-rm/src/p2prm.cpp
@@ -1,6 +1,10 @@
 #include "ftl/p2p-rm.hpp"
+#include "ftl/p2p-rm/blob.hpp"
+#include "ftl/p2p-rm/protocol.hpp"
 
 #include <ftl/uri.hpp>
+#include <ftl/net.hpp>
+
 #include <map>
 #include <string>
 
@@ -13,6 +17,29 @@ void ftl::rm::reset() {
 	blobs.clear();
 }
 
+struct SyncHeader {
+	uint32_t blobid;
+	uint32_t offset;
+	uint32_t size;
+};
+
+void ftl::rm::_sync(const ftl::rm::Blob &blob, size_t offset, size_t size) {
+	// Sanity check
+	if (offset + size > size_) throw -1;
+
+	// TODO Delay send to collate many write operations?
+
+	if (blob.sockets_.size() > 0) {
+		SyncHeader header{blob.blobid_,static_cast<uint32_t>(offset),static_cast<uint32_t>(size)};
+	
+		for (auto s : blob.sockets_) {
+			// Send over network
+			s->send2(P2P_SYNC, std::string((const char*)&header,sizeof(header)),
+				std::string(&data_[offset],size));
+		}
+	}
+}
+
 ftl::rm::Blob *ftl::rm::_lookup(const char *uri) {
 	URI u(uri);
 	if (!u.isValid()) return NULL;
diff --git a/p2p-rm/test/p2p-rm.cpp b/p2p-rm/test/p2p-rm.cpp
index d124341a3..663e3574d 100644
--- a/p2p-rm/test/p2p-rm.cpp
+++ b/p2p-rm/test/p2p-rm.cpp
@@ -1,9 +1,13 @@
 #include "catch.hpp"
 #include <ftl/p2p-rm.hpp>
+#include <vector>
 
 // --- Mock Socket Send
 
+static std::vector<uint32_t> msgs;
+
 int ftl::net::raw::Socket::send2(uint32_t service, const std::string &data1, const std::string &data2) {
+	msgs.push_back(service);
 	return 0;
 }
 
@@ -73,5 +77,12 @@ SCENARIO( "Getting a read_ref", "[get]" ) {
 		REQUIRE( r.pointer().is_local() );
 		REQUIRE( r == 89 );
 	}
+	
+	GIVEN( "a valid URI to remote memory" ) {
+		const auto r = ftl::rm::getReadable<int>("ftl://uti.fi/memory/remote0");
+		REQUIRE( r.is_valid() );
+		REQUIRE( !r.pointer().is_local() );
+		REQUIRE( r == 888 );
+	}
 }
 
-- 
GitLab