2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "COLANDER"
24 #include <babeltrace/lib-logging-internal.h>
26 #include <babeltrace/object.h>
27 #include <babeltrace/graph/component-class-sink.h>
28 #include <babeltrace/graph/self-component-sink.h>
29 #include <babeltrace/graph/self-component-port.h>
30 #include <babeltrace/graph/self-component-port-input-notification-iterator.h>
31 #include <babeltrace/graph/self-component.h>
32 #include <babeltrace/graph/component-class-sink-colander-internal.h>
33 #include <babeltrace/assert-internal.h>
37 struct bt_component_class_sink
*colander_comp_cls
;
39 struct colander_data
{
40 bt_notification_array_const notifs
;
42 struct bt_self_component_port_input_notification_iterator
*notif_iter
;
46 enum bt_self_component_status
colander_init(
47 struct bt_self_component_sink
*self_comp
,
48 const struct bt_value
*params
, void *init_method_data
)
50 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
51 struct colander_data
*colander_data
= NULL
;
52 struct bt_component_class_sink_colander_data
*user_provided_data
=
55 if (!init_method_data
) {
56 BT_LOGW_STR("Component initialization method data is NULL.");
57 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
61 colander_data
= g_new0(struct colander_data
, 1);
63 BT_LOGE_STR("Failed to allocate colander data.");
64 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
68 colander_data
->notifs
= user_provided_data
->notifs
;
69 colander_data
->count_addr
= user_provided_data
->count_addr
;
70 status
= bt_self_component_sink_add_input_port(self_comp
, "in",
72 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
73 BT_LOGE_STR("Cannot add input port.");
77 bt_self_component_set_data(
78 bt_self_component_sink_as_self_component(self_comp
),
86 void colander_finalize(struct bt_self_component_sink
*self_comp
)
88 struct colander_data
*colander_data
=
89 bt_self_component_get_data(
90 bt_self_component_sink_as_self_component(self_comp
));
96 BT_OBJECT_PUT_REF_AND_RESET(colander_data
->notif_iter
);
97 g_free(colander_data
);
101 enum bt_self_component_status
colander_input_port_connected(
102 struct bt_self_component_sink
*self_comp
,
103 struct bt_self_component_port_input
*self_port
,
104 const struct bt_port_output
*other_port
)
106 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
107 struct colander_data
*colander_data
=
108 bt_self_component_get_data(
109 bt_self_component_sink_as_self_component(self_comp
));
111 BT_ASSERT(colander_data
);
112 BT_OBJECT_PUT_REF_AND_RESET(colander_data
->notif_iter
);
113 colander_data
->notif_iter
=
114 bt_self_component_port_input_notification_iterator_create(
116 if (!colander_data
->notif_iter
) {
117 BT_LIB_LOGE("Cannot create notification iterator on "
118 "self component input port: %![port-]+p",
120 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
129 enum bt_self_component_status
colander_consume(
130 struct bt_self_component_sink
*self_comp
)
132 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
133 enum bt_notification_iterator_status notif_iter_status
;
134 struct colander_data
*colander_data
=
135 bt_self_component_get_data(
136 bt_self_component_sink_as_self_component(self_comp
));
137 bt_notification_array_const notifs
;
139 BT_ASSERT(colander_data
);
141 if (!colander_data
->notif_iter
) {
142 BT_LIB_LOGW("Trying to consume without an "
143 "upstream notification iterator: %![comp-]+c",
149 bt_self_component_port_input_notification_iterator_next(
150 colander_data
->notif_iter
, ¬ifs
,
151 colander_data
->count_addr
);
152 switch (notif_iter_status
) {
153 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
:
154 status
= BT_SELF_COMPONENT_STATUS_OK
;
156 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
157 status
= BT_SELF_COMPONENT_STATUS_AGAIN
;
159 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
160 status
= BT_SELF_COMPONENT_STATUS_END
;
162 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
163 /* Move notifications to user (count already set) */
164 memcpy(colander_data
->notifs
, notifs
,
165 sizeof(*notifs
) * *colander_data
->count_addr
);
168 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
176 struct bt_component_class_sink
*bt_component_class_sink_colander_get(void)
178 if (colander_comp_cls
) {
182 colander_comp_cls
= bt_component_class_sink_create("colander",
184 if (!colander_comp_cls
) {
185 BT_LOGE_STR("Cannot create sink colander component class.");
189 (void) bt_component_class_sink_set_init_method(
190 colander_comp_cls
, colander_init
);
191 (void) bt_component_class_sink_set_finalize_method(
192 colander_comp_cls
, colander_finalize
);
193 (void) bt_component_class_sink_set_input_port_connected_method(
194 colander_comp_cls
, colander_input_port_connected
);
197 bt_object_get_ref(colander_comp_cls
);
198 return (void *) colander_comp_cls
;
201 __attribute__((destructor
)) static
202 void put_colander(void) {
203 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls
);