blob: 900ed0dba20a7f252ed8619df35b84eb703ee6f6 [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
15Ping::Ping(EventLoop *event_loop)
16 : event_loop_(event_loop),
Austin Schuh5d89cf52019-12-28 16:27:42 -080017 sender_(event_loop_->MakeSender<examples::Ping>("/test")) {
18 timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); });
19 timer_handle_->set_name("ping");
Austin Schuh6b9c4152019-11-29 12:45:24 -080020
21 event_loop_->MakeWatcher(
22 "/test", [this](const examples::Pong &pong) { HandlePong(pong); });
23
24 event_loop_->OnRun([this]() {
Austin Schuh5d89cf52019-12-28 16:27:42 -080025 timer_handle_->Setup(event_loop_->monotonic_now(),
26 chrono::milliseconds(FLAGS_sleep_ms));
Austin Schuh6b9c4152019-11-29 12:45:24 -080027 });
28
29 event_loop_->SetRuntimeRealtimePriority(5);
30}
31
32void Ping::SendPing() {
33 ++count_;
Austin Schuh5d89cf52019-12-28 16:27:42 -080034 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 Schuh6b9c4152019-11-29 12:45:24 -080038 event_loop_->monotonic_now().time_since_epoch().count());
milind1f1dca32021-07-03 13:50:07 -070039 builder.CheckOk(builder.Send(ping_builder.Finish()));
Austin Schuh6b9c4152019-11-29 12:45:24 -080040 VLOG(2) << "Sending ping";
41}
42
43void Ping::HandlePong(const examples::Pong &pong) {
Austin Schuh6b9c4152019-11-29 12:45:24 -080044 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 Schuh4c3b9702020-08-30 11:34:55 -070052 if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) {
Austin Schuh5d89cf52019-12-28 16:27:42 -080053 LOG(WARNING) << "Pong message lost";
54 }
55
Austin Schuh6b9c4152019-11-29 12:45:24 -080056 if (pong.value() == count_) {
57 VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns "
58 << FlatbufferToJson(&pong);
Austin Schuh4c3b9702020-08-30 11:34:55 -070059 } else if (!quiet_ || VLOG_IS_ON(1)) {
Austin Schuh5d89cf52019-12-28 16:27:42 -080060 LOG(WARNING) << "Missmatched pong message, got " << FlatbufferToJson(&pong)
61 << " expected " << count_;
Austin Schuh6b9c4152019-11-29 12:45:24 -080062 }
Austin Schuh5d89cf52019-12-28 16:27:42 -080063
64 last_pong_value_ = pong.value();
Austin Schuh6b9c4152019-11-29 12:45:24 -080065}
Austin Schuh5d89cf52019-12-28 16:27:42 -080066
Austin Schuh6b9c4152019-11-29 12:45:24 -080067} // namespace aos