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>
30 #include <babeltrace/graph/component-class-sink-colander-internal.h>
35 struct bt_component_class
*colander_comp_cls
;
37 struct colander_data
{
38 struct bt_notification
**user_notif
;
39 enum bt_notification_type
*notif_types
;
40 struct bt_notification_iterator
*notif_iter
;
44 enum bt_component_status
colander_init(
45 struct bt_private_component
*priv_comp
,
46 struct bt_value
*params
, void *init_method_data
)
48 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
49 struct colander_data
*colander_data
= NULL
;
50 struct bt_component_class_sink_colander_data
*user_provided_data
=
52 const enum bt_notification_type
*notif_type
;
54 if (!init_method_data
) {
55 BT_LOGW_STR("Component initialization method data is NULL.");
56 status
= BT_COMPONENT_STATUS_INVALID
;
60 colander_data
= g_new0(struct colander_data
, 1);
62 BT_LOGE_STR("Failed to allocate colander data.");
63 status
= BT_COMPONENT_STATUS_NOMEM
;
67 colander_data
->user_notif
= user_provided_data
->notification
;
69 if (user_provided_data
->notification_types
) {
70 notif_type
= user_provided_data
->notification_types
;
73 while (*notif_type
!= BT_NOTIFICATION_TYPE_SENTINEL
) {
77 count
= notif_type
- user_provided_data
->notification_types
+ 1;
79 colander_data
->notif_types
=
80 g_new0(enum bt_notification_type
, count
);
81 if (!colander_data
->notif_types
) {
82 BT_LOGE_STR("Failed to allocate an array of notification types.");
83 status
= BT_COMPONENT_STATUS_NOMEM
;
87 memcpy(colander_data
->notif_types
,
88 user_provided_data
->notification_types
,
89 count
* sizeof(enum bt_notification_type
));
92 status
= bt_private_component_sink_add_input_private_port(
93 priv_comp
, "in", NULL
, NULL
);
94 if (status
!= BT_COMPONENT_STATUS_OK
) {
95 BT_LOGE_STR("Cannot add input port.");
99 (void) bt_private_component_set_user_data(priv_comp
, colander_data
);
106 void colander_finalize(struct bt_private_component
*priv_comp
)
108 struct colander_data
*colander_data
=
109 bt_private_component_get_user_data(priv_comp
);
111 if (!colander_data
) {
115 if (colander_data
->notif_iter
) {
116 bt_put(colander_data
->notif_iter
);
119 g_free(colander_data
->notif_types
);
120 g_free(colander_data
);
124 void colander_port_connected(struct bt_private_component
*priv_comp
,
125 struct bt_private_port
*self_priv_port
,
126 struct bt_port
*other_port
)
128 enum bt_connection_status conn_status
;
129 struct bt_private_connection
*priv_conn
=
130 bt_private_port_get_private_connection(self_priv_port
);
131 struct colander_data
*colander_data
=
132 bt_private_component_get_user_data(priv_comp
);
135 assert(colander_data
);
136 BT_PUT(colander_data
->notif_iter
);
137 conn_status
= bt_private_connection_create_notification_iterator(
138 priv_conn
, colander_data
->notif_types
,
139 &colander_data
->notif_iter
);
141 BT_LOGE("Cannot create notification iterator from connection: "
142 "comp-addr=%p, conn-addr=%p", priv_comp
, priv_conn
);
151 enum bt_component_status
colander_consume(
152 struct bt_private_component
*priv_comp
)
154 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
155 enum bt_notification_iterator_status notif_iter_status
;
156 struct bt_notification
*notif
= NULL
;
157 struct colander_data
*colander_data
=
158 bt_private_component_get_user_data(priv_comp
);
160 assert(colander_data
);
162 if (!colander_data
->notif_iter
) {
163 BT_LOGW("Trying to consume without an upstream notification iterator: "
164 "comp-addr=%p", priv_comp
);
168 notif_iter_status
= bt_notification_iterator_next(
169 colander_data
->notif_iter
);
170 switch (notif_iter_status
) {
171 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
:
172 status
= BT_COMPONENT_STATUS_OK
;
174 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
175 status
= BT_COMPONENT_STATUS_AGAIN
;
177 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
178 status
= BT_COMPONENT_STATUS_END
;
180 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
183 status
= BT_COMPONENT_STATUS_ERROR
;
187 notif
= bt_notification_iterator_get_notification(
188 colander_data
->notif_iter
);
192 /* Move notification to user's pointer, even if NULL. */
193 *colander_data
->user_notif
= notif
;
197 struct bt_component_class
*bt_component_class_sink_colander_get(void)
199 if (colander_comp_cls
) {
203 colander_comp_cls
= bt_component_class_sink_create("colander",
205 if (!colander_comp_cls
) {
206 BT_LOGE_STR("Cannot create sink colander component class.");
210 (void) bt_component_class_set_init_method(colander_comp_cls
,
212 (void) bt_component_class_set_finalize_method(colander_comp_cls
,
214 (void) bt_component_class_set_port_connected_method(colander_comp_cls
,
215 colander_port_connected
);
216 (void) bt_component_class_freeze(colander_comp_cls
);
219 return bt_get(colander_comp_cls
);
222 __attribute__((destructor
)) static
223 void put_colander(void) {
224 BT_PUT(colander_comp_cls
);