diff --git a/net/cpp/include/ftl/net/peer.hpp b/net/cpp/include/ftl/net/peer.hpp index da30ee52011406d2ee8c3550aae6999c003ec718..339528ead285188c8448f9a9eeab45288ec039b0 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 728421c47b244656f004c9e398298b0cef727c39..a92a67bbc84eabbbfd82beb76c747bbbc5b915da 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 421f5eebe90d6cabd6758cd99c0ceae9d99332fd..7a09a9b4da86e2ab3b9b70f2b44581e5b8b6c3e4 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 f6e4de548701237d7d2d031bd177aa148aba1e1b..9197bab23bbf73ee43f9ad6d78b9169c80a46bee 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) ); } }