From a474c47ce8c2bd74b2d51bab3d24e8659d3c8a5d Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Tue, 7 May 2019 10:38:20 +0300 Subject: [PATCH] Fixes for net findOne missing results --- net/cpp/include/ftl/net/peer.hpp | 2 +- net/cpp/include/ftl/net/universe.hpp | 5 ++++- net/cpp/src/universe.cpp | 10 +++++----- net/cpp/test/net_integration.cpp | 15 ++++++++++++++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/net/cpp/include/ftl/net/peer.hpp b/net/cpp/include/ftl/net/peer.hpp index da30ee520..339528ead 100644 --- a/net/cpp/include/ftl/net/peer.hpp +++ b/net/cpp/include/ftl/net/peer.hpp @@ -298,7 +298,7 @@ int Peer::asyncCall( // Register the CB callbacks_[rpcid] = std::make_unique<caller<T>>(cb); - + _send(); return rpcid; } diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp index 728421c47..a92a67bbc 100644 --- a/net/cpp/include/ftl/net/universe.hpp +++ b/net/cpp/include/ftl/net/universe.hpp @@ -126,6 +126,8 @@ class Universe { bool createResource(const std::string &uri); std::optional<ftl::UUID> findOwner(const std::string &res); + + void setLocalID(const ftl::UUID &u) { this_peer = u; }; private: void _run(); @@ -138,6 +140,7 @@ class Universe { private: bool active_; + ftl::UUID this_peer; nlohmann::json config_; std::mutex net_mutex_; fd_set sfderror_; @@ -199,7 +202,7 @@ std::optional<R> Universe::findOne(const std::string &name, ARGS... args) { std::map<Peer*, int> record; for (auto p : peers_) { - record[p] = p->asyncCall<std::optional<R>>(name, handler, std::forward<ARGS>(args)...); + record[p] = p->asyncCall<std::optional<R>>(name, handler, args...); } { // Block thread until async callback notifies us diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp index 421f5eebe..7a09a9b4d 100644 --- a/net/cpp/src/universe.cpp +++ b/net/cpp/src/universe.cpp @@ -18,12 +18,12 @@ using std::optional; using std::unique_lock; using std::mutex; -Universe::Universe() : active_(true), thread_(Universe::__start, this) { +Universe::Universe() : active_(true), this_peer(ftl::net::this_peer), thread_(Universe::__start, this) { _installBindings(); } Universe::Universe(nlohmann::json &config) : - active_(true), config_(config), thread_(Universe::__start, this) { + active_(true), this_peer(ftl::net::this_peer), config_(config), thread_(Universe::__start, this) { if (config["listen"].is_array()) { for (auto &l : config["listen"]) { listen(l); @@ -133,7 +133,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; + if (owned_.count(res) > 0) return this_peer; else return {}; }); } @@ -179,7 +179,7 @@ bool Universe::_subscribe(const std::string &res) { optional<UUID> pid = findOwner(res); if (pid) { - return call<bool>(*pid, "__subscribe__", ftl::net::this_peer, res); + return call<bool>(*pid, "__subscribe__", this_peer, res); } else { // No resource found LOG(WARNING) << "Subscribe to unknown resource: " << res; @@ -219,7 +219,7 @@ void Universe::_run() { //Some kind of error occured, it is usually possible to recover from this. if (selres < 0) { - std::cout << "SELECT ERROR " << selres << std::endl; + std::cout << "SELECT ERROR " << selres << " - " << strerror(errno) << std::endl; //return false; continue; } else if (selres == 0) { diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp index f6e4de548..9197bab23 100644 --- a/net/cpp/test/net_integration.cpp +++ b/net/cpp/test/net_integration.cpp @@ -170,10 +170,23 @@ TEST_CASE("Universe::findOwner()", "") { SECTION("three peers and one owner") { Universe c; c.connect("tcp://localhost:7077"); + b.setLocalID(ftl::UUID(7)); while (a.numberOfPeers() < 2) sleep_for(milliseconds(20)); b.createResource("ftl://test"); - REQUIRE( *(a.findOwner("ftl://test")) == ftl::net::this_peer ); + REQUIRE( *(a.findOwner("ftl://test")) == ftl::UUID(7) ); + } + + SECTION("three peers and one owner (2)") { + Universe c; + c.connect("tcp://localhost:7077"); + c.setLocalID(ftl::UUID(7)); + while (a.numberOfPeers() < 2) sleep_for(milliseconds(20)); + + c.createResource("ftl://test"); + auto r = a.findOwner("ftl://test"); + REQUIRE( r ); + REQUIRE( *r == ftl::UUID(7) ); } } -- GitLab