ping-pong-2-threads-preemt.cpp 6.0 KB

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