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