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

shutdown.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MESSAGE_BUS_SERVICE_SHUTDOWN_HPP
10 #define EAGINE_MESSAGE_BUS_SERVICE_SHUTDOWN_HPP
11 
12 #include "../serialize.hpp"
13 #include "../subscriber.hpp"
14 #include <chrono>
15 
16 namespace eagine::msgbus {
17 //------------------------------------------------------------------------------
18 // TODO replace with utc clock when available
19 using shutdown_service_clock = std::chrono::system_clock;
20 using shutdown_service_duration =
21  std::chrono::duration<std::int64_t, std::milli>;
22 //------------------------------------------------------------------------------
23 template <typename Base = subscriber>
24 class shutdown_target
25  : public Base
26  , protected shutdown_service_clock {
27  using This = shutdown_target;
28 
29 protected:
30  using Base::Base;
31 
32  void add_methods() {
33  Base::add_methods();
34  Base::add_method(
35  this, EAGINE_MSG_MAP(Shutdown, shutdown, This, _handle_shutdown));
36  }
37 
38 public:
39  virtual void on_shutdown(
40  std::chrono::milliseconds age,
41  identifier_t source_id,
42  verification_bits verified) = 0;
43 
44 private:
45  auto _handle_shutdown(const message_context&, stored_message& message)
46  -> bool {
47  typename shutdown_service_duration::rep count{0};
48  if(default_deserialize(count, message.content())) {
49  const shutdown_service_duration ticks{count};
50  const typename shutdown_service_clock::time_point ts{ticks};
51  const auto age{this->now() - ts};
52  on_shutdown(
53  std::chrono::duration_cast<std::chrono::milliseconds>(age),
54  message.source_id,
55  this->verify_bits(message));
56  }
57  return true;
58  }
59 };
60 //------------------------------------------------------------------------------
61 template <typename Base = subscriber>
62 class shutdown_invoker
63  : public Base
64  , protected shutdown_service_clock {
65 
66  using This = shutdown_invoker;
67 
68 protected:
69  using Base::Base;
70 
71 public:
72  void shutdown_one(identifier_t target_id) {
73  std::array<byte, 32> temp{};
74  const auto ts{this->now()};
75  const auto ticks{std::chrono::duration_cast<shutdown_service_duration>(
76  ts.time_since_epoch())};
77  const auto count{ticks.count()};
78  auto serialized{default_serialize(count, cover(temp))};
79  EAGINE_ASSERT(serialized);
80 
81  message_view message{extract(serialized)};
82  message.set_target_id(target_id);
83  this->bus().post_signed(EAGINE_MSG_ID(Shutdown, shutdown), message);
84  }
85 };
86 //------------------------------------------------------------------------------
87 } // namespace eagine::msgbus
88 
89 #endif // EAGINE_MESSAGE_BUS_SERVICE_SHUTDOWN_HPP
bitfield< verification_bit > verification_bits
Alias for a bus message verification bitfield.
Definition: verification.hpp:47
#define EAGINE_MSG_MAP(CLASS_ID, METHOD_ID, CLASS, METHOD)
Constructs an instance of static message handler map.
Definition: handler_map.hpp:72
#define EAGINE_MSG_ID(API, NAME)
Macro for instantiating objects of static_message_id.
Definition: message_id.hpp:148
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
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
auto default_serialize(T &value, memory::block blk) -> serialization_result< memory::const_block >
Uses the default backend to serialize a value into a memory block.
Definition: serialize.hpp:191
Message bus code is placed in this namespace.
Definition: eagine.hpp:58
auto default_deserialize(T &value, memory::const_block blk) -> deserialization_result< memory::const_block >
Uses the default backend to deserialize a value from a memory block.
Definition: serialize.hpp:235
std::uint64_t identifier_t
The underlying integer type for eagine::identifier.
Definition: identifier_t.hpp:19
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).