messages.hpp 13 KB

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