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