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