blob: e8bf171e56c2db0dcffee87a1979449ef58d4b35 [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#include "aos/events/pong_lib.h"
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "glog/logging.h"
4
Austin Schuh6b9c4152019-11-29 12:45:24 -08005#include "aos/events/event_loop.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -08006#include "aos/events/ping_generated.h"
Austin Schuh5d89cf52019-12-28 16:27:42 -08007#include "aos/events/pong_generated.h"
Austin Schuh6b9c4152019-11-29 12:45:24 -08008
James Kuszmaul60bb8682023-08-07 07:39:34 -07009DEFINE_bool(fetch, false, "Poll & fetch messages instead of using a watcher.");
10DEFINE_uint32(fetch_period_ms, 10, "Frequency at which to fetch.");
11
Austin Schuh6b9c4152019-11-29 12:45:24 -080012namespace aos {
13
14Pong::Pong(EventLoop *event_loop)
15 : event_loop_(event_loop),
James Kuszmaul60bb8682023-08-07 07:39:34 -070016 fetcher_(event_loop_->MakeFetcher<examples::Ping>("/test")),
Austin Schuh6b9c4152019-11-29 12:45:24 -080017 sender_(event_loop_->MakeSender<examples::Pong>("/test")) {
James Kuszmaul60bb8682023-08-07 07:39:34 -070018 if (FLAGS_fetch) {
19 event_loop_
20 ->AddPhasedLoop(
21 [this](int) {
22 while (fetcher_.FetchNext()) {
23 HandlePing(*fetcher_.get());
24 }
25 },
26 std::chrono::milliseconds(FLAGS_fetch_period_ms))
27 ->set_name("pong");
28 } else {
29 event_loop_->MakeWatcher(
30 "/test", [this](const examples::Ping &ping) { HandlePing(ping); });
31 }
Austin Schuh6b9c4152019-11-29 12:45:24 -080032
33 event_loop_->SetRuntimeRealtimePriority(5);
34}
35
James Kuszmaul60bb8682023-08-07 07:39:34 -070036void Pong::HandlePing(const examples::Ping &ping) {
37 if (last_value_ == ping.value() && (!quiet_ || VLOG_IS_ON(1))) {
38 LOG(WARNING) << "Duplicate ping value at " << last_value_
39 << " time difference " << ping.send_time() - last_send_time_;
40 }
41 last_value_ = ping.value();
42 last_send_time_ = ping.send_time();
43 aos::Sender<examples::Pong>::Builder builder = sender_.MakeBuilder();
44 examples::Pong::Builder pong_builder = builder.MakeBuilder<examples::Pong>();
45 pong_builder.add_value(ping.value());
46 pong_builder.add_initial_send_time(ping.send_time());
47 builder.CheckOk(builder.Send(pong_builder.Finish()));
48}
49
Austin Schuh6b9c4152019-11-29 12:45:24 -080050} // namespace aos