Commit | Line | Data |
---|---|---|
484a3024 SM |
1 | /* |
2 | * SPDX-License-Identifier: GPL-2.0-only | |
3 | * | |
4 | * Copyright (C) 2020-2023 EfficiOS, inc. | |
5 | */ | |
6 | ||
7 | #include <utility> | |
8 | ||
9 | #include "run-in.hpp" | |
10 | #include "common/assert.h" | |
11 | ||
cc610c52 PP |
12 | namespace { |
13 | ||
484a3024 SM |
14 | struct RunInData final |
15 | { | |
16 | RunInCompClsQueryFunc compClsCtxFunc; | |
17 | RunInCompClsInitFunc compCtxFunc; | |
18 | RunInMsgIterClsInitFunc msgIterCtxFunc; | |
19 | }; | |
20 | ||
cc610c52 | 21 | const RunInData& runInDataFromMethodData(void * const methodData) |
484a3024 SM |
22 | { |
23 | return *static_cast<const RunInData *>(methodData); | |
24 | } | |
25 | ||
cc610c52 PP |
26 | bt_component_class_initialize_method_status compClsInit(bt_self_component_source * const selfComp, |
27 | bt_self_component_source_configuration *, | |
28 | const bt_value *, | |
29 | void * const initMethodData) | |
484a3024 SM |
30 | { |
31 | const auto status = | |
32 | bt_self_component_source_add_output_port(selfComp, "out", initMethodData, nullptr); | |
33 | ||
34 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); | |
35 | ||
36 | auto& data = runInDataFromMethodData(initMethodData); | |
37 | ||
38 | if (data.compCtxFunc) { | |
39 | data.compCtxFunc(bt_self_component_source_as_self_component(selfComp)); | |
40 | } | |
41 | ||
42 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
43 | } | |
44 | ||
cc610c52 | 45 | bt_component_class_query_method_status |
484a3024 SM |
46 | compClsQuery(bt_self_component_class_source * const selfCompCls, bt_private_query_executor *, |
47 | const char *, const bt_value *, void * const methodData, | |
48 | const bt_value ** const result) | |
49 | { | |
50 | auto& data = runInDataFromMethodData(methodData); | |
51 | ||
52 | if (data.compClsCtxFunc) { | |
53 | data.compClsCtxFunc(bt_self_component_class_source_as_self_component_class(selfCompCls)); | |
54 | } | |
55 | ||
56 | *result = bt_value_null; | |
57 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
58 | } | |
59 | ||
cc610c52 | 60 | bt_message_iterator_class_initialize_method_status |
484a3024 SM |
61 | msgIterClsInit(bt_self_message_iterator * const selfMsgIter, |
62 | bt_self_message_iterator_configuration *, bt_self_component_port_output * const port) | |
63 | { | |
64 | auto& data = runInDataFromMethodData(bt_self_component_port_get_data( | |
65 | bt_self_component_port_output_as_self_component_port(port))); | |
66 | ||
67 | if (data.msgIterCtxFunc) { | |
68 | data.msgIterCtxFunc(selfMsgIter); | |
69 | } | |
70 | ||
71 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
72 | } | |
73 | ||
cc610c52 | 74 | bt_message_iterator_class_next_method_status |
484a3024 SM |
75 | msgIterClsNext(bt_self_message_iterator *, bt_message_array_const, uint64_t, uint64_t *) |
76 | { | |
77 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
78 | } | |
79 | ||
08b4db41 SM |
80 | struct DummySinkData |
81 | { | |
82 | bt_message_iterator *msgIter; | |
83 | }; | |
84 | ||
cc610c52 PP |
85 | bt_component_class_initialize_method_status dummySinkInit(bt_self_component_sink * const self, |
86 | bt_self_component_sink_configuration *, | |
87 | const bt_value *, | |
88 | void * const initMethodData) | |
08b4db41 SM |
89 | { |
90 | const auto status = bt_self_component_sink_add_input_port(self, "in", NULL, nullptr); | |
cc610c52 | 91 | |
08b4db41 SM |
92 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); |
93 | bt_self_component_set_data(bt_self_component_sink_as_self_component(self), initMethodData); | |
94 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
95 | } | |
96 | ||
cc610c52 | 97 | DummySinkData& dummySinkDataFromSelfCompSink(bt_self_component_sink * const self) |
08b4db41 SM |
98 | { |
99 | return *static_cast<DummySinkData *>( | |
100 | bt_self_component_get_data(bt_self_component_sink_as_self_component(self))); | |
101 | } | |
102 | ||
cc610c52 | 103 | bt_component_class_sink_graph_is_configured_method_status |
0ab97251 | 104 | dummySinkGraphIsConfigured(bt_self_component_sink * const self) |
08b4db41 | 105 | { |
08b4db41 | 106 | const auto port = bt_self_component_sink_borrow_input_port_by_name(self, "in"); |
cc610c52 | 107 | |
08b4db41 | 108 | BT_ASSERT(port); |
cc610c52 PP |
109 | |
110 | const auto status = bt_message_iterator_create_from_sink_component( | |
111 | self, port, &dummySinkDataFromSelfCompSink(self).msgIter); | |
112 | ||
08b4db41 SM |
113 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK); |
114 | return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; | |
115 | } | |
116 | ||
cc610c52 | 117 | bt_component_class_sink_consume_method_status dummySinkConsume(bt_self_component_sink * const self) |
08b4db41 | 118 | { |
08b4db41 SM |
119 | bt_message_array_const msgs; |
120 | uint64_t msgCount; | |
cc610c52 PP |
121 | const auto status = |
122 | bt_message_iterator_next(dummySinkDataFromSelfCompSink(self).msgIter, &msgs, &msgCount); | |
123 | ||
08b4db41 SM |
124 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_NEXT_STATUS_END); |
125 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; | |
126 | } | |
127 | ||
cc610c52 PP |
128 | } /* namespace */ |
129 | ||
484a3024 SM |
130 | void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, |
131 | RunInMsgIterClsInitFunc msgIterCtxFunc) | |
132 | { | |
133 | RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)}; | |
134 | ||
135 | /* Create and configure custom source component class */ | |
136 | const auto msgIterCls = bt_message_iterator_class_create(msgIterClsNext); | |
137 | ||
138 | BT_ASSERT(msgIterCls); | |
139 | ||
140 | { | |
141 | const auto status = | |
142 | bt_message_iterator_class_set_initialize_method(msgIterCls, msgIterClsInit); | |
143 | ||
144 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_CLASS_SET_METHOD_STATUS_OK); | |
145 | } | |
146 | ||
147 | const auto srcCompCls = bt_component_class_source_create("yo", msgIterCls); | |
148 | ||
149 | BT_ASSERT(srcCompCls); | |
150 | ||
151 | { | |
152 | const auto status = | |
153 | bt_component_class_source_set_initialize_method(srcCompCls, compClsInit); | |
154 | ||
155 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); | |
156 | } | |
157 | ||
158 | { | |
159 | const auto status = bt_component_class_source_set_query_method(srcCompCls, compClsQuery); | |
160 | ||
161 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); | |
162 | } | |
163 | ||
164 | /* Execute a query (executes `compClsCtxFunc`) */ | |
165 | { | |
166 | const auto queryExec = bt_query_executor_create_with_method_data( | |
167 | bt_component_class_source_as_component_class(srcCompCls), "", nullptr, &data); | |
168 | ||
169 | BT_ASSERT(queryExec); | |
170 | ||
171 | const bt_value *queryRes; | |
172 | const auto status = bt_query_executor_query(queryExec, &queryRes); | |
173 | ||
174 | BT_ASSERT(status == BT_QUERY_EXECUTOR_QUERY_STATUS_OK); | |
484a3024 SM |
175 | bt_value_put_ref(queryRes); |
176 | bt_query_executor_put_ref(queryExec); | |
177 | } | |
178 | ||
08b4db41 SM |
179 | /* Create a dummy sink component */ |
180 | const auto sinkCompCls = bt_component_class_sink_create("dummy", dummySinkConsume); | |
cc610c52 | 181 | |
08b4db41 SM |
182 | BT_ASSERT(sinkCompCls); |
183 | ||
184 | { | |
185 | const auto status = | |
186 | bt_component_class_sink_set_initialize_method(sinkCompCls, dummySinkInit); | |
cc610c52 | 187 | |
08b4db41 SM |
188 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
189 | } | |
190 | ||
191 | { | |
192 | const auto status = bt_component_class_sink_set_graph_is_configured_method( | |
193 | sinkCompCls, dummySinkGraphIsConfigured); | |
cc610c52 | 194 | |
08b4db41 SM |
195 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
196 | } | |
197 | ||
484a3024 SM |
198 | /* Create graph */ |
199 | const auto graph = bt_graph_create(0); | |
200 | ||
201 | BT_ASSERT(graph); | |
202 | ||
203 | /* Add custom source component (executes `compCtxFunc`) */ | |
204 | const bt_component_source *srcComp; | |
205 | ||
206 | { | |
207 | const auto status = bt_graph_add_source_component_with_initialize_method_data( | |
08b4db41 | 208 | graph, srcCompCls, "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp); |
484a3024 SM |
209 | |
210 | BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); | |
211 | } | |
212 | ||
08b4db41 | 213 | /* Add dummy sink component */ |
484a3024 | 214 | const bt_component_sink *sinkComp; |
08b4db41 | 215 | DummySinkData dummySinkData; |
484a3024 SM |
216 | |
217 | { | |
08b4db41 SM |
218 | const auto status = bt_graph_add_sink_component_with_initialize_method_data( |
219 | graph, sinkCompCls, "the-sink", NULL, &dummySinkData, BT_LOGGING_LEVEL_NONE, &sinkComp); | |
cc610c52 | 220 | |
484a3024 SM |
221 | BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); |
222 | } | |
223 | ||
224 | /* Connect ports */ | |
225 | { | |
226 | const auto outPort = bt_component_source_borrow_output_port_by_name_const(srcComp, "out"); | |
227 | ||
228 | BT_ASSERT(outPort); | |
229 | ||
230 | const auto inPort = bt_component_sink_borrow_input_port_by_name_const(sinkComp, "in"); | |
231 | ||
232 | BT_ASSERT(inPort); | |
233 | ||
234 | const auto status = bt_graph_connect_ports(graph, outPort, inPort, nullptr); | |
235 | ||
236 | BT_ASSERT(status == BT_GRAPH_CONNECT_PORTS_STATUS_OK); | |
237 | } | |
238 | ||
239 | /* Run graph (executes `msgIterCtxFunc`) */ | |
cc610c52 PP |
240 | { |
241 | const auto status = bt_graph_run(graph); | |
484a3024 | 242 | |
cc610c52 PP |
243 | BT_ASSERT(status == BT_GRAPH_RUN_STATUS_OK); |
244 | } | |
484a3024 | 245 | |
cc610c52 | 246 | /* Discard owned objects */ |
484a3024 SM |
247 | bt_graph_put_ref(graph); |
248 | bt_component_class_source_put_ref(srcCompCls); | |
08b4db41 | 249 | bt_component_class_sink_put_ref(sinkCompCls); |
484a3024 SM |
250 | bt_message_iterator_class_put_ref(msgIterCls); |
251 | } | |
252 | ||
253 | void runInCompClsQuery(RunInCompClsQueryFunc func) | |
254 | { | |
255 | runIn(std::move(func), nullptr, nullptr); | |
256 | } | |
257 | ||
258 | void runInCompClsInit(RunInCompClsInitFunc func) | |
259 | { | |
260 | runIn(nullptr, std::move(func), nullptr); | |
261 | } | |
262 | ||
263 | void runInMsgIterClsInit(RunInMsgIterClsInitFunc func) | |
264 | { | |
265 | runIn(nullptr, nullptr, std::move(func)); | |
266 | } |