supervisor.h 25 KB

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