pong-registry.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2019-2020 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include <rotor/ev.hpp>
  7. #include <rotor/registry.h>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <chrono>
  11. #include <cstdlib>
  12. /* NB: there is some race-condition, as the ponger might be register self in the registry
  13. * later then pinger asks for ponger address; then pinger receives discovery falilure
  14. * reply and shuts self down.
  15. *
  16. * The correct behavior will be introduced later in rotor with erlang-like actor restart
  17. * policy, i.e.: pinger should be downed, and it's supervisor should restart if (may be
  18. * a few times, may be with some delay in between).
  19. */
  20. namespace payload {
  21. struct pong_t {};
  22. struct ping_t {
  23. using response_t = pong_t;
  24. };
  25. } // namespace payload
  26. namespace message {
  27. using ping_t = rotor::request_traits_t<payload::ping_t>::request::message_t;
  28. using pong_t = rotor::request_traits_t<payload::ping_t>::response::message_t;
  29. } // namespace message
  30. static const char ponger_name[] = "service:ponger";
  31. static const auto timeout = boost::posix_time::milliseconds{5};
  32. struct pinger_t : public rotor::actor_base_t {
  33. using rotor::actor_base_t::actor_base_t;
  34. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  35. rotor::actor_base_t::configure(plugin);
  36. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  37. plugin.with_casted<rotor::plugin::registry_plugin_t>([&](auto &p) {
  38. p.discover_name(ponger_name, ponger_addr).link(true).callback([](auto phase, auto &ec) {
  39. auto p = (phase == rotor::plugin::registry_plugin_t::phase_t::linking) ? "link" : "discovery";
  40. std::cout << "executing " << p << " in accordance with ponger : " << (!ec ? "yes" : "no") << "\n";
  41. });
  42. });
  43. }
  44. void on_start() noexcept override {
  45. rotor::actor_base_t::on_start();
  46. std::cout << "lets send ping\n";
  47. request<payload::ping_t>(ponger_addr).send(timeout);
  48. }
  49. void on_pong(message::pong_t &) noexcept {
  50. std::cout << "pong received, going to shutdown\n";
  51. supervisor->do_shutdown();
  52. }
  53. std::uint32_t attempts = 3;
  54. rotor::address_ptr_t ponger_addr;
  55. };
  56. struct ponger_t : public rotor::actor_base_t {
  57. using rotor::actor_base_t::actor_base_t;
  58. void set_registry_addr(const rotor::address_ptr_t &addr) { registry_addr = addr; }
  59. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  60. rotor::actor_base_t::configure(plugin);
  61. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  62. plugin.with_casted<rotor::plugin::registry_plugin_t>([&](auto &p) { p.register_name(ponger_name, address); });
  63. }
  64. void on_ping(message::ping_t &req) noexcept {
  65. std::cout << "ponger recevied ping request\n";
  66. reply_to(req);
  67. }
  68. rotor::address_ptr_t registry_addr;
  69. };
  70. int main() {
  71. try {
  72. auto *loop = ev_loop_new(0);
  73. auto system_context = rotor::ev::system_context_ptr_t{new rotor::ev::system_context_ev_t()};
  74. auto timeout = boost::posix_time::milliseconds{10};
  75. auto sup = system_context->create_supervisor<rotor::ev::supervisor_ev_t>()
  76. .loop(loop)
  77. .loop_ownership(true) /* let supervisor takes ownership on the loop */
  78. .timeout(timeout)
  79. .create_registry(true)
  80. .finish();
  81. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  82. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  83. sup->start();
  84. ev_run(loop);
  85. } catch (const std::exception &ex) {
  86. std::cout << "exception : " << ex.what();
  87. }
  88. std::cout << "exiting...\n";
  89. return 0;
  90. }
  91. /*
  92. sample output:
  93. discovered & linked with ponger : yes
  94. lets send ping
  95. ponger recevied ping request
  96. pong received, going to shutdown
  97. exiting...
  98. */