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

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