Simplify ping and pong
The simple demo apps grew to the point where they were no longer simple
and easy to understand since I was using them to test out timing reports
and accidentally committed the changes. Restore the simplicity.
Change-Id: I453fa0351247c4549098660cd21191659269f515
diff --git a/aos/events/ping.cc b/aos/events/ping.cc
index 5a79635..f829b41 100644
--- a/aos/events/ping.cc
+++ b/aos/events/ping.cc
@@ -6,11 +6,13 @@
#include "gflags/gflags.h"
#include "glog/logging.h"
+DEFINE_string(config, "aos/events/pingpong_config.json", "Path to the config.");
+
int main(int argc, char **argv) {
aos::InitGoogle(&argc, &argv);
aos::FlatbufferDetachedBuffer<aos::Configuration> config =
- aos::configuration::ReadConfig("aos/events/pingpong_config.json");
+ aos::configuration::ReadConfig(FLAGS_config);
aos::ShmEventLoop event_loop(&config.message());
diff --git a/aos/events/ping_lib.cc b/aos/events/ping_lib.cc
index 4bbf580..9bc2446 100644
--- a/aos/events/ping_lib.cc
+++ b/aos/events/ping_lib.cc
@@ -1,13 +1,12 @@
#include "aos/events/ping_lib.h"
-#include "aos/events/pong_generated.h"
#include "aos/events/ping_generated.h"
+#include "aos/events/pong_generated.h"
#include "aos/json_to_flatbuffer.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
DEFINE_int32(sleep_ms, 10, "Time to sleep between pings");
-DEFINE_bool(phased_loop, false, "If true, use a phased loop");
namespace aos {
@@ -15,25 +14,16 @@
Ping::Ping(EventLoop *event_loop)
: event_loop_(event_loop),
- sender_(event_loop_->MakeSender<examples::Ping>("/test")),
- pong_fetcher_(event_loop_->MakeFetcher<examples::Pong>("/test")) {
- if (FLAGS_phased_loop) {
- phased_loop_handle_ = event_loop_->AddPhasedLoop(
- [this](int) { SendPing(); }, chrono::milliseconds(FLAGS_sleep_ms));
- phased_loop_handle_->set_name("ping");
- } else {
- timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); });
- timer_handle_->set_name("ping");
- }
+ sender_(event_loop_->MakeSender<examples::Ping>("/test")) {
+ timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); });
+ timer_handle_->set_name("ping");
event_loop_->MakeWatcher(
"/test", [this](const examples::Pong &pong) { HandlePong(pong); });
event_loop_->OnRun([this]() {
- if (!FLAGS_phased_loop) {
- timer_handle_->Setup(event_loop_->monotonic_now(),
- chrono::milliseconds(FLAGS_sleep_ms));
- }
+ timer_handle_->Setup(event_loop_->monotonic_now(),
+ chrono::milliseconds(FLAGS_sleep_ms));
});
event_loop_->SetRuntimeRealtimePriority(5);
@@ -41,17 +31,16 @@
void Ping::SendPing() {
++count_;
- aos::Sender<examples::Ping>::Builder msg = sender_.MakeBuilder();
- examples::Ping::Builder builder = msg.MakeBuilder<examples::Ping>();
- builder.add_value(count_);
- builder.add_send_time(
+ aos::Sender<examples::Ping>::Builder builder = sender_.MakeBuilder();
+ examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>();
+ ping_builder.add_value(count_);
+ ping_builder.add_send_time(
event_loop_->monotonic_now().time_since_epoch().count());
- CHECK(msg.Send(builder.Finish()));
+ CHECK(builder.Send(ping_builder.Finish()));
VLOG(2) << "Sending ping";
}
void Ping::HandlePong(const examples::Pong &pong) {
- pong_fetcher_.Fetch();
const aos::monotonic_clock::time_point monotonic_send_time(
chrono::nanoseconds(pong.initial_send_time()));
const aos::monotonic_clock::time_point monotonic_now =
@@ -60,11 +49,19 @@
const chrono::nanoseconds round_trip_time =
monotonic_now - monotonic_send_time;
+ if (last_pong_value_ + 1 != pong.value()) {
+ LOG(WARNING) << "Pong message lost";
+ }
+
if (pong.value() == count_) {
VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns "
<< FlatbufferToJson(&pong);
} else {
- VLOG(1) << "Missmatched pong message";
+ LOG(WARNING) << "Missmatched pong message, got " << FlatbufferToJson(&pong)
+ << " expected " << count_;
}
+
+ last_pong_value_ = pong.value();
}
+
} // namespace aos
diff --git a/aos/events/ping_lib.h b/aos/events/ping_lib.h
index 6be75ed..e14c3f2 100644
--- a/aos/events/ping_lib.h
+++ b/aos/events/ping_lib.h
@@ -25,10 +25,10 @@
aos::Sender<examples::Ping> sender_;
// Timer handle which sends the Ping message.
aos::TimerHandler *timer_handle_;
- aos::PhasedLoopHandler *phased_loop_handle_;
- aos::Fetcher<examples::Pong> pong_fetcher_;
// Number of pings sent.
int count_ = 0;
+ // Last pong value received so we can detect missed pongs.
+ int last_pong_value_ = 0;
};
} // namespace aos
diff --git a/aos/events/pong.cc b/aos/events/pong.cc
index 339d4d7..581d04f 100644
--- a/aos/events/pong.cc
+++ b/aos/events/pong.cc
@@ -6,13 +6,15 @@
#include "aos/init.h"
#include "glog/logging.h"
+DEFINE_string(config, "aos/events/pingpong_config.json", "Path to the config.");
+
int main(int argc, char **argv) {
FLAGS_logtostderr = true;
google::InitGoogleLogging(argv[0]);
::gflags::ParseCommandLineFlags(&argc, &argv, true);
aos::FlatbufferDetachedBuffer<aos::Configuration> config =
- aos::configuration::ReadConfig("aos/events/pingpong_config.json");
+ aos::configuration::ReadConfig(FLAGS_config);
::aos::ShmEventLoop event_loop(&config.message());
diff --git a/aos/events/pong_lib.cc b/aos/events/pong_lib.cc
index 5ff38b9..9d7731d 100644
--- a/aos/events/pong_lib.cc
+++ b/aos/events/pong_lib.cc
@@ -1,8 +1,8 @@
#include "aos/events/pong_lib.h"
#include "aos/events/event_loop.h"
-#include "aos/events/pong_generated.h"
#include "aos/events/ping_generated.h"
+#include "aos/events/pong_generated.h"
#include "glog/logging.h"
namespace aos {
@@ -11,11 +11,12 @@
: event_loop_(event_loop),
sender_(event_loop_->MakeSender<examples::Pong>("/test")) {
event_loop_->MakeWatcher("/test", [this](const examples::Ping &ping) {
- aos::Sender<examples::Pong>::Builder msg = sender_.MakeBuilder();
- examples::Pong::Builder builder = msg.MakeBuilder<examples::Pong>();
- builder.add_value(ping.value());
- builder.add_initial_send_time(ping.send_time());
- CHECK(msg.Send(builder.Finish()));
+ aos::Sender<examples::Pong>::Builder builder = sender_.MakeBuilder();
+ examples::Pong::Builder pong_builder =
+ builder.MakeBuilder<examples::Pong>();
+ pong_builder.add_value(ping.value());
+ pong_builder.add_initial_send_time(ping.send_time());
+ CHECK(builder.Send(pong_builder.Finish()));
});
event_loop_->SetRuntimeRealtimePriority(5);