rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
supervisor_asio.h
1#pragma once
2
3//
4// Copyright (c) 2019-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "rotor/supervisor.h"
10#include "rotor/asio/export.h"
11#include "supervisor_config_asio.h"
12#include "system_context_asio.h"
13#include "forwarder.hpp"
14#include <boost/asio.hpp>
15#include <unordered_map>
16#include <memory>
17
18#if defined(_MSC_VER)
19#pragma warning(push)
20#pragma warning(disable : 4251)
21#endif
22
23namespace rotor {
24namespace asio {
25
26namespace asio = boost::asio;
27namespace sys = boost::system;
28
29template <typename Actor, typename Handler, typename ArgsCount, typename ErrHandler> struct forwarder_t;
30
56struct ROTOR_ASIO_API supervisor_asio_t : public supervisor_t {
57
60
62 template <typename Supervisor> using config_builder_t = supervisor_config_asio_builder_t<Supervisor>;
63
66
67 virtual address_ptr_t make_address() noexcept override;
68
69 virtual void start() noexcept override;
70 virtual void shutdown() noexcept override;
71 virtual void enqueue(message_ptr_t message) noexcept override;
72 virtual void shutdown_finish() noexcept override;
73
75 template <typename Handler, typename ErrHandler>
76 auto create_forwarder(Handler &&handler, ErrHandler &&err_handler) {
77 return forwarder_t{*this, std::move(handler), std::move(err_handler)};
78 }
79
81 template <typename Handler> auto create_forwarder(Handler &&handler) {
82 return forwarder_t{*this, std::move(handler)};
83 }
84
86 inline asio::io_context::strand &get_strand() noexcept { return *strand; }
87
89 void do_process() noexcept;
90
91 protected:
94 struct timer_t : public asio::deadline_timer {
95
98
100 timer_t(timer_handler_base_t *handler_, asio::io_context &io_context)
101 : asio::deadline_timer(io_context), handler{handler_} {}
102 };
103
105 using timer_ptr_t = std::unique_ptr<timer_t>;
106
108 using timers_map_t = std::unordered_map<request_id_t, timer_ptr_t>;
109
110 void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept override;
111 void do_cancel_timer(request_id_t timer_id) noexcept override;
112
114 using guard_t = asio::executor_work_guard<asio::io_context::executor_type>;
115
117 using guard_ptr_t = std::unique_ptr<guard_t>;
118
121
124
127
128 private:
129 void invoke_shutdown() noexcept;
130};
131
132template <typename Actor> inline boost::asio::io_context::strand &get_strand(Actor &actor) {
133 return actor.get_strand();
134}
135
136} // namespace asio
137} // namespace rotor
138
139#if defined(_MSC_VER)
140#pragma warning(pop)
141#endif
forwarder_t(Actor &, Handler &&) -> forwarder_t< Actor, Handler, typename details::callback_traits< Handler >::args_count, void >
Basic namespace for all rotor functionalities.
Definition rotor.hpp:21
intrusive_ptr_t< message_base_t > message_ptr_t
intrusive pointer for message
Definition message.h:118
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
std::size_t request_id_t
timer identifier type in the scope of the actor
Definition forward.hpp:34
Definition forwarder.hpp:111
boos::asio::deadline_timer with embedded timer handler
Definition supervisor_asio.h:94
timer_t(timer_handler_base_t *handler_, asio::io_context &io_context)
constructs timer using timer handler and boost asio io_context
Definition supervisor_asio.h:100
timer_handler_base_t * handler
non-owning pointer to timer handler
Definition supervisor_asio.h:97
delivers rotor-messages on top of boost asio event loop using strand for serialization
Definition supervisor_asio.h:56
timers_map_t timers_map
timer id to timer pointer mapping
Definition supervisor_asio.h:120
void do_cancel_timer(request_id_t timer_id) noexcept override
cancels timer (to be implemented in descendants)
std::unique_ptr< timer_t > timer_ptr_t
unique pointer to timer
Definition supervisor_asio.h:105
guard_ptr_t guard
guard to control ownership of the io-context
Definition supervisor_asio.h:126
void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept override
starts non-recurring timer (to be implemented in descendants)
virtual address_ptr_t make_address() noexcept override
creates new address_t linked with the supervisor
std::unordered_map< request_id_t, timer_ptr_t > timers_map_t
timer id to timer pointer mapping type
Definition supervisor_asio.h:108
asio::executor_work_guard< asio::io_context::executor_type > guard_t
guard type : alias for asio executor_work_guard
Definition supervisor_asio.h:114
supervisor_config_asio_t::strand_ptr_t strand
config for the supervisor
Definition supervisor_asio.h:123
supervisor_asio_t(supervisor_config_asio_t &config)
constructs new supervisor from asio supervisor config
std::unique_ptr< guard_t > guard_ptr_t
alias for a guard
Definition supervisor_asio.h:117
void do_process() noexcept
process queue of messages of locality leader
asio::io_context::strand & get_strand() noexcept
returns execution strand
Definition supervisor_asio.h:86
auto create_forwarder(Handler &&handler)
an helper for creation forwarder_t (no-error handler case)
Definition supervisor_asio.h:81
CRTP supervisor asio config builder.
Definition supervisor_config_asio.h:40
boost::asio supervisor config, which holds pointer to strand
Definition supervisor_config_asio.h:23
std::shared_ptr< strand_t > strand_ptr_t
type for strand shared pointer
Definition supervisor_config_asio.h:28
supervisor is responsible for managing actors (workers) lifetime
Definition supervisor.h:69
Base class for timer handler.
Definition timer_handler.hpp:17