Use the new solver to compute time

Now that all the infrastructure exists, hook it up.  Track which nodes
are connected, if there are any orphaned nodes, and everything else the
old code used to do.

This doesn't yet handle single directions going and coming.

Change-Id: I658347797384f7608870d231a3ebbb2c05dad1dc
diff --git a/aos/events/event_scheduler_test.cc b/aos/events/event_scheduler_test.cc
index e2523db..1ad0a84 100644
--- a/aos/events/event_scheduler_test.cc
+++ b/aos/events/event_scheduler_test.cc
@@ -8,10 +8,52 @@
 
 namespace chrono = std::chrono;
 
+// Legacy time converter for keeping old tests working.  Has numerical precision
+// problems.
+class SlopeOffsetTimeConverter final : public TimeConverter {
+ public:
+  SlopeOffsetTimeConverter(size_t nodes_count)
+      : distributed_offset_(nodes_count, std::chrono::seconds(0)),
+        distributed_slope_(nodes_count, 1.0) {}
+
+  // Sets the offset between the distributed and monotonic clock.
+  //   monotonic = distributed * slope + offset;
+  void SetDistributedOffset(size_t node_index,
+                            std::chrono::nanoseconds distributed_offset,
+                            double distributed_slope) {
+    distributed_offset_[node_index] = distributed_offset;
+    distributed_slope_[node_index] = distributed_slope;
+  }
+
+  distributed_clock::time_point ToDistributedClock(
+      size_t node_index, monotonic_clock::time_point time) override {
+    return distributed_clock::epoch() +
+           std::chrono::duration_cast<std::chrono::nanoseconds>(
+               (time.time_since_epoch() - distributed_offset_[node_index]) /
+               distributed_slope_[node_index]);
+  }
+
+  monotonic_clock::time_point FromDistributedClock(
+      size_t node_index, distributed_clock::time_point time) override {
+    return monotonic_clock::epoch() +
+           std::chrono::duration_cast<std::chrono::nanoseconds>(
+               time.time_since_epoch() * distributed_slope_[node_index]) +
+           distributed_offset_[node_index];
+  }
+
+ private:
+  // Offset to the distributed clock.
+  //   distributed = monotonic + offset;
+  std::vector<std::chrono::nanoseconds> distributed_offset_;
+  std::vector<double> distributed_slope_;
+};
+
 // Tests that the default parameters (slope of 1, offest of 0) behave as
 // an identity.
 TEST(EventSchedulerTest, IdentityTimeConversion) {
+  SlopeOffsetTimeConverter time(1);
   EventScheduler s;
+  s.SetTimeConverter(0u, &time);
   EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
             monotonic_clock::epoch());
 
@@ -22,15 +64,16 @@
   EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch()),
             distributed_clock::epoch());
 
-  EXPECT_EQ(
-      s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(1)),
-      distributed_clock::epoch() + chrono::seconds(1));
+  EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(1)),
+            distributed_clock::epoch() + chrono::seconds(1));
 }
 
 // Tests that a non-unity slope is computed correctly.
 TEST(EventSchedulerTest, DoubleTimeConversion) {
+  SlopeOffsetTimeConverter time(1);
   EventScheduler s;
-  s.SetDistributedOffset(std::chrono::seconds(7), 2.0);
+  s.SetTimeConverter(0u, &time);
+  time.SetDistributedOffset(0u, std::chrono::seconds(7), 2.0);
 
   EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
             monotonic_clock::epoch() + chrono::seconds(7));
@@ -42,9 +85,8 @@
   EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(7)),
             distributed_clock::epoch());
 
-  EXPECT_EQ(
-      s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(9)),
-      distributed_clock::epoch() + chrono::seconds(1));
+  EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(9)),
+            distributed_clock::epoch() + chrono::seconds(1));
 }
 
 }  // namespace aos