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