Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1 | #ifndef _AOS_EVENTS_EVENT_LOOP_H_ |
| 2 | #define _AOS_EVENTS_EVENT_LOOP_H_ |
| 3 | |
| 4 | #include <string> |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/queue.h" |
| 6 | #include "aos/time/time.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 7 | #include "aos/events/raw-event-loop.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | |
| 11 | // Fetches the newest message from a queue. |
| 12 | template <typename T> |
| 13 | class Fetcher { |
| 14 | public: |
| 15 | Fetcher() {} |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame^] | 16 | // Fetches the next message. Returns true if it fetched a new message. This |
| 17 | // method will only return messages sent after the Fetcher was created. |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 18 | bool FetchNext() { return fetcher_->FetchNext(); } |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame^] | 19 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 20 | // This will return the latest message regardless of if it was sent before or |
| 21 | // after the fetcher was created. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 22 | bool Fetch() { return fetcher_->Fetch(); } |
| 23 | |
| 24 | const T *get() const { |
| 25 | return reinterpret_cast<const T *>(fetcher_->most_recent()); |
| 26 | } |
| 27 | const T &operator*() const { return *get(); } |
| 28 | const T *operator->() const { return get(); } |
| 29 | |
| 30 | private: |
| 31 | friend class EventLoop; |
| 32 | Fetcher(std::unique_ptr<RawFetcher> fetcher) : fetcher_(std::move(fetcher)) {} |
| 33 | std::unique_ptr<RawFetcher> fetcher_; |
| 34 | }; |
| 35 | |
| 36 | // Sends messages to a queue. |
| 37 | template <typename T> |
| 38 | class Sender { |
| 39 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 40 | typedef T Type; |
| 41 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 42 | Sender() {} |
| 43 | |
| 44 | // Represents a single message about to be sent to the queue. |
| 45 | // The lifecycle goes: |
| 46 | // |
| 47 | // Message msg = sender.MakeMessage(); |
| 48 | // Populate(msg.get()); |
| 49 | // msg.Send(); |
| 50 | // |
| 51 | // Or: |
| 52 | // |
| 53 | // Message msg = sender.MakeMessage(); |
| 54 | // PopulateOrNot(msg.get()); |
| 55 | class Message { |
| 56 | public: |
| 57 | Message(RawSender *sender) |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame] | 58 | : msg_(reinterpret_cast<T *>(sender->GetMessage()), *sender) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 59 | msg_->Zero(); |
| 60 | } |
| 61 | |
| 62 | T *get() { return msg_.get(); } |
| 63 | const T *get() const { return msg_.get(); } |
| 64 | T &operator*() { return *get(); } |
| 65 | T *operator->() { return get(); } |
| 66 | const T &operator*() const { return *get(); } |
| 67 | const T *operator->() const { return get(); } |
| 68 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 69 | // Sends the message to the queue. Should only be called once. Returns true |
| 70 | // if the message was successfully sent, and false otherwise. |
| 71 | bool Send() { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 72 | RawSender *sender = &msg_.get_deleter(); |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame] | 73 | return sender->Send(msg_.release()); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::unique_ptr<T, RawSender &> msg_; |
| 78 | }; |
| 79 | |
| 80 | // Constructs an above message. |
| 81 | Message MakeMessage(); |
| 82 | |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 83 | // Returns the name of the underlying queue. |
| 84 | const char *name() const { return sender_->name(); } |
| 85 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 86 | private: |
| 87 | friend class EventLoop; |
| 88 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 89 | std::unique_ptr<RawSender> sender_; |
| 90 | }; |
| 91 | |
| 92 | // TODO(parker): Consider making EventLoop wrap a RawEventLoop rather than |
| 93 | // inheriting. |
| 94 | class EventLoop : public RawEventLoop { |
| 95 | public: |
| 96 | virtual ~EventLoop() {} |
| 97 | |
| 98 | // Current time. |
| 99 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 100 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 101 | // Note, it is supported to create: |
| 102 | // multiple fetchers, and (one sender or one watcher) per <path, type> |
| 103 | // tuple. |
| 104 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 105 | // Makes a class that will always fetch the most recent value |
| 106 | // sent to path. |
| 107 | template <typename T> |
| 108 | Fetcher<T> MakeFetcher(const std::string &path) { |
| 109 | return Fetcher<T>(MakeRawFetcher(path, QueueTypeInfo::Get<T>())); |
| 110 | } |
| 111 | |
| 112 | // Makes class that allows constructing and sending messages to |
| 113 | // address path. |
| 114 | template <typename T> |
| 115 | Sender<T> MakeSender(const std::string &path) { |
| 116 | return Sender<T>(MakeRawSender(path, QueueTypeInfo::Get<T>())); |
| 117 | } |
| 118 | |
| 119 | // Watch is a functor that have a call signature like so: |
| 120 | // void Event(const MessageType& type); |
| 121 | // |
| 122 | // This will watch messages sent to path. |
| 123 | // Note that T needs to match both send and recv side. |
Austin Schuh | a1654ed | 2019-01-27 17:24:54 -0800 | [diff] [blame] | 124 | // TODO(parker): Need to support ::std::bind. For now, use lambdas. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 125 | template <typename Watch> |
| 126 | void MakeWatcher(const std::string &path, Watch &&w); |
| 127 | |
| 128 | // The passed in function will be called when the event loop starts. |
| 129 | // Use this to run code once the thread goes into "real-time-mode", |
| 130 | virtual void OnRun(std::function<void()>) = 0; |
| 131 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 132 | // TODO(austin): Sort out how to switch to realtime on run. |
| 133 | // virtual void RunRealtime() = 0; |
| 134 | |
| 135 | // Stops receiving events |
| 136 | virtual void Exit() = 0; |
| 137 | }; |
| 138 | |
| 139 | } // namespace aos |
| 140 | |
| 141 | #include "aos/events/event-loop-tmpl.h" |
| 142 | |
| 143 | #endif // _AOS_EVENTS_EVENT_LOOP_H |