supervisor.h 25 KB

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