Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / component-class-sink-simple.c
CommitLineData
5efafa1a
PP
1/*
2 * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
23#define BT_LOG_TAG "LIB/COMPONENT-CLASS-SINK-SIMPLE"
24#include "lib/logging.h"
25
26#include "common/assert.h"
27#include "common/common.h"
28#include "lib/assert-pre.h"
29#include "lib/object.h"
7704a0af 30#include <babeltrace2/graph/component-class.h>
5efafa1a 31#include <babeltrace2/graph/self-component-port.h>
5efafa1a 32#include <babeltrace2/graph/self-component.h>
7704a0af 33#include <babeltrace2/graph/message-iterator.h>
5efafa1a
PP
34#include <glib.h>
35
36#include "component-class-sink-simple.h"
37#include "lib/func-status.h"
38
39/*
40 * We keep a single simple sink component class reference. It's created
41 * the first time bt_component_class_sink_simple_borrow() is called and
42 * put by the put_simple_sink_component_class() library destructor.
43 */
44static
45struct bt_component_class_sink *simple_comp_cls;
46
47struct simple_sink_data {
fbd8a4e0 48 bt_message_iterator *msg_iter;
5efafa1a
PP
49 struct simple_sink_init_method_data init_method_data;
50};
51
52static
4175c1d5 53enum bt_component_class_initialize_method_status simple_sink_init(
e3250e61
SM
54 bt_self_component_sink *self_comp,
55 bt_self_component_sink_configuration *config,
5efafa1a
PP
56 const struct bt_value *params, void *init_method_data)
57{
58 int status = BT_FUNC_STATUS_OK;
59 struct simple_sink_data *data = NULL;
60
61 data = g_new0(struct simple_sink_data, 1);
62 if (!data) {
63 BT_LIB_LOGE_APPEND_CAUSE(
64 "Failed to allocate simple sink component private data.");
65 status = BT_FUNC_STATUS_MEMORY_ERROR;
66 goto end;
67 }
68
69 BT_ASSERT(init_method_data);
70 data->init_method_data =
71 *((struct simple_sink_init_method_data *) init_method_data);
72
73 /* Add input port */
74 status = bt_self_component_sink_add_input_port(self_comp, "in",
75 NULL, NULL);
76 if (status != BT_FUNC_STATUS_OK) {
77 BT_LIB_LOGE_APPEND_CAUSE(
78 "Cannot add input port to simple sink component.");
79 goto end;
80 }
81
82 bt_self_component_set_data(
83 bt_self_component_sink_as_self_component(self_comp), data);
84
85end:
86 return status;
87}
88
89static
90void simple_sink_finalize(struct bt_self_component_sink *self_comp)
91{
92 struct simple_sink_data *data = bt_self_component_get_data(
93 bt_self_component_sink_as_self_component(self_comp));
94
95 if (data) {
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);
100 }
101
102 BT_OBJECT_PUT_REF_AND_RESET(data->msg_iter);
103 g_free(data);
104 }
105}
106
107static
108enum bt_component_class_sink_graph_is_configured_method_status
109simple_sink_graph_is_configured(
110 bt_self_component_sink *self_comp)
111{
ab8b2b1b 112 bt_component_class_sink_graph_is_configured_method_status status;
fbd8a4e0 113 bt_message_iterator_create_from_sink_component_status
ab8b2b1b 114 msg_iter_status;
5efafa1a
PP
115 struct simple_sink_data *data = bt_self_component_get_data(
116 bt_self_component_sink_as_self_component(self_comp));
117
118 struct bt_self_component_port_input *self_port =
119 bt_self_component_sink_borrow_input_port_by_name(self_comp,
120 "in");
121
122 if (!bt_port_is_connected(bt_self_component_port_as_port(
123 bt_self_component_port_input_as_self_component_port(self_port)))) {
124 BT_LIB_LOGE_APPEND_CAUSE(
125 "Simple sink component's input port is not connected: "
126 "%![comp-]+c, %![port-]+p", self_comp, self_port);
ab8b2b1b 127 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
5efafa1a
PP
128 goto end;
129 }
130
131 BT_ASSERT(data);
fbd8a4e0 132 msg_iter_status = bt_message_iterator_create_from_sink_component(
ab8b2b1b 133 self_comp, self_port, &data->msg_iter);
fbd8a4e0 134 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
5efafa1a
PP
135 BT_LIB_LOGE_APPEND_CAUSE(
136 "Cannot create input port message iterator: "
137 "%![comp-]+c, %![port-]+p", self_comp, self_port);
ab8b2b1b 138 status = (int) msg_iter_status;
5efafa1a
PP
139 goto end;
140 }
141
142 if (data->init_method_data.init_func) {
4175c1d5 143 bt_graph_simple_sink_component_initialize_func_status init_status;
ab8b2b1b 144
5efafa1a 145 /* Call user's initialization function */
ab8b2b1b 146 init_status = data->init_method_data.init_func(data->msg_iter,
5efafa1a 147 data->init_method_data.user_data);
4175c1d5 148 if (init_status != BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK) {
5efafa1a
PP
149 BT_LIB_LOGW_APPEND_CAUSE(
150 "Simple sink component's user's initialization function failed: "
151 "status=%s, %![comp-]+c, %![port-]+p",
ab8b2b1b 152 bt_common_func_status_string(init_status),
5efafa1a 153 self_comp, self_port);
ab8b2b1b 154 status = (int) init_status;
5efafa1a
PP
155 goto end;
156 }
157 }
158
ab8b2b1b
SM
159 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
160
5efafa1a
PP
161end:
162 return status;
163}
164
165static
166enum bt_component_class_sink_consume_method_status simple_sink_consume(
167 struct bt_self_component_sink *self_comp)
168{
169 int status;
170 struct simple_sink_data *data = bt_self_component_get_data(
171 bt_self_component_sink_as_self_component(self_comp));
172
ec4a3354
PP
173 BT_ASSERT_DBG(data);
174 BT_ASSERT_DBG(data->init_method_data.consume_func);
175 BT_ASSERT_DBG(data->msg_iter);
5efafa1a
PP
176
177 /* Call user's "consume" function */
178 status = data->init_method_data.consume_func(data->msg_iter,
179 data->init_method_data.user_data);
eb657c7b 180 if (status < 0) {
5efafa1a
PP
181 BT_LIB_LOGW_APPEND_CAUSE(
182 "Simple sink component's user's \"consume\" function failed: "
183 "status=%s, %![comp-]+c",
184 bt_common_func_status_string(status), self_comp);
185 }
186
187 return status;
188}
189
190struct bt_component_class_sink *bt_component_class_sink_simple_borrow(void)
191{
192 enum bt_component_class_set_method_status set_method_status;
193
7c7324d3
SM
194 BT_ASSERT_PRE_NO_ERROR();
195
5efafa1a
PP
196 if (simple_comp_cls) {
197 goto end;
198 }
199
200 simple_comp_cls = bt_component_class_sink_create("simple-sink",
201 simple_sink_consume);
202 if (!simple_comp_cls) {
203 BT_LIB_LOGE_APPEND_CAUSE(
204 "Cannot create simple sink component class.");
205 goto end;
206 }
207
4175c1d5 208 set_method_status = bt_component_class_sink_set_initialize_method(
5efafa1a
PP
209 simple_comp_cls, simple_sink_init);
210 BT_ASSERT(set_method_status == BT_FUNC_STATUS_OK);
211 set_method_status = bt_component_class_sink_set_finalize_method(
212 simple_comp_cls, simple_sink_finalize);
213 BT_ASSERT(set_method_status == BT_FUNC_STATUS_OK);
214 set_method_status = bt_component_class_sink_set_graph_is_configured_method(
215 simple_comp_cls, simple_sink_graph_is_configured);
216 BT_ASSERT(set_method_status == BT_FUNC_STATUS_OK);
217
218end:
219 return simple_comp_cls;
220}
221
222__attribute__((destructor)) static
223void put_simple_sink_component_class(void) {
224 BT_OBJECT_PUT_REF_AND_RESET(simple_comp_cls);
225}
This page took 0.045529 seconds and 4 git commands to generate.