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