lib: notification iterator: transfer a batch of notifications
[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>
07245ac2 32#include <babeltrace/graph/private-connection-notification-iterator.h>
9d408fca 33#include <babeltrace/graph/private-component.h>
8ed535b5 34#include <babeltrace/graph/component-class-sink-colander-internal.h>
f6ccaed9 35#include <babeltrace/assert-internal.h>
706a18f9 36#include <glib.h>
706a18f9
PP
37
38static
39struct bt_component_class *colander_comp_cls;
40
41struct colander_data {
d4393e08
PP
42 bt_notification_array notifs;
43 uint64_t *count_addr;
706a18f9
PP
44 struct bt_notification_iterator *notif_iter;
45};
46
47static
48enum 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;
8ed535b5
PP
54 struct bt_component_class_sink_colander_data *user_provided_data =
55 init_method_data;
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
d4393e08
PP
70 colander_data->notifs = user_provided_data->notifs;
71 colander_data->count_addr = user_provided_data->count_addr;
706a18f9
PP
72 status = bt_private_component_sink_add_input_private_port(
73 priv_comp, "in", NULL, NULL);
74 if (status != BT_COMPONENT_STATUS_OK) {
75 BT_LOGE_STR("Cannot add input port.");
76 goto end;
77 }
78
79 (void) bt_private_component_set_user_data(priv_comp, colander_data);
80
81end:
82 return status;
83}
84
85static
86void colander_finalize(struct bt_private_component *priv_comp)
87{
88 struct colander_data *colander_data =
89 bt_private_component_get_user_data(priv_comp);
90
91 if (!colander_data) {
92 return;
93 }
94
95 if (colander_data->notif_iter) {
96 bt_put(colander_data->notif_iter);
97 }
98
99 g_free(colander_data);
100}
101
102static
103void 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
f6ccaed9
PP
113 BT_ASSERT(priv_conn);
114 BT_ASSERT(colander_data);
706a18f9
PP
115 BT_PUT(colander_data->notif_iter);
116 conn_status = bt_private_connection_create_notification_iterator(
f42867e2 117 priv_conn, &colander_data->notif_iter);
706a18f9
PP
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
124end:
125 bt_put(priv_conn);
126}
127
128static
129enum 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;
706a18f9
PP
134 struct colander_data *colander_data =
135 bt_private_component_get_user_data(priv_comp);
d4393e08 136 bt_notification_array notifs;
706a18f9 137
f6ccaed9 138 BT_ASSERT(colander_data);
706a18f9
PP
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
07245ac2 146 notif_iter_status = bt_private_connection_notification_iterator_next(
d4393e08 147 colander_data->notif_iter, &notifs, colander_data->count_addr);
706a18f9
PP
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:
d4393e08
PP
159 /* Move notifications to user (count already set) */
160 memcpy(colander_data->notifs, notifs,
161 sizeof(*notifs) * *colander_data->count_addr);
706a18f9
PP
162 break;
163 default:
164 status = BT_COMPONENT_STATUS_ERROR;
165 goto end;
166 }
167
706a18f9
PP
168end:
169 /* Move notification to user's pointer, even if NULL. */
706a18f9
PP
170 return status;
171}
172
173struct 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
194end:
195 return bt_get(colander_comp_cls);
196}
197
198__attribute__((destructor)) static
199void put_colander(void) {
200 BT_PUT(colander_comp_cls);
201}
This page took 0.033393 seconds and 4 git commands to generate.