954e758aa1a5b3e997d3dc8ec4b6aaa182c6c3f7
[babeltrace.git] / lib / component / sink.c
1 /*
2 * sink.c
3 *
4 * Babeltrace Sink Component
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/values.h>
31 #include <babeltrace/component/component-sink-internal.h>
32 #include <babeltrace/component/component-internal.h>
33 #include <babeltrace/component/notification/notification.h>
34
35 BT_HIDDEN
36 enum bt_component_status bt_component_sink_validate(
37 struct bt_component *component)
38 {
39 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
40 struct bt_component_sink *sink;
41
42 if (!component) {
43 ret = BT_COMPONENT_STATUS_INVALID;
44 goto end;
45 }
46
47 if (!component->class) {
48 ret = BT_COMPONENT_STATUS_INVALID;
49 goto end;
50 }
51
52 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
53 ret = BT_COMPONENT_STATUS_INVALID;
54 goto end;
55 }
56
57 sink = container_of(component, struct bt_component_sink, parent);
58 ret = component_input_validate(&sink->input);
59 if (ret) {
60 goto end;
61 }
62 end:
63 return ret;
64 }
65
66 static
67 void bt_component_sink_destroy(struct bt_component *component)
68 {
69 struct bt_component_sink *sink = container_of(component,
70 struct bt_component_sink, parent);
71
72 component_input_fini(&sink->input);
73 }
74
75 BT_HIDDEN
76 struct bt_component *bt_component_sink_create(
77 struct bt_component_class *class, struct bt_value *params)
78 {
79 struct bt_component_sink *sink = NULL;
80 enum bt_component_status ret;
81
82 sink = g_new0(struct bt_component_sink, 1);
83 if (!sink) {
84 goto end;
85 }
86
87 sink->parent.class = bt_get(class);
88 ret = bt_component_init(&sink->parent, bt_component_sink_destroy);
89 if (ret != BT_COMPONENT_STATUS_OK) {
90 goto error;
91 }
92
93 /*
94 ret = bt_component_sink_register_notification_type(&sink->parent,
95 BT_NOTIFICATION_TYPE_EVENT);
96 if (ret != BT_COMPONENT_STATUS_OK) {
97 goto error;
98 }
99 */
100 if (component_input_init(&sink->input)) {
101 goto error;
102 }
103 end:
104 return sink ? &sink->parent : NULL;
105 error:
106 BT_PUT(sink);
107 return NULL;
108 }
109
110 static
111 enum bt_component_status validate_inputs(struct bt_component_sink *sink)
112 {
113 size_t array_size = sink->input.iterators->len;
114
115 if (array_size < sink->input.min_count ||
116 array_size > sink->input.max_count) {
117 return BT_COMPONENT_STATUS_INVALID;
118 }
119
120 return BT_COMPONENT_STATUS_OK;
121 }
122
123 enum bt_component_status bt_component_sink_consume(
124 struct bt_component *component)
125 {
126 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
127 struct bt_component_sink *sink = NULL;
128 struct bt_component_class_sink *sink_class = NULL;
129
130 if (!component) {
131 ret = BT_COMPONENT_STATUS_INVALID;
132 goto end;
133 }
134
135 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
136 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
137 goto end;
138 }
139
140 sink = container_of(component, struct bt_component_sink, parent);
141 if (!sink->input.validated) {
142 ret = validate_inputs(sink);
143 if (ret != BT_COMPONENT_STATUS_OK) {
144 goto end;
145 }
146 sink->input.validated = true;
147 }
148
149 sink_class = container_of(component->class, struct bt_component_class_sink, parent);
150 assert(sink_class->methods.consume);
151 ret = sink_class->methods.consume(component);
152 end:
153 return ret;
154 }
155 /*
156 static
157 enum bt_component_status bt_component_sink_register_notification_type(
158 struct bt_component *component, enum bt_notification_type type)
159 {
160 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
161 struct bt_component_sink *sink = NULL;
162
163 if (!component) {
164 ret = BT_COMPONENT_STATUS_INVALID;
165 goto end;
166 }
167
168 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
169 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
170 goto end;
171 }
172
173 if (type <= BT_NOTIFICATION_TYPE_UNKNOWN ||
174 type >= BT_NOTIFICATION_TYPE_NR) {
175 ret = BT_COMPONENT_STATUS_INVALID;
176 goto end;
177 }
178 sink = container_of(component, struct bt_component_sink, parent);
179 if (type == BT_NOTIFICATION_TYPE_ALL) {
180 sink->registered_notifications_mask = ~(notification_mask_t) 0;
181 } else {
182 sink->registered_notifications_mask |=
183 (notification_mask_t) 1 << type;
184 }
185 end:
186 return ret;
187 }
188 */
189
190 enum bt_component_status bt_component_sink_set_minimum_input_count(
191 struct bt_component *component,
192 unsigned int minimum)
193 {
194 struct bt_component_sink *sink;
195 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
196
197 if (!component) {
198 ret = BT_COMPONENT_STATUS_INVALID;
199 goto end;
200 }
201
202 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
203 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
204 goto end;
205 }
206
207 if (!component->initializing) {
208 ret = BT_COMPONENT_STATUS_INVALID;
209 goto end;
210 }
211
212 sink = container_of(component, struct bt_component_sink, parent);
213 sink->input.min_count = minimum;
214 end:
215 return ret;
216 }
217
218 enum bt_component_status bt_component_sink_set_maximum_input_count(
219 struct bt_component *component,
220 unsigned int maximum)
221 {
222 struct bt_component_sink *sink;
223 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
224
225 if (!component) {
226 ret = BT_COMPONENT_STATUS_INVALID;
227 goto end;
228 }
229
230 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
231 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
232 goto end;
233 }
234
235 if (!component->initializing) {
236 ret = BT_COMPONENT_STATUS_INVALID;
237 goto end;
238 }
239
240 sink = container_of(component, struct bt_component_sink, parent);
241 sink->input.max_count = maximum;
242 end:
243 return ret;
244 }
245
246 enum bt_component_status
247 bt_component_sink_get_input_count(struct bt_component *component,
248 unsigned int *count)
249 {
250 struct bt_component_sink *sink;
251 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
252
253 if (!component || !count) {
254 ret = BT_COMPONENT_STATUS_INVALID;
255 goto end;
256 }
257
258 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
259 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
260 goto end;
261 }
262
263 sink = container_of(component, struct bt_component_sink, parent);
264 *count = (unsigned int) sink->input.iterators->len;
265 end:
266 return ret;
267 }
268
269 enum bt_component_status
270 bt_component_sink_get_input_iterator(struct bt_component *component,
271 unsigned int input, struct bt_notification_iterator **iterator)
272 {
273 struct bt_component_sink *sink;
274 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
275
276 if (!component || !iterator) {
277 ret = BT_COMPONENT_STATUS_INVALID;
278 goto end;
279 }
280
281 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
282 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
283 goto end;
284 }
285
286 sink = container_of(component, struct bt_component_sink, parent);
287 if (input >= (unsigned int) sink->input.iterators->len) {
288 ret = BT_COMPONENT_STATUS_INVALID;
289 goto end;
290 }
291
292 *iterator = bt_get(g_ptr_array_index(sink->input.iterators, input));
293 end:
294 return ret;
295 }
296
297 enum bt_component_status
298 bt_component_sink_add_iterator(struct bt_component *component,
299 struct bt_notification_iterator *iterator)
300 {
301 struct bt_component_sink *sink;
302 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
303 struct bt_component_class_sink *sink_class;
304
305 if (!component || !iterator) {
306 ret = BT_COMPONENT_STATUS_INVALID;
307 goto end;
308 }
309
310 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
311 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
312 goto end;
313 }
314
315 sink = container_of(component, struct bt_component_sink, parent);
316 if (sink->input.iterators->len == sink->input.max_count) {
317 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
318 goto end;
319 }
320
321 sink_class = container_of(component->class, struct bt_component_class_sink, parent);
322
323 if (sink_class->methods.add_iterator) {
324 ret = sink_class->methods.add_iterator(component, iterator);
325 if (ret != BT_COMPONENT_STATUS_OK) {
326 goto end;
327 }
328 }
329
330 g_ptr_array_add(sink->input.iterators, bt_get(iterator));
331 end:
332 return ret;
333 }
This page took 0.055539 seconds and 3 git commands to generate.