messages.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "message.h"
  9. #include "state.h"
  10. #include "request.hpp"
  11. namespace rotor {
  12. struct handler_base_t;
  13. using actor_ptr_t = intrusive_ptr_t<actor_base_t>;
  14. using handler_ptr_t = intrusive_ptr_t<handler_base_t>;
  15. namespace payload {
  16. using callback_t = std::function<void()>;
  17. using callback_ptr_t = std::shared_ptr<callback_t>;
  18. /** \struct initialize_confirmation_t
  19. * \brief Message with this payload is sent from an actor to its supervisor to
  20. * confirm successful initialization
  21. */
  22. struct initialize_confirmation_t {};
  23. /** \struct initialize_actor_t
  24. * \brief Message with this payload is sent from a supervisor to an actor as
  25. * initialization request
  26. */
  27. struct initialize_actor_t {
  28. /** \brief link to response payload type */
  29. using response_t = initialize_confirmation_t;
  30. /** \brief target actor address, which is asked for initialization
  31. *
  32. * The `actor_address` might be useful for observing the actor initialization
  33. * in some other actor
  34. */
  35. address_ptr_t actor_address;
  36. };
  37. /** \struct start_actor_t
  38. * \brief Message with this payload is sent from a supervisor to an actor as
  39. * start confirmation
  40. */
  41. struct start_actor_t {
  42. /** \brief target actor address, which is asked for start
  43. *
  44. * The `actor_address` might be useful for observing the actor start
  45. * in some other actor
  46. */
  47. address_ptr_t actor_address;
  48. };
  49. /** \struct create_actor_t
  50. * \brief Message with this payload is sent to supervisor when an actor is
  51. * created (constructed).
  52. *
  53. * The message is needed for internal {@link supervisor_t} housekeeping.
  54. *
  55. */
  56. struct create_actor_t {
  57. /** \brief the intrusive pointer to created actor */
  58. actor_ptr_t actor;
  59. /** \brief maximum time for actor initialization
  60. *
  61. * If an actor isn't able to confirm initialization in time, it
  62. * will be asked to shutdown (default behavior)
  63. *
  64. */
  65. pt::time_duration timeout;
  66. };
  67. /** \struct shutdown_trigger_t
  68. * \brief Message with this payload is sent to ask an actor's supervisor
  69. * to initate shutdown procedure.
  70. *
  71. */
  72. struct shutdown_trigger_t {
  73. /** \brief the actor to be shutted down */
  74. address_ptr_t actor_address;
  75. };
  76. /** \struct shutdown_confirmation_t
  77. * \brief Message with this payload is sent from an actor to its supervisor to
  78. * confirm successful shutdown.
  79. */
  80. struct shutdown_confirmation_t {};
  81. /** \struct shutdown_request_t
  82. * \brief Message with this payload is sent from a supervisor to an actor as
  83. * shutdown request
  84. */
  85. struct shutdown_request_t {
  86. /** \brief link to response payload type */
  87. using response_t = shutdown_confirmation_t;
  88. /** \brief source actor address, which has been shutted down
  89. *
  90. * The `actor_address` might be useful for observing the actor shutting down
  91. * in some other actor
  92. */
  93. address_ptr_t actor_address;
  94. };
  95. /** \struct handler_call_t
  96. * \brief Message with this payload is forwarded to the handler's supervisor for
  97. * the delivery of the original message.
  98. *
  99. * An `address` in `rotor` is always generated by a supervisor. All messages to the
  100. * address are initially pre-processed by the supervisor: if the destination handler
  101. * supervisor is the same as the message address supervisor, the handler is invoked
  102. * immediately. Otherwise, if a handler belongs to different supervisor (i.e. may
  103. * be to different event loop), then the delivery of the message is forwarded to
  104. * that supersior.
  105. *
  106. */
  107. struct handler_call_t {
  108. /** \brief The original message (intrusive pointer) sent to an address */
  109. message_ptr_t orig_message;
  110. /** \brief The handler (intrusive pointer) on some external supervisor,
  111. * which can process the original message */
  112. handler_ptr_t handler;
  113. };
  114. /** \struct external_subscription_t
  115. * \brief Message with this payload is forwarded to the target address supervisor
  116. * for recording subscription in the external (foreign) handler
  117. *
  118. * When a supervisor process subscription requests from it's (local) actors, it
  119. * can found that the `target_address` belongs to some other (external/foreign)
  120. * supervisor. In that case the subscription is forwarded to the external
  121. * supervisor.
  122. *
  123. */
  124. struct external_subscription_t {
  125. /** \brief The target address for subscription */
  126. address_ptr_t target_address;
  127. /** \brief The handler (intrusive pointer) for processing message */
  128. handler_ptr_t handler;
  129. };
  130. /** \struct subscription_confirmation_t
  131. * \brief Message with this payload is sent from a supervisor to an actor when
  132. * successfull subscription to the `target` address occurs.
  133. *
  134. * The message is needed for internal {@link actor_base_t} housekeeping.
  135. *
  136. */
  137. struct subscription_confirmation_t {
  138. /** \brief The target address for subscription */
  139. address_ptr_t target_address;
  140. /** \brief The handler (intrusive pointer) for processing message */
  141. handler_ptr_t handler;
  142. };
  143. /** \struct external_unsubscription_t
  144. * \brief Message with this payload is forwarded to the target address supervisor
  145. * for recording unsubscription in the external (foreign) handler.
  146. *
  147. * The message is symmetrical to the {@link external_subscription_t}.
  148. *
  149. */
  150. struct external_unsubscription_t {
  151. /** \brief The target address for unsubscription */
  152. address_ptr_t target_address;
  153. /** \brief The handler (intrusive pointer) for processing message */
  154. handler_ptr_t handler;
  155. };
  156. /** \struct commit_unsubscription_t
  157. * \brief Message with this payload is sent to the target address supervisor
  158. * for confirming unsubscription in the external (foreign) handler.
  159. *
  160. * The message is an actor-reply to {@link external_subscription_t} request.
  161. *
  162. */
  163. struct commit_unsubscription_t {
  164. /** \brief The target address for unsubscription */
  165. address_ptr_t target_address;
  166. /** \brief The handler (intrusive pointer) for processing message */
  167. handler_ptr_t handler;
  168. };
  169. /** \struct unsubscription_confirmation_t
  170. * \brief Message with this payload is sent from a supervisor to an actor with
  171. * confirmation that `handler` is no longer subscribed to `target_address`
  172. */
  173. struct unsubscription_confirmation_t {
  174. /** \brief The target address for unsubscription */
  175. address_ptr_t target_address;
  176. /** \brief The handler (intrusive pointer) for processing message */
  177. handler_ptr_t handler;
  178. /** \brief the optional callback to be invoked once message is locally
  179. * delivered, i.e. when it is destroyed.
  180. */
  181. callback_ptr_t callback;
  182. ~unsubscription_confirmation_t() {
  183. if (callback) {
  184. (*callback)();
  185. }
  186. }
  187. };
  188. /** \struct state_response_t
  189. * \brief Message with this payload is sent to an actor, which
  190. * asked for the state of the subject actor (represented by it's address)
  191. *
  192. */
  193. struct state_response_t {
  194. /** \brief The state of the asked actor */
  195. state_t state;
  196. };
  197. /** \struct state_request_t
  198. * \brief Message with this payload is sent to supervisor to query
  199. * actor (defined by it's address - `subject_addr`).
  200. */
  201. struct state_request_t {
  202. /** \brief link to response payload type */
  203. using response_t = state_response_t;
  204. /** \brief The actor address in question */
  205. address_ptr_t subject_addr;
  206. };
  207. /** \struct registration_response_t
  208. * \brief Successful registraction response (no content)
  209. */
  210. struct registration_response_t {};
  211. /** \struct registration_request_t
  212. * \brief "name - >service address mapping" request
  213. */
  214. struct registration_request_t {
  215. /** \brief link to registration response payload type */
  216. using response_t = registration_response_t;
  217. /** \brief (unique) name of the sevice address in the registry */
  218. std::string service_name;
  219. /** \brief actual service address */
  220. address_ptr_t service_addr;
  221. };
  222. /** \struct deregistration_notify_t
  223. * \brief deregistration notification for all names associated
  224. * with service address
  225. */
  226. struct deregistration_notify_t {
  227. /** \brief service address for deregistration */
  228. address_ptr_t service_addr;
  229. };
  230. /** \struct deregistration_service_t
  231. * \brief removes single service by name from a registry
  232. */
  233. struct deregistration_service_t {
  234. /** \brief the name of the sevice address to be remoed for a registry */
  235. std::string service_name;
  236. };
  237. /** \struct discovery_reply_t
  238. * \brief successful result of service discovery
  239. */
  240. struct discovery_reply_t {
  241. /** \brief the service address found by name in a registry */
  242. address_ptr_t service_addr;
  243. };
  244. /** \struct discovery_request_t
  245. * \brief discover service by name in a registry
  246. */
  247. struct discovery_request_t {
  248. /** \brief link to discovery response payload type */
  249. using response_t = discovery_reply_t;
  250. /** \brief the service name to be looked in a registry */
  251. std::string service_name;
  252. };
  253. } // namespace payload
  254. namespace message {
  255. using init_request_t = request_traits_t<payload::initialize_actor_t>::request::message_t;
  256. using init_response_t = request_traits_t<payload::initialize_actor_t>::response::message_t;
  257. using start_trigger_t = message_t<payload::start_actor_t>;
  258. using shutdown_trigger_t = message_t<payload::shutdown_trigger_t>;
  259. using shutdown_request_t = request_traits_t<payload::shutdown_request_t>::request::message_t;
  260. using shutdown_response_t = request_traits_t<payload::shutdown_request_t>::response::message_t;
  261. using state_request_t = request_traits_t<payload::state_request_t>::request::message_t;
  262. using state_response_t = request_traits_t<payload::state_request_t>::response::message_t;
  263. using registration_request_t = request_traits_t<payload::registration_request_t>::request::message_t;
  264. using registration_response_t = request_traits_t<payload::registration_request_t>::response::message_t;
  265. using deregistration_notify_t = message_t<payload::deregistration_notify_t>;
  266. using deregistration_service_t = message_t<payload::deregistration_service_t>;
  267. using discovery_request_t = request_traits_t<payload::discovery_request_t>::request::message_t;
  268. using discovery_response_t = request_traits_t<payload::discovery_request_t>::response::message_t;
  269. } // namespace message
  270. } // namespace rotor