4 * Babeltrace Plugin Component
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/component/component.h>
30 #include <babeltrace/component/component-internal.h>
31 #include <babeltrace/component/component-class-internal.h>
32 #include <babeltrace/component/component-source-internal.h>
33 #include <babeltrace/component/component-filter-internal.h>
34 #include <babeltrace/component/component-sink-internal.h>
35 #include <babeltrace/component/component-graph-internal.h>
36 #include <babeltrace/component/notification/iterator-internal.h>
37 #include <babeltrace/babeltrace-internal.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/ref.h>
42 struct bt_component
* (* const component_create_funcs
[])(
43 struct bt_component_class
*, struct bt_value
*) = {
44 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
45 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
46 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
50 enum bt_component_status (* const component_validation_funcs
[])(
51 struct bt_component
*) = {
52 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_validate
,
53 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_validate
,
54 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_validate
,
58 void bt_component_destroy(struct bt_object
*obj
)
60 struct bt_component
*component
= NULL
;
61 struct bt_component_class
*component_class
= NULL
;
67 component
= container_of(obj
, struct bt_component
, base
);
68 component_class
= component
->class;
71 * User data is destroyed first, followed by the concrete component
74 if (component
->class->methods
.destroy
) {
75 component
->class->methods
.destroy(component
);
78 if (component
->destroy
) {
79 component
->destroy(component
);
82 g_string_free(component
->name
, TRUE
);
83 bt_put(component_class
);
88 enum bt_component_status
bt_component_init(struct bt_component
*component
,
89 bt_component_class_destroy_method destroy
)
91 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
94 ret
= BT_COMPONENT_STATUS_INVALID
;
98 component
->destroy
= destroy
;
103 enum bt_component_class_type
bt_component_get_class_type(
104 struct bt_component
*component
)
106 return component
? component
->class->type
: BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
110 struct bt_notification_iterator
*bt_component_create_iterator(
111 struct bt_component
*component
, void *init_method_data
)
113 enum bt_notification_iterator_status ret_iterator
;
114 enum bt_component_class_type type
;
115 struct bt_notification_iterator
*iterator
= NULL
;
116 struct bt_component_class
*class = component
->class;
122 type
= bt_component_get_class_type(component
);
123 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
124 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
125 /* Unsupported operation. */
129 iterator
= bt_notification_iterator_create(component
);
135 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
137 struct bt_component_class_source
*source_class
;
138 enum bt_notification_iterator_status status
;
140 source_class
= container_of(class, struct bt_component_class_source
, parent
);
142 if (source_class
->methods
.iterator
.init
) {
143 status
= source_class
->methods
.iterator
.init(component
,
144 iterator
, init_method_data
);
151 case BT_COMPONENT_CLASS_TYPE_FILTER
:
153 struct bt_component_class_filter
*filter_class
;
154 enum bt_notification_iterator_status status
;
156 filter_class
= container_of(class, struct bt_component_class_filter
, parent
);
158 if (filter_class
->methods
.iterator
.init
) {
159 status
= filter_class
->methods
.iterator
.init(component
,
160 iterator
, init_method_data
);
172 ret_iterator
= bt_notification_iterator_validate(iterator
);
173 if (ret_iterator
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
183 struct bt_component
*bt_component_create_with_init_method_data(
184 struct bt_component_class
*component_class
, const char *name
,
185 struct bt_value
*params
, void *init_method_data
)
188 struct bt_component
*component
= NULL
;
189 enum bt_component_class_type type
;
191 if (!component_class
) {
195 type
= bt_component_class_get_type(component_class
);
196 if (type
<= BT_COMPONENT_CLASS_TYPE_UNKNOWN
||
197 type
> BT_COMPONENT_CLASS_TYPE_FILTER
) {
201 component
= component_create_funcs
[type
](component_class
, params
);
206 bt_object_init(component
, bt_component_destroy
);
207 component
->name
= g_string_new(name
);
208 if (!component
->name
) {
213 component
->initializing
= true;
215 if (component_class
->methods
.init
) {
216 ret
= component_class
->methods
.init(component
, params
,
218 component
->initializing
= false;
219 if (ret
!= BT_COMPONENT_STATUS_OK
) {
225 component
->initializing
= false;
226 ret
= component_validation_funcs
[type
](component
);
227 if (ret
!= BT_COMPONENT_STATUS_OK
) {
232 bt_component_class_freeze(component
->class);
237 struct bt_component
*bt_component_create(
238 struct bt_component_class
*component_class
, const char *name
,
239 struct bt_value
*params
)
241 return bt_component_create_with_init_method_data(component_class
, name
,
245 const char *bt_component_get_name(struct bt_component
*component
)
247 const char *ret
= NULL
;
253 ret
= component
->name
->len
== 0 ? NULL
: component
->name
->str
;
258 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
261 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
263 if (!component
|| !name
|| name
[0] == '\0') {
264 ret
= BT_COMPONENT_STATUS_INVALID
;
268 g_string_assign(component
->name
, name
);
273 struct bt_component_class
*bt_component_get_class(
274 struct bt_component
*component
)
276 return component
? bt_get(component
->class) : NULL
;
279 void *bt_component_get_private_data(struct bt_component
*component
)
281 return component
? component
->user_data
: NULL
;
284 enum bt_component_status
285 bt_component_set_private_data(struct bt_component
*component
,
288 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
290 if (!component
|| !component
->initializing
) {
291 ret
= BT_COMPONENT_STATUS_INVALID
;
295 component
->user_data
= data
;
301 enum bt_component_status
bt_component_set_graph(struct bt_component
*component
,
302 struct bt_graph
*graph
)
304 bt_object_set_parent(component
, &graph
->base
);
305 return BT_COMPONENT_STATUS_OK
;
308 struct bt_graph
*bt_component_get_graph(
309 struct bt_component
*component
)
311 return (struct bt_graph
*) bt_object_get_parent(&component
->base
);
315 int bt_component_init_input_ports(struct bt_component
*component
,
316 GPtrArray
**input_ports
)
319 struct bt_port
*default_port
;
321 *input_ports
= g_ptr_array_new_with_free_func(bt_object_release
);
327 default_port
= bt_port_create(component
, BT_PORT_TYPE_INPUT
,
328 DEFAULT_INPUT_PORT_NAME
);
334 g_ptr_array_add(*input_ports
, default_port
);
340 int bt_component_init_output_ports(struct bt_component
*component
,
341 GPtrArray
**output_ports
)
344 struct bt_port
*default_port
;
346 *output_ports
= g_ptr_array_new_with_free_func(bt_object_release
);
352 default_port
= bt_port_create(component
, BT_PORT_TYPE_OUTPUT
,
353 DEFAULT_OUTPUT_PORT_NAME
);
359 g_ptr_array_add(*output_ports
, default_port
);
365 struct bt_port
*bt_component_get_port(GPtrArray
*ports
, const char *name
)
368 struct bt_port
*ret_port
= NULL
;
370 for (i
= 0; i
< ports
->len
; i
++) {
371 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
372 const char *port_name
= bt_port_get_name(port
);
378 if (!strcmp(name
, port_name
)) {
379 ret_port
= bt_get(port
);
388 struct bt_port
*bt_component_get_port_at_index(GPtrArray
*ports
, int index
)
390 struct bt_port
*port
= NULL
;
392 if (index
< 0 || index
>= ports
->len
) {
396 port
= bt_get(g_ptr_array_index(ports
, index
));
402 struct bt_port
*bt_component_add_port(
403 struct bt_component
*component
,GPtrArray
*ports
,
404 enum bt_port_type port_type
, const char *name
)
407 struct bt_port
*new_port
;
409 if (!component
->initializing
|| !name
|| *name
== '\0') {
414 new_port
= bt_port_create(component
, port_type
, name
);
419 /* Look for a port having the same name. */
420 for (i
= 0; i
< ports
->len
; i
++) {
421 const char *port_name
;
422 struct bt_port
*port
= g_ptr_array_index(
425 port_name
= bt_port_get_name(port
);
430 if (!strcmp(name
, port_name
)) {
431 /* Port name clash, abort. */
436 /* No name clash, add the port. */
437 g_ptr_array_add(ports
, bt_get(new_port
));
446 enum bt_component_status
bt_component_remove_port(
447 struct bt_component
*component
, GPtrArray
*ports
,
451 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
453 if (!component
->initializing
|| !name
) {
454 status
= BT_COMPONENT_STATUS_INVALID
;
458 for (i
= 0; i
< ports
->len
; i
++) {
459 const char *port_name
;
460 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
462 port_name
= bt_port_get_name(port
);
467 if (!strcmp(name
, port_name
)) {
468 g_ptr_array_remove_index(ports
, i
);
472 status
= BT_COMPONENT_STATUS_NOT_FOUND
;