1 #pragma once
2 #include <sdbusplus/async/context.hpp>
3 #include <sdbusplus/async/execution.hpp>
4 #include <sdbusplus/bus/match.hpp> // IWYU pragma: export
5 #include <sdbusplus/message.hpp>
6 #include <sdbusplus/slot.hpp>
7
8 #include <condition_variable>
9 #include <mutex>
10 #include <queue>
11 #include <string_view>
12
13 namespace sdbusplus::async
14 {
15 namespace match_ns
16 {
17 struct match_completion;
18 }
19
20 /** Generator of dbus match Senders.
21 *
22 * This class registers a signal match pattern with the dbus and generates
23 * Senders using `next` to await the next matching signal.
24 */
25 class match : private bus::details::bus_friend
26 {
27 public:
28 match() = delete;
29 match(const match&) = delete;
30 match& operator=(const match&) = delete;
31 match(match&&) = delete;
32 match& operator=(match&&) = delete;
33 ~match();
34
35 /** Construct the match using the `pattern` string on the bus managed by the
36 * context. */
37 match(context& ctx, const std::string_view& pattern);
38
39 /** Get the Sender for the next event (as message).
40 *
41 * Note: the implementation only supports a single awaiting task. Two
42 * tasks should not share this object and both call `next`.
43 */
44 auto next() noexcept;
45
46 /** Get the Sender for the next event, which yields one of:
47 * void, Rs, tuple<Rs...>
48 *
49 * Note: the implementation only supports a single awaiting task. Two
50 * tasks should not share this object and both call `next`.
51 */
52 template <typename... Rs>
53 auto next() noexcept;
54
55 friend match_ns::match_completion;
56
57 private:
58 sdbusplus::slot_t slot;
59
60 std::mutex lock{};
61 std::queue<sdbusplus::message_t> queue{};
62 match_ns::match_completion* complete = nullptr;
63
64 /** Handle an incoming match event. */
65 void handle_match(message_t&&) noexcept;
66
67 /** Signal completion if there is an awaiting Receiver.
68 *
69 * This must be called with `lock` held (and ownership transfers).
70 */
71 void handle_completion(std::unique_lock<std::mutex>&&) noexcept;
72
73 slot_t makeMatch(context& ctx, const std::string_view& pattern);
74 };
75
76 namespace match_ns
77 {
78 // Virtual class to handle the match Receiver completions.
79 struct match_completion
80 {
81 match_completion() = delete;
82 match_completion(match_completion&&) = delete;
83
match_completionsdbusplus::async::match_ns::match_completion84 explicit match_completion(match& m) : m(m) {};
85 virtual ~match_completion() = default;
86
87 friend match;
88
89 void start() noexcept;
90
91 private:
92 virtual void complete(message_t&&) noexcept = 0;
93 virtual void stop() noexcept = 0;
94
95 match& m;
96 };
97
98 // Implementation (templated based on Receiver) of match_completion.
99 template <execution::receiver Receiver>
100 struct match_operation : match_completion
101 {
match_operationsdbusplus::async::match_ns::match_operation102 match_operation(match& m, Receiver r) :
103 match_completion(m), receiver(std::move(r))
104 {}
105
106 private:
completesdbusplus::async::match_ns::match_operation107 void complete(message_t&& msg) noexcept override final
108 {
109 execution::set_value(std::move(receiver), std::move(msg));
110 }
111
stopsdbusplus::async::match_ns::match_operation112 void stop() noexcept override final
113 {
114 execution::set_stopped(std::move(receiver));
115 }
116
117 Receiver receiver;
118 };
119
120 // match Sender implementation.
121 struct match_sender
122 {
123 using sender_concept = execution::sender_t;
124
125 match_sender() = delete;
match_sendersdbusplus::async::match_ns::match_sender126 explicit match_sender(match& m) noexcept : m(m) {};
127
128 template <typename Self, class... Env>
129 static constexpr auto get_completion_signatures(Self&&, Env&&...)
130 -> execution::completion_signatures<execution::set_value_t(message_t),
131 execution::set_stopped_t()>;
132
133 template <execution::receiver R>
connectsdbusplus::async::match_ns::match_sender134 auto connect(R r) -> match_operation<R>
135 {
136 return {m, std::move(r)};
137 }
138
139 private:
140 match& m;
141 };
142
143 }; // namespace match_ns
144
next()145 inline auto match::next() noexcept
146 {
147 return match_ns::match_sender(*this);
148 }
149
150 template <typename... Rs>
next()151 auto match::next() noexcept
152 {
153 return match_ns::match_sender(*this) |
154 execution::then([](message_t&& m) { return m.unpack<Rs...>(); });
155 }
156
157 } // namespace sdbusplus::async
158