Commit | Line | Data |
---|---|---|
de713ce0 JG |
1 | /* |
2 | * component.c | |
3 | * | |
4 | * Babeltrace Plugin Component | |
5 | * | |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
47e5a032 | 29 | #include <babeltrace/plugin/component.h> |
de713ce0 | 30 | #include <babeltrace/plugin/component-internal.h> |
38b48196 | 31 | #include <babeltrace/plugin/source-internal.h> |
5645cd95 JG |
32 | #include <babeltrace/plugin/filter-internal.h> |
33 | #include <babeltrace/plugin/notification/iterator-internal.h> | |
38b48196 | 34 | #include <babeltrace/plugin/sink-internal.h> |
de713ce0 JG |
35 | #include <babeltrace/babeltrace-internal.h> |
36 | #include <babeltrace/compiler.h> | |
b8a06801 | 37 | #include <babeltrace/ref.h> |
de713ce0 | 38 | |
7c7c0433 JG |
39 | static |
40 | struct bt_component * (* const component_create_funcs[])( | |
41 | struct bt_component_class *, struct bt_value *) = { | |
42 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_create, | |
43 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_create, | |
44 | }; | |
45 | ||
46 | static | |
47 | enum bt_component_status (* const component_validation_funcs[])( | |
48 | struct bt_component *) = { | |
49 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate, | |
50 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate, | |
51 | }; | |
52 | ||
b8a06801 JG |
53 | static |
54 | void bt_component_destroy(struct bt_object *obj) | |
55 | { | |
56 | struct bt_component *component = NULL; | |
57 | struct bt_component_class *component_class = NULL; | |
58 | ||
59 | if (!obj) { | |
60 | return; | |
61 | } | |
62 | ||
63 | component = container_of(obj, struct bt_component, base); | |
7c7c0433 JG |
64 | component_class = component->class; |
65 | ||
66 | /* | |
b8a06801 JG |
67 | * User data is destroyed first, followed by the concrete component |
68 | * instance. | |
69 | */ | |
7c7c0433 | 70 | if (component->user_destroy) { |
760051fa | 71 | component->user_destroy(component); |
7c7c0433 | 72 | } |
b8a06801 | 73 | |
ab09f844 JG |
74 | if (component->destroy) { |
75 | component->destroy(component); | |
76 | } | |
77 | ||
7c7c0433 | 78 | g_string_free(component->name, TRUE); |
b8a06801 | 79 | bt_put(component_class); |
7c7c0433 | 80 | g_free(component); |
b8a06801 JG |
81 | } |
82 | ||
83 | BT_HIDDEN | |
84 | enum bt_component_status bt_component_init(struct bt_component *component, | |
b8a06801 JG |
85 | bt_component_destroy_cb destroy) |
86 | { | |
87 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
88 | ||
ab09f844 | 89 | if (!component) { |
30d619df | 90 | ret = BT_COMPONENT_STATUS_INVALID; |
b8a06801 JG |
91 | goto end; |
92 | } | |
93 | ||
b8a06801 JG |
94 | component->destroy = destroy; |
95 | end: | |
96 | return ret; | |
97 | } | |
de713ce0 | 98 | |
38b48196 JG |
99 | BT_HIDDEN |
100 | enum bt_component_type bt_component_get_type(struct bt_component *component) | |
101 | { | |
102 | return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN; | |
103 | } | |
104 | ||
5645cd95 JG |
105 | BT_HIDDEN |
106 | struct bt_notification_iterator *bt_component_create_iterator( | |
107 | struct bt_component *component) | |
108 | { | |
109 | enum bt_notification_iterator_status ret_iterator; | |
110 | enum bt_component_type component_type; | |
111 | struct bt_notification_iterator *iterator = NULL; | |
112 | ||
113 | if (!component) { | |
114 | goto error; | |
115 | } | |
116 | ||
117 | component_type = bt_component_get_type(component); | |
118 | if (component_type != BT_COMPONENT_TYPE_SOURCE && | |
119 | component_type != BT_COMPONENT_TYPE_FILTER) { | |
120 | /* Unsupported operation. */ | |
121 | goto error; | |
122 | } | |
123 | ||
124 | iterator = bt_notification_iterator_create(component); | |
125 | if (!iterator) { | |
126 | goto error; | |
127 | } | |
128 | ||
129 | switch (component_type) { | |
130 | case BT_COMPONENT_TYPE_SOURCE: | |
131 | { | |
132 | struct bt_component_source *source; | |
133 | enum bt_component_status ret_component; | |
134 | ||
135 | source = container_of(component, struct bt_component_source, parent); | |
136 | assert(source->init_iterator); | |
137 | ret_component = source->init_iterator(component, iterator); | |
138 | if (ret_component != BT_COMPONENT_STATUS_OK) { | |
139 | goto error; | |
140 | } | |
141 | break; | |
142 | ||
143 | break; | |
144 | } | |
145 | case BT_COMPONENT_TYPE_FILTER: | |
146 | { | |
147 | struct bt_component_filter *filter; | |
148 | enum bt_component_status ret_component; | |
149 | ||
150 | filter = container_of(component, struct bt_component_filter, parent); | |
151 | assert(filter->init_iterator); | |
152 | ret_component = filter->init_iterator(component, iterator); | |
153 | if (ret_component != BT_COMPONENT_STATUS_OK) { | |
154 | goto error; | |
155 | } | |
156 | break; | |
157 | } | |
158 | default: | |
159 | /* Unreachable. */ | |
160 | assert(0); | |
161 | } | |
162 | ||
163 | ret_iterator = bt_notification_iterator_validate(iterator); | |
164 | if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
165 | goto error; | |
166 | } | |
167 | ||
168 | return iterator; | |
169 | error: | |
170 | BT_PUT(iterator); | |
171 | return iterator; | |
172 | } | |
173 | ||
38b48196 | 174 | struct bt_component *bt_component_create( |
7c7c0433 JG |
175 | struct bt_component_class *component_class, const char *name, |
176 | struct bt_value *params) | |
38b48196 | 177 | { |
7c7c0433 | 178 | int ret; |
38b48196 | 179 | struct bt_component *component = NULL; |
7c7c0433 | 180 | enum bt_component_type type; |
38b48196 JG |
181 | |
182 | if (!component_class) { | |
183 | goto end; | |
184 | } | |
185 | ||
7c7c0433 JG |
186 | type = bt_component_class_get_type(component_class); |
187 | if (type <= BT_COMPONENT_TYPE_UNKNOWN || | |
188 | type >= BT_COMPONENT_TYPE_FILTER) { | |
189 | /* Filter components are not supported yet. */ | |
190 | goto end; | |
191 | } | |
192 | ||
193 | component = component_create_funcs[type](component_class, params); | |
194 | if (!component) { | |
195 | goto end; | |
196 | } | |
197 | ||
198 | bt_object_init(component, bt_component_destroy); | |
7c7c0433 | 199 | component->name = g_string_new(name); |
4b70dd83 | 200 | if (!component->name) { |
7c7c0433 JG |
201 | BT_PUT(component); |
202 | goto end; | |
203 | } | |
204 | ||
fec2a9f2 | 205 | component->initializing = true; |
7c7c0433 | 206 | component_class->init(component, params); |
fec2a9f2 | 207 | component->initializing = false; |
7c7c0433 | 208 | ret = component_validation_funcs[type](component); |
692b38d2 | 209 | if (ret != BT_COMPONENT_STATUS_OK) { |
7c7c0433 | 210 | BT_PUT(component); |
38b48196 JG |
211 | goto end; |
212 | } | |
213 | end: | |
214 | return component; | |
215 | } | |
216 | ||
de713ce0 JG |
217 | const char *bt_component_get_name(struct bt_component *component) |
218 | { | |
219 | const char *ret = NULL; | |
220 | ||
221 | if (!component) { | |
222 | goto end; | |
223 | } | |
224 | ||
225 | ret = component->name->str; | |
226 | end: | |
227 | return ret; | |
228 | } | |
229 | ||
230 | enum bt_component_status bt_component_set_name(struct bt_component *component, | |
231 | const char *name) | |
232 | { | |
233 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
234 | ||
235 | if (!component || !name || name[0] == '\0') { | |
30d619df | 236 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
237 | goto end; |
238 | } | |
239 | ||
240 | g_string_assign(component->name, name); | |
241 | end: | |
242 | return ret; | |
243 | } | |
244 | ||
38b48196 JG |
245 | struct bt_component_class *bt_component_get_class( |
246 | struct bt_component *component) | |
de713ce0 | 247 | { |
38b48196 | 248 | return component ? bt_get(component->class) : NULL; |
de713ce0 JG |
249 | } |
250 | ||
de713ce0 JG |
251 | void *bt_component_get_private_data(struct bt_component *component) |
252 | { | |
38b48196 | 253 | return component ? component->user_data : NULL; |
de713ce0 JG |
254 | } |
255 | ||
256 | enum bt_component_status | |
257 | bt_component_set_private_data(struct bt_component *component, | |
258 | void *data) | |
259 | { | |
260 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
261 | ||
fec2a9f2 | 262 | if (!component || !component->initializing) { |
30d619df | 263 | ret = BT_COMPONENT_STATUS_INVALID; |
de713ce0 JG |
264 | goto end; |
265 | } | |
266 | ||
267 | component->user_data = data; | |
268 | end: | |
269 | return ret; | |
270 | } | |
42d7dce5 JG |
271 | |
272 | enum bt_component_status bt_component_set_destroy_cb( | |
273 | struct bt_component *component, bt_component_destroy_cb destroy) | |
274 | { | |
275 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
276 | ||
fec2a9f2 | 277 | if (!component || !component->initializing) { |
42d7dce5 JG |
278 | ret = BT_COMPONENT_STATUS_INVALID; |
279 | goto end; | |
280 | } | |
281 | ||
282 | component->user_destroy = destroy; | |
283 | end: | |
284 | return ret; | |
285 | } |