Commit bd9f84ce authored by Joseph Noir's avatar Joseph Noir

Unify message handling for UDP and TCP

parent 66e1ca2b
...@@ -102,9 +102,7 @@ inline bool operator!=(const header& lhs, const header& rhs) { ...@@ -102,9 +102,7 @@ inline bool operator!=(const header& lhs, const header& rhs) {
/// Checks whether given header contains a handshake. /// Checks whether given header contains a handshake.
inline bool is_handshake(const header& hdr) { inline bool is_handshake(const header& hdr) {
return hdr.operation == message_type::server_handshake return hdr.operation == message_type::server_handshake
|| hdr.operation == message_type::client_handshake || hdr.operation == message_type::client_handshake;
|| hdr.operation == message_type::udp_client_handshake
|| hdr.operation == message_type::udp_server_handshake;
} }
/// Checks wheter given header contains a heartbeat. /// Checks wheter given header contains a heartbeat.
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#define CAF_IO_BASP_INSTANCE_HPP #define CAF_IO_BASP_INSTANCE_HPP
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/binary_deserializer.hpp" // ---
#include "caf/io/hook.hpp" #include "caf/io/hook.hpp"
#include "caf/io/middleman.hpp" #include "caf/io/middleman.hpp"
...@@ -199,12 +201,12 @@ public: ...@@ -199,12 +201,12 @@ public:
buffer_type& buf, const node_id& remote_side); buffer_type& buf, const node_id& remote_side);
/// Start handshake ... TODO /// Start handshake ... TODO
void write_udp_client_handshake(execution_unit* ctx, buffer_type& buf); //void write_udp_client_handshake(execution_unit* ctx, buffer_type& buf);
/// Answer client handshake ... TODO /// Answer client handshake ... TODO
void write_udp_server_handshake(execution_unit* ctx, buffer_type& buf, //void write_udp_server_handshake(execution_unit* ctx, buffer_type& buf,
const node_id& remote_side, // const node_id& remote_side,
optional<uint16_t> port); // optional<uint16_t> port);
/// Writes an `announce_proxy` to `buf`. /// Writes an `announce_proxy` to `buf`.
void write_announce_proxy(execution_unit* ctx, buffer_type& buf, void write_announce_proxy(execution_unit* ctx, buffer_type& buf,
...@@ -233,6 +235,167 @@ public: ...@@ -233,6 +235,167 @@ public:
return callee_.system(); return callee_.system();
} }
template <class Handle>
bool handle_msg(execution_unit* ctx, const Handle& hdl, header& hdr,
std::vector<char>* payload, bool tcp_based,
optional<uint16_t> port) {
std::cerr << "Handling " << to_string(hdr.operation) << " msg" << std::endl;
auto payload_valid = [&]() -> bool {
return payload != nullptr && payload->size() == hdr.payload_len;
};
// handle message to ourselves
switch (hdr.operation) {
case message_type::server_handshake: {
actor_id aid = invalid_actor_id;
std::set<std::string> sigs;
if (payload_valid()) {
binary_deserializer bd{ctx, *payload};
std::string remote_appid;
auto e = bd(remote_appid);
if (e)
return false;
if (remote_appid != callee_.system().config().middleman_app_identifier) {
CAF_LOG_ERROR("app identifier mismatch");
return false;
}
e = bd(aid, sigs);
if (e)
return false;
} else {
CAF_LOG_ERROR("fail to receive the app identifier");
return false;
}
// close self connection after handshake is done
if (hdr.source_node == this_node_) {
CAF_LOG_INFO("close connection to self immediately");
callee_.finalize_handshake(hdr.source_node, aid, sigs);
return false;
}
// close this connection if we already have a direct connection
if (tbl_.lookup_hdl(hdr.source_node)) {
CAF_LOG_INFO("close connection since we already have a "
"direct connection: " << CAF_ARG(hdr.source_node));
callee_.finalize_handshake(hdr.source_node, aid, sigs);
return false;
}
// add direct route to this node and remove any indirect entry
CAF_LOG_INFO("new direct connection:" << CAF_ARG(hdr.source_node));
tbl_.add(hdl, hdr.source_node);
//auto was_indirect = tbl_.erase_indirect(hdr.source_node);
// write handshake as client in response
auto path = tbl_.lookup(hdr.source_node);
if (!path) {
CAF_LOG_ERROR("no route to host after server handshake");
return false;
}
if (tcp_based)
write_client_handshake(ctx, path->wr_buf, hdr.source_node);
callee_.learned_new_node_directly(hdr.source_node);
callee_.finalize_handshake(hdr.source_node, aid, sigs);
flush(*path);
break;
}
case message_type::client_handshake: {
auto is_known_node = tbl_.lookup_hdl(hdr.source_node);
if (is_known_node && tcp_based) {
CAF_LOG_INFO("received second client handshake:"
<< CAF_ARG(hdr.source_node));
break;
}
if (payload_valid()) {
binary_deserializer bd{ctx, *payload};
std::string remote_appid;
auto e = bd(remote_appid);
if (e)
return false;
if (remote_appid != callee_.system().config().middleman_app_identifier) {
CAF_LOG_ERROR("app identifier mismatch");
return false;
}
} else {
CAF_LOG_ERROR("fail to receive the app identifier");
return false;
}
if (!is_known_node) {
// add direct route to this node and remove any indirect entry
CAF_LOG_INFO("new direct connection:" << CAF_ARG(hdr.source_node));
tbl_.add(hdl, hdr.source_node);
}
auto path = tbl_.lookup(hdr.source_node);
if (!path) {
CAF_LOG_ERROR("no route to host after server handshake");
return false;
}
if (!tcp_based)
write_server_handshake(ctx, path->wr_buf, port);
if (!is_known_node) {
//auto was_indirect = tbl_.erase_indirect(hdr.source_node);
callee_.learned_new_node_directly(hdr.source_node);
}
flush(*path);
break;
}
case message_type::dispatch_message: {
if (!payload_valid())
return false;
// in case the sender of this message was received via a third node,
// we assume that that node to offers a route to the original source
//auto last_hop = tbl_.lookup_node(dm.handle);
//if (hdr.source_node != none
// && hdr.source_node != this_node_
// && last_hop != hdr.source_node
// && tbl_.lookup_direct(hdr.source_node) == invalid_connection_handle
// && tbl_.add_indirect(last_hop, hdr.source_node))
// callee_.learned_new_node_indirectly(hdr.source_node);
binary_deserializer bd{ctx, *payload};
auto receiver_name = static_cast<atom_value>(0);
std::vector<strong_actor_ptr> forwarding_stack;
message msg;
if (hdr.has(header::named_receiver_flag)) {
auto e = bd(receiver_name);
if (e)
return false;
}
auto e = bd(forwarding_stack, msg);
if (e)
return false;
CAF_LOG_DEBUG(CAF_ARG(forwarding_stack) << CAF_ARG(msg));
if (hdr.has(header::named_receiver_flag))
callee_.deliver(hdr.source_node, hdr.source_actor, receiver_name,
message_id::from_integer_value(hdr.operation_data),
forwarding_stack, msg);
else
callee_.deliver(hdr.source_node, hdr.source_actor, hdr.dest_actor,
message_id::from_integer_value(hdr.operation_data),
forwarding_stack, msg);
break;
}
case message_type::announce_proxy:
callee_.proxy_announced(hdr.source_node, hdr.dest_actor);
break;
case message_type::kill_proxy: {
if (!payload_valid())
return false;
binary_deserializer bd{ctx, *payload};
error fail_state;
auto e = bd(fail_state);
if (e)
return false;
callee_.kill_proxy(hdr.source_node, hdr.source_actor, fail_state);
break;
}
case message_type::heartbeat: {
CAF_LOG_TRACE("received heartbeat: " << CAF_ARG(hdr.source_node));
callee_.handle_heartbeat(hdr.source_node);
break;
}
default:
CAF_LOG_ERROR("invalid operation");
return false;
}
return true;
}
private: private:
routing_table tbl_; routing_table tbl_;
published_actor_map published_actors_; published_actor_map published_actors_;
......
...@@ -68,11 +68,7 @@ enum class message_type : uint8_t { ...@@ -68,11 +68,7 @@ enum class message_type : uint8_t {
/// in order to detect disconnects. /// in order to detect disconnects.
/// ///
/// ![](heartbeat.png) /// ![](heartbeat.png)
heartbeat = 0x05, heartbeat = 0x05
// TODO: udp handshakes
udp_client_handshake = 0x06,
udp_server_handshake = 0x07
}; };
/// @relates message_type /// @relates message_type
......
...@@ -132,7 +132,7 @@ public: ...@@ -132,7 +132,7 @@ public:
/// including the node that is assigned as direct path for `hdl`. /// including the node that is assigned as direct path for `hdl`.
// void erase_direct(const endpoint_handle& hdl, erase_callback& cb); // void erase_direct(const endpoint_handle& hdl, erase_callback& cb);
void erase(const endpoint_handle& hdl, erase_callback& cb); void erase(const endpoint_handle& hdl, erase_callback& cb);
/* /*
/// Removes any entry for indirect connection to `dest` and returns /// Removes any entry for indirect connection to `dest` and returns
/// `true` if `dest` had an indirect route, otherwise `false`. /// `true` if `dest` had an indirect route, otherwise `false`.
......
...@@ -596,12 +596,8 @@ behavior basp_broker::make_behavior() { ...@@ -596,12 +596,8 @@ behavior basp_broker::make_behavior() {
<< CAF_ARG(msg.handle)); << CAF_ARG(msg.handle));
ctx.callback->deliver(make_error(sec::disconnect_during_handshake)); ctx.callback->deliver(make_error(sec::disconnect_during_handshake));
} }
// TODO: some error?
} else { } else {
if (ctx.callback) { // TODO: Do we need to handle available callbacks here?
// TODO: if callback available, handle that
}
// TODO: Is there configuration necessary?
configure_datagram_size(msg.handle, 1500); configure_datagram_size(msg.handle, 1500);
} }
}, },
...@@ -778,7 +774,7 @@ behavior basp_broker::make_behavior() { ...@@ -778,7 +774,7 @@ behavior basp_broker::make_behavior() {
ctx.remote_port = port; ctx.remote_port = port;
ctx.callback = rp; ctx.callback = rp;
auto& bi = state.instance; auto& bi = state.instance;
bi.write_udp_client_handshake(context(), wr_buf(hdl)); bi.write_client_handshake(context(), wr_buf(hdl), none);
flush(hdl); flush(hdl);
configure_datagram_size(hdl, 1500); configure_datagram_size(hdl, 1500);
} else { } else {
......
...@@ -2089,7 +2089,7 @@ expected<native_socket> new_dgram_scribe_impl(const std::string& host, ...@@ -2089,7 +2089,7 @@ expected<native_socket> new_dgram_scribe_impl(const std::string& host,
: new_ip_acceptor_impl<AF_INET6>(fd, 0, nullptr); : new_ip_acceptor_impl<AF_INET6>(fd, 0, nullptr);
if (!p) if (!p)
return std::move(p.error()); return std::move(p.error());
std::cerr << "[NDSI] Bound to " << *p << std::endl; //std::cerr << "[NDSI] Bound to " << *p << std::endl;
return sguard.release(); return sguard.release();
} }
...@@ -2126,7 +2126,7 @@ new_dgram_doorman_impl(uint16_t port, const char* addr, bool reuse_addr) { ...@@ -2126,7 +2126,7 @@ new_dgram_doorman_impl(uint16_t port, const char* addr, bool reuse_addr) {
return std::move(p.error()); return std::move(p.error());
// ok, no errors so far // ok, no errors so far
CAF_LOG_DEBUG(CAF_ARG(fd) << CAF_ARG(p)); CAF_LOG_DEBUG(CAF_ARG(fd) << CAF_ARG(p));
std::cerr << "[NDDI] Bound to " << *p << std::endl; //std::cerr << "[NDDI] Bound to " << *p << std::endl;
return std::make_pair(sguard.release(), *p); return std::make_pair(sguard.release(), *p);
} }
......
...@@ -64,9 +64,6 @@ bool dgram_doorman::delegate_msg(execution_unit* ctx, ...@@ -64,9 +64,6 @@ bool dgram_doorman::delegate_msg(execution_unit* ctx,
auto& buf = rd_buf(); auto& buf = rd_buf();
CAF_ASSERT(buf.size() >= num_bytes); CAF_ASSERT(buf.size() >= num_bytes);
buf.resize(num_bytes); buf.resize(num_bytes);
std::cerr << "Delegating message, doorman on " << parent()->local_port(hdl())
<< " and scribe on " << parent()->local_port(endpoint)
<< std::endl;
tmp_t tmp{strong_actor_ptr{}, message_id::make(), tmp_t tmp{strong_actor_ptr{}, message_id::make(),
mailbox_element::forwarding_stack{}, mailbox_element::forwarding_stack{},
delegate_t{endpoint, std::move(buf), delegate_t{endpoint, std::move(buf),
......
...@@ -71,37 +71,22 @@ bool zero(T val) { ...@@ -71,37 +71,22 @@ bool zero(T val) {
return val == 0; return val == 0;
} }
// TODO: Remove comments
bool server_handshake_valid(const header& hdr) { bool server_handshake_valid(const header& hdr) {
return valid(hdr.source_node) return valid(hdr.source_node)
&& !valid(hdr.dest_node) //&& !valid(hdr.dest_node)
&& zero(hdr.dest_actor) && zero(hdr.dest_actor)
&& !zero(hdr.operation_data); && !zero(hdr.operation_data);
} }
// TODO: Remove comments
bool client_handshake_valid(const header& hdr) { bool client_handshake_valid(const header& hdr) {
return valid(hdr.source_node) return valid(hdr.source_node)
&& valid(hdr.dest_node) //&& valid(hdr.dest_node)
&& hdr.source_node != hdr.dest_node && hdr.source_node != hdr.dest_node
&& zero(hdr.source_actor) && zero(hdr.source_actor)
&& zero(hdr.dest_actor) && zero(hdr.dest_actor);
&& zero(hdr.operation_data); //&& zero(hdr.operation_data);
}
bool udp_server_handshake_valid(const header& hdr) {
return valid(hdr.source_node)
&& valid(hdr.dest_node)
&& hdr.source_node != hdr.dest_node
//&& zero(hdr.source_actor)
&& zero(hdr.dest_actor)
&& !zero(hdr.operation_data);
}
bool udp_client_handshake_valid(const header& hdr) {
return valid(hdr.source_node)
&& !valid(hdr.dest_node)
&& zero(hdr.source_actor)
&& zero(hdr.dest_actor)
&& !zero(hdr.operation_data);
} }
bool dispatch_message_valid(const header& hdr) { bool dispatch_message_valid(const header& hdr) {
...@@ -158,10 +143,6 @@ bool valid(const header& hdr) { ...@@ -158,10 +143,6 @@ bool valid(const header& hdr) {
return kill_proxy_instance_valid(hdr); return kill_proxy_instance_valid(hdr);
case message_type::heartbeat: case message_type::heartbeat:
return heartbeat_valid(hdr); return heartbeat_valid(hdr);
case message_type::udp_server_handshake:
return udp_server_handshake_valid(hdr);
case message_type::udp_client_handshake:
return udp_client_handshake_valid(hdr);
} }
} }
......
This diff is collapsed.
...@@ -33,9 +33,7 @@ const char* message_type_strings[] = { ...@@ -33,9 +33,7 @@ const char* message_type_strings[] = {
"dispatch_message", "dispatch_message",
"announce_proxy_instance", "announce_proxy_instance",
"kill_proxy_instance", "kill_proxy_instance",
"heartbeat", "heartbeat"
"udp_client_handshake",
"udp_server_handshake"
}; };
} // namespace <anonymous> } // namespace <anonymous>
......
...@@ -122,12 +122,12 @@ CAF_TEST(test_datagram_remote_actor) { ...@@ -122,12 +122,12 @@ CAF_TEST(test_datagram_remote_actor) {
auto res3 = io::uri::make(string(uri_no_port) + ":" + to_string(*res2)); auto res3 = io::uri::make(string(uri_no_port) + ":" + to_string(*res2));
CAF_CHECK(res3); CAF_CHECK(res3);
CAF_MESSAGE("Published pong on: " + to_string(*res3) + "."); CAF_MESSAGE("Published pong on: " + to_string(*res3) + ".");
/*
CAF_SET_LOGGER_SYS(&server_side); CAF_SET_LOGGER_SYS(&server_side);
CAF_MESSAGE("Local call to remote actor should acquire the actor."); CAF_MESSAGE("Local call to remote actor should acquire the actor.");
auto res4 = server_side_mm.remote_actor(*res3); auto res4 = server_side_mm.remote_actor(*res3);
CAF_CHECK(res4); CAF_CHECK(res4);
*/
CAF_MESSAGE("Checking from different actor system next."); CAF_MESSAGE("Checking from different actor system next.");
auto res5 = client_side_mm.remote_actor(*res3); auto res5 = client_side_mm.remote_actor(*res3);
CAF_CHECK(res5); CAF_CHECK(res5);
......
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