ping-pong-ev-2-threads.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Copyright (c) 2021-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include <rotor/ev.hpp>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <chrono>
  10. #include <cstdlib>
  11. #include <thread>
  12. #include <boost/asio/detail/winsock_init.hpp> // for calling WSAStartup on Windows
  13. struct ping_t {};
  14. struct pong_t {};
  15. struct pinger_t : public rotor::actor_base_t {
  16. using timepoint_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
  17. using rotor::actor_base_t::actor_base_t;
  18. void set_pings(std::size_t pings) { pings_left = pings_count = pings; }
  19. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  20. rotor::actor_base_t::configure(plugin);
  21. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) {
  22. std::cout << "pinger_t::configure, going to subscribe on_pong\n";
  23. p.subscribe_actor(&pinger_t::on_pong);
  24. });
  25. plugin.with_casted<rotor::plugin::registry_plugin_t>(
  26. [&](auto &p) { p.discover_name("ponger", ponger_addr, true).link(); });
  27. }
  28. void on_start() noexcept override {
  29. rotor::actor_base_t::on_start();
  30. std::cout << "pings start (" << pings_left << ")\n";
  31. start = std::chrono::high_resolution_clock::now();
  32. send_ping();
  33. }
  34. void on_pong(rotor::message_t<pong_t> &) noexcept {
  35. // std::cout << "pinger_t::on_pong\n";
  36. send_ping();
  37. }
  38. private:
  39. void send_ping() {
  40. if (pings_left) {
  41. send<ping_t>(ponger_addr);
  42. --pings_left;
  43. } else {
  44. using namespace std::chrono;
  45. auto end = high_resolution_clock::now();
  46. std::chrono::duration<double> diff = end - start;
  47. double freq = ((double)pings_count) / diff.count();
  48. std::cout << "pings finishes (" << pings_left << ") in " << diff.count() << "s"
  49. << ", freq = " << std::fixed << std::setprecision(10) << freq << ", real freq = " << std::fixed
  50. << std::setprecision(10) << freq * 2 << "\n";
  51. ponger_addr->supervisor.shutdown();
  52. }
  53. }
  54. timepoint_t start;
  55. rotor::address_ptr_t ponger_addr;
  56. std::size_t pings_left;
  57. std::size_t pings_count;
  58. };
  59. struct ponger_t : public rotor::actor_base_t {
  60. using rotor::actor_base_t::actor_base_t;
  61. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  62. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  63. rotor::actor_base_t::configure(plugin);
  64. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) {
  65. std::cout << "ponger_t::configure, going to subscribe on_ping\n";
  66. p.subscribe_actor(&ponger_t::on_ping);
  67. });
  68. plugin.with_casted<rotor::plugin::registry_plugin_t>(
  69. [&](auto &p) { p.register_name("ponger", get_address()); });
  70. }
  71. void on_ping(rotor::message_t<ping_t> &) noexcept { send<pong_t>(pinger_addr); }
  72. private:
  73. rotor::address_ptr_t pinger_addr;
  74. };
  75. int main(int argc, char **argv) {
  76. try {
  77. std::uint32_t count = 10000;
  78. if (argc > 1) {
  79. count = static_cast<std::uint32_t>(std::atoi(argv[1]));
  80. }
  81. auto timeout = boost::posix_time::milliseconds{100};
  82. auto *loop1 = ev_loop_new(0);
  83. auto ctx1 = rotor::ev::system_context_ev_t();
  84. auto sup1 = ctx1.create_supervisor<rotor::ev::supervisor_ev_t>()
  85. .loop(loop1)
  86. .loop_ownership(true) /* let supervisor takes ownership on the loop */
  87. .timeout(timeout)
  88. .create_registry()
  89. .finish();
  90. auto *loop2 = ev_loop_new(0);
  91. auto ctx2 = rotor::ev::system_context_ev_t();
  92. auto sup2 = ctx2.create_supervisor<rotor::ev::supervisor_ev_t>()
  93. .loop(loop2)
  94. .loop_ownership(true) /* let supervisor takes ownership on the loop */
  95. .timeout(timeout)
  96. .registry_address(sup1->get_registry_address())
  97. .finish();
  98. auto pinger = sup1->create_actor<pinger_t>().timeout(timeout).autoshutdown_supervisor().finish();
  99. auto ponger = sup2->create_actor<ponger_t>().timeout(timeout).finish();
  100. ponger->set_pinger_addr(pinger->get_address());
  101. pinger->set_pings(count);
  102. sup1->start();
  103. sup2->start();
  104. auto thread_aux = std::thread([&] { ev_run(loop2); });
  105. ev_run(loop1);
  106. sup2->shutdown();
  107. thread_aux.join();
  108. } catch (const std::exception &ex) {
  109. std::cout << "exception : " << ex.what();
  110. }
  111. std::cout << "exiting...\n";
  112. return 0;
  113. }