Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_PING_LIB_H_ |
| 2 | #define AOS_EVENTS_PING_LIB_H_ |
| 3 | |
| 4 | #include <chrono> |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 5 | #include <string_view> |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 6 | |
| 7 | #include "aos/events/event_loop.h" |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 8 | #include "aos/events/ping_static.h" |
| 9 | #include "aos/events/pong_static.h" |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // Class which sends out a Ping message every X ms, and times the response. |
| 14 | class Ping { |
| 15 | public: |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 16 | Ping(EventLoop *event_loop, std::string_view channel_name = "/test"); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 18 | void set_quiet(bool quiet) { quiet_ = quiet; } |
| 19 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 20 | private: |
| 21 | // Sends out the ping message with an incrementing count. |
| 22 | void SendPing(); |
| 23 | |
| 24 | // Receives the reply and measures the latency. |
| 25 | void HandlePong(const examples::Pong &pong); |
| 26 | |
| 27 | aos::EventLoop *event_loop_; |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 28 | aos::Sender<examples::PingStatic> sender_; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 29 | // Timer handle which sends the Ping message. |
| 30 | aos::TimerHandler *timer_handle_; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 31 | // Number of pings sent. |
| 32 | int count_ = 0; |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 33 | // Last pong value received so we can detect missed pongs. |
| 34 | int last_pong_value_ = 0; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 35 | |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 36 | bool quiet_ = true; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | } // namespace aos |
| 40 | |
| 41 | #endif // AOS_EVENTS_PING_LIB_H_ |