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

eagine/message_bus/005_byte_histogram.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 <array>
#include <thread>
namespace eagine {
auto main(main_ctx& ctx) -> int {
std::array<span_size_t, 256> byte_counts{};
auto log_byte_hist = [&ctx, &byte_counts](
const msgbus::message_context& mc,
msgbus::stored_message& msg) {
zero(cover(byte_counts));
span_size_t max_count{0};
for(auto b : msg.content()) {
max_count = math::maximum(max_count, ++byte_counts[std_size(b)]);
}
ctx.log()
.info("received blob message ${message}")
.arg(EAGINE_ID(message), mc.msg_id())
.arg_func([&byte_counts, max_count](logger_backend& backend) {
for(std::size_t i = 0; i < 256; ++i) {
backend.add_float(
EAGINE_ID(Histogram),
float(0),
float(byte_counts[i]),
float(max_count));
}
});
return true;
};
auto allow_all_blobs = [](message_id) {
return true;
};
msgbus::router_address address{ctx};
msgbus::connection_setup conn_setup(ctx);
msgbus::endpoint bus{
main_ctx_object{EAGINE_ID(Temporary), ctx},
conn_setup.setup_connectors(bus, address);
timeout idle_too_long{std::chrono::minutes{1}};
while(!idle_too_long) {
if(
bus.update() ||
bus.process_everything({construct_from, log_byte_hist})) {
idle_too_long.reset();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
bus.finish();
return 0;
}
} // namespace eagine
blob_manipulator::filter_function blob_filter_function
Alias for blob message type filter callable reference.
Definition: endpoint.hpp:47
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
basic_address< false > address
Type alias for non-const memory address values.
Definition: address.hpp:203
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
static constexpr auto maximum(T a, T b) noexcept
Returns the maximum value of a and b.
Definition: functions.hpp:61
static auto zero(basic_span< T, P, S > spn) -> std::enable_if_t< std::is_integral_v< T >||std::is_floating_point_v< T >, basic_span< T, P, S >>
Fills a span with zero value of type T.
Definition: span_algo.hpp:548
auto byte_to_identifier(byte b) noexcept -> identifier
Creates an identifier representing a byte value as a hex string.
@ message_id
The message type id has been verified.

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