Trimmer: Filter-out packet that are not in the specified range
[babeltrace.git] / lib / plugin-system / component.c
CommitLineData
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
39static
40struct 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,
11b27444 44 [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_create,
7c7c0433
JG
45};
46
47static
48enum bt_component_status (* const component_validation_funcs[])(
49 struct bt_component *) = {
50 [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate,
51 [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate,
11b27444 52 [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_validate,
7c7c0433
JG
53};
54
b8a06801
JG
55static
56void bt_component_destroy(struct bt_object *obj)
57{
58 struct bt_component *component = NULL;
59 struct bt_component_class *component_class = NULL;
60
61 if (!obj) {
62 return;
63 }
64
65 component = container_of(obj, struct bt_component, base);
7c7c0433
JG
66 component_class = component->class;
67
68 /*
b8a06801
JG
69 * User data is destroyed first, followed by the concrete component
70 * instance.
71 */
7c7c0433 72 if (component->user_destroy) {
760051fa 73 component->user_destroy(component);
7c7c0433 74 }
b8a06801 75
ab09f844
JG
76 if (component->destroy) {
77 component->destroy(component);
78 }
79
7c7c0433 80 g_string_free(component->name, TRUE);
b8a06801 81 bt_put(component_class);
7c7c0433 82 g_free(component);
b8a06801
JG
83}
84
85BT_HIDDEN
86enum bt_component_status bt_component_init(struct bt_component *component,
b8a06801
JG
87 bt_component_destroy_cb destroy)
88{
89 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
90
ab09f844 91 if (!component) {
30d619df 92 ret = BT_COMPONENT_STATUS_INVALID;
b8a06801
JG
93 goto end;
94 }
95
b8a06801
JG
96 component->destroy = destroy;
97end:
98 return ret;
99}
de713ce0 100
38b48196
JG
101BT_HIDDEN
102enum bt_component_type bt_component_get_type(struct bt_component *component)
103{
104 return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN;
105}
106
5645cd95
JG
107BT_HIDDEN
108struct bt_notification_iterator *bt_component_create_iterator(
109 struct bt_component *component)
110{
111 enum bt_notification_iterator_status ret_iterator;
112 enum bt_component_type component_type;
113 struct bt_notification_iterator *iterator = NULL;
114
115 if (!component) {
116 goto error;
117 }
118
119 component_type = bt_component_get_type(component);
120 if (component_type != BT_COMPONENT_TYPE_SOURCE &&
121 component_type != BT_COMPONENT_TYPE_FILTER) {
122 /* Unsupported operation. */
123 goto error;
124 }
125
126 iterator = bt_notification_iterator_create(component);
127 if (!iterator) {
128 goto error;
129 }
130
131 switch (component_type) {
132 case BT_COMPONENT_TYPE_SOURCE:
133 {
134 struct bt_component_source *source;
135 enum bt_component_status ret_component;
136
137 source = container_of(component, struct bt_component_source, parent);
138 assert(source->init_iterator);
139 ret_component = source->init_iterator(component, iterator);
140 if (ret_component != BT_COMPONENT_STATUS_OK) {
141 goto error;
142 }
143 break;
144
145 break;
146 }
147 case BT_COMPONENT_TYPE_FILTER:
148 {
149 struct bt_component_filter *filter;
150 enum bt_component_status ret_component;
151
152 filter = container_of(component, struct bt_component_filter, parent);
153 assert(filter->init_iterator);
154 ret_component = filter->init_iterator(component, iterator);
155 if (ret_component != BT_COMPONENT_STATUS_OK) {
156 goto error;
157 }
158 break;
159 }
160 default:
161 /* Unreachable. */
162 assert(0);
163 }
164
165 ret_iterator = bt_notification_iterator_validate(iterator);
166 if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
167 goto error;
168 }
169
170 return iterator;
171error:
172 BT_PUT(iterator);
173 return iterator;
174}
175
38b48196 176struct bt_component *bt_component_create(
7c7c0433
JG
177 struct bt_component_class *component_class, const char *name,
178 struct bt_value *params)
38b48196 179{
7c7c0433 180 int ret;
38b48196 181 struct bt_component *component = NULL;
7c7c0433 182 enum bt_component_type type;
38b48196
JG
183
184 if (!component_class) {
185 goto end;
186 }
187
7c7c0433
JG
188 type = bt_component_class_get_type(component_class);
189 if (type <= BT_COMPONENT_TYPE_UNKNOWN ||
11b27444 190 type > BT_COMPONENT_TYPE_FILTER) {
7c7c0433
JG
191 goto end;
192 }
193
194 component = component_create_funcs[type](component_class, params);
195 if (!component) {
196 goto end;
197 }
198
199 bt_object_init(component, bt_component_destroy);
7c7c0433 200 component->name = g_string_new(name);
4b70dd83 201 if (!component->name) {
7c7c0433
JG
202 BT_PUT(component);
203 goto end;
204 }
205
fec2a9f2 206 component->initializing = true;
7c7c0433 207 component_class->init(component, params);
fec2a9f2 208 component->initializing = false;
7c7c0433 209 ret = component_validation_funcs[type](component);
692b38d2 210 if (ret != BT_COMPONENT_STATUS_OK) {
7c7c0433 211 BT_PUT(component);
38b48196
JG
212 goto end;
213 }
214end:
215 return component;
216}
217
de713ce0
JG
218const char *bt_component_get_name(struct bt_component *component)
219{
220 const char *ret = NULL;
221
222 if (!component) {
223 goto end;
224 }
225
226 ret = component->name->str;
227end:
228 return ret;
229}
230
231enum bt_component_status bt_component_set_name(struct bt_component *component,
232 const char *name)
233{
234 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
235
236 if (!component || !name || name[0] == '\0') {
30d619df 237 ret = BT_COMPONENT_STATUS_INVALID;
de713ce0
JG
238 goto end;
239 }
240
241 g_string_assign(component->name, name);
242end:
243 return ret;
244}
245
38b48196
JG
246struct bt_component_class *bt_component_get_class(
247 struct bt_component *component)
de713ce0 248{
38b48196 249 return component ? bt_get(component->class) : NULL;
de713ce0
JG
250}
251
de713ce0
JG
252void *bt_component_get_private_data(struct bt_component *component)
253{
38b48196 254 return component ? component->user_data : NULL;
de713ce0
JG
255}
256
257enum bt_component_status
258bt_component_set_private_data(struct bt_component *component,
259 void *data)
260{
261 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
262
fec2a9f2 263 if (!component || !component->initializing) {
30d619df 264 ret = BT_COMPONENT_STATUS_INVALID;
de713ce0
JG
265 goto end;
266 }
267
268 component->user_data = data;
269end:
270 return ret;
271}
42d7dce5
JG
272
273enum bt_component_status bt_component_set_destroy_cb(
274 struct bt_component *component, bt_component_destroy_cb destroy)
275{
276 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
277
fec2a9f2 278 if (!component || !component->initializing) {
42d7dce5
JG
279 ret = BT_COMPONENT_STATUS_INVALID;
280 goto end;
281 }
282
283 component->user_destroy = destroy;
284end:
285 return ret;
286}
This page took 0.036254 seconds and 4 git commands to generate.