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