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 <babeltrace2/lib-logging-internal.h>
26 #include <babeltrace2/assert-internal.h>
27 #include <babeltrace2/assert-pre-internal.h>
28 #include <babeltrace2/object-internal.h>
29 #include <babeltrace2/graph/component-class-sink.h>
30 #include <babeltrace2/graph/self-component-sink.h>
31 #include <babeltrace2/graph/self-component-port.h>
32 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
33 #include <babeltrace2/graph/self-component.h>
34 #include <babeltrace2/graph/component-class-sink-colander-internal.h>
38 struct bt_component_class_sink
*colander_comp_cls
;
41 enum bt_self_component_status
colander_init(
42 struct bt_self_component_sink
*self_comp
,
43 const struct bt_value
*params
, void *init_method_data
)
45 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
46 struct bt_component_class_sink_colander_priv_data
*colander_data
= NULL
;
47 struct bt_component_class_sink_colander_data
*user_provided_data
=
50 if (!init_method_data
) {
51 BT_LOGW_STR("Component initialization method data is NULL.");
52 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
56 colander_data
= g_new0(
57 struct bt_component_class_sink_colander_priv_data
, 1);
59 BT_LOGE_STR("Failed to allocate colander data.");
60 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
64 colander_data
->msgs
= user_provided_data
->msgs
;
65 colander_data
->count_addr
= user_provided_data
->count_addr
;
66 status
= bt_self_component_sink_add_input_port(self_comp
, "in",
68 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
69 BT_LOGE_STR("Cannot add input port.");
73 bt_self_component_set_data(
74 bt_self_component_sink_as_self_component(self_comp
),
82 void colander_finalize(struct bt_self_component_sink
*self_comp
)
84 struct bt_component_class_sink_colander_priv_data
*colander_data
=
85 bt_self_component_get_data(
86 bt_self_component_sink_as_self_component(self_comp
));
92 BT_OBJECT_PUT_REF_AND_RESET(colander_data
->msg_iter
);
93 g_free(colander_data
);
97 enum bt_self_component_status
colander_graph_is_configured(
98 bt_self_component_sink
*self_comp
)
100 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
101 struct bt_component_class_sink_colander_priv_data
*colander_data
=
102 bt_self_component_get_data(
103 bt_self_component_sink_as_self_component(self_comp
));
105 struct bt_self_component_port_input
*self_port
=
106 bt_self_component_sink_borrow_input_port_by_name(self_comp
, "in");
107 BT_ASSERT(self_port
);
109 BT_ASSERT(colander_data
);
110 BT_OBJECT_PUT_REF_AND_RESET(colander_data
->msg_iter
);
111 colander_data
->msg_iter
=
112 bt_self_component_port_input_message_iterator_create(
114 if (!colander_data
->msg_iter
) {
115 BT_LIB_LOGE("Cannot create message iterator on "
116 "self component input port: %![port-]+p",
118 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
127 enum bt_self_component_status
colander_consume(
128 struct bt_self_component_sink
*self_comp
)
130 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
131 enum bt_message_iterator_status msg_iter_status
;
132 struct bt_component_class_sink_colander_priv_data
*colander_data
=
133 bt_self_component_get_data(
134 bt_self_component_sink_as_self_component(self_comp
));
135 bt_message_array_const msgs
;
137 BT_ASSERT(colander_data
);
139 if (!colander_data
->msg_iter
) {
140 BT_LIB_LOGW("Trying to consume without an "
141 "upstream message iterator: %![comp-]+c",
147 bt_self_component_port_input_message_iterator_next(
148 colander_data
->msg_iter
, &msgs
,
149 colander_data
->count_addr
);
150 switch (msg_iter_status
) {
151 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
152 status
= BT_SELF_COMPONENT_STATUS_AGAIN
;
154 case BT_MESSAGE_ITERATOR_STATUS_END
:
155 status
= BT_SELF_COMPONENT_STATUS_END
;
157 case BT_MESSAGE_ITERATOR_STATUS_OK
:
158 /* Move messages to user (count already set) */
159 memcpy(colander_data
->msgs
, msgs
,
160 sizeof(*msgs
) * *colander_data
->count_addr
);
163 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
171 struct bt_component_class_sink
*bt_component_class_sink_colander_get(void)
173 if (colander_comp_cls
) {
177 colander_comp_cls
= bt_component_class_sink_create("colander",
179 if (!colander_comp_cls
) {
180 BT_LOGE_STR("Cannot create sink colander component class.");
184 (void) bt_component_class_sink_set_init_method(
185 colander_comp_cls
, colander_init
);
186 (void) bt_component_class_sink_set_finalize_method(
187 colander_comp_cls
, colander_finalize
);
188 (void) bt_component_class_sink_set_graph_is_configured_method(
189 colander_comp_cls
, colander_graph_is_configured
);
192 bt_object_get_ref(colander_comp_cls
);
193 return (void *) colander_comp_cls
;
196 __attribute__((destructor
)) static
197 void put_colander(void) {
198 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls
);