Commit 362c5b5e authored by Dominik Charousset's avatar Dominik Charousset

Add interface to get a remote actor's IP and port

Close #419
parent 86ab5ae8
......@@ -109,7 +109,9 @@ using middleman_actor =
reacts_to<close_atom, uint16_t>,
replies_to<spawn_atom, node_id, std::string, message>
::with<ok_atom, actor_addr, std::set<std::string>>>;
::with<ok_atom, actor_addr, std::set<std::string>>,
replies_to<get_atom, node_id>::with<node_id, std::string, uint16_t>>;
/// @relates middleman_actor
middleman_actor make_middleman_actor(actor_system& sys, actor default_broker);
......
......@@ -659,6 +659,17 @@ behavior basp_broker::make_behavior() {
});
}
},
[=](get_atom, const node_id& x)
-> std::tuple<node_id, std::string, uint16_t> {
std::string addr;
uint16_t port = 0;
auto hdl = state.instance.tbl().lookup_direct(x);
if (hdl != invalid_connection_handle) {
addr = remote_addr(hdl);
port = remote_port(hdl);
}
return std::make_tuple(x, std::move(addr), port);
},
// catch-all error handler
others >> [=] {
CAF_LOG_ERROR("unexpected message:" << CAF_ARG(current_message()));
......
......@@ -152,6 +152,12 @@ public:
forward_current_message(broker_);
return {};
},
[=](get_atom, const node_id&)
-> delegated<node_id, std::string, uint16_t> {
CAF_LOG_TRACE("");
forward_current_message(broker_);
return {};
},
[=](const down_msg& dm) {
auto i = cached_.begin();
auto e = cached_.end();
......
......@@ -79,11 +79,11 @@ void test_multiplexer::assign_tcp_scribe(abstract_broker* ptr,
}
std::string addr() const override {
return std::string{};
return "test";
}
uint16_t port() const override {
return 0;
return static_cast<uint16_t>(hdl().id());
}
private:
......@@ -150,7 +150,7 @@ void test_multiplexer::assign_tcp_doorman(abstract_broker* ptr,
}
std::string addr() const override {
return std::string{};
return "test";
}
uint16_t port() const override {
......
......@@ -472,6 +472,21 @@ CAF_TEST(non_empty_server_handshake) {
CAF_CHECK(hexstr(buf) == hexstr(expected_buf));
}
CAF_TEST(remote_address_and_port) {
connect_node(1);
auto mm = system.middleman().actor_handle();
self()->send(mm, get_atom::value, remote_node(1));
mpx()->exec_runnable();
self()->receive(
[&](const node_id& nid, const std::string& addr, uint16_t port) {
CAF_CHECK(nid == remote_node(1));
// all test nodes have address "test" and connection handle ID as port
CAF_CHECK(addr == "test");
CAF_CHECK(port == remote_hdl(1).id());
}
);
}
CAF_TEST(client_handshake_and_dispatch) {
connect_node(0);
// send a message via `dispatch` from node 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment