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