From 3a1bd82a865a27ffb07ec21582a4533b3177f844 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 2 Feb 2019 14:59:59 +0200
Subject: [PATCH] Fix for initial net commit

---
 .gitignore                  |   1 +
 net/include/ftl/net.hpp     |  14 +++++
 net/include/ftl/net/raw.hpp |   6 +--
 net/include/ftl/uri.hpp     | 102 ++++++++++++++++++++++++++++++++++++
 net/test/net_raw.cpp        |  17 +++---
 p2p-remote-array/README.md  |   5 ++
 6 files changed, 133 insertions(+), 12 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 net/include/ftl/uri.hpp
 create mode 100644 p2p-remote-array/README.md

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000..386853da7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+net/build
diff --git a/net/include/ftl/net.hpp b/net/include/ftl/net.hpp
index e69de29bb..3252bf881 100644
--- a/net/include/ftl/net.hpp
+++ b/net/include/ftl/net.hpp
@@ -0,0 +1,14 @@
+#ifndef _FTL_NET_HPP_
+#define _FTL_NET_HPP_
+
+#include "ftl/net/raw.hpp"
+
+namespace ftl {
+namespace net {
+
+raw::Socket *connect(const char *uri) { return raw::connect(uri); }
+
+}
+}
+
+#endif // _FTL_NET_HPP_
diff --git a/net/include/ftl/net/raw.hpp b/net/include/ftl/net/raw.hpp
index 8c4317448..0dcd8e05b 100644
--- a/net/include/ftl/net/raw.hpp
+++ b/net/include/ftl/net/raw.hpp
@@ -1,5 +1,5 @@
-#ifndef _FTL_NET_HPP_
-#define _FTL_NET_HPP_
+#ifndef _FTL_NET_RAW_HPP_
+#define _FTL_NET_RAW_HPP_
 
 #include <functional>
 #include <sstream>
@@ -107,4 +107,4 @@ const int MAX_CONNECTIONS = 100; // TODO Is this a good number?
 } // net
 } // ftl
 
-#endif // _FTL_NET_HPP_
+#endif // _FTL_NET_RAW_HPP_
diff --git a/net/include/ftl/uri.hpp b/net/include/ftl/uri.hpp
new file mode 100644
index 000000000..142e8afd8
--- /dev/null
+++ b/net/include/ftl/uri.hpp
@@ -0,0 +1,102 @@
+#ifndef _FTL_URI_HPP_
+#define _FTL_URI_HPP_
+
+#include <uriparser/Uri.h>
+#include <string>
+#include <vector>
+
+namespace ftl {
+
+	typedef const char * uri_t;
+
+	class URI {
+		public:
+		URI(uri_t puri) {
+			UriUriA uri;
+
+			#ifdef HAVE_URIPARSESINGLE
+			const char *errpos;
+			if (uriParseSingleUriA(&uri, puri, &errpos) != URI_SUCCESS) {
+			#else
+			UriParserStateA uris;
+			uris.uri = &uri;
+			if (uriParseUriA(&uris, puri) != URI_SUCCESS) {
+			#endif
+				m_valid = false;
+				m_host = "none";
+				m_port = -1;
+				m_proto = SCHEME_NONE;
+				m_path = "";
+			} else {
+				m_host = std::string(uri.hostText.first, uri.hostText.afterLast - uri.hostText.first);
+				
+				std::string prototext = std::string(uri.scheme.first, uri.scheme.afterLast - uri.scheme.first);
+				if (prototext == "tcp") m_proto = SCHEME_TCP;
+				else if (prototext == "udp") m_proto = SCHEME_UDP;
+				else if (prototext == "ftl") m_proto = SCHEME_FTL;
+				else if (prototext == "http") m_proto = SCHEME_HTTP;
+				else if (prototext == "ws") m_proto = SCHEME_WS;
+				else if (prototext == "ipc") m_proto = SCHEME_IPC;
+				else m_proto = SCHEME_OTHER;
+
+				std::string porttext = std::string(uri.portText.first, uri.portText.afterLast - uri.portText.first);
+				m_port = atoi(porttext.c_str());
+
+				for (auto h=uri.pathHead; h!=NULL; h=h->next) {
+					auto pstr = std::string(
+							h->text.first, h->text.afterLast - h->text.first);
+
+					m_path += "/";
+					m_path += pstr;
+					m_pathseg.push_back(pstr);
+				}
+
+				m_query = std::string(uri.query.first, uri.query.afterLast - uri.query.first);
+
+				uriFreeUriMembersA(&uri);
+
+				m_valid = m_proto != SCHEME_NONE && m_host.size() > 0;
+
+				if (m_valid) {
+					if (m_query.size() > 0) m_base = std::string(uri.scheme.first, uri.query.first - uri.scheme.first - 1);
+					else m_base = std::string(uri.scheme.first);
+				}
+			}
+		}
+
+		~URI() {};
+
+		enum scheme_t : int {
+			SCHEME_NONE,
+			SCHEME_TCP,
+			SCHEME_UDP,
+			SCHEME_FTL,		// Future Tech Lab
+			SCHEME_HTTP,
+			SCHEME_WS,
+			SCHEME_IPC,
+			SCHEME_FILE,
+			SCHEME_OTHER
+		};
+
+		bool isValid() { return m_valid; };
+		std::string &getHost() { return m_host; };
+		int getPort() { return m_port; };
+		scheme_t getProtocol() { return m_proto; };
+		scheme_t getScheme() { return m_proto; };
+		std::string &getPath() { return m_path; };
+		std::string &getQuery() { return m_query; };
+		std::string &getBaseURI() { return m_base; };
+
+		private:
+		bool m_valid;
+		std::string m_host;
+		std::string m_path;
+		std::string m_base;
+		std::vector<std::string> m_pathseg;
+		int m_port;
+		scheme_t m_proto;
+		std::string m_query;
+	};
+}
+
+#endif // _FTL_URI_HPP_
diff --git a/net/test/net_raw.cpp b/net/test/net_raw.cpp
index aefaa890f..1aafbc4b3 100644
--- a/net/test/net_raw.cpp
+++ b/net/test/net_raw.cpp
@@ -1,7 +1,6 @@
 #include "catch.hpp"
 #include <string.h>
 #include <ftl/net/raw.hpp>
-#include <ftl/net/services.hpp>
 #include <iostream>
 
 
@@ -241,12 +240,12 @@ TEST_CASE("Socket.onMessage()", "[net]") {
 	accept_connection();
 
 	SECTION("small valid message") {
-		send_json(ftl::net::SERVICE_CONFIGURE, "{message: \"Hello\"}");
+		send_json(1, "{message: \"Hello\"}");
 
 		bool msg = false;
 
 		sock->onMessage([&](int service, std::string &data) {
-			REQUIRE(service == ftl::net::SERVICE_CONFIGURE);
+			REQUIRE(service == 1);
 			REQUIRE(data == "{message: \"Hello\"}");
 			msg = true;	
 		});
@@ -256,12 +255,12 @@ TEST_CASE("Socket.onMessage()", "[net]") {
 	}
 
 	SECTION("empty message") {
-		send_json(ftl::net::SERVICE_CONFIGURE, "");
+		send_json(1, "");
 
 		bool msg = false;
 
 		sock->onMessage([&](int service, std::string &data) {
-			REQUIRE(service == ftl::net::SERVICE_CONFIGURE);
+			REQUIRE(service == 1);
 			REQUIRE(data == "");
 			msg = true;	
 		});
@@ -271,13 +270,13 @@ TEST_CASE("Socket.onMessage()", "[net]") {
 	}
 
 	SECTION("multiple valid messages") {
-		send_json(ftl::net::SERVICE_CONFIGURE, "{message: \"Hello\"}");
-		send_json(ftl::net::SERVICE_CONFIGURE, "{test: \"world\"}");
+		send_json(1, "{message: \"Hello\"}");
+		send_json(1, "{test: \"world\"}");
 
 		int msg = 0;
 
 		sock->onMessage([&](int service, std::string &data) {
-			REQUIRE(service == ftl::net::SERVICE_CONFIGURE);
+			REQUIRE(service == 1);
 			if (msg == 0) REQUIRE(data == "{message: \"Hello\"}");
 			else REQUIRE(data == "{test: \"world\"}");
 			msg++;	
@@ -288,7 +287,7 @@ TEST_CASE("Socket.onMessage()", "[net]") {
 	}
 
 	SECTION("disconnected does not get message") {
-		send_json(ftl::net::SERVICE_CONFIGURE, "world");
+		send_json(1, "world");
 
 		bool msg = false;
 
diff --git a/p2p-remote-array/README.md b/p2p-remote-array/README.md
new file mode 100644
index 000000000..7f250a23b
--- /dev/null
+++ b/p2p-remote-array/README.md
@@ -0,0 +1,5 @@
+# Peer-2-Peer Remote Array Library
+Provides a remote memory access abstraction to allocate, read and write to arrays stored
+in different peers on the network.
+
+Requires `libftl-net`
-- 
GitLab