Commit e8ca5421 authored by Jakob Otto's avatar Jakob Otto

Refactor basp::application

parent bddef5ec
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "caf/net/basp/header.hpp" #include "caf/net/basp/header.hpp"
#include "caf/net/basp/message_type.hpp" #include "caf/net/basp/message_type.hpp"
#include "caf/net/endpoint_manager.hpp" #include "caf/net/endpoint_manager.hpp"
#include "caf/net/packet_writer.hpp"
#include "caf/net/receive_policy.hpp" #include "caf/net/receive_policy.hpp"
#include "caf/node_id.hpp" #include "caf/node_id.hpp"
#include "caf/proxy_registry.hpp" #include "caf/proxy_registry.hpp"
...@@ -77,11 +78,14 @@ public: ...@@ -77,11 +78,14 @@ public:
if (!std::is_base_of<test_tag, Parent>::value) if (!std::is_base_of<test_tag, Parent>::value)
manager_ = &parent.manager(); manager_ = &parent.manager();
// Write handshake. // Write handshake.
if (auto err = generate_handshake()) auto header_buf = parent.next_header_buffer();
auto payload_buf = parent.next_buffer();
if (auto err = generate_handshake(payload_buf))
return err; return err;
auto hdr = to_bytes(header{message_type::handshake, to_bytes(header{message_type::handshake,
static_cast<uint32_t>(buf_.size()), version}); static_cast<uint32_t>(payload_buf.size()), version},
parent.write_packet(hdr, buf_); header_buf);
parent.write_packet(header_buf, payload_buf);
parent.transport().configure_read(receive_policy::exactly(header_size)); parent.transport().configure_read(receive_policy::exactly(header_size));
return none; return none;
} }
...@@ -89,52 +93,50 @@ public: ...@@ -89,52 +93,50 @@ public:
template <class Parent> template <class Parent>
error write_message(Parent& parent, error write_message(Parent& parent,
std::unique_ptr<endpoint_manager_queue::message> ptr) { std::unique_ptr<endpoint_manager_queue::message> ptr) {
auto write_packet = make_callback([&](byte_span hdr, byte_span payload) { return write(parent, std::move(ptr));
parent.write_packet(hdr, payload);
return none;
});
return write(write_packet, std::move(ptr));
} }
template <class Parent> template <class Parent>
error handle_data(Parent& parent, byte_span bytes) { error handle_data(Parent& parent, byte_span bytes) {
auto write_packet = make_callback([&](byte_span hdr, byte_span payload) { static_assert(std::is_base_of<packet_writer, Parent>::value,
parent.write_packet(hdr, payload); "parent must implement packet_writer");
return none;
});
size_t next_read_size = header_size; size_t next_read_size = header_size;
if (auto err = handle(next_read_size, write_packet, bytes)) if (auto err = handle(next_read_size, parent, bytes))
return err; return err;
parent.transport().configure_read(receive_policy::exactly(next_read_size)); parent.transport().configure_read(receive_policy::exactly(next_read_size));
return none; return none;
} }
// TODO: unessecary indirection
template <class Parent> template <class Parent>
void resolve(Parent& parent, string_view path, actor listener) { void resolve(Parent& parent, string_view path, const actor& listener) {
auto write_packet = make_callback([&](byte_span hdr, byte_span payload) { static_assert(std::is_base_of<packet_writer, Parent>::value,
parent.write_packet(hdr, payload); "parent must implement `packet_writer`");
return none; resolve_remote_path(parent, path, listener);
});
resolve_remote_path(write_packet, path, listener);
} }
// TODO: can be packet_writer&?
template <class Parent> template <class Parent>
void new_proxy(Parent& parent, actor_id id) { void new_proxy(Parent& parent, actor_id id) {
header hdr{message_type::monitor_message, 0, static_cast<uint64_t>(id)}; header hdr{message_type::monitor_message, 0, static_cast<uint64_t>(id)};
auto bytes = to_bytes(hdr); auto header_buf = parent.next_header_buffer();
parent.write_packet(make_span(bytes), span<const byte>{}); to_bytes(hdr, header_buf);
parent.write_packet(header_buf);
} }
// TODO: can be packet_writer&?
template <class Parent> template <class Parent>
void local_actor_down(Parent& parent, actor_id id, error reason) { void local_actor_down(Parent& parent, actor_id id, error reason) {
buf_.clear(); auto header_buf = parent.next_header_buffer();
serializer_impl<buffer_type> sink{system(), buf_}; auto payload_buf = parent.next_buffer();
serializer_impl<buffer_type> sink{system(), payload_buf};
if (auto err = sink(reason)) if (auto err = sink(reason))
CAF_RAISE_ERROR("unable to serialize an error"); CAF_RAISE_ERROR("unable to serialize an error");
header hdr{message_type::down_message, static_cast<uint32_t>(buf_.size()), header hdr{message_type::down_message,
static_cast<uint32_t>(payload_buf.size()),
static_cast<uint64_t>(id)}; static_cast<uint64_t>(id)};
auto bytes = to_bytes(hdr); to_bytes(hdr, header_buf);
parent.write_packet(make_span(bytes), make_span(buf_)); parent.write_packet(header_buf, payload_buf);
} }
template <class Parent> template <class Parent>
...@@ -153,8 +155,8 @@ public: ...@@ -153,8 +155,8 @@ public:
strong_actor_ptr resolve_local_path(string_view path); strong_actor_ptr resolve_local_path(string_view path);
void resolve_remote_path(write_packet_callback& write_packet, void resolve_remote_path(packet_writer& writer, string_view path,
string_view path, actor listener); const actor& listener);
// -- properties ------------------------------------------------------------- // -- properties -------------------------------------------------------------
...@@ -169,37 +171,34 @@ public: ...@@ -169,37 +171,34 @@ public:
private: private:
// -- handling of outgoing messages ------------------------------------------ // -- handling of outgoing messages ------------------------------------------
error write(write_packet_callback& write_packet, error write(packet_writer& writer,
std::unique_ptr<endpoint_manager_queue::message> ptr); std::unique_ptr<endpoint_manager_queue::message> ptr);
// -- handling of incoming messages ------------------------------------------ // -- handling of incoming messages ------------------------------------------
error handle(size_t& next_read_size, write_packet_callback& write_packet, error handle(size_t& next_read_size, packet_writer& writer, byte_span bytes);
byte_span bytes);
error handle(write_packet_callback& write_packet, header hdr, error handle(packet_writer& writer, header hdr, byte_span payload);
byte_span payload);
error handle_handshake(write_packet_callback& write_packet, header hdr, error handle_handshake(packet_writer& writer, header hdr, byte_span payload);
byte_span payload);
error handle_actor_message(write_packet_callback& write_packet, header hdr, error handle_actor_message(packet_writer& writer, header hdr,
byte_span payload); byte_span payload);
error handle_resolve_request(write_packet_callback& write_packet, header hdr, error handle_resolve_request(packet_writer& writer, header hdr,
byte_span payload); byte_span payload);
error handle_resolve_response(write_packet_callback& write_packet, header hdr, error handle_resolve_response(packet_writer& writer, header hdr,
byte_span payload); byte_span payload);
error handle_monitor_message(write_packet_callback& write_packet, header hdr, error handle_monitor_message(packet_writer& writer, header hdr,
byte_span payload); byte_span payload);
error handle_down_message(write_packet_callback& write_packet, header hdr, error handle_down_message(packet_writer& writer, header hdr,
byte_span payload); byte_span payload);
/// Writes the handshake payload to `buf_`. /// Writes the handshake payload to `buf_`.
error generate_handshake(); error generate_handshake(std::vector<byte>& buf);
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
...@@ -212,14 +211,11 @@ private: ...@@ -212,14 +211,11 @@ private:
/// Caches the last header while waiting for the matching payload. /// Caches the last header while waiting for the matching payload.
header hdr_; header hdr_;
/// Re-usable buffer for storing payloads.
buffer_type buf_;
/// Stores the ID of our peer. /// Stores the ID of our peer.
node_id peer_id_; node_id peer_id_;
/// Tracks which local actors our peer monitors. /// Tracks which local actors our peer monitors.
std::unordered_set<actor_addr> monitored_actors_; std::unordered_set<actor_addr> monitored_actors_; // TODO: this is unused
/// Caches actor handles obtained via `resolve`. /// Caches actor handles obtained via `resolve`.
std::unordered_map<uint64_t, response_promise> pending_resolves_; std::unordered_map<uint64_t, response_promise> pending_resolves_;
......
This diff is collapsed.
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "caf/net/basp/connection_state.hpp" #include "caf/net/basp/connection_state.hpp"
#include "caf/net/basp/constants.hpp" #include "caf/net/basp/constants.hpp"
#include "caf/net/basp/ec.hpp" #include "caf/net/basp/ec.hpp"
#include "caf/net/packet_writer.hpp"
#include "caf/none.hpp" #include "caf/none.hpp"
#include "caf/uri.hpp" #include "caf/uri.hpp"
...@@ -43,7 +44,8 @@ namespace { ...@@ -43,7 +44,8 @@ namespace {
struct fixture : test_coordinator_fixture<>, struct fixture : test_coordinator_fixture<>,
proxy_registry::backend, proxy_registry::backend,
basp::application::test_tag { basp::application::test_tag,
public packet_writer {
using buffer_type = std::vector<byte>; using buffer_type = std::vector<byte>;
fixture() : proxies(sys, *this), app(proxies) { fixture() : proxies(sys, *this), app(proxies) {
...@@ -66,11 +68,6 @@ struct fixture : test_coordinator_fixture<>, ...@@ -66,11 +68,6 @@ struct fixture : test_coordinator_fixture<>,
input = to_buf(xs...); input = to_buf(xs...);
} }
void write_packet(span<const byte> hdr, span<const byte> payload) {
output.insert(output.end(), hdr.begin(), hdr.end());
output.insert(output.end(), payload.begin(), payload.end());
}
void handle_handshake() { void handle_handshake() {
CAF_CHECK_EQUAL(app.state(), CAF_CHECK_EQUAL(app.state(),
basp::connection_state::await_handshake_header); basp::connection_state::await_handshake_header);
...@@ -114,6 +111,14 @@ struct fixture : test_coordinator_fixture<>, ...@@ -114,6 +111,14 @@ struct fixture : test_coordinator_fixture<>,
CAF_FAIL("unexpected function call"); CAF_FAIL("unexpected function call");
} }
buffer_type next_buffer() override {
return {};
}
buffer_type next_header_buffer() override {
return {};
}
template <class... Ts> template <class... Ts>
void configure_read(Ts...) { void configure_read(Ts...) {
// nop // nop
...@@ -130,6 +135,12 @@ struct fixture : test_coordinator_fixture<>, ...@@ -130,6 +135,12 @@ struct fixture : test_coordinator_fixture<>,
// nop // nop
} }
protected:
void write_impl(span<buffer_type*> buffers) override {
for (auto buf : buffers)
output.insert(output.end(), buf->begin(), buf->end());
}
buffer_type input; buffer_type input;
buffer_type output; buffer_type output;
......
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