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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2019-2022 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 = 0;
  30. timepoint_t start;
  31. bool stop = false;
  32. using r::actor_base_t::actor_base_t;
  33. void set_ponger_addr(const r::address_ptr_t &addr) { ponger_addr = addr; }
  34. void set_pings(std::size_t pings) { pings_count = pings_left = pings; }
  35. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  36. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  37. plugin.with_casted<r::plugin::link_client_plugin_t>([&](auto &p) {
  38. p.link(ponger_addr, true, [&](auto &ec) {
  39. if (ec) {
  40. std::cout << "error linking pinger with ponger :: " << ec->message() << "\n";
  41. } else {
  42. std::cout << "pinger has been successfully linked with ponger\n";
  43. }
  44. });
  45. });
  46. }
  47. void on_start() noexcept override {
  48. r::actor_base_t::on_start();
  49. std::cout << "pinger::on_start\n";
  50. start = std::chrono::high_resolution_clock::now();
  51. do_send_ping();
  52. do_send_ping();
  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. }
  62. void on_pong(r::message_t<pong_t> &) noexcept { do_send_ping(); }
  63. void do_send_ping() {
  64. // std::cout << "pinger::do_send_ping, left: " << pings_left << "\n";
  65. if (pings_left) {
  66. send<ping_t>(ponger_addr);
  67. --pings_left;
  68. } else {
  69. if (!stop) {
  70. stop = true;
  71. using namespace std::chrono;
  72. auto end = high_resolution_clock::now();
  73. std::chrono::duration<double> diff = end - start;
  74. double freq = ((double)pings_count) / diff.count();
  75. std::cout << "pings finishes (" << pings_left << ") in " << diff.count() << "s"
  76. << ", freq = " << std::fixed << std::setprecision(10) << freq
  77. << ", real freq = " << std::fixed << std::setprecision(10) << freq * 2 << "\n";
  78. do_shutdown();
  79. ponger_addr->supervisor.shutdown();
  80. }
  81. }
  82. }
  83. // we need of this to let somebody shutdown everything
  84. void shutdown_finish() noexcept override {
  85. r::actor_base_t::shutdown_finish();
  86. do_shutdown();
  87. ponger_addr->supervisor.shutdown();
  88. std::cout << "pinger shutdown finish\n";
  89. }
  90. r::address_ptr_t ponger_addr;
  91. };
  92. struct ponger_t : public r::actor_base_t {
  93. using r::actor_base_t::actor_base_t;
  94. void set_pinger_addr(const r::address_ptr_t &addr) { pinger_addr = addr; }
  95. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  96. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) {
  97. std::cout << "ponger::configure, subscribing on_ping\n";
  98. p.subscribe_actor(&ponger_t::on_ping);
  99. });
  100. }
  101. void on_start() noexcept override {
  102. r::actor_base_t::on_start();
  103. std::cout << "ponger::on_start\n";
  104. }
  105. void on_ping(r::message_t<ping_t> &) noexcept {
  106. // std::cout << "ponger::on_ping\n";
  107. send<pong_t>(pinger_addr);
  108. }
  109. private:
  110. r::address_ptr_t pinger_addr;
  111. };
  112. int main(int argc, char **argv) {
  113. asio::io_context io_ctx;
  114. try {
  115. std::uint32_t count = 10000;
  116. if (argc > 1) {
  117. boost::conversion::try_lexical_convert(argv[1], count);
  118. }
  119. auto sys_ctx = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_ctx)};
  120. auto strand1 = std::make_shared<asio::io_context::strand>(io_ctx);
  121. auto strand2 = std::make_shared<asio::io_context::strand>(io_ctx);
  122. auto timeout = boost::posix_time::milliseconds{500};
  123. auto sup1 = sys_ctx->create_supervisor<ra::supervisor_asio_t>().strand(strand1).timeout(timeout).finish();
  124. auto sup2 = sup1->create_actor<ra::supervisor_asio_t>().strand(strand2).timeout(timeout).finish();
  125. auto pinger = sup1->create_actor<pinger_t>().timeout(timeout).autoshutdown_supervisor().finish();
  126. auto ponger = sup2->create_actor<ponger_t>().timeout(timeout).finish();
  127. pinger->set_ponger_addr(ponger->get_address());
  128. pinger->set_pings(count);
  129. ponger->set_pinger_addr(pinger->get_address());
  130. sup1->start();
  131. auto t1 = std::thread([&] { io_ctx.run(); });
  132. auto t2 = std::thread([&] { io_ctx.run(); });
  133. t1.join();
  134. t2.join();
  135. std::cout << "pings left: " << pinger->pings_left << "\n";
  136. } catch (const std::exception &ex) {
  137. std::cout << "exception : " << ex.what();
  138. }
  139. std::cout << "exiting...\n";
  140. return 0;
  141. }