2 * Copyright 2017 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/graph/component-class-sink.h>
27 #include <babeltrace/graph/private-component-sink.h>
28 #include <babeltrace/graph/private-port.h>
29 #include <babeltrace/graph/private-connection.h>
34 struct bt_component_class
*colander_comp_cls
;
36 struct colander_data
{
37 struct bt_notification
**user_notif
;
38 struct bt_notification_iterator
*notif_iter
;
42 enum bt_component_status
colander_init(
43 struct bt_private_component
*priv_comp
,
44 struct bt_value
*params
, void *init_method_data
)
46 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
47 struct colander_data
*colander_data
= NULL
;
49 if (!init_method_data
) {
50 BT_LOGW_STR("Component initialization method data is NULL.");
51 status
= BT_COMPONENT_STATUS_INVALID
;
55 colander_data
= g_new0(struct colander_data
, 1);
57 BT_LOGE_STR("Failed to allocate colander data.");
58 status
= BT_COMPONENT_STATUS_NOMEM
;
62 colander_data
->user_notif
= init_method_data
;
63 status
= bt_private_component_sink_add_input_private_port(
64 priv_comp
, "in", NULL
, NULL
);
65 if (status
!= BT_COMPONENT_STATUS_OK
) {
66 BT_LOGE_STR("Cannot add input port.");
70 (void) bt_private_component_set_user_data(priv_comp
, colander_data
);
77 void colander_finalize(struct bt_private_component
*priv_comp
)
79 struct colander_data
*colander_data
=
80 bt_private_component_get_user_data(priv_comp
);
86 if (colander_data
->notif_iter
) {
87 bt_put(colander_data
->notif_iter
);
90 g_free(colander_data
);
94 void colander_port_connected(struct bt_private_component
*priv_comp
,
95 struct bt_private_port
*self_priv_port
,
96 struct bt_port
*other_port
)
98 enum bt_connection_status conn_status
;
99 struct bt_private_connection
*priv_conn
=
100 bt_private_port_get_private_connection(self_priv_port
);
101 struct colander_data
*colander_data
=
102 bt_private_component_get_user_data(priv_comp
);
105 assert(colander_data
);
106 BT_PUT(colander_data
->notif_iter
);
107 conn_status
= bt_private_connection_create_notification_iterator(
108 priv_conn
, NULL
, &colander_data
->notif_iter
);
110 BT_LOGE("Cannot create notification iterator from connection: "
111 "comp-addr=%p, conn-addr=%p", priv_comp
, priv_conn
);
120 enum bt_component_status
colander_consume(
121 struct bt_private_component
*priv_comp
)
123 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
124 enum bt_notification_iterator_status notif_iter_status
;
125 struct bt_notification
*notif
= NULL
;
126 struct colander_data
*colander_data
=
127 bt_private_component_get_user_data(priv_comp
);
129 assert(colander_data
);
131 if (!colander_data
->notif_iter
) {
132 BT_LOGW("Trying to consume without an upstream notification iterator: "
133 "comp-addr=%p", priv_comp
);
137 notif_iter_status
= bt_notification_iterator_next(
138 colander_data
->notif_iter
);
139 switch (notif_iter_status
) {
140 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
:
141 status
= BT_COMPONENT_STATUS_OK
;
143 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
144 status
= BT_COMPONENT_STATUS_AGAIN
;
146 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
147 status
= BT_COMPONENT_STATUS_END
;
149 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
152 status
= BT_COMPONENT_STATUS_ERROR
;
156 notif
= bt_notification_iterator_get_notification(
157 colander_data
->notif_iter
);
161 /* Move notification to user's pointer, even if NULL. */
162 *colander_data
->user_notif
= notif
;
166 struct bt_component_class
*bt_component_class_sink_colander_get(void)
168 if (colander_comp_cls
) {
172 colander_comp_cls
= bt_component_class_sink_create("colander",
174 if (!colander_comp_cls
) {
175 BT_LOGE_STR("Cannot create sink colander component class.");
179 (void) bt_component_class_set_init_method(colander_comp_cls
,
181 (void) bt_component_class_set_finalize_method(colander_comp_cls
,
183 (void) bt_component_class_set_port_connected_method(colander_comp_cls
,
184 colander_port_connected
);
185 (void) bt_component_class_freeze(colander_comp_cls
);
188 return bt_get(colander_comp_cls
);
191 __attribute__((destructor
)) static
192 void put_colander(void) {
193 BT_PUT(colander_comp_cls
);