Copyright Matus Chochlik. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
#include <iostream>
namespace msgbus {
struct str_utils_server
: main_ctx_object
, static_subscriber<1> {
using this_class = str_utils_server;
using base = static_subscriber<1>;
, base(
ep,
this,
auto reverse(
const message_context&, stored_message& msg) ->
bool {
auto str = msg.text_content();
return true;
}
};
struct str_utils_client
: main_ctx_object
, static_subscriber<1> {
using this_class = str_utils_client;
using base = static_subscriber<1>;
, base{ep,
this,
EAGINE_MSG_MAP(StrUtilRes, Reverse, this_class, print)} {
}
++_remaining;
}
auto print(const message_context&, stored_message& msg) -> bool {
log_info(
"received response: ${content}")
.arg(
EAGINE_ID(content), msg.text_content());
--_remaining;
return true;
}
auto is_done() const -> bool {
return _remaining <= 0;
}
private:
int _remaining{0};
};
}
auto main(main_ctx& ctx) -> int {
msgbus::endpoint bus{
EAGINE_ID(Loopback), ctx};
bus.add_connection(std::make_unique<msgbus::loopback_connection>());
msgbus::str_utils_server server(bus);
msgbus::str_utils_client client(bus);
client.call_reverse("foo");
client.call_reverse("bar");
client.call_reverse("baz");
client.call_reverse("qux");
while(!client.is_done()) {
bus.update();
server.process_one();
client.process_one();
}
return 0;
}
}
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
#define EAGINE_MSG_MAP(CLASS_ID, METHOD_ID, CLASS, METHOD)
Constructs an instance of static message handler map.
Definition: handler_map.hpp:72
Common code is placed in this namespace.
Definition: eagine.hpp:21
#define EAGINE_MSG_ID(API, NAME)
Macro for instantiating objects of static_message_id.
Definition: message_id.hpp:148
auto log_trace(string_view format) noexcept
Create a log message entry for tracing, with specified format.
Definition: logger.hpp:347
@ endpoint
Message bus client endpoint.
main_ctx_object(identifier obj_id, main_ctx_parent parent) noexcept
Initialization from object id and parent.
Definition: main_ctx_object.hpp:77
auto log_info(string_view format) noexcept
Create a log message entry for information, with specified format.
Definition: logger.hpp:329
static auto reverse(basic_span< T, P, S > spn) -> basic_span< T, P, S >
Reverses the elements in a span.
Definition: span_algo.hpp:561
auto bus() noexcept -> auto &
Returns a reference to the associated endpoint.
Definition: subscriber.hpp:38