blob: aa40d6b435cc0c0d1ed4d9217e5376c6910811d2 [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#ifndef AOS_EVENTS_PING_LIB_H_
2#define AOS_EVENTS_PING_LIB_H_
3
4#include <chrono>
Sanjay Narayanan5ec00232022-07-08 15:21:30 -07005#include <string_view>
Austin Schuh6b9c4152019-11-29 12:45:24 -08006
7#include "aos/events/event_loop.h"
James Kuszmaul6f255b22023-11-06 13:46:54 -08008#include "aos/events/ping_static.h"
9#include "aos/events/pong_static.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -080010
11namespace aos {
12
13// Class which sends out a Ping message every X ms, and times the response.
14class Ping {
15 public:
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070016 Ping(EventLoop *event_loop, std::string_view channel_name = "/test");
Austin Schuh6b9c4152019-11-29 12:45:24 -080017
Austin Schuh4c3b9702020-08-30 11:34:55 -070018 void set_quiet(bool quiet) { quiet_ = quiet; }
19
Austin Schuh6b9c4152019-11-29 12:45:24 -080020 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 Kuszmaul6f255b22023-11-06 13:46:54 -080028 aos::Sender<examples::PingStatic> sender_;
Austin Schuh6b9c4152019-11-29 12:45:24 -080029 // Timer handle which sends the Ping message.
30 aos::TimerHandler *timer_handle_;
Austin Schuh6b9c4152019-11-29 12:45:24 -080031 // Number of pings sent.
32 int count_ = 0;
Austin Schuh5d89cf52019-12-28 16:27:42 -080033 // Last pong value received so we can detect missed pongs.
34 int last_pong_value_ = 0;
Austin Schuh4c3b9702020-08-30 11:34:55 -070035
Austin Schuh9b1d6282022-06-10 17:03:21 -070036 bool quiet_ = true;
Austin Schuh6b9c4152019-11-29 12:45:24 -080037};
38
39} // namespace aos
40
41#endif // AOS_EVENTS_PING_LIB_H_