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