Make LogReader::OnStart able to start applications at times

There are 2 main features missing from OnStart to make it truly useful
at scale.
1) We need to be able to start applications...
2) Only replaying a time range (typically on the RT clock) is very
   helpful.

Add support for 1.

2 is more tricky.  I considered putting the logic outside LogReader, but
in the end, the bookkeeping for starting and stopping is bad enough that
it really helps to have LogReader manage and synchronize it.  Maybe we
can refactor it back out later.

Change-Id: I4ddf6e18819d3aadd02f38776bbc7aef843a96b0
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index a2428a4..a1ef95d 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -1268,6 +1268,11 @@
       event_loop->SetIsRunning(true);
     }
   });
+  scheduler_.set_stopped([this]() {
+    for (SimulatedEventLoop *event_loop : event_loops_) {
+      event_loop->SetIsRunning(false);
+    }
+  });
   scheduler_.set_on_shutdown([this]() {
     VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node())
             << monotonic_now() << " Shutting down node.";
@@ -1339,7 +1344,7 @@
 
 void NodeEventLoopFactory::Shutdown() {
   for (SimulatedEventLoop *event_loop : event_loops_) {
-    event_loop->SetIsRunning(false);
+    CHECK(!event_loop->is_running());
   }
 
   CHECK(started_);
@@ -1367,12 +1372,11 @@
 
 void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) {
   // This sets running to true too.
-  scheduler_scheduler_.RunOnStartup();
   scheduler_scheduler_.RunFor(duration);
   for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
     if (node) {
       for (SimulatedEventLoop *loop : node->event_loops_) {
-        loop->SetIsRunning(false);
+        CHECK(!loop->is_running());
       }
     }
   }
@@ -1380,12 +1384,11 @@
 
 void SimulatedEventLoopFactory::Run() {
   // This sets running to true too.
-  scheduler_scheduler_.RunOnStartup();
   scheduler_scheduler_.Run();
   for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
     if (node) {
       for (SimulatedEventLoop *loop : node->event_loops_) {
-        loop->SetIsRunning(false);
+        CHECK(!loop->is_running());
       }
     }
   }
@@ -1458,6 +1461,11 @@
   return std::move(result);
 }
 
+void SimulatedEventLoopFactory::AllowApplicationCreationDuring(
+    std::function<void()> fn) {
+  scheduler_scheduler_.TemporarilyStopAndRun(std::move(fn));
+}
+
 void NodeEventLoopFactory::Disconnect(const Node *other) {
   factory_->bridge_->Disconnect(node_, other);
 }