ping_pong-2-theads.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2019 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. #include <thread>
  19. namespace asio = boost::asio;
  20. namespace pt = boost::posix_time;
  21. namespace ra = rotor::asio;
  22. namespace r = rotor;
  23. struct ping_t {};
  24. struct pong_t {};
  25. struct pinger_t : public r::actor_base_t {
  26. using timepoint_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
  27. std::size_t pings_left;
  28. std::size_t pings_count;
  29. std::uint32_t request_attempts;
  30. timepoint_t start;
  31. pinger_t(r::supervisor_t &sup, std::size_t pings) : r::actor_base_t{sup}, pings_left{pings}, pings_count{pings} {
  32. request_attempts = 0;
  33. }
  34. void set_ponger_addr(const r::address_ptr_t &addr) { ponger_addr = addr; }
  35. void init_start() noexcept override {
  36. subscribe(&pinger_t::on_pong);
  37. subscribe(&pinger_t::on_ponger_start, ponger_addr);
  38. subscribe(&pinger_t::on_state);
  39. request_ponger_status();
  40. std::cout << "pinger::init_start()\n";
  41. }
  42. void inline request_ponger_status() noexcept {
  43. request<r::payload::state_request_t>(ponger_addr->supervisor.get_address(), ponger_addr)
  44. .send(r::pt::millisec{1});
  45. }
  46. void on_start(r::message_t<r::payload::start_actor_t> &msg) noexcept override {
  47. std::cout << "pinger::on_start\n";
  48. r::actor_base_t::on_start(msg);
  49. unsubscribe(&pinger_t::on_ponger_start, ponger_addr);
  50. unsubscribe(&pinger_t::on_state);
  51. start = std::chrono::high_resolution_clock::now();
  52. do_send_ping();
  53. }
  54. void on_pong(r::message_t<pong_t> &) noexcept { do_send_ping(); }
  55. void on_ponger_start(r::message_t<r::payload::start_actor_t> &) noexcept {
  56. std::cout << "pinger::on_ponger_start\n";
  57. if (state == r::state_t::INITIALIZING) {
  58. r::actor_base_t::init_start();
  59. }
  60. // we already get the right
  61. }
  62. void on_state(r::message::state_response_t &msg) noexcept {
  63. // IRL we should check for errors in msg.payload.ec and react appropriately
  64. auto &target_state = msg.payload.res.state;
  65. std::cout << "pinger::on_state " << static_cast<int>(target_state) << "\n";
  66. if (state == r::state_t::INITIALIZED) {
  67. return; // we are already on_ponger_start
  68. }
  69. if (target_state == r::state_t::OPERATIONAL) {
  70. r::actor_base_t::init_start();
  71. } else {
  72. if (request_attempts > 3) {
  73. // do_shutdown();
  74. } else {
  75. request_ponger_status();
  76. }
  77. }
  78. }
  79. void do_send_ping() {
  80. // std::cout << "pinger::do_send_ping, left: " << pings_left << "\n";
  81. if (pings_left) {
  82. send<ping_t>(ponger_addr);
  83. --pings_left;
  84. } else {
  85. using namespace std::chrono;
  86. auto end = high_resolution_clock::now();
  87. std::chrono::duration<double> diff = end - start;
  88. double freq = ((double)pings_count) / diff.count();
  89. std::cout << "pings finishes (" << pings_left << ") in " << diff.count() << "s"
  90. << ", freq = " << std::fixed << std::setprecision(10) << freq << ", real freq = " << std::fixed
  91. << std::setprecision(10) << freq * 2 << "\n";
  92. supervisor.shutdown();
  93. ponger_addr->supervisor.shutdown();
  94. }
  95. }
  96. r::address_ptr_t ponger_addr;
  97. };
  98. struct ponger_t : public r::actor_base_t {
  99. ponger_t(r::supervisor_t &sup) : r::actor_base_t{sup} {}
  100. void set_pinger_addr(const r::address_ptr_t &addr) { pinger_addr = addr; }
  101. void init_start() noexcept override {
  102. std::cout << "ponger::init_start\n";
  103. subscribe(&ponger_t::on_ping);
  104. r::actor_base_t::init_start();
  105. }
  106. void on_start(r::message_t<r::payload::start_actor_t> &msg) noexcept override {
  107. std::cout << "ponger::on_start\n";
  108. r::actor_base_t::on_start(msg);
  109. }
  110. void on_ping(r::message_t<ping_t> &) noexcept {
  111. // std::cout << "ponger::on_ping\n";
  112. send<pong_t>(pinger_addr);
  113. }
  114. private:
  115. r::address_ptr_t pinger_addr;
  116. };
  117. struct holding_supervisor_t : public ra::supervisor_asio_t {
  118. using guard_t = asio::executor_work_guard<asio::io_context::executor_type>;
  119. holding_supervisor_t(ra::supervisor_asio_t *sup, const ra::supervisor_config_asio_t &cfg)
  120. : ra::supervisor_asio_t{sup, cfg}, guard{asio::make_work_guard(cfg.strand->context())} {}
  121. guard_t guard;
  122. void shutdown_finish() noexcept override {
  123. ra::supervisor_asio_t::shutdown_finish();
  124. guard.reset();
  125. std::cout << "holding_supervisor_t::shutdown_finish\n";
  126. }
  127. };
  128. int main(int argc, char **argv) {
  129. asio::io_context io_ctx1;
  130. asio::io_context io_ctx2;
  131. try {
  132. std::uint32_t count = 10000;
  133. if (argc > 1) {
  134. boost::conversion::try_lexical_convert(argv[1], count);
  135. }
  136. auto sys_ctx1 = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_ctx1)};
  137. auto sys_ctx2 = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_ctx2)};
  138. auto stand1 = std::make_shared<asio::io_context::strand>(io_ctx1);
  139. auto stand2 = std::make_shared<asio::io_context::strand>(io_ctx2);
  140. auto timeout = boost::posix_time::milliseconds{10};
  141. ra::supervisor_config_asio_t conf1{timeout, std::move(stand1)};
  142. ra::supervisor_config_asio_t conf2{timeout, std::move(stand2)};
  143. auto sup1 = sys_ctx1->create_supervisor<holding_supervisor_t>(conf1);
  144. auto sup2 = sys_ctx2->create_supervisor<holding_supervisor_t>(conf2);
  145. auto pinger = sup1->create_actor<pinger_t>(timeout, count);
  146. auto ponger = sup2->create_actor<ponger_t>(timeout);
  147. pinger->set_ponger_addr(ponger->get_address());
  148. ponger->set_pinger_addr(pinger->get_address());
  149. sup1->start();
  150. sup2->start();
  151. auto t1 = std::thread([&] { io_ctx1.run(); });
  152. auto t2 = std::thread([&] { io_ctx2.run(); });
  153. t1.join();
  154. t2.join();
  155. std::cout << "pings left: " << pinger->pings_left << "\n";
  156. } catch (const std::exception &ex) {
  157. std::cout << "exception : " << ex.what();
  158. }
  159. std::cout << "exiting...\n";
  160. return 0;
  161. }