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