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