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/object.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-connection-notification-iterator.h>
33 #include <babeltrace/graph/private-component.h>
34 #include <babeltrace/graph/component-class-sink-colander-internal.h>
35 #include <babeltrace/assert-internal.h>
39 struct bt_component_class
*colander_comp_cls
;
41 struct colander_data
{
42 bt_notification_array notifs
;
44 struct bt_notification_iterator
*notif_iter
;
48 enum bt_component_status
colander_init(
49 struct bt_private_component
*priv_comp
,
50 struct bt_value
*params
, void *init_method_data
)
52 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
53 struct colander_data
*colander_data
= NULL
;
54 struct bt_component_class_sink_colander_data
*user_provided_data
=
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
->notifs
= user_provided_data
->notifs
;
71 colander_data
->count_addr
= user_provided_data
->count_addr
;
72 status
= bt_private_component_sink_add_input_private_port(
73 priv_comp
, "in", NULL
, NULL
);
74 if (status
!= BT_COMPONENT_STATUS_OK
) {
75 BT_LOGE_STR("Cannot add input port.");
79 (void) bt_private_component_set_user_data(priv_comp
, colander_data
);
86 void colander_finalize(struct bt_private_component
*priv_comp
)
88 struct colander_data
*colander_data
=
89 bt_private_component_get_user_data(priv_comp
);
95 if (colander_data
->notif_iter
) {
96 bt_object_put_ref(colander_data
->notif_iter
);
99 g_free(colander_data
);
103 enum bt_component_status
colander_port_connected(struct bt_private_component
*priv_comp
,
104 struct bt_private_port
*self_priv_port
,
105 struct bt_port
*other_port
)
107 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
108 enum bt_connection_status conn_status
;
109 struct bt_private_connection
*priv_conn
=
110 bt_private_port_get_private_connection(self_priv_port
);
111 struct colander_data
*colander_data
=
112 bt_private_component_get_user_data(priv_comp
);
114 BT_ASSERT(priv_conn
);
115 BT_ASSERT(colander_data
);
116 BT_OBJECT_PUT_REF_AND_RESET(colander_data
->notif_iter
);
117 conn_status
= bt_private_connection_create_notification_iterator(
118 priv_conn
, &colander_data
->notif_iter
);
120 BT_LOGE("Cannot create notification iterator from connection: "
121 "comp-addr=%p, conn-addr=%p", priv_comp
, priv_conn
);
122 status
= BT_COMPONENT_STATUS_ERROR
;
127 bt_object_put_ref(priv_conn
);
132 enum bt_component_status
colander_consume(
133 struct bt_private_component
*priv_comp
)
135 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
136 enum bt_notification_iterator_status notif_iter_status
;
137 struct colander_data
*colander_data
=
138 bt_private_component_get_user_data(priv_comp
);
139 bt_notification_array notifs
;
141 BT_ASSERT(colander_data
);
143 if (!colander_data
->notif_iter
) {
144 BT_LOGW("Trying to consume without an upstream notification iterator: "
145 "comp-addr=%p", priv_comp
);
149 notif_iter_status
= bt_private_connection_notification_iterator_next(
150 colander_data
->notif_iter
, ¬ifs
, colander_data
->count_addr
);
151 switch (notif_iter_status
) {
152 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
:
153 status
= BT_COMPONENT_STATUS_OK
;
155 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
156 status
= BT_COMPONENT_STATUS_AGAIN
;
158 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
159 status
= BT_COMPONENT_STATUS_END
;
161 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
162 /* Move notifications to user (count already set) */
163 memcpy(colander_data
->notifs
, notifs
,
164 sizeof(*notifs
) * *colander_data
->count_addr
);
167 status
= BT_COMPONENT_STATUS_ERROR
;
172 /* Move notification to user's pointer, even if NULL. */
176 struct bt_component_class
*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_set_init_method(colander_comp_cls
,
191 (void) bt_component_class_set_finalize_method(colander_comp_cls
,
193 (void) bt_component_class_set_port_connected_method(colander_comp_cls
,
194 colander_port_connected
);
195 (void) bt_component_class_freeze(colander_comp_cls
);
198 return bt_object_get_ref(colander_comp_cls
);
201 __attribute__((destructor
)) static
202 void put_colander(void) {
203 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls
);