Add internal BT_ASSERT() and BT_ASSERT_PRE() helpers
[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
9d408fca
PP
26#include <babeltrace/ref.h>
27#include <babeltrace/graph/connection.h>
706a18f9
PP
28#include <babeltrace/graph/component-class-sink.h>
29#include <babeltrace/graph/private-component-sink.h>
30#include <babeltrace/graph/private-port.h>
31#include <babeltrace/graph/private-connection.h>
9d408fca 32#include <babeltrace/graph/private-component.h>
8ed535b5 33#include <babeltrace/graph/component-class-sink-colander-internal.h>
706a18f9
PP
34#include <glib.h>
35#include <assert.h>
36
37static
38struct bt_component_class *colander_comp_cls;
39
40struct colander_data {
41 struct bt_notification **user_notif;
8ed535b5 42 enum bt_notification_type *notif_types;
706a18f9
PP
43 struct bt_notification_iterator *notif_iter;
44};
45
46static
47enum bt_component_status colander_init(
48 struct bt_private_component *priv_comp,
49 struct bt_value *params, void *init_method_data)
50{
51 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
52 struct colander_data *colander_data = NULL;
8ed535b5
PP
53 struct bt_component_class_sink_colander_data *user_provided_data =
54 init_method_data;
55 const enum bt_notification_type *notif_type;
706a18f9
PP
56
57 if (!init_method_data) {
58 BT_LOGW_STR("Component initialization method data is NULL.");
59 status = BT_COMPONENT_STATUS_INVALID;
60 goto end;
61 }
62
63 colander_data = g_new0(struct colander_data, 1);
64 if (!colander_data) {
65 BT_LOGE_STR("Failed to allocate colander data.");
66 status = BT_COMPONENT_STATUS_NOMEM;
67 goto end;
68 }
69
8ed535b5
PP
70 colander_data->user_notif = user_provided_data->notification;
71
72 if (user_provided_data->notification_types) {
73 notif_type = user_provided_data->notification_types;
74 unsigned long count;
75
76 while (*notif_type != BT_NOTIFICATION_TYPE_SENTINEL) {
77 notif_type++;
78 }
79
80 count = notif_type - user_provided_data->notification_types + 1;
81
82 colander_data->notif_types =
83 g_new0(enum bt_notification_type, count);
84 if (!colander_data->notif_types) {
85 BT_LOGE_STR("Failed to allocate an array of notification types.");
86 status = BT_COMPONENT_STATUS_NOMEM;
87 goto end;
88 }
89
90 memcpy(colander_data->notif_types,
91 user_provided_data->notification_types,
92 count * sizeof(enum bt_notification_type));
93 }
94
706a18f9
PP
95 status = bt_private_component_sink_add_input_private_port(
96 priv_comp, "in", NULL, NULL);
97 if (status != BT_COMPONENT_STATUS_OK) {
98 BT_LOGE_STR("Cannot add input port.");
99 goto end;
100 }
101
102 (void) bt_private_component_set_user_data(priv_comp, colander_data);
103
104end:
105 return status;
106}
107
108static
109void colander_finalize(struct bt_private_component *priv_comp)
110{
111 struct colander_data *colander_data =
112 bt_private_component_get_user_data(priv_comp);
113
114 if (!colander_data) {
115 return;
116 }
117
118 if (colander_data->notif_iter) {
119 bt_put(colander_data->notif_iter);
120 }
121
8ed535b5 122 g_free(colander_data->notif_types);
706a18f9
PP
123 g_free(colander_data);
124}
125
126static
127void colander_port_connected(struct bt_private_component *priv_comp,
128 struct bt_private_port *self_priv_port,
129 struct bt_port *other_port)
130{
131 enum bt_connection_status conn_status;
132 struct bt_private_connection *priv_conn =
133 bt_private_port_get_private_connection(self_priv_port);
134 struct colander_data *colander_data =
135 bt_private_component_get_user_data(priv_comp);
136
137 assert(priv_conn);
138 assert(colander_data);
139 BT_PUT(colander_data->notif_iter);
140 conn_status = bt_private_connection_create_notification_iterator(
8ed535b5
PP
141 priv_conn, colander_data->notif_types,
142 &colander_data->notif_iter);
706a18f9
PP
143 if (conn_status) {
144 BT_LOGE("Cannot create notification iterator from connection: "
145 "comp-addr=%p, conn-addr=%p", priv_comp, priv_conn);
146 goto end;
147 }
148
149end:
150 bt_put(priv_conn);
151}
152
153static
154enum bt_component_status colander_consume(
155 struct bt_private_component *priv_comp)
156{
157 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
158 enum bt_notification_iterator_status notif_iter_status;
159 struct bt_notification *notif = NULL;
160 struct colander_data *colander_data =
161 bt_private_component_get_user_data(priv_comp);
162
163 assert(colander_data);
164
165 if (!colander_data->notif_iter) {
166 BT_LOGW("Trying to consume without an upstream notification iterator: "
167 "comp-addr=%p", priv_comp);
168 goto end;
169 }
170
171 notif_iter_status = bt_notification_iterator_next(
172 colander_data->notif_iter);
173 switch (notif_iter_status) {
174 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
175 status = BT_COMPONENT_STATUS_OK;
176 goto end;
177 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
178 status = BT_COMPONENT_STATUS_AGAIN;
179 goto end;
180 case BT_NOTIFICATION_ITERATOR_STATUS_END:
181 status = BT_COMPONENT_STATUS_END;
182 goto end;
183 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
184 break;
185 default:
186 status = BT_COMPONENT_STATUS_ERROR;
187 goto end;
188 }
189
190 notif = bt_notification_iterator_get_notification(
191 colander_data->notif_iter);
192 assert(notif);
193
194end:
195 /* Move notification to user's pointer, even if NULL. */
196 *colander_data->user_notif = notif;
197 return status;
198}
199
200struct bt_component_class *bt_component_class_sink_colander_get(void)
201{
202 if (colander_comp_cls) {
203 goto end;
204 }
205
206 colander_comp_cls = bt_component_class_sink_create("colander",
207 colander_consume);
208 if (!colander_comp_cls) {
209 BT_LOGE_STR("Cannot create sink colander component class.");
210 goto end;
211 }
212
213 (void) bt_component_class_set_init_method(colander_comp_cls,
214 colander_init);
215 (void) bt_component_class_set_finalize_method(colander_comp_cls,
216 colander_finalize);
217 (void) bt_component_class_set_port_connected_method(colander_comp_cls,
218 colander_port_connected);
219 (void) bt_component_class_freeze(colander_comp_cls);
220
221end:
222 return bt_get(colander_comp_cls);
223}
224
225__attribute__((destructor)) static
226void put_colander(void) {
227 BT_PUT(colander_comp_cls);
228}
This page took 0.031867 seconds and 4 git commands to generate.