Commit 046a6ef4 authored by Dominik Charousset's avatar Dominik Charousset

Migrate to latest upstream CAF API

parent 11dd2cf0
......@@ -49,7 +49,7 @@ if(CAF_INC_ENABLE_STANDALONE_BUILD)
FetchContent_Declare(
actor_framework
GIT_REPOSITORY https://github.com/actor-framework/actor-framework.git
GIT_TAG 6b2cbd4
GIT_TAG 70af2bb9
)
FetchContent_Populate(actor_framework)
set(CAF_ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE)
......
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#include <algorithm>
#include <cstring>
#include <fstream>
......@@ -94,6 +98,27 @@ int main(int argc, char** argv) {
cerr << "empty enum name found\n";
return EXIT_FAILURE;
}
std::string case_label_prefix;
if (is_enum_class)
case_label_prefix = enum_name + "::";
// Read until hitting the closing '}'.
std::vector<std::string> enum_values;
for (;;) {
if (!getline(in, line)) {
cerr << "unable to read enum values\n";
return EXIT_FAILURE;
}
trim(line);
if (line.empty())
continue;
if (line[0] == '}')
break;
if (line[0] != '/') {
keep_alnum(line);
enum_values.emplace_back(line);
}
}
// Generate output file.
std::ofstream out{argv[2]};
if (!out) {
cerr << "unable to open output file: " << argv[2] << '\n';
......@@ -104,6 +129,9 @@ int main(int argc, char** argv) {
<< "// DO NOT EDIT: "
"this file is auto-generated by caf-generate-enum-strings.\n"
"// Run the target update-enum-strings if this file is out of sync.\n"
"#include \"caf/config.hpp\"\n"
"#include \"caf/string_view.hpp\"\n\n"
"CAF_PUSH_DEPRECATED_WARNING\n\n"
<< "#include \"" << namespaces[0];
for (size_t i = 1; i < namespaces.size(); ++i)
out << '/' << namespaces[i];
......@@ -112,34 +140,44 @@ int main(int argc, char** argv) {
<< "namespace " << namespaces[0] << " {\n";
for (size_t i = 1; i < namespaces.size(); ++i)
out << "namespace " << namespaces[i] << " {\n";
out << "\nstd::string to_string(" << enum_name << " x) {\n"
out << '\n';
// Generate to_string implementation.
out << "std::string to_string(" << enum_name << " x) {\n"
<< " switch(x) {\n"
<< " default:\n"
<< " return \"???\";\n";
// Read until hitting the closing '}'.
std::string case_label_prefix;
if (is_enum_class)
case_label_prefix = enum_name + "::";
for (;;) {
if (!getline(in, line)) {
cerr << "unable to read enum values\n";
return EXIT_FAILURE;
}
trim(line);
if (line.empty())
continue;
if (line[0] == '}')
break;
if (line[0] != '/') {
keep_alnum(line);
out << " case " << case_label_prefix << line << ":\n"
<< " return \"" << line << "\";\n";
}
}
// Done. Print file footer and exit.
for (auto& val : enum_values)
out << " case " << case_label_prefix << val << ":\n"
<< " return \"" << val << "\";\n";
out << " };\n"
<< "}\n\n";
// Generate from_string implementation.
out << "bool from_string(string_view in, " << enum_name << "& out) {\n ";
for (auto& val : enum_values)
out << "if (in == \"" << val << "\") {\n"
<< " out = " << case_label_prefix << val << ";\n"
<< " return true;\n"
<< " } else ";
out << "{\n"
<< " return false;\n"
<< " }\n"
<< "}\n\n";
// Generate from_integer implementation.
out << "bool from_integer(std::underlying_type_t<" << enum_name << "> in,\n"
<< " " << enum_name << "& out) {\n"
<< " auto result = static_cast<" << enum_name << ">(in);\n"
<< " switch(result) {\n"
<< " default:\n"
<< " return false;\n";
for (auto& val : enum_values)
out << " case " << case_label_prefix << val << ":\n";
out << " out = result;\n"
<< " return true;\n"
<< " };\n"
<< "}\n\n";
// Done. Print file footer and exit.
for (auto i = namespaces.rbegin(); i != namespaces.rend(); ++i)
out << "} // namespace " << *i << '\n';
out << "\nCAF_POP_WARNINGS\n";
return EXIT_SUCCESS;
}
......@@ -20,10 +20,6 @@ caf_incubator_add_component(
HEADERS
${CAF_NET_HEADERS}
SOURCES
src/basp/connection_state_strings.cpp
src/basp/ec_strings.cpp
src/basp/message_type_strings.cpp
src/basp/operation_strings.cpp
src/convert_ip_endpoint.cpp
src/datagram_socket.cpp
src/defaults.cpp
......@@ -35,6 +31,10 @@ caf_incubator_add_component(
src/multiplexer.cpp
src/net/abstract_actor_shell.cpp
src/net/actor_shell.cpp
src/net/basp/connection_state_strings.cpp
src/net/basp/ec_strings.cpp
src/net/basp/message_type_strings.cpp
src/net/basp/operation_strings.cpp
src/net/middleman.cpp
src/net/middleman_backend.cpp
src/net/packet_writer.cpp
......
......@@ -4,14 +4,20 @@
#pragma once
#include <cstdint>
#include <string>
#include <type_traits>
#include "caf/default_enum_inspect.hpp"
#include "caf/detail/net_export.hpp"
#include "caf/fwd.hpp"
namespace caf::net::basp {
/// @addtogroup BASP
/// Stores the state of a connection in a `basp::application`.
enum class connection_state {
enum class connection_state : uint8_t {
/// Initial state for any connection to wait for the peer's handshake.
await_handshake_header,
/// Indicates that the header for the peer's handshake arrived and BASP
......@@ -28,7 +34,20 @@ enum class connection_state {
};
/// @relates connection_state
std::string to_string(connection_state x);
CAF_NET_EXPORT std::string to_string(connection_state x);
/// @relates connection_state
CAF_NET_EXPORT bool from_string(string_view, connection_state&);
/// @relates connection_state
CAF_NET_EXPORT bool from_integer(std::underlying_type_t<connection_state>,
connection_state&);
/// @relates connection_state
template <class Inspector>
bool inspect(Inspector& f, connection_state& x) {
return default_enum_inspect(f, x);
}
/// @}
......
......@@ -7,7 +7,9 @@
#include <cstdint>
#include <string>
#include "caf/default_enum_inspect.hpp"
#include "caf/detail/net_export.hpp"
#include "caf/fwd.hpp"
#include "caf/is_error_code_enum.hpp"
namespace caf::net::basp {
......@@ -33,6 +35,18 @@ enum class ec : uint8_t {
/// @relates ec
CAF_NET_EXPORT std::string to_string(ec x);
/// @relates ec
CAF_NET_EXPORT bool from_string(string_view, ec&);
/// @relates ec
CAF_NET_EXPORT bool from_integer(std::underlying_type_t<ec>, ec&);
/// @relates ec
template <class Inspector>
bool inspect(Inspector& f, ec& x) {
return default_enum_inspect(f, x);
}
} // namespace caf::net::basp
CAF_ERROR_CODE_ENUM(caf::net::basp::ec)
......@@ -14,6 +14,7 @@
#include "caf/meta/type_name.hpp"
#include "caf/net/basp/constants.hpp"
#include "caf/net/basp/message_type.hpp"
#include "caf/type_id.hpp"
namespace caf::net::basp {
......@@ -78,3 +79,12 @@ bool inspect(Inspector& f, header& x) {
/// @}
} // namespace caf::net::basp
namespace caf {
template <>
struct type_name<net::basp::header> {
static constexpr string_view value = "caf::net::basp::header";
};
} // namespace caf
......@@ -6,6 +6,12 @@
#include <cstdint>
#include <string>
#include <type_traits>
#include "caf/default_enum_inspect.hpp"
#include "caf/detail/net_export.hpp"
#include "caf/fwd.hpp"
#include "caf/is_error_code_enum.hpp"
namespace caf::net::basp {
......@@ -55,7 +61,20 @@ enum class message_type : uint8_t {
};
/// @relates message_type
std::string to_string(message_type);
CAF_NET_EXPORT std::string to_string(message_type x);
/// @relates message_type
CAF_NET_EXPORT bool from_string(string_view, message_type&);
/// @relates message_type
CAF_NET_EXPORT bool from_integer(std::underlying_type_t<message_type>,
message_type&);
/// @relates message_type
template <class Inspector>
bool inspect(Inspector& f, message_type& x) {
return default_enum_inspect(f, x);
}
/// @}
......
......@@ -4,7 +4,14 @@
#pragma once
#include <cstdint>
#include <string>
#include <type_traits>
#include "caf/default_enum_inspect.hpp"
#include "caf/detail/net_export.hpp"
#include "caf/fwd.hpp"
#include "caf/is_error_code_enum.hpp"
namespace caf::net {
......@@ -17,22 +24,39 @@ enum class operation {
shutdown = 0x04,
};
/// @relates operation
constexpr operation operator|(operation x, operation y) {
return static_cast<operation>(static_cast<int>(x) | static_cast<int>(y));
}
/// @relates operation
constexpr operation operator&(operation x, operation y) {
return static_cast<operation>(static_cast<int>(x) & static_cast<int>(y));
}
/// @relates operation
constexpr operation operator^(operation x, operation y) {
return static_cast<operation>(static_cast<int>(x) ^ static_cast<int>(y));
}
/// @relates operation
constexpr operation operator~(operation x) {
return static_cast<operation>(~static_cast<int>(x));
}
std::string to_string(operation x);
/// @relates operation
CAF_NET_EXPORT std::string to_string(operation x);
/// @relates operation
CAF_NET_EXPORT bool from_string(string_view, operation&);
/// @relates operation
CAF_NET_EXPORT bool from_integer(std::underlying_type_t<operation>, operation&);
/// @relates operation
template <class Inspector>
bool inspect(Inspector& f, operation& x) {
return default_enum_inspect(f, x);
}
} // namespace caf::net
// clang-format off
// DO NOT EDIT: this file is auto-generated by caf-generate-enum-strings.
// Run the target update-enum-strings if this file is out of sync.
#include "caf/config.hpp"
#include "caf/string_view.hpp"
CAF_PUSH_DEPRECATED_WARNING
#include "caf/net/basp/connection_state.hpp"
#include <string>
......@@ -26,6 +31,45 @@ std::string to_string(connection_state x) {
};
}
bool from_string(string_view in, connection_state& out) {
if (in == "await_handshake_header") {
out = connection_state::await_handshake_header;
return true;
} else if (in == "await_handshake_payload") {
out = connection_state::await_handshake_payload;
return true;
} else if (in == "await_header") {
out = connection_state::await_header;
return true;
} else if (in == "await_payload") {
out = connection_state::await_payload;
return true;
} else if (in == "shutdown") {
out = connection_state::shutdown;
return true;
} else {
return false;
}
}
bool from_integer(std::underlying_type_t<connection_state> in,
connection_state& out) {
auto result = static_cast<connection_state>(in);
switch(result) {
default:
return false;
case connection_state::await_handshake_header:
case connection_state::await_handshake_payload:
case connection_state::await_header:
case connection_state::await_payload:
case connection_state::shutdown:
out = result;
return true;
};
}
} // namespace basp
} // namespace net
} // namespace caf
CAF_POP_WARNINGS
// clang-format off
// DO NOT EDIT: this file is auto-generated by caf-generate-enum-strings.
// Run the target update-enum-strings if this file is out of sync.
#include "caf/config.hpp"
#include "caf/string_view.hpp"
CAF_PUSH_DEPRECATED_WARNING
#include "caf/net/basp/ec.hpp"
#include <string>
......@@ -44,6 +49,81 @@ std::string to_string(ec x) {
};
}
bool from_string(string_view in, ec& out) {
if (in == "invalid_magic_number") {
out = ec::invalid_magic_number;
return true;
} else if (in == "unexpected_number_of_bytes") {
out = ec::unexpected_number_of_bytes;
return true;
} else if (in == "unexpected_payload") {
out = ec::unexpected_payload;
return true;
} else if (in == "missing_payload") {
out = ec::missing_payload;
return true;
} else if (in == "illegal_state") {
out = ec::illegal_state;
return true;
} else if (in == "invalid_handshake") {
out = ec::invalid_handshake;
return true;
} else if (in == "missing_handshake") {
out = ec::missing_handshake;
return true;
} else if (in == "unexpected_handshake") {
out = ec::unexpected_handshake;
return true;
} else if (in == "version_mismatch") {
out = ec::version_mismatch;
return true;
} else if (in == "unimplemented") {
out = ec::unimplemented;
return true;
} else if (in == "app_identifiers_mismatch") {
out = ec::app_identifiers_mismatch;
return true;
} else if (in == "invalid_payload") {
out = ec::invalid_payload;
return true;
} else if (in == "invalid_scheme") {
out = ec::invalid_scheme;
return true;
} else if (in == "invalid_locator") {
out = ec::invalid_locator;
return true;
} else {
return false;
}
}
bool from_integer(std::underlying_type_t<ec> in,
ec& out) {
auto result = static_cast<ec>(in);
switch(result) {
default:
return false;
case ec::invalid_magic_number:
case ec::unexpected_number_of_bytes:
case ec::unexpected_payload:
case ec::missing_payload:
case ec::illegal_state:
case ec::invalid_handshake:
case ec::missing_handshake:
case ec::unexpected_handshake:
case ec::version_mismatch:
case ec::unimplemented:
case ec::app_identifiers_mismatch:
case ec::invalid_payload:
case ec::invalid_scheme:
case ec::invalid_locator:
out = result;
return true;
};
}
} // namespace basp
} // namespace net
} // namespace caf
CAF_POP_WARNINGS
// clang-format off
// DO NOT EDIT: this file is auto-generated by caf-generate-enum-strings.
// Run the target update-enum-strings if this file is out of sync.
#include "caf/config.hpp"
#include "caf/string_view.hpp"
CAF_PUSH_DEPRECATED_WARNING
#include "caf/net/basp/message_type.hpp"
#include <string>
......@@ -30,6 +35,53 @@ std::string to_string(message_type x) {
};
}
bool from_string(string_view in, message_type& out) {
if (in == "handshake") {
out = message_type::handshake;
return true;
} else if (in == "actor_message") {
out = message_type::actor_message;
return true;
} else if (in == "resolve_request") {
out = message_type::resolve_request;
return true;
} else if (in == "resolve_response") {
out = message_type::resolve_response;
return true;
} else if (in == "monitor_message") {
out = message_type::monitor_message;
return true;
} else if (in == "down_message") {
out = message_type::down_message;
return true;
} else if (in == "heartbeat") {
out = message_type::heartbeat;
return true;
} else {
return false;
}
}
bool from_integer(std::underlying_type_t<message_type> in,
message_type& out) {
auto result = static_cast<message_type>(in);
switch(result) {
default:
return false;
case message_type::handshake:
case message_type::actor_message:
case message_type::resolve_request:
case message_type::resolve_response:
case message_type::monitor_message:
case message_type::down_message:
case message_type::heartbeat:
out = result;
return true;
};
}
} // namespace basp
} // namespace net
} // namespace caf
CAF_POP_WARNINGS
// clang-format off
// DO NOT EDIT: this file is auto-generated by caf-generate-enum-strings.
// Run the target update-enum-strings if this file is out of sync.
#include "caf/config.hpp"
#include "caf/string_view.hpp"
CAF_PUSH_DEPRECATED_WARNING
#include "caf/net/operation.hpp"
#include <string>
namespace caf {
namespace net {
std::string to_string(operation x) {
switch(x) {
default:
return "???";
case operation::none:
return "none";
case operation::read:
return "read";
case operation::write:
return "write";
case operation::read_write:
return "read_write";
case operation::shutdown:
return "shutdown";
};
}
bool from_string(string_view in, operation& out) {
if (in == "none") {
out = operation::none;
return true;
} else if (in == "read") {
out = operation::read;
return true;
} else if (in == "write") {
out = operation::write;
return true;
} else if (in == "read_write") {
out = operation::read_write;
return true;
} else if (in == "shutdown") {
out = operation::shutdown;
return true;
} else {
return false;
}
}
bool from_integer(std::underlying_type_t<operation> in,
operation& out) {
auto result = static_cast<operation>(in);
switch(result) {
default:
return false;
case operation::none:
case operation::read:
case operation::write:
case operation::read_write:
case operation::shutdown:
out = result;
return true;
};
}
} // namespace net
} // namespace caf
CAF_POP_WARNINGS
......@@ -40,5 +40,6 @@ CAF_TEST(serialization) {
CAF_TEST(to_string) {
basp::header x{basp::message_type::handshake, 42, 4};
CAF_CHECK_EQUAL(deep_to_string(x), "basp::header(handshake, 42, 4)");
CAF_CHECK_EQUAL(deep_to_string(x),
"caf::net::basp::header(handshake, 42, 4)");
}
......@@ -80,10 +80,10 @@ struct app_t {
auto sub_res = consume(down, buf.subspan(1), {});
return sub_res >= 0 ? sub_res + 1 : sub_res;
}
// Deserialize config value from received line.
auto num_bytes = std::distance(buf.begin(), i) + 1;
string_view line{reinterpret_cast<const char*>(buf.data()),
static_cast<size_t>(num_bytes) - 1};
CAF_MESSAGE("deserialize config value from : " << line);
config_value val;
if (auto parsed_res = config_value::parse(line)) {
val = std::move(*parsed_res);
......@@ -91,13 +91,7 @@ struct app_t {
down->abort_reason(std::move(parsed_res.error()));
return -1;
}
if (!holds_alternative<settings>(val)) {
down->abort_reason(
make_error(pec::type_mismatch,
"expected a dictionary, got a "s + val.type_name()));
return -1;
}
// Deserialize message from received dictionary.
CAF_MESSAGE("deserialize message from: " << val);
config_value_reader reader{&val};
caf::message msg;
if (!reader.apply(msg)) {
......@@ -190,7 +184,7 @@ struct fixture : host_fixture, test_coordinator_fixture<> {
};
constexpr std::string_view input = R"__(
{ values = [ { "@type" : "int32_t", value: 123 } ] }
[{"@type": "int32_t", "value": 123}]
)__";
} // namespace
......
......@@ -94,12 +94,6 @@ struct app_t {
down->abort_reason(std::move(parsed_res.error()));
return -1;
}
if (!holds_alternative<settings>(val)) {
down->abort_reason(
make_error(pec::type_mismatch,
"expected a dictionary, got a "s + val.type_name()));
return -1;
}
// Deserialize message from received dictionary.
config_value_reader reader{&val};
caf::message msg;
......@@ -193,7 +187,7 @@ struct fixture : host_fixture, test_coordinator_fixture<> {
};
constexpr std::string_view input = R"__(
{ values = [ { "@type" : "int32_t", value: 123 } ] }
[ { "@type" : "int32_t", value: 123 } ]
)__";
} // namespace
......
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