OGLplus  (0.59.0) a C++ wrapper for rendering APIs

eagine/message_bus/001_loopback.cpp

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 <eagine/main.hpp>
#include <iostream>
namespace eagine {
namespace msgbus {
//------------------------------------------------------------------------------
struct str_utils_server
: main_ctx_object
, static_subscriber<1> {
using this_class = str_utils_server;
using base = static_subscriber<1>;
using base::bus;
str_utils_server(endpoint& ep)
: main_ctx_object{EAGINE_ID(Server), ep}
, base(
ep,
this,
EAGINE_MSG_MAP(StrUtilReq, Reverse, this_class, reverse)) {}
auto reverse(const message_context&, stored_message& msg) -> bool {
auto str = msg.text_content();
log_trace("received request: ${content}").arg(EAGINE_ID(content), str);
bus().post(EAGINE_MSG_ID(StrUtilRes, Reverse), as_bytes(str));
return true;
}
};
//------------------------------------------------------------------------------
struct str_utils_client
: main_ctx_object
, static_subscriber<1> {
using this_class = str_utils_client;
using base = static_subscriber<1>;
using base::bus;
str_utils_client(endpoint& ep)
: main_ctx_object{EAGINE_ID(Client), ep}
, base{ep, this, EAGINE_MSG_MAP(StrUtilRes, Reverse, this_class, print)} {
}
void call_reverse(string_view str) {
++_remaining;
bus().post(EAGINE_MSG_ID(StrUtilReq, Reverse), as_bytes(str));
}
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};
};
//------------------------------------------------------------------------------
} // namespace msgbus
auto main(main_ctx& ctx) -> int {
msgbus::endpoint bus{EAGINE_ID(Loopback), ctx};
bus.set_id(EAGINE_ID(BusExample));
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;
}
} // namespace eagine
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

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).