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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/queue.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 6 | |
| 7 | namespace aos { |
| 8 | namespace { |
| 9 | class SimulatedFetcher : public RawFetcher { |
| 10 | public: |
| 11 | explicit SimulatedFetcher(SimulatedQueue *queue) : queue_(queue) {} |
| 12 | ~SimulatedFetcher() {} |
| 13 | |
| 14 | bool Fetch() override { |
| 15 | if (index_ == queue_->index()) return false; |
| 16 | |
| 17 | // Fetched message is newer |
| 18 | msg_ = queue_->latest_message(); |
| 19 | index_ = queue_->index(); |
| 20 | set_most_recent(reinterpret_cast<FetchValue *>(msg_.get())); |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | private: |
| 25 | int64_t index_ = -1; |
| 26 | SimulatedQueue *queue_; |
| 27 | RefCountedBuffer msg_; |
| 28 | }; |
| 29 | |
| 30 | class SimulatedSender : public RawSender { |
| 31 | public: |
| 32 | SimulatedSender(SimulatedQueue *queue) : queue_(queue) {} |
| 33 | ~SimulatedSender() {} |
| 34 | |
| 35 | SendContext *GetContext() override { |
| 36 | return reinterpret_cast<SendContext *>( |
| 37 | RefCountedBuffer(queue_->size()).release()); |
| 38 | } |
| 39 | |
| 40 | void Free(SendContext *context) override { |
| 41 | RefCountedBuffer(reinterpret_cast<aos::Message *>(context)); |
| 42 | } |
| 43 | |
| 44 | bool Send(SendContext *context) override { |
| 45 | queue_->Send(RefCountedBuffer(reinterpret_cast<aos::Message *>(context))); |
| 46 | return true; // Maybe false instead? :) |
| 47 | } |
| 48 | |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 49 | const char *name() const override { return queue_->name(); } |
| 50 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 51 | private: |
| 52 | SimulatedQueue *queue_; |
| 53 | }; |
| 54 | } // namespace |
| 55 | |
| 56 | EventScheduler::Token EventScheduler::Schedule( |
| 57 | ::aos::monotonic_clock::time_point time, ::std::function<void()> callback) { |
| 58 | return events_list_.emplace(time, callback); |
| 59 | } |
| 60 | |
| 61 | void EventScheduler::Deschedule(EventScheduler::Token token) { |
| 62 | events_list_.erase(token); |
| 63 | } |
| 64 | |
| 65 | void EventScheduler::Run() { |
| 66 | is_running_ = true; |
| 67 | while (!events_list_.empty() && is_running_) { |
| 68 | auto iter = events_list_.begin(); |
| 69 | now_ = iter->first; |
| 70 | ::std::function<void()> callback = ::std::move(iter->second); |
| 71 | events_list_.erase(iter); |
| 72 | callback(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void SimulatedEventLoop::MakeRawWatcher( |
| 77 | const std::string &path, const QueueTypeInfo &type, |
| 78 | std::function<void(const aos::Message *message)> watcher) { |
| 79 | Take(path); |
| 80 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
| 81 | GetSimulatedQueue(key)->MakeRawWatcher(watcher); |
| 82 | } |
| 83 | |
| 84 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 85 | const std::string &path, const QueueTypeInfo &type) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame^] | 86 | Take(path); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 87 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
| 88 | return GetSimulatedQueue(key)->MakeRawSender(); |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 92 | const std::string &path, const QueueTypeInfo &type) { |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 93 | ::std::pair<::std::string, QueueTypeInfo> key(path, type); |
| 94 | return GetSimulatedQueue(key)->MakeRawFetcher(); |
| 95 | } |
| 96 | |
| 97 | SimulatedQueue *SimulatedEventLoop::GetSimulatedQueue( |
| 98 | const ::std::pair<::std::string, QueueTypeInfo> &type) { |
| 99 | auto it = queues_->find(type); |
| 100 | if (it == queues_->end()) { |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 101 | it = |
| 102 | queues_ |
| 103 | ->emplace(type, SimulatedQueue(type.second, type.first, scheduler_)) |
| 104 | .first; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 105 | } |
| 106 | return &it->second; |
| 107 | } |
| 108 | |
| 109 | void SimulatedQueue::MakeRawWatcher( |
| 110 | std::function<void(const aos::Message *message)> watcher) { |
| 111 | watchers_.push_back(watcher); |
| 112 | } |
| 113 | |
| 114 | std::unique_ptr<RawSender> SimulatedQueue::MakeRawSender() { |
| 115 | return std::unique_ptr<RawSender>(new SimulatedSender(this)); |
| 116 | } |
| 117 | |
| 118 | std::unique_ptr<RawFetcher> SimulatedQueue::MakeRawFetcher() { |
| 119 | return std::unique_ptr<RawFetcher>(new SimulatedFetcher(this)); |
| 120 | } |
| 121 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame^] | 122 | void SimulatedEventLoop::Take(const ::std::string &path) { |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 123 | if (is_running()) { |
| 124 | ::aos::Die("Cannot add new objects while running.\n"); |
| 125 | } |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame^] | 126 | const auto prior = ::std::find(taken_.begin(), taken_.end(), path); |
| 127 | if (prior != taken_.end()) { |
| 128 | ::aos::Die("%s is already being used.", path.c_str()); |
| 129 | } else { |
| 130 | taken_.emplace_back(path); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } // namespace aos |