supervisor.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2024 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "actor_base.h"
  8. #include "handler.h"
  9. #include "message.h"
  10. #include "subscription.h"
  11. #include "system_context.h"
  12. #include "supervisor_config.h"
  13. #include "address_mapping.h"
  14. #include "error_code.h"
  15. #include "spawner.h"
  16. #include <functional>
  17. #include <unordered_map>
  18. #include <unordered_set>
  19. #include <boost/lockfree/queue.hpp>
  20. #if defined(_MSC_VER)
  21. #pragma warning(push)
  22. #pragma warning(disable : 4251)
  23. #endif
  24. namespace rotor {
  25. /** \struct supervisor_t
  26. * \brief supervisor is responsible for managing actors (workers) lifetime
  27. *
  28. * Supervisor starts, stops actors (children/workers) and process messages.
  29. * The message processing is basically sorting messages by their destination
  30. * {@link address_t}: if an address belongs to the supervisor, then message
  31. * is dispatched locally, otherwise the message is forwarded to supervisor,
  32. * which owns address.
  33. *
  34. * During message dispatching phase, supervisor examines handlers
  35. * ({@link handler_base_t}), if they are local, then a message in immediately
  36. * delivered to it (i.e. a local actor is invoked immediately), otherwise
  37. * is is forwarded for delivery to the supervisor, which owns the handler.
  38. *
  39. * Supervisor is responsible for managing it's local actors lifetime, i.e.
  40. * sending initialization, start, shutdown requests etc.
  41. *
  42. * Supervisor is locality-aware: i.e. if two supervisors have the same
  43. * locality (i.e. executed in the same thread/event loop), it takes advantage
  44. * of this and immediately delivers message to the target supervisor
  45. * without involving any synchronization mechanisms. In other words,
  46. * a message is delivered to any actor of the locality, even if the
  47. * actor is not child of the current supervisor.
  48. *
  49. * As supervisor is special kind of actor, it should be possible to spawn
  50. * other supervisors constructing tree-like organization of responsibilities.
  51. *
  52. * Unlike Erlang's supervisor, rotor's supervisor does not spawn actors
  53. * if they terminated. It should be possible, however, to implement it in derived
  54. * classes with application-specific logic.
  55. *
  56. * This supervisor class is abstract, and the concrete implementation is
  57. * is event-loop specific, i.e. it should know how to start/stop shutdown
  58. * timers, how to trigger messages processing in thread-safe way, how
  59. * to deliver message to a supervisor in a thread-safe way etc.
  60. *
  61. */
  62. struct ROTOR_API supervisor_t : public actor_base_t {
  63. // clang-format off
  64. /** \brief the default list of plugins for an supervisor
  65. *
  66. * The order of plugins is very important, as they are initialized in the direct order
  67. * and deinitilized in the reverse order.
  68. *
  69. */
  70. using plugins_list_t = std::tuple<
  71. plugin::address_maker_plugin_t,
  72. plugin::locality_plugin_t,
  73. plugin::delivery_plugin_t<plugin::default_local_delivery_t>,
  74. plugin::lifetime_plugin_t,
  75. plugin::init_shutdown_plugin_t,
  76. plugin::foreigners_support_plugin_t,
  77. plugin::child_manager_plugin_t,
  78. plugin::link_server_plugin_t,
  79. plugin::link_client_plugin_t,
  80. plugin::registry_plugin_t,
  81. plugin::resources_plugin_t,
  82. plugin::starter_plugin_t>;
  83. // clang-format on
  84. /** \brief injects an alias for supervisor_config_t */
  85. using config_t = supervisor_config_t;
  86. /** \brief injects templated supervisor_config_builder_t */
  87. template <typename Supervisor> using config_builder_t = supervisor_config_builder_t<Supervisor>;
  88. /** \brief constructs new supervisor with optional parent supervisor */
  89. supervisor_t(supervisor_config_t &config);
  90. supervisor_t(const supervisor_t &) = delete;
  91. supervisor_t(supervisor_t &&) = delete;
  92. ~supervisor_t();
  93. virtual void do_initialize(system_context_t *ctx) noexcept override;
  94. /** \brief process queue of messages of locality leader
  95. *
  96. * The locality leaders queue `queue` of messages is processed.
  97. *
  98. * -# It takes message from the queue
  99. * -# If the message destination address belongs to the foreign the supervisor,
  100. * then it is forwarded to it immediately.
  101. * -# Otherwise, the message is local, i.e. either for the supervisor or one
  102. * of its non-supervisor children (internal), or to other supervisor within
  103. * the same locality.
  104. * -# in the former case the message is immediately delivered locally in
  105. * the context of current supervisor; in the latter case in the context
  106. * of other supervisor. In the both cases `deliver_local` method is used.
  107. *
  108. * It is expected, that derived classes should invoke `do_process` message,
  109. * whenever it is known that there are messages for processing. The invocation
  110. * should be performed in safe thread/loop context.
  111. *
  112. * The method should be invoked in event-loop context only.
  113. *
  114. * The method returns amount of messages it enqueued for other locality leaders
  115. * (i.e. to be processed externally).
  116. *
  117. */
  118. inline size_t do_process() noexcept { return locality_leader->delivery->process(); }
  119. /** \brief creates new {@link address_t} linked with the supervisor */
  120. virtual address_ptr_t make_address() noexcept;
  121. /** \brief removes the subscription point: local address and (foreign-or-local)
  122. * handler pair
  123. */
  124. virtual void commit_unsubscription(const subscription_info_ptr_t &info) noexcept;
  125. /** \brief thread-safe version of `do_process`
  126. *
  127. * Starts supervisor to processing messages queue in safe thread/loop
  128. * context. Once it becomes empty, the method returns
  129. */
  130. virtual void start() noexcept = 0;
  131. void on_start() noexcept override;
  132. /** \brief thread-safe version of `do_shutdown`, i.e. send shutdown request
  133. * let it be processed by the supervisor */
  134. virtual void shutdown() noexcept = 0;
  135. void do_shutdown(const extended_error_ptr_t &reason = {}) noexcept override;
  136. void shutdown_finish() noexcept override;
  137. /** \brief supervisor hook for reaction on child actor init */
  138. virtual void on_child_init(actor_base_t *actor, const extended_error_ptr_t &ec) noexcept;
  139. /** \brief supervisor hook for reaction on child actor shutdown */
  140. virtual void on_child_shutdown(actor_base_t *actor) noexcept;
  141. /** \brief enqueues messages thread safe way and triggers processing
  142. *
  143. * This is the only method for deliver message outside of `rotor` context.
  144. * Basically it is `put` and `process` in the event loop context.
  145. *
  146. * The thread-safety should be guaranteed by derived class and/or used event-loop.
  147. *
  148. * This method is used for messaging between supervisors with different
  149. * localities, event loops or threads.
  150. *
  151. */
  152. virtual void enqueue(message_ptr_t message) noexcept = 0;
  153. /** \brief puts a message into internal supervisor queue for further processing
  154. *
  155. * This is thread-unsafe method. The `enqueue` method should be used to put
  156. * a new message from external context in thread-safe way.
  157. *
  158. */
  159. inline void put(message_ptr_t message) { locality_leader->queue.emplace_back(std::move(message)); }
  160. /** \brief templated version of `subscribe_actor` */
  161. template <typename Handler> void subscribe(actor_base_t &actor, Handler &&handler) {
  162. supervisor->subscribe(actor.address, wrap_handler(actor, std::move(handler)));
  163. }
  164. /** \brief convenient templated version of `unsubscribe_actor */
  165. template <typename Handler> inline void unsubscribe_actor(const address_ptr_t &addr, Handler &&handler) noexcept {
  166. handler_ptr_t wrapped_handler(std::forward<Handler>(handler));
  167. lifetime->unsubscribe(wrapped_handler, addr);
  168. }
  169. /** \brief creates child-actor builder */
  170. template <typename Actor> auto create_actor() {
  171. using builder_t = typename Actor::template config_builder_t<Actor>;
  172. assert(manager && "child_manager_plugin_t should be already initialized");
  173. return builder_t([this](auto &actor) { manager->create_child(actor); }, this);
  174. }
  175. /** \brief convenient method for request building
  176. *
  177. * The built request isn't sent immediately, but only after invoking `send(timeout)`
  178. *
  179. */
  180. template <typename T, typename... Args>
  181. request_builder_t<T> do_request(actor_base_t &actor, const address_ptr_t &dest_addr, const address_ptr_t &reply_to,
  182. Args &&...args) noexcept {
  183. return request_builder_t<T>(*this, actor, dest_addr, reply_to, std::forward<Args>(args)...);
  184. }
  185. /**
  186. * \brief main subscription implementation
  187. *
  188. * The subscription point is materialized into subscription info. If address is
  189. * internal/local, then it is immediately confirmed to the source actor as
  190. * {@link payload::subscription_confirmation_t}.
  191. *
  192. * Otherwise, if the address is external (foreign), then subscription request
  193. * is forwarded to appropiate supervisor as {@link payload::external_subscription_t}
  194. * request.
  195. *
  196. * The materialized subscription info is returned.
  197. *
  198. */
  199. subscription_info_ptr_t subscribe(const handler_ptr_t &handler, const address_ptr_t &addr,
  200. const actor_base_t *owner_ptr, owner_tag_t owner_tag) noexcept;
  201. /** \brief returns an actor spawner
  202. *
  203. * Spawner allows to create a new actor instance, when the current actor
  204. * instance is down. Different policies (reactions) can be applied.
  205. *
  206. */
  207. spawner_t spawn(factory_t) noexcept;
  208. using actor_base_t::subscribe;
  209. /** \brief returns registry actor address (if it was defined or registry actor was created) */
  210. inline const address_ptr_t &get_registry_address() const noexcept { return registry_address; }
  211. /** \brief generic non-public fields accessor */
  212. template <typename T> auto &access() noexcept;
  213. /** \brief generic non-public methods accessor */
  214. template <typename T, typename... Args> auto access(Args... args) noexcept;
  215. /** \brief lock-free queue for inbound messages */
  216. using inbound_queue_t = boost::lockfree::queue<message_base_t *>;
  217. protected:
  218. /** \brief creates new address with respect to supervisor locality mark */
  219. virtual address_ptr_t instantiate_address(const void *locality) noexcept;
  220. /** \brief timer to response with timeout procedure type */
  221. using request_map_t = std::unordered_map<request_id_t, request_curry_t>;
  222. /** \brief invoked as timer callback; creates response or just clean up for previously set request */
  223. void on_request_trigger(request_id_t timer_id, bool cancelled) noexcept;
  224. /** \brief starts non-recurring timer (to be implemented in descendants) */
  225. virtual void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept = 0;
  226. /** \brief cancels timer (to be implemented in descendants) */
  227. virtual void do_cancel_timer(request_id_t timer_id) noexcept = 0;
  228. /** \brief intercepts message delivery for the tagged handler */
  229. virtual void intercept(message_ptr_t &message, const void *tag, const continuation_t &continuation) noexcept;
  230. /** \brief non-owning pointer to system context. */
  231. system_context_t *context;
  232. /** \brief queue of unprocessed messages */
  233. messages_queue_t queue;
  234. /** \brief counter for request/timer ids */
  235. request_id_t last_req_id;
  236. /** \brief timer to response with timeout procedure */
  237. request_map_t request_map;
  238. /** \brief main subscription support class */
  239. subscription_t subscription_map;
  240. /** \brief non-owning pointer to parent supervisor, `NULL` for root supervisor */
  241. supervisor_t *parent;
  242. /** \brief delivery plugin pointer */
  243. plugin::delivery_plugin_base_t *delivery = nullptr;
  244. /** \brief child manager plugin pointer */
  245. plugin::child_manager_plugin_t *manager = nullptr;
  246. /** \brief root supervisor for the locality */
  247. supervisor_t *locality_leader;
  248. /** \brief inbound queue for external messages */
  249. inbound_queue_t inbound_queue;
  250. /** \brief size of inbound queue */
  251. size_t inbound_queue_size;
  252. /** \brief how much time spend in active inbound queue polling */
  253. pt::time_duration poll_duration;
  254. /** \brief when flag is set, the supervisor will shut self down */
  255. const std::atomic_bool *shutdown_flag = nullptr;
  256. /** \brief frequency to check atomic shutdown flag */
  257. pt::time_duration shutdown_poll_frequency = pt::millisec{100};
  258. private:
  259. using actors_set_t = std::unordered_set<const actor_base_t *>;
  260. bool create_registry;
  261. bool synchronize_start;
  262. address_ptr_t registry_address;
  263. actors_set_t alive_actors;
  264. supervisor_policy_t policy;
  265. /** \brief per-actor and per-message request tracking support */
  266. address_mapping_t address_mapping;
  267. template <typename T> friend struct request_builder_t;
  268. template <typename Supervisor> friend struct actor_config_builder_t;
  269. friend struct plugin::delivery_plugin_base_t;
  270. friend struct actor_base_t;
  271. template <typename T> friend struct plugin::delivery_plugin_t;
  272. void discard_request(request_id_t request_id) noexcept;
  273. void uplift_last_message() noexcept;
  274. void on_shutdown_check_timer(request_id_t, bool cancelled) noexcept;
  275. inline request_id_t next_request_id() noexcept {
  276. AGAIN:
  277. auto &map = locality_leader->request_map;
  278. auto it = map.find(++locality_leader->last_req_id);
  279. if (it != map.end()) {
  280. goto AGAIN;
  281. }
  282. return locality_leader->last_req_id;
  283. }
  284. };
  285. using supervisor_ptr_t = intrusive_ptr_t<supervisor_t>;
  286. /* third-party classes implementations */
  287. template <typename Supervisor> auto system_context_t::create_supervisor() {
  288. using builder_t = typename Supervisor::template config_builder_t<Supervisor>;
  289. return builder_t(
  290. [this](auto &actor) {
  291. if (supervisor) {
  292. auto ec = make_error_code(error_code_t::supervisor_defined);
  293. on_error(actor.get(), make_error(identity(), ec));
  294. actor.reset();
  295. } else {
  296. this->supervisor = actor;
  297. actor->do_initialize(this);
  298. }
  299. },
  300. *this);
  301. }
  302. template <typename M, typename... Args> void actor_base_t::send(const address_ptr_t &addr, Args &&...args) {
  303. supervisor->put(make_message<M>(addr, std::forward<Args>(args)...));
  304. }
  305. template <typename Delegate, typename Method>
  306. void actor_base_t::start_timer(request_id_t request_id, const pt::time_duration &interval, Delegate &delegate,
  307. Method method) noexcept {
  308. using final_handler_t = timer_handler_t<Delegate, Method>;
  309. auto handler = std::make_unique<final_handler_t>(this, request_id, &delegate, std::forward<Method>(method));
  310. supervisor->do_start_timer(interval, *handler);
  311. timers_map.emplace(request_id, std::move(handler));
  312. }
  313. template <typename Delegate, typename Method, typename>
  314. request_id_t actor_base_t::start_timer(const pt::time_duration &interval, Delegate &delegate, Method method) noexcept {
  315. auto request_id = supervisor->next_request_id();
  316. start_timer(request_id, interval, delegate, std::forward<Method>(method));
  317. return request_id;
  318. }
  319. /** \brief wraps handler (pointer to member function) and actor address into intrusive pointer */
  320. template <typename Handler> handler_ptr_t wrap_handler(actor_base_t &actor, Handler &&handler) {
  321. using final_handler_t = handler_t<Handler>;
  322. auto handler_raw = new final_handler_t(actor, std::move(handler));
  323. return handler_ptr_t{handler_raw};
  324. }
  325. template <typename Handler> subscription_info_ptr_t actor_base_t::subscribe(Handler &&h) noexcept {
  326. auto wrapped_handler = wrap_handler(*this, std::move(h));
  327. return supervisor->subscribe(wrapped_handler, address, this, owner_tag_t::ANONYMOUS);
  328. }
  329. template <typename Handler>
  330. subscription_info_ptr_t actor_base_t::subscribe(Handler &&h, const address_ptr_t &addr) noexcept {
  331. auto wrapped_handler = wrap_handler(*this, std::move(h));
  332. return supervisor->subscribe(wrapped_handler, addr, this, owner_tag_t::ANONYMOUS);
  333. }
  334. namespace plugin {
  335. template <typename Handler>
  336. subscription_info_ptr_t plugin_base_t::subscribe(Handler &&h, const address_ptr_t &addr) noexcept {
  337. using final_handler_t = handler_t<Handler>;
  338. handler_ptr_t wrapped_handler(new final_handler_t(*this, std::move(h)));
  339. auto info = actor->supervisor->subscribe(wrapped_handler, addr, actor, owner_tag_t::PLUGIN);
  340. own_subscriptions.emplace_back(info);
  341. return info;
  342. }
  343. template <typename Handler> subscription_info_ptr_t plugin_base_t::subscribe(Handler &&h) noexcept {
  344. return subscribe(std::forward<Handler>(h), actor->address);
  345. }
  346. template <> inline auto &plugin_base_t::access<plugin::starter_plugin_t>() noexcept { return own_subscriptions; }
  347. template <typename Handler> subscription_info_ptr_t starter_plugin_t::subscribe_actor(Handler &&handler) noexcept {
  348. auto &address = actor->get_address();
  349. return subscribe_actor(std::forward<Handler>(handler), address);
  350. }
  351. template <typename Handler>
  352. subscription_info_ptr_t starter_plugin_t::subscribe_actor(Handler &&handler, const address_ptr_t &addr) noexcept {
  353. auto wrapped_handler = wrap_handler(*actor, std::move(handler));
  354. auto info = actor->get_supervisor().subscribe(wrapped_handler, addr, actor, owner_tag_t::PLUGIN);
  355. assert(std::count_if(tracked.begin(), tracked.end(), [&](auto &it) { return *it == *info; }) == 0 &&
  356. "already subscribed");
  357. tracked.emplace_back(info);
  358. access<starter_plugin_t>().emplace_back(info);
  359. return info;
  360. }
  361. template <> inline size_t delivery_plugin_t<plugin::local_delivery_t>::process() noexcept {
  362. size_t enqueued_messages{0};
  363. while (queue->size()) {
  364. auto message = message_ptr_t(queue->front().detach(), false);
  365. auto &dest = message->address;
  366. queue->pop_front();
  367. auto internal = dest->same_locality(*address);
  368. if (internal) { /* subscriptions are handled by me */
  369. auto local_recipients = subscription_map->get_recipients(*message);
  370. if (local_recipients) {
  371. plugin::local_delivery_t::delivery(message, *local_recipients);
  372. }
  373. } else {
  374. dest->supervisor.enqueue(std::move(message));
  375. ++enqueued_messages;
  376. }
  377. }
  378. return enqueued_messages;
  379. }
  380. template <> inline size_t delivery_plugin_t<plugin::inspected_local_delivery_t>::process() noexcept {
  381. size_t enqueued_messages{0};
  382. while (queue->size()) {
  383. auto message = message_ptr_t(queue->front().detach(), false);
  384. auto &dest = message->address;
  385. queue->pop_front();
  386. auto internal = dest->same_locality(*address);
  387. const subscription_t::joint_handlers_t *local_recipients = nullptr;
  388. bool delivery_attempt = false;
  389. if (internal) { /* subscriptions are handled by me */
  390. local_recipients = subscription_map->get_recipients(*message);
  391. delivery_attempt = true;
  392. } else {
  393. dest->supervisor.enqueue(std::move(message));
  394. ++enqueued_messages;
  395. }
  396. if (local_recipients) {
  397. plugin::inspected_local_delivery_t::delivery(message, *local_recipients, stringifier);
  398. } else {
  399. if (delivery_attempt) {
  400. plugin::inspected_local_delivery_t::discard(message, stringifier);
  401. }
  402. }
  403. }
  404. return enqueued_messages;
  405. }
  406. } // namespace plugin
  407. template <typename Handler, typename Enabled> void actor_base_t::unsubscribe(Handler &&h) noexcept {
  408. supervisor->unsubscribe_actor(address, wrap_handler(*this, std::move(h)));
  409. }
  410. template <typename Handler, typename Enabled>
  411. void actor_base_t::unsubscribe(Handler &&h, address_ptr_t &addr) noexcept {
  412. supervisor->unsubscribe_actor(addr, wrap_handler(*this, std::move(h)));
  413. }
  414. template <typename T>
  415. template <typename... Args>
  416. request_builder_t<T>::request_builder_t(supervisor_t &sup_, actor_base_t &actor_, const address_ptr_t &destination_,
  417. const address_ptr_t &reply_to_, Args &&...args)
  418. : sup{sup_}, actor{actor_}, request_id{sup.next_request_id()}, destination{destination_}, reply_to{reply_to_},
  419. do_install_handler{false} {
  420. auto addr = sup.address_mapping.get_mapped_address(actor_, response_message_t::message_type);
  421. if (addr) {
  422. imaginary_address = addr;
  423. } else {
  424. // subscribe to imaginary address instead of real one because of
  425. // 1. faster dispatching
  426. // 2. need to distinguish between "timeout guarded responses" and "responses to own requests"
  427. imaginary_address = sup.make_address();
  428. do_install_handler = true;
  429. }
  430. req.reset(
  431. new request_message_t{destination, request_id, imaginary_address, reply_to_, std::forward<Args>(args)...});
  432. }
  433. template <typename T> request_id_t request_builder_t<T>::send(const pt::time_duration &timeout_) noexcept {
  434. if (do_install_handler) {
  435. install_handler();
  436. }
  437. auto fn = &request_traits_t<T>::make_error_response;
  438. sup.request_map.emplace(request_id, request_curry_t{fn, reply_to, req, &actor});
  439. sup.put(req);
  440. sup.start_timer(request_id, timeout_, sup, &supervisor_t::on_request_trigger);
  441. actor.active_requests.emplace(request_id);
  442. return request_id;
  443. }
  444. template <typename T> void request_builder_t<T>::install_handler() noexcept {
  445. auto handler = lambda<response_message_t>([supervisor = &sup](response_message_t &msg) {
  446. auto request_id = msg.payload.request_id();
  447. auto &request_map = supervisor->request_map;
  448. auto it = request_map.find(request_id);
  449. // if a response to request has arrived and no timer can be found
  450. // that means that either timeout timer already triggered
  451. // and error-message already delivered or response is not expected.
  452. // just silently drop it anyway
  453. if (it != request_map.end()) {
  454. auto &curry = it->second;
  455. auto &orig_addr = curry.origin;
  456. supervisor->template send<wrapped_res_t>(orig_addr, msg.payload);
  457. supervisor->discard_request(request_id);
  458. // keep order, i.e. deliver response immediately
  459. supervisor->uplift_last_message();
  460. }
  461. });
  462. auto wrapped_handler = wrap_handler(sup, std::move(handler));
  463. auto info = sup.subscribe(wrapped_handler, imaginary_address, &actor, owner_tag_t::SUPERVISOR);
  464. sup.address_mapping.set(actor, info);
  465. }
  466. /** \brief makes an request to the destination address with the message constructed from `args`
  467. *
  468. * The `reply_to` address is defaulted to actor's main address.1
  469. *
  470. */
  471. template <typename Request, typename... Args>
  472. request_builder_t<typename request_wrapper_t<Request>::request_t> actor_base_t::request(const address_ptr_t &dest_addr,
  473. Args &&...args) {
  474. using request_t = typename request_wrapper_t<Request>::request_t;
  475. return supervisor->do_request<request_t>(*this, dest_addr, address, std::forward<Args>(args)...);
  476. }
  477. /** \brief makes an request to the destination address with the message constructed from `args`
  478. *
  479. * The `reply_addr` is used to specify the exact destination address, where reply should be
  480. * delivered.
  481. *
  482. */
  483. template <typename Request, typename... Args>
  484. request_builder_t<typename request_wrapper_t<Request>::request_t>
  485. actor_base_t::request_via(const address_ptr_t &dest_addr, const address_ptr_t &reply_addr, Args &&...args) {
  486. using request_t = typename request_wrapper_t<Request>::request_t;
  487. return supervisor->do_request<request_t>(*this, dest_addr, reply_addr, std::forward<Args>(args)...);
  488. }
  489. template <typename Request> auto actor_base_t::make_response(Request &message, const extended_error_ptr_t &ec) {
  490. using payload_t = typename Request::payload_t::request_t;
  491. using traits_t = request_traits_t<payload_t>;
  492. return traits_t::make_error_response(message.payload.reply_to, message, ec);
  493. }
  494. template <typename Request, typename... Args> auto actor_base_t::make_response(Request &message, Args &&...args) {
  495. using payload_t = typename Request::payload_t::request_t;
  496. using req_traits_t = request_traits_t<payload_t>;
  497. using response_t = typename req_traits_t::response::wrapped_t;
  498. using request_ptr_t = typename req_traits_t::request::message_ptr_t;
  499. return make_message<response_t>(message.payload.reply_to, request_ptr_t{&message}, std::forward<Args>(args)...);
  500. }
  501. template <typename Request, typename... Args> void actor_base_t::reply_to(Request &message, Args &&...args) {
  502. supervisor->put(make_response<Request>(message, std::forward<Args>(args)...));
  503. }
  504. template <typename Request> void actor_base_t::reply_with_error(Request &message, const extended_error_ptr_t &ec) {
  505. supervisor->put(make_response<Request>(message, ec));
  506. }
  507. template <typename Actor>
  508. actor_config_builder_t<Actor>::actor_config_builder_t(install_action_t &&action_, supervisor_t *supervisor_)
  509. : install_action{std::move(action_)}, supervisor{supervisor_},
  510. system_context{*supervisor_->context}, config{supervisor_} {
  511. init_ctor();
  512. }
  513. template <typename Actor> intrusive_ptr_t<Actor> actor_config_builder_t<Actor>::finish() && {
  514. intrusive_ptr_t<Actor> actor_ptr;
  515. if (!validate()) {
  516. auto ec = make_error_code(error_code_t::actor_misconfigured);
  517. system_context.on_error(actor_ptr.get(), make_error(system_context.identity(), ec));
  518. } else {
  519. auto &cfg = static_cast<typename builder_t::config_t &>(config);
  520. auto actor = new Actor(cfg);
  521. actor_ptr.reset(actor);
  522. install_action(actor_ptr);
  523. }
  524. return actor_ptr;
  525. }
  526. } // namespace rotor
  527. #if defined(_MSC_VER)
  528. #pragma warning(pop)
  529. #endif