blob: e80fd753ad8366ceba57dca44409d7ec20370186 [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#include "aos/events/ping_lib.h"
2
Austin Schuh6b9c4152019-11-29 12:45:24 -08003#include "aos/events/ping_generated.h"
Austin Schuh5d89cf52019-12-28 16:27:42 -08004#include "aos/events/pong_generated.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -08005#include "aos/json_to_flatbuffer.h"
6#include "gflags/gflags.h"
7#include "glog/logging.h"
8
9DEFINE_int32(sleep_ms, 10, "Time to sleep between pings");
10
11namespace aos {
12
13namespace chrono = std::chrono;
14
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070015Ping::Ping(EventLoop *event_loop, std::string_view channel_name)
Austin Schuh6b9c4152019-11-29 12:45:24 -080016 : event_loop_(event_loop),
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070017 sender_(event_loop_->MakeSender<examples::Ping>(channel_name)) {
Austin Schuh5d89cf52019-12-28 16:27:42 -080018 timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); });
19 timer_handle_->set_name("ping");
Austin Schuh6b9c4152019-11-29 12:45:24 -080020
Sanjay Narayanan5ec00232022-07-08 15:21:30 -070021 if (event_loop_->HasChannel<examples::Pong>(channel_name)) {
22 event_loop_->MakeWatcher(
23 channel_name, [this](const examples::Pong &pong) { HandlePong(pong); });
24 }
Austin Schuh6b9c4152019-11-29 12:45:24 -080025
26 event_loop_->OnRun([this]() {
Austin Schuh5d89cf52019-12-28 16:27:42 -080027 timer_handle_->Setup(event_loop_->monotonic_now(),
28 chrono::milliseconds(FLAGS_sleep_ms));
Austin Schuh6b9c4152019-11-29 12:45:24 -080029 });
30
31 event_loop_->SetRuntimeRealtimePriority(5);
32}
33
34void Ping::SendPing() {
Sarah Newman2f98cf02022-04-01 16:42:00 -070035 if (last_pong_value_ != count_ && (!quiet_ || VLOG_IS_ON(1))) {
36 LOG(WARNING) << "Did not receive response to " << count_ << " within "
37 << FLAGS_sleep_ms << "ms.";
38 }
Austin Schuh6b9c4152019-11-29 12:45:24 -080039 ++count_;
Austin Schuh5d89cf52019-12-28 16:27:42 -080040 aos::Sender<examples::Ping>::Builder builder = sender_.MakeBuilder();
41 examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>();
42 ping_builder.add_value(count_);
43 ping_builder.add_send_time(
Austin Schuh6b9c4152019-11-29 12:45:24 -080044 event_loop_->monotonic_now().time_since_epoch().count());
milind1f1dca32021-07-03 13:50:07 -070045 builder.CheckOk(builder.Send(ping_builder.Finish()));
Austin Schuh6b9c4152019-11-29 12:45:24 -080046 VLOG(2) << "Sending ping";
47}
48
49void Ping::HandlePong(const examples::Pong &pong) {
Austin Schuh6b9c4152019-11-29 12:45:24 -080050 const aos::monotonic_clock::time_point monotonic_send_time(
51 chrono::nanoseconds(pong.initial_send_time()));
52 const aos::monotonic_clock::time_point monotonic_now =
53 event_loop_->monotonic_now();
54
55 const chrono::nanoseconds round_trip_time =
56 monotonic_now - monotonic_send_time;
57
Austin Schuh4c3b9702020-08-30 11:34:55 -070058 if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) {
Sarah Newman2f98cf02022-04-01 16:42:00 -070059 LOG(WARNING) << "Unexpected pong value, wanted " << last_pong_value_ + 1
60 << ", got " << pong.value();
Austin Schuh5d89cf52019-12-28 16:27:42 -080061 }
62
Austin Schuh6b9c4152019-11-29 12:45:24 -080063 if (pong.value() == count_) {
64 VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns "
65 << FlatbufferToJson(&pong);
Austin Schuh4c3b9702020-08-30 11:34:55 -070066 } else if (!quiet_ || VLOG_IS_ON(1)) {
Sarah Newman2f98cf02022-04-01 16:42:00 -070067 LOG(WARNING) << "Unexpected pong response, got " << FlatbufferToJson(&pong)
68 << " expected " << count_ << ", elapsed time "
69 << round_trip_time.count() << " ns ";
Austin Schuh6b9c4152019-11-29 12:45:24 -080070 }
Austin Schuh5d89cf52019-12-28 16:27:42 -080071
72 last_pong_value_ = pong.value();
Austin Schuh6b9c4152019-11-29 12:45:24 -080073}
Austin Schuh5d89cf52019-12-28 16:27:42 -080074
Austin Schuh6b9c4152019-11-29 12:45:24 -080075} // namespace aos