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