messages.hpp 13 KB

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