rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
actor_base.h
1#pragma once
2
3//
4// Copyright (c) 2019-2024 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "forward.hpp"
10#include "address.hpp"
11#include "actor_config.h"
12#include "messages.hpp"
13#include "state.h"
14#include "handler.h"
15#include "extended_error.h"
16#include "timer_handler.hpp"
17#include <set>
18
19#if defined(_MSC_VER)
20#pragma warning(push)
21#pragma warning(disable : 4251)
22#endif
23
24namespace rotor {
25
47struct ROTOR_API actor_base_t : public arc_base_t<actor_base_t> {
50
52 template <typename Actor> using config_builder_t = actor_config_builder_t<Actor>;
53
59 template <typename Handler>
60 using is_handler =
61 std::enable_if_t<std::is_member_function_pointer_v<Handler> || std::is_base_of_v<handler_base_t, Handler>>;
62
63 // clang-format off
70 using plugins_list_t = std::tuple<
79 // clang-format on
80
89 virtual ~actor_base_t();
90
98 virtual void do_initialize(system_context_t *ctx) noexcept;
99
108 virtual void do_shutdown(const extended_error_ptr_t &reason = {}) noexcept;
109
115 virtual void on_start() noexcept;
116
125 template <typename M, typename... Args> void send(const address_ptr_t &addr, Args &&...args);
126
134 template <typename R, typename... Args>
135 request_builder_t<typename request_wrapper_t<R>::request_t> request(const address_ptr_t &dest_addr, Args &&...args);
136
148 template <typename R, typename... Args>
149 request_builder_t<typename request_wrapper_t<R>::request_t>
150 request_via(const address_ptr_t &dest_addr, const address_ptr_t &reply_addr, Args &&...args);
151
156 template <typename Request, typename... Args> void reply_to(Request &message, Args &&...args);
157
159 template <typename Request> void reply_with_error(Request &message, const extended_error_ptr_t &ec);
160
169 template <typename Request, typename... Args> auto make_response(Request &message, Args &&...args);
170
179 template <typename Request> auto make_response(Request &message, const extended_error_ptr_t &ec);
180
182 template <typename Handler> subscription_info_ptr_t subscribe(Handler &&h, const address_ptr_t &addr) noexcept;
183
185 template <typename Handler> subscription_info_ptr_t subscribe(Handler &&h) noexcept;
186
188 template <typename Handler, typename = is_handler<Handler>>
189 void unsubscribe(Handler &&h, address_ptr_t &addr) noexcept;
190
192 template <typename Handler, typename = is_handler<Handler>> void unsubscribe(Handler &&h) noexcept;
193
194 /* \brief initiates handler unsubscription from the address
195 *
196 * If the address is local, then unsubscription confirmation is sent immediately,
197 * otherwise {@link payload::external_subscription_t} request is sent to the external
198 * supervisor, which owns the address.
199 *
200 * The optional call can be provided to be called upon message destruction.
201 *
202 */
203
205 inline void unsubscribe(const handler_ptr_t &h) noexcept { lifetime->unsubscribe(h, address); }
206
208 void activate_plugins() noexcept;
209
211 void commit_plugin_activation(plugin::plugin_base_t &plugin, bool success) noexcept;
212
214 void deactivate_plugins() noexcept;
215
217 void commit_plugin_deactivation(plugin::plugin_base_t &plugin) noexcept;
218
220 void on_subscription(message::subscription_t &message) noexcept;
221
223 void on_unsubscription(message::unsubscription_t &message) noexcept;
224
226 void on_unsubscription_external(message::unsubscription_external_t &message) noexcept;
227
229 address_ptr_t create_address() noexcept;
230
236 virtual void shutdown_start() noexcept;
237
245 void shutdown_continue() noexcept;
246
258 virtual void shutdown_finish() noexcept;
259
265 virtual void init_start() noexcept;
266
274 void init_continue() noexcept;
275
281 virtual void init_finish() noexcept;
282
284 virtual void configure(plugin::plugin_base_t &plugin) noexcept;
285
287 template <typename T> auto &access() noexcept;
288
290 template <typename T, typename... Args> auto access(Args... args) noexcept;
291
293 template <typename T> auto &access() const noexcept;
294
296 template <typename T, typename... Args> auto access(Args... args) const noexcept;
297
299 inline const address_ptr_t &get_address() const noexcept { return address; }
300
302 inline supervisor_t &get_supervisor() const noexcept { return *supervisor; }
303
322 template <typename Delegate, typename Method,
323 typename = std::enable_if_t<std::is_invocable_v<Method, Delegate *, request_id_t, bool>>>
324 request_id_t start_timer(const pt::time_duration &interval, Delegate &delegate, Method method) noexcept;
325
333 void cancel_timer(request_id_t request_id) noexcept;
334
340 inline const extended_error_ptr_t &get_shutdown_reason() const noexcept { return shutdown_reason; }
341
347 inline const std::string &get_identity() const noexcept { return identity; }
348
350 static const constexpr std::uint32_t PROGRESS_INIT = 1 << 0;
351
353 static const constexpr std::uint32_t PROGRESS_SHUTDOWN = 1 << 1;
354
363 static const constexpr std::uint32_t ESCALATE_FALIURE = 1 << 2;
364
373 static const constexpr std::uint32_t AUTOSHUTDOWN_SUPERVISOR = 1 << 3;
374
385 virtual bool should_restart() const noexcept;
386
387 protected:
389 using timers_map_t = std::unordered_map<request_id_t, timer_handler_ptr_t>;
390
392 using requests_t = std::unordered_set<request_id_t>;
393
395 void on_timer_trigger(request_id_t request_id, bool cancelled) noexcept;
396
398 template <typename Delegate, typename Method>
399 void start_timer(request_id_t request_id, const pt::time_duration &interval, Delegate &delegate,
400 Method method) noexcept;
401
403 void assign_shutdown_reason(extended_error_ptr_t reason) noexcept;
404
406 extended_error_ptr_t make_error(const std::error_code &ec, const extended_error_ptr_t &next = {},
407 const message_ptr_t &request = {}) const noexcept;
408
414 virtual bool on_unlink(const address_ptr_t &server_addr) noexcept;
415
418
421
424
427
429 std::string identity;
430
433
436
439
441 pt::time_duration init_timeout;
442
444 pt::time_duration shutdown_timeout;
445
448
450 plugin::address_maker_plugin_t *address_maker = nullptr;
451
453 plugin::lifetime_plugin_t *lifetime = nullptr;
454
456 plugin::link_server_plugin_t *link_server = nullptr;
457
459 plugin::resources_plugin_t *resources = nullptr;
460
465 plugin::plugin_base_t *get_plugin(const std::type_index &) const noexcept;
466
468 std::set<const std::type_index *> activating_plugins;
469
471 std::set<const std::type_index *> deactivating_plugins;
472
475
478
486 std::uint32_t continuation_mask = 0;
487
490
491 friend struct plugin::plugin_base_t;
492 friend struct plugin::lifetime_plugin_t;
493 friend struct supervisor_t;
494 template <typename T> friend struct request_builder_t;
495 template <typename T, typename M> friend struct accessor_t;
496};
497
498} // namespace rotor
499
500#if defined(_MSC_VER)
501#pragma warning(pop)
502#endif
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:115
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
intrusive_ptr_t< extended_error_t > extended_error_ptr_t
intrusive pointer to extended error type
Definition extended_error.h:25
std::unique_ptr< plugin_storage_base_t > plugin_storage_ptr_t
smart pointer for plugin_storage_base_t
Definition actor_config.h:38
state_t
state of actor in rotor
Definition state.h:12
boost::intrusive_ref_counter< T, counter_policy_t > arc_base_t
base class to inject ref-counter with the specified policy
Definition arc.hpp:24
intrusive_ptr_t< handler_base_t > handler_ptr_t
intrusive pointer for handler
Definition forward.hpp:26
std::size_t request_id_t
timer identifier type in the scope of the actor
Definition forward.hpp:34
intrusive_ptr_t< subscription_info_t > subscription_info_ptr_t
intrusive pointer for subscription_info_t
Definition subscription_point.h:127
std::deque< plugin::plugin_base_t * > plugins_t
list of raw plugin pointers
Definition actor_config.h:26
boost::intrusive_ptr< T > intrusive_ptr_t
alias for intrusive pointer
Definition arc.hpp:27
std::unique_ptr< timer_handler_base_t > timer_handler_ptr_t
alias for timer smart pointer
Definition timer_handler.hpp:36
universal primitive of concurrent computation
Definition actor_base.h:47
std::enable_if_t< std::is_member_function_pointer_v< Handler >||std::is_base_of_v< handler_base_t, Handler > > is_handler
SFINAE handler detector.
Definition actor_base.h:61
timers_map_t timers_map
timer-id to timer-handler map
Definition actor_base.h:474
pt::time_duration init_timeout
timeout for actor initialization (used by supervisor)
Definition actor_base.h:441
pt::time_duration shutdown_timeout
timeout for actor shutdown (used by supervisor)
Definition actor_base.h:444
std::tuple< plugin::address_maker_plugin_t, plugin::lifetime_plugin_t, plugin::init_shutdown_plugin_t, plugin::link_server_plugin_t, plugin::link_client_plugin_t, plugin::registry_plugin_t, plugin::resources_plugin_t, plugin::starter_plugin_t > plugins_list_t
the default list of plugins for an actor
Definition actor_base.h:78
virtual void do_shutdown(const extended_error_ptr_t &reason={}) noexcept
convenient method to send actor's supervisor shutdown trigger message
requests_t active_requests
list of ids of active requests
Definition actor_base.h:477
virtual bool should_restart() const noexcept
whether spawner should create a new instance of the actor
void activate_plugins() noexcept
starts plugins activation
std::set< const std::type_index * > deactivating_plugins
set of deactivating plugin identities
Definition actor_base.h:471
std::unordered_set< request_id_t > requests_t
list of ids of active requests (type)
Definition actor_base.h:392
actor_base_t(config_t &cfg)
constructs actor and links it's supervisor
supervisor_t & get_supervisor() const noexcept
returns actor's supervisor
Definition actor_base.h:302
const std::string & get_identity() const noexcept
retuns human-readable actor identity
Definition actor_base.h:347
plugins_t plugins
non-owning list of plugins
Definition actor_base.h:438
plugin_storage_ptr_t plugins_storage
opaque plugins storage (owning)
Definition actor_base.h:435
plugin::plugin_base_t * get_plugin(const std::type_index &) const noexcept
finds plugin by plugin class identity
address_ptr_t spawner_address
actor spawner address
Definition actor_base.h:426
std::set< const std::type_index * > activating_plugins
set of activating plugin identities
Definition actor_base.h:468
const extended_error_ptr_t & get_shutdown_reason() const noexcept
returns actor shutdown reason
Definition actor_base.h:340
virtual void on_start() noexcept
actor is fully initialized and it's supervisor has sent signal to start
virtual void do_initialize(system_context_t *ctx) noexcept
early actor initialization (pre-initialization)
intrusive_ptr_t< message::init_request_t > init_request
suspended init request message
Definition actor_base.h:417
extended_error_ptr_t shutdown_reason
explanation, why actor is been requested for shut down
Definition actor_base.h:489
state_t state
current actor state
Definition actor_base.h:447
supervisor_t * supervisor
non-owning pointer to actor's execution / infrastructure context
Definition actor_base.h:432
std::unordered_map< request_id_t, timer_handler_ptr_t > timers_map_t
timer-id to timer-handler map (type)
Definition actor_base.h:389
void cancel_timer(request_id_t request_id) noexcept
cancels previously started timer
intrusive_ptr_t< message::shutdown_request_t > shutdown_request
suspended shutdown request message
Definition actor_base.h:420
virtual bool on_unlink(const address_ptr_t &server_addr) noexcept
notification, when actor has been unlinked from server actor
address_ptr_t address
actor address
Definition actor_base.h:423
std::string identity
actor identity, which might have some meaning for developers
Definition actor_base.h:429
CRTP actor config builder.
Definition actor_config.h:92
basic actor configuration: init and shutdown timeouts, etc.
Definition actor_config.h:62
create actor's addresses
Definition address_maker.h:24
manages actors init and shutdown procedures
Definition init_shutdown.h:22
manages all actor subscriptions (i.e. from plugins or actor itself).
Definition lifetime.h:21
base class for all actor plugins
Definition plugin_base.h:23
handy access to registry_t, for name registration and discovery
Definition registry.h:35
"lock" for external resources
Definition resources.h:39
allows custom (actor) subscriptions and it is responsible for starting actor when it receives message...
Definition starter.h:19
builder pattern implementation for the original request
Definition request.hpp:387
optionally wraps request type into intrusive pointer
Definition request.hpp:43
Holds and classifies message handlers on behalf of supervisor.
Definition subscription.h:30
supervisor is responsible for managing actors (workers) lifetime
Definition supervisor.h:69
The system context holds root supervisor_t (intrusive pointer) and may be loop-related details in der...
Definition system_context.h:32