Add a timeout to aos_dump

There are cases, especially when scripting with the --count flag, to
wait for a chunk of time for a message before giving up.  Add a
--timeout flag to aos_dump to implement this behavior.

Change-Id: I65719e8b55aa653d691e3e35f3cc88acc4e60127
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/aos_dump.cc b/aos/aos_dump.cc
index 477b6fb..1662b47 100644
--- a/aos/aos_dump.cc
+++ b/aos/aos_dump.cc
@@ -20,6 +20,9 @@
 DEFINE_int32(rate_limit, 0,
              "The minimum amount of time to wait in milliseconds before "
              "sending another message");
+DEFINE_int32(timeout, -1,
+             "The max time in milliseconds to wait for messages before "
+             "exiting.  -1 means forever, 0 means don't wait.");
 
 namespace {
 
@@ -100,6 +103,10 @@
       return 0;
     }
 
+    if (FLAGS_timeout == 0) {
+      continue;
+    }
+
     cli_info.event_loop->MakeRawWatcher(
         channel,
         [channel, &str_builder, &cli_info, &message_count, &next_send_time](
@@ -119,6 +126,21 @@
         });
   }
 
+  if (FLAGS_timeout == 0) {
+    return 0;
+  }
+
+  if (FLAGS_timeout > 0) {
+    aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
+        [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
+
+    cli_info.event_loop->OnRun(
+        [handle, event_loop = &cli_info.event_loop.value()]() {
+          handle->Setup(event_loop->monotonic_now() +
+                        std::chrono::milliseconds(FLAGS_timeout));
+        });
+  }
+
   cli_info.event_loop->Run();
 
   return 0;