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/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
->initializing
= true;
99 component
->destroy
= destroy
;
104 enum bt_component_class_type
bt_component_get_class_type(
105 struct bt_component
*component
)
107 return component
? component
->class->type
: BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
111 struct bt_notification_iterator
*bt_component_create_iterator(
112 struct bt_component
*component
, void *init_method_data
)
114 enum bt_notification_iterator_status ret_iterator
;
115 enum bt_component_class_type type
;
116 struct bt_notification_iterator
*iterator
= NULL
;
117 struct bt_component_class
*class = component
->class;
123 type
= bt_component_get_class_type(component
);
124 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
125 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
126 /* Unsupported operation. */
130 iterator
= bt_notification_iterator_create(component
);
136 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
138 struct bt_component_class_source
*source_class
;
139 enum bt_notification_iterator_status status
;
141 source_class
= container_of(class, struct bt_component_class_source
, parent
);
143 if (source_class
->methods
.iterator
.init
) {
144 status
= source_class
->methods
.iterator
.init(component
,
145 iterator
, init_method_data
);
152 case BT_COMPONENT_CLASS_TYPE_FILTER
:
154 struct bt_component_class_filter
*filter_class
;
155 enum bt_notification_iterator_status status
;
157 filter_class
= container_of(class, struct bt_component_class_filter
, parent
);
159 if (filter_class
->methods
.iterator
.init
) {
160 status
= filter_class
->methods
.iterator
.init(component
,
161 iterator
, init_method_data
);
173 ret_iterator
= bt_notification_iterator_validate(iterator
);
174 if (ret_iterator
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
184 struct bt_component
*bt_component_create_with_init_method_data(
185 struct bt_component_class
*component_class
, const char *name
,
186 struct bt_value
*params
, void *init_method_data
)
189 struct bt_component
*component
= NULL
;
190 enum bt_component_class_type type
;
192 if (!component_class
) {
196 type
= bt_component_class_get_type(component_class
);
197 if (type
<= BT_COMPONENT_CLASS_TYPE_UNKNOWN
||
198 type
> BT_COMPONENT_CLASS_TYPE_FILTER
) {
202 component
= component_create_funcs
[type
](component_class
, params
);
207 bt_object_init(component
, bt_component_destroy
);
208 component
->name
= g_string_new(name
);
209 if (!component
->name
) {
214 if (component_class
->methods
.init
) {
215 ret
= component_class
->methods
.init(component
, params
,
217 component
->initializing
= false;
218 if (ret
!= BT_COMPONENT_STATUS_OK
) {
224 component
->initializing
= false;
225 ret
= component_validation_funcs
[type
](component
);
226 if (ret
!= BT_COMPONENT_STATUS_OK
) {
231 bt_component_class_freeze(component
->class);
236 struct bt_component
*bt_component_create(
237 struct bt_component_class
*component_class
, const char *name
,
238 struct bt_value
*params
)
240 return bt_component_create_with_init_method_data(component_class
, name
,
244 const char *bt_component_get_name(struct bt_component
*component
)
246 const char *ret
= NULL
;
252 ret
= component
->name
->len
== 0 ? NULL
: component
->name
->str
;
257 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
260 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
262 if (!component
|| !name
|| name
[0] == '\0') {
263 ret
= BT_COMPONENT_STATUS_INVALID
;
267 g_string_assign(component
->name
, name
);
272 struct bt_component_class
*bt_component_get_class(
273 struct bt_component
*component
)
275 return component
? bt_get(component
->class) : NULL
;
278 void *bt_component_get_private_data(struct bt_component
*component
)
280 return component
? component
->user_data
: NULL
;
283 enum bt_component_status
284 bt_component_set_private_data(struct bt_component
*component
,
287 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
289 if (!component
|| !component
->initializing
) {
290 ret
= BT_COMPONENT_STATUS_INVALID
;
294 component
->user_data
= data
;
300 void bt_component_set_graph(struct bt_component
*component
,
301 struct bt_graph
*graph
)
303 struct bt_object
*parent
= bt_object_get_parent(&component
->base
);
305 assert(!parent
|| parent
== &graph
->base
);
307 bt_object_set_parent(component
, &graph
->base
);
312 struct bt_graph
*bt_component_get_graph(
313 struct bt_component
*component
)
315 return (struct bt_graph
*) bt_object_get_parent(&component
->base
);
319 int bt_component_init_input_ports(struct bt_component
*component
,
320 GPtrArray
**input_ports
)
323 struct bt_port
*default_port
;
325 *input_ports
= g_ptr_array_new_with_free_func(bt_object_release
);
331 default_port
= bt_component_add_port(component
, *input_ports
,
332 BT_PORT_TYPE_INPUT
, DEFAULT_INPUT_PORT_NAME
);
337 bt_put(default_port
);
343 int bt_component_init_output_ports(struct bt_component
*component
,
344 GPtrArray
**output_ports
)
347 struct bt_port
*default_port
;
349 *output_ports
= g_ptr_array_new_with_free_func(bt_object_release
);
350 if (!*output_ports
) {
355 default_port
= bt_component_add_port(component
, *output_ports
,
356 BT_PORT_TYPE_OUTPUT
, DEFAULT_OUTPUT_PORT_NAME
);
361 bt_put(default_port
);
367 struct bt_port
*bt_component_get_port(GPtrArray
*ports
, const char *name
)
370 struct bt_port
*ret_port
= NULL
;
372 for (i
= 0; i
< ports
->len
; i
++) {
373 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
374 const char *port_name
= bt_port_get_name(port
);
380 if (!strcmp(name
, port_name
)) {
381 ret_port
= bt_get(port
);
390 struct bt_port
*bt_component_get_port_at_index(GPtrArray
*ports
, int index
)
392 struct bt_port
*port
= NULL
;
394 if (index
< 0 || index
>= ports
->len
) {
398 port
= bt_get(g_ptr_array_index(ports
, index
));
404 struct bt_port
*bt_component_add_port(
405 struct bt_component
*component
,GPtrArray
*ports
,
406 enum bt_port_type port_type
, const char *name
)
409 struct bt_port
*new_port
= NULL
;
411 if (!component
->initializing
|| !name
|| *name
== '\0') {
415 /* Look for a port having the same name. */
416 for (i
= 0; i
< ports
->len
; i
++) {
417 const char *port_name
;
418 struct bt_port
*port
= g_ptr_array_index(
421 port_name
= bt_port_get_name(port
);
426 if (!strcmp(name
, port_name
)) {
427 /* Port name clash, abort. */
432 new_port
= bt_port_create(component
, port_type
, name
);
438 * No name clash, add the port.
439 * The component is now the port's parent; it should _not_
440 * hold a reference to the port since the port's lifetime
441 * is now protected by the component's own lifetime.
443 g_ptr_array_add(ports
, new_port
);
449 enum bt_component_status
bt_component_remove_port(
450 struct bt_component
*component
, GPtrArray
*ports
,
454 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
456 if (!component
->initializing
|| !name
) {
457 status
= BT_COMPONENT_STATUS_INVALID
;
461 for (i
= 0; i
< ports
->len
; i
++) {
462 const char *port_name
;
463 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
465 port_name
= bt_port_get_name(port
);
470 if (!strcmp(name
, port_name
)) {
471 g_ptr_array_remove_index(ports
, i
);
475 status
= BT_COMPONENT_STATUS_NOT_FOUND
;
481 enum bt_component_status
bt_component_new_connection(
482 struct bt_component
*component
, struct bt_port
*own_port
,
483 struct bt_connection
*connection
)
485 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
487 if (component
->class->methods
.new_connection_method
) {
488 status
= component
->class->methods
.new_connection_method(
489 own_port
, connection
);