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() { |
| 33 | ++count_; |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 34 | aos::Sender<examples::Ping>::Builder builder = sender_.MakeBuilder(); |
| 35 | examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>(); |
| 36 | ping_builder.add_value(count_); |
| 37 | ping_builder.add_send_time( |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 38 | event_loop_->monotonic_now().time_since_epoch().count()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 39 | builder.CheckOk(builder.Send(ping_builder.Finish())); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 40 | VLOG(2) << "Sending ping"; |
| 41 | } |
| 42 | |
| 43 | void Ping::HandlePong(const examples::Pong &pong) { |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 44 | const aos::monotonic_clock::time_point monotonic_send_time( |
| 45 | chrono::nanoseconds(pong.initial_send_time())); |
| 46 | const aos::monotonic_clock::time_point monotonic_now = |
| 47 | event_loop_->monotonic_now(); |
| 48 | |
| 49 | const chrono::nanoseconds round_trip_time = |
| 50 | monotonic_now - monotonic_send_time; |
| 51 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 52 | if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) { |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 53 | LOG(WARNING) << "Pong message lost"; |
| 54 | } |
| 55 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 56 | if (pong.value() == count_) { |
| 57 | VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns " |
| 58 | << FlatbufferToJson(&pong); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 59 | } else if (!quiet_ || VLOG_IS_ON(1)) { |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 60 | LOG(WARNING) << "Missmatched pong message, got " << FlatbufferToJson(&pong) |
| 61 | << " expected " << count_; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 62 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 63 | |
| 64 | last_pong_value_ = pong.value(); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 65 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 67 | } // namespace aos |