behavior.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. //
  3. // Copyright (c) 2019 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "address.hpp"
  8. #include <system_error>
  9. #include <unordered_set>
  10. namespace rotor {
  11. struct actor_base_t;
  12. struct supervisor_t;
  13. /** \brief the substate of actor, to be tracked by behavior */
  14. enum class behavior_state_t {
  15. UNKNOWN,
  16. INIT_STARTED,
  17. INIT_ENDED,
  18. SHUTDOWN_STARTED,
  19. SHUTDOWN_CHILDREN_STARTED,
  20. SHUTDOWN_CHILDREN_FINISHED,
  21. UNSUBSCRIPTION_STARTED,
  22. UNSUBSCRIPTION_FINISHED,
  23. SHUTDOWN_ENDED,
  24. };
  25. /** \struct actor_behavior_t
  26. * \brief Actor customization point : default lifetime events and reactions to them
  27. *
  28. * The behavior lifetime is bounded to it's actor lifetime. Actor's state is changed
  29. * from the behavior.
  30. *
  31. */
  32. struct actor_behavior_t {
  33. /** \brief behaviror constructor */
  34. actor_behavior_t(actor_base_t &actor_) : actor{actor_}, substate{behavior_state_t::UNKNOWN} {}
  35. virtual ~actor_behavior_t();
  36. /** \brief sends init-confirmation message (it it was asked) and calls `action_finish_init` */
  37. virtual void action_confirm_init() noexcept;
  38. /** \brief invokes `init_finish` actor's method */
  39. virtual void action_finish_init() noexcept;
  40. /** \brief trigger actor unsubscription */
  41. virtual void action_unsubscribe_self() noexcept;
  42. /** \brief confirms shutdown request (if it was asked) and calls `action_commit_shutdown` */
  43. virtual void action_confirm_shutdown() noexcept;
  44. /** \brief changes actors state to `SHUTTED_DOWN` and calls `action_finish_shutdown` */
  45. virtual void action_commit_shutdown() noexcept;
  46. /** \brief invokes `shutdown_finish` actor's method */
  47. virtual void action_finish_shutdown() noexcept;
  48. /** \brief event, which triggers initialization actions sequence
  49. *
  50. * - action_confirm_init()
  51. * - action_finish_init()
  52. *
  53. */
  54. virtual void on_start_init() noexcept;
  55. /** \brief event, which triggers shutdown actions sequence
  56. *
  57. * - action_unsubscribe_self()
  58. * - (waits unsubscription confirmation)
  59. * - action_confirm_shutdown()
  60. * - action_commit_shutdown()
  61. * - action_finish_shutdown()
  62. */
  63. virtual void on_start_shutdown() noexcept;
  64. /** \brief event, which continues shutdown */
  65. virtual void on_unsubscription() noexcept;
  66. protected:
  67. /** \brief a reference for the led actor */
  68. actor_base_t &actor;
  69. /** \brief internal behavior state for housekeeping */
  70. behavior_state_t substate;
  71. };
  72. /** \struct supervisor_behavior_t
  73. * \brief supervisor specifict events and default actions
  74. */
  75. struct supervisor_behavior_t : public actor_behavior_t {
  76. /** \brief behaviror constructor */
  77. supervisor_behavior_t(supervisor_t &sup);
  78. /** \brief triggers shutdown requests on all supervisor's children actors */
  79. virtual void action_shutdown_children() noexcept;
  80. /** \brief event, which triggers shutdown actions sequence
  81. * - action_shutdown_children()
  82. * - (waits children shutdown confirmation)
  83. * - action_unsubscribe_self()
  84. * - (waits unsubscription confirmation)
  85. * - action_confirm_shutdown()
  86. * - action_commit_shutdown()
  87. * - action_finish_shutdown()
  88. */
  89. virtual void on_start_shutdown() noexcept override;
  90. /** \brief event which occurs, when all children actors are removed, continue shuddown sequence */
  91. virtual void on_childen_removed() noexcept;
  92. /** \brief reaction on child shutdown failure. By default it is treated as fatal
  93. * and forwared to the system context */
  94. virtual void on_shutdown_fail(const address_ptr_t &address, const std::error_code &ec) noexcept;
  95. virtual void on_start_init() noexcept override;
  96. /** \brief supervisor behaviour on child-actor initialization result
  97. *
  98. * - if child-initialization failed and a supervisor is in INITIALIZING phase,
  99. * then the reaction defined by `supervisor_policy`, i.e. either shutdown
  100. * self (and all children) or just failing child;
  101. * - if child successfully initialized, then send start message to it;
  102. * - if there are no more initializing children, then finalize actor initialization
  103. */
  104. virtual void on_init(const address_ptr_t &address, const std::error_code &ec) noexcept;
  105. /** \brief records child-actor address if it was created during supervsisor initialization */
  106. virtual void on_create_child(const address_ptr_t &address) noexcept;
  107. /** \brief type for keeping list of initializing actors (during supervisor inititalization) */
  108. using initializing_actors_t = std::unordered_set<address_ptr_t>;
  109. /** \brief list of initializing actors (during supervisor inititalization) */
  110. initializing_actors_t initializing_actors;
  111. };
  112. } // namespace rotor