blob: cd3637804938107d28d8a4a35b8a8534ab81bc1c [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#include "aos/events/ping_lib.h"
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "gflags/gflags.h"
4#include "glog/logging.h"
5
James Kuszmaul6f255b22023-11-06 13:46:54 -08006#include "aos/events/ping_static.h"
7#include "aos/events/pong_static.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -08008#include "aos/json_to_flatbuffer.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -08009
James Kuszmaul60bb8682023-08-07 07:39:34 -070010DEFINE_int32(sleep_us, 10000, "Time to sleep between pings");
Austin Schuh6b9c4152019-11-29 12:45:24 -080011
12namespace aos {
13
14namespace chrono = std::chrono;
15
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070016Ping::Ping(EventLoop *event_loop, std::string_view channel_name)
Austin Schuh6b9c4152019-11-29 12:45:24 -080017 : event_loop_(event_loop),
James Kuszmaul6f255b22023-11-06 13:46:54 -080018 sender_(event_loop_->MakeSender<examples::PingStatic>(channel_name)) {
Austin Schuh5d89cf52019-12-28 16:27:42 -080019 timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); });
20 timer_handle_->set_name("ping");
Austin Schuh6b9c4152019-11-29 12:45:24 -080021
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070022 if (event_loop_->HasChannel<examples::Pong>(channel_name)) {
23 event_loop_->MakeWatcher(
24 channel_name, [this](const examples::Pong &pong) { HandlePong(pong); });
25 }
Austin Schuh6b9c4152019-11-29 12:45:24 -080026
27 event_loop_->OnRun([this]() {
Philipp Schradera6712522023-07-05 20:25:11 -070028 timer_handle_->Schedule(event_loop_->monotonic_now(),
James Kuszmaul60bb8682023-08-07 07:39:34 -070029 chrono::microseconds(FLAGS_sleep_us));
Austin Schuh6b9c4152019-11-29 12:45:24 -080030 });
31
32 event_loop_->SetRuntimeRealtimePriority(5);
33}
34
35void Ping::SendPing() {
Sarah Newman2f98cf02022-04-01 16:42:00 -070036 if (last_pong_value_ != count_ && (!quiet_ || VLOG_IS_ON(1))) {
37 LOG(WARNING) << "Did not receive response to " << count_ << " within "
James Kuszmaul60bb8682023-08-07 07:39:34 -070038 << FLAGS_sleep_us << "us.";
Sarah Newman2f98cf02022-04-01 16:42:00 -070039 }
Austin Schuh6b9c4152019-11-29 12:45:24 -080040 ++count_;
James Kuszmaul6f255b22023-11-06 13:46:54 -080041 aos::Sender<examples::PingStatic>::StaticBuilder builder =
42 sender_.MakeStaticBuilder();
James Kuszmauldde65632023-12-07 16:12:26 -080043 builder->set_value(count_);
44 builder->set_send_time(
45 event_loop_->monotonic_now().time_since_epoch().count());
James Kuszmaul6f255b22023-11-06 13:46:54 -080046 builder.CheckOk(builder.Send());
Austin Schuh6b9c4152019-11-29 12:45:24 -080047 VLOG(2) << "Sending ping";
48}
49
50void Ping::HandlePong(const examples::Pong &pong) {
Austin Schuh6b9c4152019-11-29 12:45:24 -080051 const aos::monotonic_clock::time_point monotonic_send_time(
52 chrono::nanoseconds(pong.initial_send_time()));
53 const aos::monotonic_clock::time_point monotonic_now =
54 event_loop_->monotonic_now();
55
56 const chrono::nanoseconds round_trip_time =
57 monotonic_now - monotonic_send_time;
58
Austin Schuh4c3b9702020-08-30 11:34:55 -070059 if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) {
Sarah Newman2f98cf02022-04-01 16:42:00 -070060 LOG(WARNING) << "Unexpected pong value, wanted " << last_pong_value_ + 1
61 << ", got " << pong.value();
Austin Schuh5d89cf52019-12-28 16:27:42 -080062 }
63
Austin Schuh6b9c4152019-11-29 12:45:24 -080064 if (pong.value() == count_) {
65 VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns "
66 << FlatbufferToJson(&pong);
Austin Schuh4c3b9702020-08-30 11:34:55 -070067 } else if (!quiet_ || VLOG_IS_ON(1)) {
Sarah Newman2f98cf02022-04-01 16:42:00 -070068 LOG(WARNING) << "Unexpected pong response, got " << FlatbufferToJson(&pong)
69 << " expected " << count_ << ", elapsed time "
70 << round_trip_time.count() << " ns ";
Austin Schuh6b9c4152019-11-29 12:45:24 -080071 }
Austin Schuh5d89cf52019-12-28 16:27:42 -080072
73 last_pong_value_ = pong.value();
Austin Schuh6b9c4152019-11-29 12:45:24 -080074}
Austin Schuh5d89cf52019-12-28 16:27:42 -080075
Austin Schuh6b9c4152019-11-29 12:45:24 -080076} // namespace aos