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/ref.h>
27 #include <babeltrace/graph/connection.h>
28 #include <babeltrace/graph/component-class-sink.h>
29 #include <babeltrace/graph/private-component-sink.h>
30 #include <babeltrace/graph/private-port.h>
31 #include <babeltrace/graph/private-connection.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/component-class-sink-colander-internal.h>
38 struct bt_component_class
*colander_comp_cls
;
40 struct colander_data
{
41 struct bt_notification
**user_notif
;
42 enum bt_notification_type
*notif_types
;
43 struct bt_notification_iterator
*notif_iter
;
47 enum bt_component_status
colander_init(
48 struct bt_private_component
*priv_comp
,
49 struct bt_value
*params
, void *init_method_data
)
51 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
52 struct colander_data
*colander_data
= NULL
;
53 struct bt_component_class_sink_colander_data
*user_provided_data
=
55 const enum bt_notification_type
*notif_type
;
57 if (!init_method_data
) {
58 BT_LOGW_STR("Component initialization method data is NULL.");
59 status
= BT_COMPONENT_STATUS_INVALID
;
63 colander_data
= g_new0(struct colander_data
, 1);
65 BT_LOGE_STR("Failed to allocate colander data.");
66 status
= BT_COMPONENT_STATUS_NOMEM
;
70 colander_data
->user_notif
= user_provided_data
->notification
;
72 if (user_provided_data
->notification_types
) {
73 notif_type
= user_provided_data
->notification_types
;
76 while (*notif_type
!= BT_NOTIFICATION_TYPE_SENTINEL
) {
80 count
= notif_type
- user_provided_data
->notification_types
+ 1;
82 colander_data
->notif_types
=
83 g_new0(enum bt_notification_type
, count
);
84 if (!colander_data
->notif_types
) {
85 BT_LOGE_STR("Failed to allocate an array of notification types.");
86 status
= BT_COMPONENT_STATUS_NOMEM
;
90 memcpy(colander_data
->notif_types
,
91 user_provided_data
->notification_types
,
92 count
* sizeof(enum bt_notification_type
));
95 status
= bt_private_component_sink_add_input_private_port(
96 priv_comp
, "in", NULL
, NULL
);
97 if (status
!= BT_COMPONENT_STATUS_OK
) {
98 BT_LOGE_STR("Cannot add input port.");
102 (void) bt_private_component_set_user_data(priv_comp
, colander_data
);
109 void colander_finalize(struct bt_private_component
*priv_comp
)
111 struct colander_data
*colander_data
=
112 bt_private_component_get_user_data(priv_comp
);
114 if (!colander_data
) {
118 if (colander_data
->notif_iter
) {
119 bt_put(colander_data
->notif_iter
);
122 g_free(colander_data
->notif_types
);
123 g_free(colander_data
);
127 void colander_port_connected(struct bt_private_component
*priv_comp
,
128 struct bt_private_port
*self_priv_port
,
129 struct bt_port
*other_port
)
131 enum bt_connection_status conn_status
;
132 struct bt_private_connection
*priv_conn
=
133 bt_private_port_get_private_connection(self_priv_port
);
134 struct colander_data
*colander_data
=
135 bt_private_component_get_user_data(priv_comp
);
138 assert(colander_data
);
139 BT_PUT(colander_data
->notif_iter
);
140 conn_status
= bt_private_connection_create_notification_iterator(
141 priv_conn
, colander_data
->notif_types
,
142 &colander_data
->notif_iter
);
144 BT_LOGE("Cannot create notification iterator from connection: "
145 "comp-addr=%p, conn-addr=%p", priv_comp
, priv_conn
);
154 enum bt_component_status
colander_consume(
155 struct bt_private_component
*priv_comp
)
157 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
158 enum bt_notification_iterator_status notif_iter_status
;
159 struct bt_notification
*notif
= NULL
;
160 struct colander_data
*colander_data
=
161 bt_private_component_get_user_data(priv_comp
);
163 assert(colander_data
);
165 if (!colander_data
->notif_iter
) {
166 BT_LOGW("Trying to consume without an upstream notification iterator: "
167 "comp-addr=%p", priv_comp
);
171 notif_iter_status
= bt_notification_iterator_next(
172 colander_data
->notif_iter
);
173 switch (notif_iter_status
) {
174 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
:
175 status
= BT_COMPONENT_STATUS_OK
;
177 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
178 status
= BT_COMPONENT_STATUS_AGAIN
;
180 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
181 status
= BT_COMPONENT_STATUS_END
;
183 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
186 status
= BT_COMPONENT_STATUS_ERROR
;
190 notif
= bt_notification_iterator_get_notification(
191 colander_data
->notif_iter
);
195 /* Move notification to user's pointer, even if NULL. */
196 *colander_data
->user_notif
= notif
;
200 struct bt_component_class
*bt_component_class_sink_colander_get(void)
202 if (colander_comp_cls
) {
206 colander_comp_cls
= bt_component_class_sink_create("colander",
208 if (!colander_comp_cls
) {
209 BT_LOGE_STR("Cannot create sink colander component class.");
213 (void) bt_component_class_set_init_method(colander_comp_cls
,
215 (void) bt_component_class_set_finalize_method(colander_comp_cls
,
217 (void) bt_component_class_set_port_connected_method(colander_comp_cls
,
218 colander_port_connected
);
219 (void) bt_component_class_freeze(colander_comp_cls
);
222 return bt_get(colander_comp_cls
);
225 __attribute__((destructor
)) static
226 void put_colander(void) {
227 BT_PUT(colander_comp_cls
);