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