supervisor.h 24 KB

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