messages.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "address.hpp"
  8. #include "message.h"
  9. #include "state.h"
  10. #include "request.hpp"
  11. #include "subscription_point.h"
  12. #include "forward.hpp"
  13. #include "extended_error.h"
  14. namespace rotor {
  15. /// namespace for rotor core payloads
  16. namespace payload {
  17. /** \struct initialize_confirmation_t
  18. * \brief Message with this payload is sent from an actor to its supervisor to
  19. * confirm successful initialization
  20. */
  21. struct initialize_confirmation_t {};
  22. /** \struct initialize_actor_t
  23. * \brief Message with this payload is sent from a supervisor to an actor as
  24. * initialization request
  25. */
  26. struct initialize_actor_t {
  27. /** \brief link to response payload type */
  28. using response_t = initialize_confirmation_t;
  29. };
  30. /** \struct start_actor_t
  31. * \brief Message with this payload is sent from a supervisor to an actor as
  32. * start confirmation
  33. */
  34. struct start_actor_t {};
  35. /** \struct create_actor_t
  36. * \brief Message with this payload is sent to supervisor when an actor is
  37. * created (constructed).
  38. *
  39. * The message is needed for internal {@link supervisor_t} housekeeping.
  40. *
  41. */
  42. struct create_actor_t {
  43. /** \brief the intrusive pointer to created actor */
  44. actor_ptr_t actor;
  45. /** \brief maximum time for actor initialization
  46. *
  47. * If an actor isn't able to confirm initialization in time, it
  48. * will be asked to shutdown (default behavior)
  49. *
  50. */
  51. pt::time_duration timeout;
  52. };
  53. /** \struct shutdown_trigger_t
  54. * \brief Message with this payload is sent to ask an actor's supervisor
  55. * to initate shutdown procedure.
  56. *
  57. */
  58. struct shutdown_trigger_t {
  59. /** \brief the actor to be shut down */
  60. address_ptr_t actor_address;
  61. /** \brief shutdown reason */
  62. extended_error_ptr_t reason;
  63. /** \brief constructs shutdown trigger for the actor, defined with address, and the shutdown reason */
  64. template <typename Address, typename Reason>
  65. shutdown_trigger_t(Address &&address_, Reason &&reason_) noexcept
  66. : actor_address(std::forward<Address>(address_)), reason(std::forward<Reason>(reason_)) {}
  67. };
  68. /** \struct shutdown_confirmation_t
  69. * \brief Message with this payload is sent from an actor to its supervisor to
  70. * confirm successful shutdown.
  71. */
  72. struct shutdown_confirmation_t {};
  73. /** \struct shutdown_request_t
  74. * \brief Message with this payload is sent from a supervisor to an actor as
  75. * shutdown request
  76. */
  77. struct shutdown_request_t {
  78. /** \brief link to response payload type */
  79. using response_t = shutdown_confirmation_t;
  80. /** \brief constructs shutdown request with shutdown reason */
  81. template <typename Reason> shutdown_request_t(Reason &&reason_) noexcept : reason(std::forward<Reason>(reason_)) {}
  82. /** \brief shutdown reason */
  83. extended_error_ptr_t reason;
  84. };
  85. /** \struct handler_call_t
  86. * \brief Message with this payload is forwarded to the handler's supervisor for
  87. * the delivery of the original message.
  88. *
  89. * An `address` in `rotor` is always generated by a supervisor. All messages to the
  90. * address are initially pre-processed by the supervisor: if the destination handler
  91. * supervisor is the same as the message address supervisor, the handler is invoked
  92. * immediately. Otherwise, if a handler belongs to different supervisor (i.e. may
  93. * be to different event loop), then the delivery of the message is forwarded to
  94. * that supersior.
  95. *
  96. */
  97. struct handler_call_t {
  98. /** \brief The original message (intrusive pointer) sent to an address */
  99. message_ptr_t orig_message;
  100. /** \brief The handler (intrusive pointer) on some external supervisor,
  101. * which can process the original message */
  102. handler_ptr_t handler;
  103. };
  104. /** \struct external_subscription_t
  105. * \brief Message with this payload is forwarded to the target address supervisor
  106. * for recording subscription in the external (foreign) handler
  107. *
  108. * When a supervisor process subscription requests from it's (local) actors, it
  109. * can found that the `target_address` belongs to some other (external/foreign)
  110. * supervisor. In that case the subscription is forwarded to the external
  111. * supervisor.
  112. *
  113. */
  114. struct external_subscription_t {
  115. /** \brief subscription details */
  116. subscription_point_t point;
  117. };
  118. /** \struct subscription_confirmation_t
  119. * \brief Message with this payload is sent from a supervisor to an actor when
  120. * successfull subscription to the `target` address occurs.
  121. *
  122. * The message is needed for internal {@link actor_base_t} housekeeping.
  123. *
  124. */
  125. struct subscription_confirmation_t {
  126. /** \brief subscription details */
  127. subscription_point_t point;
  128. };
  129. /** \struct external_unsubscription_t
  130. * \brief Message with this payload is forwarded to the target address supervisor
  131. * for recording unsubscription in the external (foreign) handler.
  132. *
  133. * The message is symmetrical to the {@link external_subscription_t}.
  134. *
  135. */
  136. struct external_unsubscription_t {
  137. /** \brief subscription details */
  138. subscription_point_t point;
  139. };
  140. /** \struct commit_unsubscription_t
  141. * \brief Message with this payload is sent to the target address supervisor
  142. * for confirming unsubscription in the external (foreign) handler.
  143. *
  144. * The message is an actor-reply to {@link external_subscription_t} request.
  145. *
  146. */
  147. struct commit_unsubscription_t {
  148. /** \brief subscription details */
  149. subscription_point_t point;
  150. };
  151. /** \struct unsubscription_confirmation_t
  152. * \brief Message with this payload is sent from a supervisor to an actor with
  153. * confirmation that `pooint` is no longer active (subscribed).`
  154. */
  155. struct unsubscription_confirmation_t {
  156. /** \brief subscription details */
  157. subscription_point_t point;
  158. };
  159. /** \struct registration_response_t
  160. * \brief Successful registraction response (no content)
  161. */
  162. struct registration_response_t {};
  163. /** \struct registration_request_t
  164. * \brief "name - >service address mapping" request
  165. */
  166. struct registration_request_t {
  167. /** \brief link to registration response payload type */
  168. using response_t = registration_response_t;
  169. /** \brief (unique) name of the sevice address in the registry */
  170. std::string service_name;
  171. /** \brief actual service address */
  172. address_ptr_t service_addr;
  173. };
  174. /** \struct deregistration_notify_t
  175. * \brief deregistration notification for all names associated
  176. * with service address
  177. */
  178. struct deregistration_notify_t {
  179. /** \brief service address for deregistration */
  180. address_ptr_t service_addr;
  181. };
  182. /** \struct deregistration_service_t
  183. * \brief removes single service by name from a registry
  184. */
  185. struct deregistration_service_t {
  186. /** \brief the name of the sevice address to be removed for a registry */
  187. std::string service_name;
  188. };
  189. /** \struct discovery_reply_t
  190. * \brief successful result of service discovery
  191. */
  192. struct discovery_reply_t {
  193. /** \brief the service address found by name in a registry */
  194. address_ptr_t service_addr;
  195. };
  196. /** \struct discovery_request_t
  197. * \brief discover service by name in a registry
  198. */
  199. struct discovery_request_t {
  200. /** \brief link to discovery response payload type */
  201. using response_t = discovery_reply_t;
  202. /** \brief the service name to be looked in a registry */
  203. std::string service_name;
  204. };
  205. /** \struct discovery_future_t
  206. * \brief delayed discovery response as soon as an address has been registered
  207. */
  208. struct discovery_future_t {
  209. /** \brief the service address found by name in a registry */
  210. address_ptr_t service_addr;
  211. };
  212. /** \struct discovery_promise_t
  213. * \brief ask registry for {@link discovery_future_t} when the target
  214. * service name has been registered
  215. */
  216. struct discovery_promise_t {
  217. /** \brief link to discovery future payload type */
  218. using response_t = discovery_future_t;
  219. /** \brief the service name to be looked in a registry */
  220. std::string service_name;
  221. };
  222. /** \struct link_response_t
  223. * \brief successful confirmation to {@link link_request_t}
  224. */
  225. struct link_response_t {};
  226. /** \struct link_request_t
  227. * \brief requests target actor to be linked with the current one
  228. */
  229. struct link_request_t {
  230. /** \brief link to link response payload type */
  231. using response_t = link_response_t;
  232. /** \brief wait until target server (actor) starts, only then reply to the source actor */
  233. bool operational_only;
  234. };
  235. /** \struct unlink_notify_t
  236. * \brief "client" notifies "server" that the connection has been closed
  237. * from its side
  238. */
  239. struct unlink_notify_t {
  240. /** \brief client actor address in unlinking */
  241. address_ptr_t client_addr;
  242. };
  243. /** \struct unlink_request_t
  244. * \brief "server" asks "client" for closing connection
  245. */
  246. struct unlink_request_t {
  247. /** \brief link to unlink response payload type */
  248. using response_t = unlink_notify_t;
  249. /** \brief server actor address in unlinking */
  250. address_ptr_t server_addr;
  251. };
  252. } // namespace payload
  253. /// namespace for rotor core messages (which just transform payloads)
  254. namespace message {
  255. // subscription-related
  256. /** \brief unsubscription confirmation message */
  257. using unsubscription_t = message_t<payload::unsubscription_confirmation_t>;
  258. /** \brief external unsubscription message */
  259. using unsubscription_external_t = message_t<payload::external_unsubscription_t>;
  260. /** \brief subscription confirmation message */
  261. using subscription_t = message_t<payload::subscription_confirmation_t>;
  262. /** \brief external subscription message */
  263. using external_subscription_t = message_t<payload::external_subscription_t>;
  264. /** \brief unsubscription commit message */
  265. using commit_unsubscription_t = message_t<payload::commit_unsubscription_t>;
  266. /** \brief delivers foreign message to the actor's supervisor
  267. *
  268. * Unpon delivery the appropriate handler on the actor will be thread-safely
  269. * called by it's supervisor
  270. */
  271. using handler_call_t = message_t<payload::handler_call_t>;
  272. // lifetime-related
  273. /** \brief actor initialization request */
  274. using init_request_t = request_traits_t<payload::initialize_actor_t>::request::message_t;
  275. /** \brief actor initialization response */
  276. using init_response_t = request_traits_t<payload::initialize_actor_t>::response::message_t;
  277. /** \brief actor start trigger */
  278. using start_trigger_t = message_t<payload::start_actor_t>;
  279. /** \brief actor shutdown trigger */
  280. using shutdown_trigger_t = message_t<payload::shutdown_trigger_t>;
  281. /** \brief actor shutdown request */
  282. using shutdown_request_t = request_traits_t<payload::shutdown_request_t>::request::message_t;
  283. /** \brief actor shutdown response */
  284. using shutdown_response_t = request_traits_t<payload::shutdown_request_t>::response::message_t;
  285. /** \brief supervisor's message upon actor instantiation */
  286. using create_actor_t = message_t<payload::create_actor_t>;
  287. // registry-related
  288. /** \brief name/address registration request */
  289. using registration_request_t = request_traits_t<payload::registration_request_t>::request::message_t;
  290. /** \brief name/address registration response */
  291. using registration_response_t = request_traits_t<payload::registration_request_t>::response::message_t;
  292. /** \brief deregistration notification (from client) */
  293. using deregistration_notify_t = message_t<payload::deregistration_notify_t>;
  294. /** \brief deregistration notification (from registry-server) */
  295. using deregistration_service_t = message_t<payload::deregistration_service_t>;
  296. /** \brief name discovery request */
  297. using discovery_request_t = request_traits_t<payload::discovery_request_t>::request::message_t;
  298. /** \brief name discovery response */
  299. using discovery_response_t = request_traits_t<payload::discovery_request_t>::response::message_t;
  300. /** \brief name discovery promise (aka get response when name will be available) */
  301. using discovery_promise_t = request_traits_t<payload::discovery_promise_t>::request::message_t;
  302. /** \brief name discovery future (reply to promise) */
  303. using discovery_future_t = request_traits_t<payload::discovery_promise_t>::response::message_t;
  304. /** \brief name discovery promise cancellation */
  305. using discovery_cancel_t = request_traits_t<payload::discovery_promise_t>::cancel::message_t;
  306. // link-related
  307. /** \brief actor link request */
  308. using link_request_t = request_traits_t<payload::link_request_t>::request::message_t;
  309. /** \brief actor link response */
  310. using link_response_t = request_traits_t<payload::link_request_t>::response::message_t;
  311. /** \brief unlink notification (client is no longer interested in the link) */
  312. using unlink_notify_t = message_t<payload::unlink_notify_t>;
  313. /** \brief unlink request (server is asking client to cancel link) */
  314. using unlink_request_t = request_traits_t<payload::unlink_request_t>::request::message_t;
  315. /** \brief unlink response (client confirms link cancellation) */
  316. using unlink_response_t = request_traits_t<payload::unlink_request_t>::response::message_t;
  317. } // namespace message
  318. } // namespace rotor