2 * Copyright 2017-2019 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 "LIB/COMPONENT-CLASS-SINK-SIMPLE"
24 #include "lib/logging.h"
26 #include "common/assert.h"
27 #include "common/common.h"
28 #include "lib/assert-pre.h"
29 #include "lib/object.h"
30 #include <babeltrace2/graph/component-class-sink.h>
31 #include <babeltrace2/graph/self-component-sink.h>
32 #include <babeltrace2/graph/self-component-port.h>
33 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
34 #include <babeltrace2/graph/self-component.h>
37 #include "component-class-sink-simple.h"
38 #include "lib/func-status.h"
41 * We keep a single simple sink component class reference. It's created
42 * the first time bt_component_class_sink_simple_borrow() is called and
43 * put by the put_simple_sink_component_class() library destructor.
46 struct bt_component_class_sink
*simple_comp_cls
;
48 struct simple_sink_data
{
49 bt_self_component_port_input_message_iterator
*msg_iter
;
50 struct simple_sink_init_method_data init_method_data
;
54 enum bt_component_class_init_method_status
simple_sink_init(
55 struct bt_self_component_sink
*self_comp
,
56 const struct bt_value
*params
, void *init_method_data
)
58 int status
= BT_FUNC_STATUS_OK
;
59 struct simple_sink_data
*data
= NULL
;
61 data
= g_new0(struct simple_sink_data
, 1);
63 BT_LIB_LOGE_APPEND_CAUSE(
64 "Failed to allocate simple sink component private data.");
65 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
69 BT_ASSERT(init_method_data
);
70 data
->init_method_data
=
71 *((struct simple_sink_init_method_data
*) init_method_data
);
74 status
= bt_self_component_sink_add_input_port(self_comp
, "in",
76 if (status
!= BT_FUNC_STATUS_OK
) {
77 BT_LIB_LOGE_APPEND_CAUSE(
78 "Cannot add input port to simple sink component.");
82 bt_self_component_set_data(
83 bt_self_component_sink_as_self_component(self_comp
), data
);
90 void simple_sink_finalize(struct bt_self_component_sink
*self_comp
)
92 struct simple_sink_data
*data
= bt_self_component_get_data(
93 bt_self_component_sink_as_self_component(self_comp
));
96 if (data
->init_method_data
.finalize_func
) {
97 /* Call user's finalization function */
98 data
->init_method_data
.finalize_func(
99 data
->init_method_data
.user_data
);
102 BT_OBJECT_PUT_REF_AND_RESET(data
->msg_iter
);
108 enum bt_component_class_sink_graph_is_configured_method_status
109 simple_sink_graph_is_configured(
110 bt_self_component_sink
*self_comp
)
112 int status
= BT_FUNC_STATUS_OK
;
113 struct simple_sink_data
*data
= bt_self_component_get_data(
114 bt_self_component_sink_as_self_component(self_comp
));
116 struct bt_self_component_port_input
*self_port
=
117 bt_self_component_sink_borrow_input_port_by_name(self_comp
,
120 if (!bt_port_is_connected(bt_self_component_port_as_port(
121 bt_self_component_port_input_as_self_component_port(self_port
)))) {
122 BT_LIB_LOGE_APPEND_CAUSE(
123 "Simple sink component's input port is not connected: "
124 "%![comp-]+c, %![port-]+p", self_comp
, self_port
);
125 status
= BT_FUNC_STATUS_ERROR
;
131 bt_self_component_port_input_message_iterator_create_from_sink_component(
132 self_comp
, self_port
);
133 if (!data
->msg_iter
) {
134 BT_LIB_LOGE_APPEND_CAUSE(
135 "Cannot create input port message iterator: "
136 "%![comp-]+c, %![port-]+p", self_comp
, self_port
);
137 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
141 if (data
->init_method_data
.init_func
) {
142 /* Call user's initialization function */
143 status
= data
->init_method_data
.init_func(data
->msg_iter
,
144 data
->init_method_data
.user_data
);
145 if (status
!= BT_FUNC_STATUS_OK
) {
146 BT_LIB_LOGW_APPEND_CAUSE(
147 "Simple sink component's user's initialization function failed: "
148 "status=%s, %![comp-]+c, %![port-]+p",
149 bt_common_func_status_string(status
),
150 self_comp
, self_port
);
160 enum bt_component_class_sink_consume_method_status
simple_sink_consume(
161 struct bt_self_component_sink
*self_comp
)
164 struct simple_sink_data
*data
= bt_self_component_get_data(
165 bt_self_component_sink_as_self_component(self_comp
));
168 BT_ASSERT(data
->init_method_data
.consume_func
);
169 BT_ASSERT(data
->msg_iter
);
171 /* Call user's "consume" function */
172 status
= data
->init_method_data
.consume_func(data
->msg_iter
,
173 data
->init_method_data
.user_data
);
174 if (status
!= BT_FUNC_STATUS_OK
) {
175 BT_LIB_LOGW_APPEND_CAUSE(
176 "Simple sink component's user's \"consume\" function failed: "
177 "status=%s, %![comp-]+c",
178 bt_common_func_status_string(status
), self_comp
);
184 struct bt_component_class_sink
*bt_component_class_sink_simple_borrow(void)
186 enum bt_component_class_set_method_status set_method_status
;
188 if (simple_comp_cls
) {
192 simple_comp_cls
= bt_component_class_sink_create("simple-sink",
193 simple_sink_consume
);
194 if (!simple_comp_cls
) {
195 BT_LIB_LOGE_APPEND_CAUSE(
196 "Cannot create simple sink component class.");
200 set_method_status
= bt_component_class_sink_set_init_method(
201 simple_comp_cls
, simple_sink_init
);
202 BT_ASSERT(set_method_status
== BT_FUNC_STATUS_OK
);
203 set_method_status
= bt_component_class_sink_set_finalize_method(
204 simple_comp_cls
, simple_sink_finalize
);
205 BT_ASSERT(set_method_status
== BT_FUNC_STATUS_OK
);
206 set_method_status
= bt_component_class_sink_set_graph_is_configured_method(
207 simple_comp_cls
, simple_sink_graph_is_configured
);
208 BT_ASSERT(set_method_status
== BT_FUNC_STATUS_OK
);
211 return simple_comp_cls
;
214 __attribute__((destructor
)) static
215 void put_simple_sink_component_class(void) {
216 BT_OBJECT_PUT_REF_AND_RESET(simple_comp_cls
);