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