Skip to content
Snippets Groups Projects
Commit d4153fb2 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Fix dispatcher response format, fix dispatch chaining bug and add working test for findOwner

parent 8b6c9189
Branches
Tags
No related merge requests found
Pipeline #9733 passed
......@@ -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_;
......
......@@ -98,13 +98,14 @@ class Universe {
*/
bool createResource(const std::string &uri);
std::optional<ftl::UUID> findOwner(const std::string &res);
private:
void _run();
int _setDescriptors();
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);
......
......@@ -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";
}
}
......
......@@ -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();
}
......
......@@ -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);
......
......@@ -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") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment