From d4153fb248b5545bea1456afe65c2fa5ab87e663 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 11 Apr 2019 14:28:41 +0300
Subject: [PATCH] Fix dispatcher response format, fix dispatch chaining bug and
 add working test for findOwner

---
 net/cpp/include/ftl/net/dispatcher.hpp |  2 +-
 net/cpp/include/ftl/net/universe.hpp   |  3 ++-
 net/cpp/src/dispatcher.cpp             | 14 +++++++++-----
 net/cpp/src/peer.cpp                   |  2 +-
 net/cpp/src/universe.cpp               |  5 +++--
 net/cpp/test/net_integration.cpp       | 17 +++++++++++++++++
 6 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/net/cpp/include/ftl/net/dispatcher.hpp b/net/cpp/include/ftl/net/dispatcher.hpp
index ba240b8c2..7291adb43 100644
--- a/net/cpp/include/ftl/net/dispatcher.hpp
+++ b/net/cpp/include/ftl/net/dispatcher.hpp
@@ -132,7 +132,7 @@ class Dispatcher {
     using notification_t = std::tuple<int8_t, std::string, msgpack::object>;
     
     using response_t =
-        std::tuple<uint32_t, uint32_t, msgpack::object, msgpack::object>;
+        std::tuple<uint32_t, uint32_t, std::string, msgpack::object>;
 	
 	private:
 	Dispatcher *parent_;
diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp
index 18adf6ce1..bfda37aee 100644
--- a/net/cpp/include/ftl/net/universe.hpp
+++ b/net/cpp/include/ftl/net/universe.hpp
@@ -97,6 +97,8 @@ class Universe {
 	 * publishing to this resource and before any peers attempt to subscribe.
 	 */
 	bool createResource(const std::string &uri);
+
+	std::optional<ftl::UUID> findOwner(const std::string &res);
 	
 	private:
 	void _run();
@@ -104,7 +106,6 @@ class Universe {
 	void _installBindings();
 	void _installBindings(Peer *);
 	bool _subscribe(const std::string &res);
-	std::optional<ftl::UUID> _findOwner(const std::string &res);
 	
 	static void __start(Universe *u);
 	
diff --git a/net/cpp/src/dispatcher.cpp b/net/cpp/src/dispatcher.cpp
index 1e71b9b9d..247a829bc 100644
--- a/net/cpp/src/dispatcher.cpp
+++ b/net/cpp/src/dispatcher.cpp
@@ -70,11 +70,12 @@ void ftl::net::Dispatcher::dispatch_call(Peer &s, const msgpack::object &msg) {
     } else if (type == 0) {
 		LOG(INFO) << "RPC " << name << "() <- " << s.getURI();
 
-		auto it_func = funcs_.find(name);
+		auto func = _locateHandler(name);
 
-		if (it_func != end(funcs_)) {
+		if (func) {
+			LOG(INFO) << "Found binding for " << name;
 		    try {
-		        auto result = (it_func->second)(args); //->get();
+		        auto result = (*func)(args); //->get();
 		        s._sendResponse(id, result->get());
 		        /*response_t res_obj = std::make_tuple(1,id,msgpack::object(),result->get());
 				std::stringstream buf;
@@ -82,22 +83,25 @@ void ftl::net::Dispatcher::dispatch_call(Peer &s, const msgpack::object &msg) {
 				s.send("__return__", buf.str());*/
 			} catch (const std::exception &e) {
 				//throw;
-				//LOG(ERROR) << "Exception when attempting to call RPC (" << e << ")";
+				LOG(ERROR) << "Exception when attempting to call RPC (" << e.what() << ")";
 		        /*response_t res_obj = std::make_tuple(1,id,msgpack::object(e.what()),msgpack::object());
 				std::stringstream buf;
 				msgpack::pack(buf, res_obj);			
 				s.send("__return__", buf.str());*/
 			} catch (int e) {
 				//throw;
-				//LOG(ERROR) << "Exception when attempting to call RPC (" << e << ")";
+				LOG(ERROR) << "Exception when attempting to call RPC (" << e << ")";
 		        /*response_t res_obj = std::make_tuple(1,id,msgpack::object(e),msgpack::object());
 				std::stringstream buf;
 				msgpack::pack(buf, res_obj);			
 				s.send("__return__", buf.str());*/
 			}
+		} else {
+			LOG(WARNING) << "No binding found for " << name;
 		}
 	} else {
 		// TODO(nick) Some error
+		LOG(ERROR) << "Unrecognised message type";
 	}
 }
 
diff --git a/net/cpp/src/peer.cpp b/net/cpp/src/peer.cpp
index 60c1e122f..f0dd6784d 100644
--- a/net/cpp/src/peer.cpp
+++ b/net/cpp/src/peer.cpp
@@ -476,7 +476,7 @@ void Peer::cancelCall(int id) {
 }
 
 void Peer::_sendResponse(uint32_t id, const msgpack::object &res) {
-	Dispatcher::response_t res_obj = std::make_tuple(1,id,msgpack::object(),res);
+	Dispatcher::response_t res_obj = std::make_tuple(1,id,std::string(""),res);
 	msgpack::pack(send_buf_, res_obj);
 	_send();
 }
diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp
index 1ccd80bb9..13036d103 100644
--- a/net/cpp/src/universe.cpp
+++ b/net/cpp/src/universe.cpp
@@ -119,6 +119,7 @@ void Universe::_installBindings() {
 	});
 	
 	bind("__owner__", [this](const std::string &res) -> optional<UUID> {
+		LOG(INFO) << "SOMEONE ASKS FOR " << res;
 		if (owned_.count(res) > 0) return ftl::net::this_peer;
 		else return {};
 	});
@@ -130,7 +131,7 @@ Peer *Universe::getPeer(const UUID &id) const {
 	else return ix->second;
 }
 
-optional<UUID> Universe::_findOwner(const string &res) {
+optional<UUID> Universe::findOwner(const string &res) {
 	// TODO(nick) cache this information
 	return findOne<UUID>("__owner__", res);
 }
@@ -142,7 +143,7 @@ bool Universe::createResource(const std::string &uri) {
 
 bool Universe::_subscribe(const std::string &res) {
 	// Need to find who owns the resource
-	optional<UUID> pid = _findOwner(res);
+	optional<UUID> pid = findOwner(res);
 	
 	if (pid) {
 		return call<bool>(*pid, "__subscribe__", id_, res);
diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp
index 0d33af7c2..925776833 100644
--- a/net/cpp/test/net_integration.cpp
+++ b/net/cpp/test/net_integration.cpp
@@ -147,6 +147,23 @@ TEST_CASE("Universe::broadcast()", "[net]") {
 	}
 }
 
+TEST_CASE("Universe::findOwner()", "") {
+	Universe a;
+	Universe b;
+	a.listen("tcp://localhost:7077");
+	b.connect("tcp://localhost:7077");
+	while (a.numberOfPeers() == 0) sleep_for(milliseconds(20));
+
+	SECTION("no owners exist") {
+		REQUIRE( !b.findOwner("ftl://test") );
+	}
+
+	SECTION("one owner exists") {
+		a.createResource("ftl://test");
+		REQUIRE( *(b.findOwner("ftl://test")) == ftl::net::this_peer );
+	}
+}
+
 /*TEST_CASE("net::listen()", "[net]") {
 
 	SECTION("tcp any interface") {
-- 
GitLab