one-shot-lambda-subscriber.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.hpp"
  7. #include "rotor/asio.hpp"
  8. #include <boost/asio.hpp>
  9. #include <boost/lexical_cast.hpp>
  10. #include <chrono>
  11. #include <functional>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include <memory>
  15. #include <type_traits>
  16. #include <utility>
  17. #include <vector>
  18. namespace asio = boost::asio;
  19. namespace pt = boost::posix_time;
  20. namespace ra = rotor::asio;
  21. namespace payload {
  22. struct pong_t {};
  23. struct ping_t {
  24. using response_t = pong_t;
  25. };
  26. } // namespace payload
  27. namespace message {
  28. using ping_t = rotor::request_traits_t<payload::ping_t>::request::message_t;
  29. using pong_t = rotor::request_traits_t<payload::ping_t>::response::message_t;
  30. } // namespace message
  31. struct pinger_t : public rotor::actor_base_t {
  32. using timepoint_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
  33. using rotor::actor_base_t::actor_base_t;
  34. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  35. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  36. rotor::actor_base_t::configure(plugin);
  37. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  38. }
  39. void on_start() noexcept override {
  40. rotor::actor_base_t::on_start();
  41. std::cout << "ping (1)\n";
  42. auto timeout = rotor::pt::milliseconds{1};
  43. request<payload::ping_t>(ponger_addr).send(timeout);
  44. }
  45. void on_pong(message::pong_t &msg) noexcept {
  46. auto &ec = msg.payload.ec;
  47. if (ec) {
  48. std::cout << "pong error: " << ec.message() << "\n";
  49. supervisor->do_shutdown();
  50. } else {
  51. std::cout << "ping (2)\n";
  52. auto timeout = rotor::pt::milliseconds{1};
  53. request<payload::ping_t>(ponger_addr).send(timeout);
  54. }
  55. }
  56. rotor::address_ptr_t ponger_addr;
  57. };
  58. struct ponger_t : public rotor::actor_base_t {
  59. rotor::handler_ptr_t pong_handler;
  60. using rotor::actor_base_t::actor_base_t;
  61. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  62. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  63. rotor::actor_base_t::configure(plugin);
  64. plugin.with_casted<rotor::plugin::starter_plugin_t>([&](auto &p) {
  65. auto lambda = rotor::lambda<message::ping_t>([&](auto &msg) {
  66. std::cout << "pong\n";
  67. unsubscribe(pong_handler);
  68. reply_to(msg);
  69. });
  70. pong_handler = p.subscribe_actor(std::move(lambda));
  71. });
  72. }
  73. void shutdown_finish() noexcept override {
  74. rotor::actor_base_t::shutdown_finish();
  75. pong_handler.reset(); // otherwise it will be memory leak
  76. }
  77. private:
  78. rotor::address_ptr_t pinger_addr;
  79. };
  80. int main() {
  81. asio::io_context io_context{1};
  82. try {
  83. auto system_context = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_context)};
  84. auto strand = std::make_shared<asio::io_context::strand>(io_context);
  85. auto timeout = boost::posix_time::milliseconds{10};
  86. auto supervisor =
  87. system_context->create_supervisor<ra::supervisor_asio_t>().strand(strand).timeout(timeout).finish();
  88. auto pinger = supervisor->create_actor<pinger_t>().timeout(timeout).finish();
  89. auto ponger = supervisor->create_actor<ponger_t>().timeout(timeout).finish();
  90. pinger->set_ponger_addr(ponger->get_address());
  91. ponger->set_pinger_addr(pinger->get_address());
  92. supervisor->start();
  93. io_context.run();
  94. } catch (const std::exception &ex) {
  95. std::cout << "exception : " << ex.what();
  96. }
  97. std::cout << "exiting...\n";
  98. return 0;
  99. }