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

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