Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 1 | #include "aos/events/ping_lib.h" |
| 2 | |
| 3 | #include "aos/events/pong_generated.h" |
| 4 | #include "aos/events/ping_generated.h" |
| 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"); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 10 | DEFINE_bool(phased_loop, false, "If true, use a phased loop"); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
| 16 | Ping::Ping(EventLoop *event_loop) |
| 17 | : event_loop_(event_loop), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 18 | sender_(event_loop_->MakeSender<examples::Ping>("/test")), |
| 19 | pong_fetcher_(event_loop_->MakeFetcher<examples::Pong>("/test")) { |
| 20 | if (FLAGS_phased_loop) { |
| 21 | phased_loop_handle_ = event_loop_->AddPhasedLoop( |
| 22 | [this](int) { SendPing(); }, chrono::milliseconds(FLAGS_sleep_ms)); |
| 23 | phased_loop_handle_->set_name("ping"); |
| 24 | } else { |
| 25 | timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); }); |
| 26 | timer_handle_->set_name("ping"); |
| 27 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 28 | |
| 29 | event_loop_->MakeWatcher( |
| 30 | "/test", [this](const examples::Pong &pong) { HandlePong(pong); }); |
| 31 | |
| 32 | event_loop_->OnRun([this]() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 33 | if (!FLAGS_phased_loop) { |
| 34 | timer_handle_->Setup(event_loop_->monotonic_now(), |
| 35 | chrono::milliseconds(FLAGS_sleep_ms)); |
| 36 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 37 | }); |
| 38 | |
| 39 | event_loop_->SetRuntimeRealtimePriority(5); |
| 40 | } |
| 41 | |
| 42 | void Ping::SendPing() { |
| 43 | ++count_; |
| 44 | aos::Sender<examples::Ping>::Builder msg = sender_.MakeBuilder(); |
| 45 | examples::Ping::Builder builder = msg.MakeBuilder<examples::Ping>(); |
| 46 | builder.add_value(count_); |
| 47 | builder.add_send_time( |
| 48 | event_loop_->monotonic_now().time_since_epoch().count()); |
| 49 | CHECK(msg.Send(builder.Finish())); |
| 50 | VLOG(2) << "Sending ping"; |
| 51 | } |
| 52 | |
| 53 | void Ping::HandlePong(const examples::Pong &pong) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 54 | pong_fetcher_.Fetch(); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 55 | const aos::monotonic_clock::time_point monotonic_send_time( |
| 56 | chrono::nanoseconds(pong.initial_send_time())); |
| 57 | const aos::monotonic_clock::time_point monotonic_now = |
| 58 | event_loop_->monotonic_now(); |
| 59 | |
| 60 | const chrono::nanoseconds round_trip_time = |
| 61 | monotonic_now - monotonic_send_time; |
| 62 | |
| 63 | if (pong.value() == count_) { |
| 64 | VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns " |
| 65 | << FlatbufferToJson(&pong); |
| 66 | } else { |
| 67 | VLOG(1) << "Missmatched pong message"; |
| 68 | } |
| 69 | } |
| 70 | } // namespace aos |