Notification iterator: transform precondition checks to BT_ASSERT_PRE()
[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>
f6ccaed9 34#include <babeltrace/assert-internal.h>
706a18f9 35#include <glib.h>
706a18f9
PP
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;
706a18f9
PP
55
56 if (!init_method_data) {
57 BT_LOGW_STR("Component initialization method data is NULL.");
58 status = BT_COMPONENT_STATUS_INVALID;
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.");
65 status = BT_COMPONENT_STATUS_NOMEM;
66 goto end;
67 }
68
8ed535b5 69 colander_data->user_notif = user_provided_data->notification;
706a18f9
PP
70 status = bt_private_component_sink_add_input_private_port(
71 priv_comp, "in", NULL, NULL);
72 if (status != BT_COMPONENT_STATUS_OK) {
73 BT_LOGE_STR("Cannot add input port.");
74 goto end;
75 }
76
77 (void) bt_private_component_set_user_data(priv_comp, colander_data);
78
79end:
80 return status;
81}
82
83static
84void colander_finalize(struct bt_private_component *priv_comp)
85{
86 struct colander_data *colander_data =
87 bt_private_component_get_user_data(priv_comp);
88
89 if (!colander_data) {
90 return;
91 }
92
93 if (colander_data->notif_iter) {
94 bt_put(colander_data->notif_iter);
95 }
96
8ed535b5 97 g_free(colander_data->notif_types);
706a18f9
PP
98 g_free(colander_data);
99}
100
101static
102void colander_port_connected(struct bt_private_component *priv_comp,
103 struct bt_private_port *self_priv_port,
104 struct bt_port *other_port)
105{
106 enum bt_connection_status conn_status;
107 struct bt_private_connection *priv_conn =
108 bt_private_port_get_private_connection(self_priv_port);
109 struct colander_data *colander_data =
110 bt_private_component_get_user_data(priv_comp);
111
f6ccaed9
PP
112 BT_ASSERT(priv_conn);
113 BT_ASSERT(colander_data);
706a18f9
PP
114 BT_PUT(colander_data->notif_iter);
115 conn_status = bt_private_connection_create_notification_iterator(
f42867e2 116 priv_conn, &colander_data->notif_iter);
706a18f9
PP
117 if (conn_status) {
118 BT_LOGE("Cannot create notification iterator from connection: "
119 "comp-addr=%p, conn-addr=%p", priv_comp, priv_conn);
120 goto end;
121 }
122
123end:
124 bt_put(priv_conn);
125}
126
127static
128enum bt_component_status colander_consume(
129 struct bt_private_component *priv_comp)
130{
131 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
132 enum bt_notification_iterator_status notif_iter_status;
133 struct bt_notification *notif = NULL;
134 struct colander_data *colander_data =
135 bt_private_component_get_user_data(priv_comp);
136
f6ccaed9 137 BT_ASSERT(colander_data);
706a18f9
PP
138
139 if (!colander_data->notif_iter) {
140 BT_LOGW("Trying to consume without an upstream notification iterator: "
141 "comp-addr=%p", priv_comp);
142 goto end;
143 }
144
145 notif_iter_status = bt_notification_iterator_next(
146 colander_data->notif_iter);
147 switch (notif_iter_status) {
148 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
149 status = BT_COMPONENT_STATUS_OK;
150 goto end;
151 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
152 status = BT_COMPONENT_STATUS_AGAIN;
153 goto end;
154 case BT_NOTIFICATION_ITERATOR_STATUS_END:
155 status = BT_COMPONENT_STATUS_END;
156 goto end;
157 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
158 break;
159 default:
160 status = BT_COMPONENT_STATUS_ERROR;
161 goto end;
162 }
163
164 notif = bt_notification_iterator_get_notification(
165 colander_data->notif_iter);
f6ccaed9 166 BT_ASSERT(notif);
706a18f9
PP
167
168end:
169 /* Move notification to user's pointer, even if NULL. */
170 *colander_data->user_notif = notif;
171 return status;
172}
173
174struct bt_component_class *bt_component_class_sink_colander_get(void)
175{
176 if (colander_comp_cls) {
177 goto end;
178 }
179
180 colander_comp_cls = bt_component_class_sink_create("colander",
181 colander_consume);
182 if (!colander_comp_cls) {
183 BT_LOGE_STR("Cannot create sink colander component class.");
184 goto end;
185 }
186
187 (void) bt_component_class_set_init_method(colander_comp_cls,
188 colander_init);
189 (void) bt_component_class_set_finalize_method(colander_comp_cls,
190 colander_finalize);
191 (void) bt_component_class_set_port_connected_method(colander_comp_cls,
192 colander_port_connected);
193 (void) bt_component_class_freeze(colander_comp_cls);
194
195end:
196 return bt_get(colander_comp_cls);
197}
198
199__attribute__((destructor)) static
200void put_colander(void) {
201 BT_PUT(colander_comp_cls);
202}
This page took 0.032701 seconds and 4 git commands to generate.