lib: rename plural file names to singular
[babeltrace.git] / lib / graph / component-class-sink-colander.c
CommitLineData
706a18f9
PP
1/*
2 * Copyright 2017 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 "COLANDER"
24#include <babeltrace/lib-logging-internal.h>
25
65300d60 26#include <babeltrace/object.h>
0d72b8c3 27#include <babeltrace/graph/component-class-sink.h>
d94d92ac
PP
28#include <babeltrace/graph/self-component-sink.h>
29#include <babeltrace/graph/self-component-port.h>
30#include <babeltrace/graph/self-component-port-input-notification-iterator.h>
31#include <babeltrace/graph/self-component.h>
8ed535b5 32#include <babeltrace/graph/component-class-sink-colander-internal.h>
f6ccaed9 33#include <babeltrace/assert-internal.h>
706a18f9 34#include <glib.h>
706a18f9
PP
35
36static
0d72b8c3 37struct bt_component_class_sink *colander_comp_cls;
706a18f9
PP
38
39struct colander_data {
0d72b8c3 40 bt_notification_array_const notifs;
d4393e08 41 uint64_t *count_addr;
d94d92ac 42 struct bt_self_component_port_input_notification_iterator *notif_iter;
706a18f9
PP
43};
44
45static
d94d92ac
PP
46enum bt_self_component_status colander_init(
47 struct bt_self_component_sink *self_comp,
05e21286 48 const struct bt_value *params, void *init_method_data)
706a18f9 49{
d94d92ac 50 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
706a18f9 51 struct colander_data *colander_data = NULL;
8ed535b5
PP
52 struct bt_component_class_sink_colander_data *user_provided_data =
53 init_method_data;
706a18f9
PP
54
55 if (!init_method_data) {
56 BT_LOGW_STR("Component initialization method data is NULL.");
d94d92ac 57 status = BT_SELF_COMPONENT_STATUS_ERROR;
706a18f9
PP
58 goto end;
59 }
60
61 colander_data = g_new0(struct colander_data, 1);
62 if (!colander_data) {
63 BT_LOGE_STR("Failed to allocate colander data.");
d94d92ac 64 status = BT_SELF_COMPONENT_STATUS_NOMEM;
706a18f9
PP
65 goto end;
66 }
67
d4393e08
PP
68 colander_data->notifs = user_provided_data->notifs;
69 colander_data->count_addr = user_provided_data->count_addr;
d94d92ac
PP
70 status = bt_self_component_sink_add_input_port(self_comp, "in",
71 NULL, NULL);
72 if (status != BT_SELF_COMPONENT_STATUS_OK) {
706a18f9
PP
73 BT_LOGE_STR("Cannot add input port.");
74 goto end;
75 }
76
d94d92ac 77 bt_self_component_set_data(
707b7d35 78 bt_self_component_sink_as_self_component(self_comp),
d94d92ac 79 colander_data);
706a18f9
PP
80
81end:
82 return status;
83}
84
85static
d94d92ac 86void colander_finalize(struct bt_self_component_sink *self_comp)
706a18f9
PP
87{
88 struct colander_data *colander_data =
d94d92ac 89 bt_self_component_get_data(
707b7d35 90 bt_self_component_sink_as_self_component(self_comp));
706a18f9
PP
91
92 if (!colander_data) {
93 return;
94 }
95
d94d92ac 96 BT_OBJECT_PUT_REF_AND_RESET(colander_data->notif_iter);
706a18f9
PP
97 g_free(colander_data);
98}
99
100static
d94d92ac
PP
101enum bt_self_component_status colander_input_port_connected(
102 struct bt_self_component_sink *self_comp,
103 struct bt_self_component_port_input *self_port,
0d72b8c3 104 const struct bt_port_output *other_port)
706a18f9 105{
d94d92ac 106 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
706a18f9 107 struct colander_data *colander_data =
d94d92ac 108 bt_self_component_get_data(
707b7d35 109 bt_self_component_sink_as_self_component(self_comp));
706a18f9 110
f6ccaed9 111 BT_ASSERT(colander_data);
65300d60 112 BT_OBJECT_PUT_REF_AND_RESET(colander_data->notif_iter);
d94d92ac
PP
113 colander_data->notif_iter =
114 bt_self_component_port_input_notification_iterator_create(
115 self_port);
116 if (!colander_data->notif_iter) {
117 BT_LIB_LOGE("Cannot create notification iterator on "
118 "self component input port: %![port-]+p",
119 self_port);
120 status = BT_SELF_COMPONENT_STATUS_NOMEM;
706a18f9
PP
121 goto end;
122 }
123
124end:
bf55043c 125 return status;
706a18f9
PP
126}
127
128static
d94d92ac
PP
129enum bt_self_component_status colander_consume(
130 struct bt_self_component_sink *self_comp)
706a18f9 131{
d94d92ac 132 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
706a18f9 133 enum bt_notification_iterator_status notif_iter_status;
706a18f9 134 struct colander_data *colander_data =
d94d92ac 135 bt_self_component_get_data(
707b7d35 136 bt_self_component_sink_as_self_component(self_comp));
0d72b8c3 137 bt_notification_array_const notifs;
706a18f9 138
f6ccaed9 139 BT_ASSERT(colander_data);
706a18f9
PP
140
141 if (!colander_data->notif_iter) {
d94d92ac
PP
142 BT_LIB_LOGW("Trying to consume without an "
143 "upstream notification iterator: %![comp-]+c",
144 self_comp);
706a18f9
PP
145 goto end;
146 }
147
d94d92ac
PP
148 notif_iter_status =
149 bt_self_component_port_input_notification_iterator_next(
150 colander_data->notif_iter, &notifs,
151 colander_data->count_addr);
706a18f9
PP
152 switch (notif_iter_status) {
153 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
d94d92ac 154 status = BT_SELF_COMPONENT_STATUS_OK;
706a18f9
PP
155 goto end;
156 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
d94d92ac 157 status = BT_SELF_COMPONENT_STATUS_AGAIN;
706a18f9
PP
158 goto end;
159 case BT_NOTIFICATION_ITERATOR_STATUS_END:
d94d92ac 160 status = BT_SELF_COMPONENT_STATUS_END;
706a18f9
PP
161 goto end;
162 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
d4393e08
PP
163 /* Move notifications to user (count already set) */
164 memcpy(colander_data->notifs, notifs,
165 sizeof(*notifs) * *colander_data->count_addr);
706a18f9
PP
166 break;
167 default:
d94d92ac 168 status = BT_SELF_COMPONENT_STATUS_ERROR;
706a18f9
PP
169 goto end;
170 }
171
706a18f9 172end:
706a18f9
PP
173 return status;
174}
175
d94d92ac 176struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
706a18f9
PP
177{
178 if (colander_comp_cls) {
179 goto end;
180 }
181
0d72b8c3
PP
182 colander_comp_cls = bt_component_class_sink_create("colander",
183 colander_consume);
706a18f9
PP
184 if (!colander_comp_cls) {
185 BT_LOGE_STR("Cannot create sink colander component class.");
186 goto end;
187 }
188
0d72b8c3 189 (void) bt_component_class_sink_set_init_method(
d94d92ac 190 colander_comp_cls, colander_init);
0d72b8c3 191 (void) bt_component_class_sink_set_finalize_method(
d94d92ac 192 colander_comp_cls, colander_finalize);
0d72b8c3 193 (void) bt_component_class_sink_set_input_port_connected_method(
d94d92ac 194 colander_comp_cls, colander_input_port_connected);
706a18f9
PP
195
196end:
398454ed
PP
197 bt_object_get_ref(colander_comp_cls);
198 return (void *) colander_comp_cls;
706a18f9
PP
199}
200
201__attribute__((destructor)) static
202void put_colander(void) {
65300d60 203 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
706a18f9 204}
This page took 0.035613 seconds and 4 git commands to generate.