Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 1 | #include "aos/events/simulated-event-loop.h" |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 2 | |
| 3 | #include <algorithm> |
| 4 | |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/queue.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | namespace { |
| 10 | class SimulatedFetcher : public RawFetcher { |
| 11 | public: |
| 12 | explicit SimulatedFetcher(SimulatedQueue *queue) : queue_(queue) {} |
| 13 | ~SimulatedFetcher() {} |
| 14 | |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 15 | bool FetchNext() override { |
| 16 | LOG(FATAL, "Simulated event loops do not support FetchNext."); |
| 17 | return false; |
| 18 | } |
| 19 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 20 | bool Fetch() override { |
| 21 | if (index_ == queue_->index()) return false; |
| 22 | |
| 23 | // Fetched message is newer |
| 24 | msg_ = queue_->latest_message(); |
| 25 | index_ = queue_->index(); |
| 26 | set_most_recent(reinterpret_cast<FetchValue *>(msg_.get())); |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | private: |
| 31 | int64_t index_ = -1; |
| 32 | SimulatedQueue *queue_; |
| 33 | RefCountedBuffer msg_; |
| 34 | }; |
| 35 | |
| 36 | class SimulatedSender : public RawSender { |
| 37 | public: |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 38 | SimulatedSender(SimulatedQueue *queue, EventLoop *event_loop) |
| 39 | : queue_(queue), event_loop_(event_loop) {} |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 40 | ~SimulatedSender() {} |
| 41 | |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame^] | 42 | aos::Message *GetMessage() override { |
| 43 | return RefCountedBuffer(queue_->size()).release(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 44 | } |
| 45 | |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame^] | 46 | void Free(aos::Message *msg) override { RefCountedBuffer tmp(msg); } |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 47 | |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame^] | 48 | bool Send(aos::Message *msg) override { |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 49 | { |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame^] | 50 | if (msg->sent_time == monotonic_clock::min_time) { |
| 51 | msg->sent_time = event_loop_->monotonic_now(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame^] | 54 | queue_->Send(RefCountedBuffer(msg)); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 55 | return true; // Maybe false instead? :) |
| 56 | } |
| 57 | |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 58 | const char *name() const override { return queue_->name(); } |
| 59 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 60 | private: |
| 61 | SimulatedQueue *queue_; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 62 | EventLoop *event_loop_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 63 | }; |
| 64 | } // namespace |
| 65 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 66 | class SimulatedTimerHandler : public TimerHandler { |
| 67 | public: |
| 68 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
| 69 | ::std::function<void()> fn) |
| 70 | : scheduler_(scheduler), fn_(fn) {} |
| 71 | ~SimulatedTimerHandler() {} |
| 72 | |
| 73 | void Setup(monotonic_clock::time_point base, |
| 74 | monotonic_clock::duration repeat_offset) override { |
| 75 | Disable(); |
| 76 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 77 | scheduler_->monotonic_now(); |
| 78 | base_ = base; |
| 79 | repeat_offset_ = repeat_offset; |
| 80 | if (base < monotonic_now) { |
| 81 | token_ = scheduler_->Schedule(monotonic_now, [this]() { HandleEvent(); }); |
| 82 | } else { |
| 83 | token_ = scheduler_->Schedule(base, [this]() { HandleEvent(); }); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void HandleEvent() { |
| 88 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 89 | scheduler_->monotonic_now(); |
| 90 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 91 | // Reschedule. |
| 92 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
| 93 | token_ = scheduler_->Schedule(base_, [this]() { HandleEvent(); }); |
| 94 | } else { |
| 95 | token_ = EventScheduler::Token(); |
| 96 | } |
| 97 | fn_(); |
| 98 | } |
| 99 | |
| 100 | void Disable() override { |
| 101 | if (token_ != EventScheduler::Token()) { |
| 102 | scheduler_->Deschedule(token_); |
| 103 | token_ = EventScheduler::Token(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | EventScheduler *scheduler_; |
| 109 | EventScheduler::Token token_; |
| 110 | // Function to be run on the thread |
| 111 | ::std::function<void()> fn_; |
| 112 | monotonic_clock::time_point base_; |
| 113 | monotonic_clock::duration repeat_offset_; |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | class SimulatedEventLoop : public EventLoop { |
| 118 | public: |
| 119 | explicit SimulatedEventLoop( |
| 120 | EventScheduler *scheduler, |
| 121 | ::std::map<::std::pair<::std::string, QueueTypeInfo>, SimulatedQueue> |
| 122 | *queues) |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 123 | : scheduler_(scheduler), queues_(queues) { |
| 124 | scheduler_->AddRawEventLoop(this); |
| 125 | } |
| 126 | ~SimulatedEventLoop() override { scheduler_->RemoveRawEventLoop(this); }; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 127 | |
| 128 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 129 | return scheduler_->monotonic_now(); |
| 130 | } |
| 131 | |
| 132 | ::std::unique_ptr<RawSender> MakeRawSender( |
| 133 | const ::std::string &path, const QueueTypeInfo &type) override; |
| 134 | |
| 135 | ::std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 136 | const ::std::string &path, const QueueTypeInfo &type) override; |
| 137 | |
| 138 | void MakeRawWatcher( |
| 139 | const ::std::string &path, const QueueTypeInfo &type, |
| 140 | ::std::function<void(const ::aos::Message *message)> watcher) override; |
| 141 | |
| 142 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
| 143 | timers_.emplace_back(new SimulatedTimerHandler(scheduler_, callback)); |
| 144 | return timers_.back().get(); |
| 145 | } |
| 146 | |
| 147 | void OnRun(::std::function<void()> on_run) override { |
| 148 | scheduler_->Schedule(scheduler_->monotonic_now(), on_run); |
| 149 | } |
| 150 | void Run() override { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 151 | LOG(FATAL, "Run from the factory instead\n"); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 152 | scheduler_->Run(); |
| 153 | } |
| 154 | void Exit() override { |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 155 | scheduler_->Exit(); |
| 156 | } |
| 157 | |
| 158 | SimulatedQueue *GetSimulatedQueue( |
| 159 | const ::std::pair<::std::string, QueueTypeInfo> &); |
| 160 | |
| 161 | void Take(const ::std::string &path); |
| 162 | |
| 163 | private: |
| 164 | EventScheduler *scheduler_; |
| 165 | ::std::map<::std::pair<::std::string, QueueTypeInfo>, SimulatedQueue> |
| 166 | *queues_; |
| 167 | ::std::vector<std::string> taken_; |
| 168 | ::std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 169 | }; |
| 170 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 171 | EventScheduler::Token EventScheduler::Schedule( |
| 172 | ::aos::monotonic_clock::time_point time, ::std::function<void()> callback) { |
| 173 | return events_list_.emplace(time, callback); |
| 174 | } |
| 175 | |
| 176 | void EventScheduler::Deschedule(EventScheduler::Token token) { |
| 177 | events_list_.erase(token); |
| 178 | } |
| 179 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 180 | void EventScheduler::RunFor(monotonic_clock::duration duration) { |
| 181 | const ::aos::monotonic_clock::time_point end_time = |
| 182 | monotonic_now() + duration; |
| 183 | for (RawEventLoop *event_loop : raw_event_loops_) { |
| 184 | event_loop->set_is_running(true); |
| 185 | } |
| 186 | is_running_ = true; |
| 187 | while (!events_list_.empty() && is_running_) { |
| 188 | auto iter = events_list_.begin(); |
| 189 | ::aos::monotonic_clock::time_point next_time = iter->first; |
| 190 | if (next_time > end_time) { |
| 191 | break; |
| 192 | } |
| 193 | now_ = iter->first; |
| 194 | ::std::function<void()> callback = ::std::move(iter->second); |
| 195 | events_list_.erase(iter); |
| 196 | callback(); |
| 197 | } |
| 198 | now_ = end_time; |
| 199 | if (!is_running_) { |
| 200 | for (RawEventLoop *event_loop : raw_event_loops_) { |
| 201 | event_loop->set_is_running(false); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 206 | void EventScheduler::Run() { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 207 | for (RawEventLoop *event_loop : raw_event_loops_) { |
| 208 | event_loop->set_is_running(true); |
| 209 | } |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 210 | is_running_ = true; |
| 211 | while (!events_list_.empty() && is_running_) { |
| 212 | auto iter = events_list_.begin(); |
| 213 | now_ = iter->first; |
| 214 | ::std::function<void()> callback = ::std::move(iter->second); |
| 215 | events_list_.erase(iter); |
| 216 | callback(); |
| 217 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 218 | if (!is_running_) { |
| 219 | for (RawEventLoop *event_loop : raw_event_loops_) { |
| 220 | event_loop->set_is_running(false); |
| 221 | } |
| 222 | } |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void SimulatedEventLoop::MakeRawWatcher( |
| 226 | const std::string &path, const QueueTypeInfo &type, |
| 227 | std::function<void(const aos::Message *message)> watcher) { |
| 228 | Take(path); |
| 229 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
| 230 | GetSimulatedQueue(key)->MakeRawWatcher(watcher); |
| 231 | } |
| 232 | |
| 233 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 234 | const std::string &path, const QueueTypeInfo &type) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 235 | Take(path); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 236 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 237 | return GetSimulatedQueue(key)->MakeRawSender(this); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 241 | const std::string &path, const QueueTypeInfo &type) { |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 242 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
| 243 | return GetSimulatedQueue(key)->MakeRawFetcher(); |
| 244 | } |
| 245 | |
| 246 | SimulatedQueue *SimulatedEventLoop::GetSimulatedQueue( |
| 247 | const ::std::pair<::std::string, QueueTypeInfo> &type) { |
| 248 | auto it = queues_->find(type); |
| 249 | if (it == queues_->end()) { |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 250 | it = |
| 251 | queues_ |
| 252 | ->emplace(type, SimulatedQueue(type.second, type.first, scheduler_)) |
| 253 | .first; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 254 | } |
| 255 | return &it->second; |
| 256 | } |
| 257 | |
| 258 | void SimulatedQueue::MakeRawWatcher( |
| 259 | std::function<void(const aos::Message *message)> watcher) { |
| 260 | watchers_.push_back(watcher); |
| 261 | } |
| 262 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 263 | std::unique_ptr<RawSender> SimulatedQueue::MakeRawSender( |
| 264 | EventLoop *event_loop) { |
| 265 | return std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | std::unique_ptr<RawFetcher> SimulatedQueue::MakeRawFetcher() { |
| 269 | return std::unique_ptr<RawFetcher>(new SimulatedFetcher(this)); |
| 270 | } |
| 271 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 272 | void SimulatedEventLoop::Take(const ::std::string &path) { |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 273 | if (is_running()) { |
| 274 | ::aos::Die("Cannot add new objects while running.\n"); |
| 275 | } |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 276 | const auto prior = ::std::find(taken_.begin(), taken_.end(), path); |
| 277 | if (prior != taken_.end()) { |
| 278 | ::aos::Die("%s is already being used.", path.c_str()); |
| 279 | } else { |
| 280 | taken_.emplace_back(path); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 283 | |
| 284 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop() { |
| 285 | return ::std::unique_ptr<EventLoop>( |
| 286 | new SimulatedEventLoop(&scheduler_, &queues_)); |
| 287 | } |
| 288 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 289 | } // namespace aos |