Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 1 | #include "aos/events/ping_lib.h" |
| 2 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 3 | #include "gflags/gflags.h" |
| 4 | #include "glog/logging.h" |
| 5 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 6 | #include "aos/events/ping_generated.h" |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 7 | #include "aos/events/pong_generated.h" |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 8 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 9 | |
| 10 | DEFINE_int32(sleep_ms, 10, "Time to sleep between pings"); |
| 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 16 | Ping::Ping(EventLoop *event_loop, std::string_view channel_name) |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 17 | : event_loop_(event_loop), |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 18 | sender_(event_loop_->MakeSender<examples::Ping>(channel_name)) { |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 19 | timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); }); |
| 20 | timer_handle_->set_name("ping"); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 21 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 22 | if (event_loop_->HasChannel<examples::Pong>(channel_name)) { |
| 23 | event_loop_->MakeWatcher( |
| 24 | channel_name, [this](const examples::Pong &pong) { HandlePong(pong); }); |
| 25 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 26 | |
| 27 | event_loop_->OnRun([this]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame^] | 28 | timer_handle_->Schedule(event_loop_->monotonic_now(), |
| 29 | chrono::milliseconds(FLAGS_sleep_ms)); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 30 | }); |
| 31 | |
| 32 | event_loop_->SetRuntimeRealtimePriority(5); |
| 33 | } |
| 34 | |
| 35 | void Ping::SendPing() { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 36 | if (last_pong_value_ != count_ && (!quiet_ || VLOG_IS_ON(1))) { |
| 37 | LOG(WARNING) << "Did not receive response to " << count_ << " within " |
| 38 | << FLAGS_sleep_ms << "ms."; |
| 39 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 40 | ++count_; |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 41 | aos::Sender<examples::Ping>::Builder builder = sender_.MakeBuilder(); |
| 42 | examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>(); |
| 43 | ping_builder.add_value(count_); |
| 44 | ping_builder.add_send_time( |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 45 | event_loop_->monotonic_now().time_since_epoch().count()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 46 | builder.CheckOk(builder.Send(ping_builder.Finish())); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 47 | VLOG(2) << "Sending ping"; |
| 48 | } |
| 49 | |
| 50 | void Ping::HandlePong(const examples::Pong &pong) { |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 51 | 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 Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 59 | if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 60 | LOG(WARNING) << "Unexpected pong value, wanted " << last_pong_value_ + 1 |
| 61 | << ", got " << pong.value(); |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 64 | if (pong.value() == count_) { |
| 65 | VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns " |
| 66 | << FlatbufferToJson(&pong); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 67 | } else if (!quiet_ || VLOG_IS_ON(1)) { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 68 | LOG(WARNING) << "Unexpected pong response, got " << FlatbufferToJson(&pong) |
| 69 | << " expected " << count_ << ", elapsed time " |
| 70 | << round_trip_time.count() << " ns "; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 71 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 72 | |
| 73 | last_pong_value_ = pong.value(); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 74 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 76 | } // namespace aos |