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

eagine/message_bus/006_pong.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 <thread>
namespace eagine {
namespace msgbus {
//------------------------------------------------------------------------------
class pong : public actor<2> {
public:
using base = actor<2>;
using base::bus;
pong(
connection_setup& conn_setup,
: base(
{EAGINE_ID(ExamplPong), parent},
this,
EAGINE_MSG_MAP(PingPong, Ping, pong, ping),
EAGINE_MSG_MAP(PingPong, Shutdown, pong, shutdown))
, _lmod{running_on_valgrind() ? 1000U : 10000U} {
this->allow_subscriptions();
conn_setup.setup_connectors(
*this,
}
auto ping(const message_context&, stored_message& msg_in) -> bool {
bus().respond_to(msg_in, EAGINE_MSG_ID(PingPong, Pong));
if(++_sent % _lmod == 0) {
bus().log_info("sent ${count} pongs").arg(EAGINE_ID(count), _sent);
}
_timeout.reset();
return true;
}
auto shutdown(const message_context&, stored_message&) -> bool {
_done = true;
bus().log_info("received shutdown message");
return true;
}
void update() {
if(!_sent && _ready_timeout) {
bus().broadcast(EAGINE_MSG_ID(PingPong, Ready));
_ready_timeout.reset();
} else {
std::this_thread::yield();
}
}
auto is_done() const noexcept {
return _done || _timeout;
}
private:
std::size_t _lmod{1};
std::size_t _sent{0};
timeout _timeout{std::chrono::minutes(1)};
timeout _ready_timeout{std::chrono::seconds(1)};
bool _done{false};
};
//------------------------------------------------------------------------------
} // namespace msgbus
auto main(main_ctx& ctx) -> int {
msgbus::router_address address{ctx};
msgbus::connection_setup conn_setup(ctx);
msgbus::pong pong(ctx, conn_setup, address);
while(!pong.is_done()) {
pong.process_all();
pong.update();
}
return 0;
}
} // namespace eagine
const main_ctx_object_parent_info & main_ctx_parent
Alias for main_ctx_object_parent_info parameter type.
Definition: main_ctx_fwd.hpp:24
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
#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
@ local_interprocess
Inter-process connection for local communication.
auto bus() noexcept -> endpoint &
Returns a reference to the associated endpoint.
Definition: actor.hpp:45
@ remote_interprocess
Inter-process connection for remote communucation.
basic_address< false > address
Type alias for non-const memory address values.
Definition: address.hpp:203
static constexpr auto running_on_valgrind() noexcept -> tribool
Indicates if the current process runs on top of valgrind.
Definition: valgrind.hpp:30

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