From c54a41bedf119f17b81b65da80a095fcfc5257de Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 26 Feb 2019 14:38:53 +0200
Subject: [PATCH] Switch to weaker protocol handshake mechanism

---
 README.md                      |  7 +++++--
 net/include/ftl/net/socket.hpp |  1 +
 net/src/listener.cpp           | 10 ++++++++++
 net/src/socket.cpp             | 29 +++++++++++++----------------
 net/test/net_integration.cpp   |  1 +
 5 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index 2efa5b69a..883249798 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,10 @@ This monorepo contains all elements of the FTL software system.
 
 * p2p-remote-array : Access 2D memory arrays on other machines in P2P group
 * fabric : C++ library implementing the neural fabric
-* fabric-modules : custom native fabric relations
-* fabric-js : Node.js bindings for fabric
+* fabric-js : Node.js bindings for C++ fabric
+* fabric-js-modules : Additional fabric-js based native and non-native components
+* peer-node : P2P node.js application providing a web-service interface
+* front-end : Client side FTL code
+* www : FTL Website
 
 The architecture relies on NodeJS with C++ native addons for processing.
diff --git a/net/include/ftl/net/socket.hpp b/net/include/ftl/net/socket.hpp
index 09d0852e3..96786e4fb 100644
--- a/net/include/ftl/net/socket.hpp
+++ b/net/include/ftl/net/socket.hpp
@@ -172,6 +172,7 @@ class Socket {
 	
 	std::string uri_;
 	std::string peerid_;
+	std::string remote_proto_;
 
 	Protocol *proto_; 
 	
diff --git a/net/src/listener.cpp b/net/src/listener.cpp
index c7cc7ed02..6fd975d1b 100644
--- a/net/src/listener.cpp
+++ b/net/src/listener.cpp
@@ -111,11 +111,21 @@ Listener::~Listener() {
 }
 
 void Listener::connection(shared_ptr<Socket> &s) {
+	Handshake hs1;
+	hs1.magic = ftl::net::MAGIC;
+	hs1.name_size = 0;
+	
 	if (default_proto_) {
 		s->setProtocol(default_proto_);
+		hs1.proto_size = default_proto_->id().size();
+		s->send(FTL_PROTOCOL_HS1, hs1, default_proto_->id());
 	} else {
 		s->setProtocol(NULL);
+		hs1.proto_size = 0;
+		s->send(FTL_PROTOCOL_HS1, hs1);
 	}
+	
+	LOG(INFO) << "Handshake initiated with " << s->getURI();
 	for (auto h : handler_connect_) h(s);
 }
 
diff --git a/net/src/socket.cpp b/net/src/socket.cpp
index d6765b1ec..0eadd46ea 100644
--- a/net/src/socket.cpp
+++ b/net/src/socket.cpp
@@ -211,20 +211,23 @@ void Socket::setProtocol(Protocol *p) {
 		if (proto_ == p) return;
 		if (proto_ && proto_->id() == p->id()) return;
 		
+		if (remote_proto_ != "") {
+			Handshake hs1;
+			hs1.magic = ftl::net::MAGIC;
+			hs1.name_size = 0;
+			hs1.proto_size = p->id().size();
+			send(FTL_PROTOCOL_HS1, hs1, p->id());
+			LOG(INFO) << "Handshake initiated with " << uri_;
+		}
+		
 		proto_ = p;
-		Handshake hs1;
-		hs1.magic = ftl::net::MAGIC;
-		hs1.name_size = 0;
-		hs1.proto_size = p->id().size();
-		send(FTL_PROTOCOL_HS1, hs1, p->id());
-		LOG(INFO) << "Handshake initiated with " << uri_;
 	} else {
-		Handshake hs1;
+		/*Handshake hs1;
 		hs1.magic = ftl::net::MAGIC;
 		hs1.name_size = 0;
 		hs1.proto_size = 0;
 		send(FTL_PROTOCOL_HS1, hs1);
-		LOG(INFO) << "Handshake initiated with " << uri_;
+		LOG(INFO) << "Handshake initiated with " << uri_;*/
 	}
 }
 
@@ -331,14 +334,8 @@ void Socket::handshake1() {
 	if (header.proto_size > 0) read(protouri,header.proto_size);
 
 	if (protouri.size() > 0) {
-		auto proto = Protocol::find(protouri);
-		if (proto == NULL) {
-			LOG(ERROR) << "Protocol (" << protouri << ") not found during handshake for " << uri_;
-			close();
-			return;
-		} else {
-			proto_ = proto;
-		}
+		remote_proto_ = protouri;
+		// TODO Validate protocols with local protocol?
 	}
 
 	send(FTL_PROTOCOL_HS2); // TODO Counterpart protocol.
diff --git a/net/test/net_integration.cpp b/net/test/net_integration.cpp
index 20b2c6760..86a478d8d 100644
--- a/net/test/net_integration.cpp
+++ b/net/test/net_integration.cpp
@@ -211,6 +211,7 @@ TEST_CASE("Net Integration", "[integrate]") {
 	l->onConnection([&s1](auto &s) { s1 = s; });
 	
 	shared_ptr<Socket> s2 = ftl::net::connect("tcp://localhost:9000");
+	s2->setProtocol(&p);
 	
 	REQUIRE( s2 != nullptr );
 	ftl::net::wait([&s2]() { return s2->isConnected(); });
-- 
GitLab