lib: private functions: do not repeat `private` word
[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
65300d60 26#include <babeltrace/object.h>
9d408fca 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;
28e6ca8b 72 status = bt_private_component_sink_add_input_port(
706a18f9
PP
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) {
65300d60 96 bt_object_put_ref(colander_data->notif_iter);
706a18f9
PP
97 }
98
99 g_free(colander_data);
100}
101
102static
bf55043c 103enum bt_component_status colander_port_connected(struct bt_private_component *priv_comp,
706a18f9
PP
104 struct bt_private_port *self_priv_port,
105 struct bt_port *other_port)
106{
bf55043c 107 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
706a18f9
PP
108 enum bt_connection_status conn_status;
109 struct bt_private_connection *priv_conn =
28e6ca8b 110 bt_private_port_get_connection(self_priv_port);
706a18f9
PP
111 struct colander_data *colander_data =
112 bt_private_component_get_user_data(priv_comp);
113
f6ccaed9
PP
114 BT_ASSERT(priv_conn);
115 BT_ASSERT(colander_data);
65300d60 116 BT_OBJECT_PUT_REF_AND_RESET(colander_data->notif_iter);
706a18f9 117 conn_status = bt_private_connection_create_notification_iterator(
f42867e2 118 priv_conn, &colander_data->notif_iter);
706a18f9
PP
119 if (conn_status) {
120 BT_LOGE("Cannot create notification iterator from connection: "
121 "comp-addr=%p, conn-addr=%p", priv_comp, priv_conn);
bf55043c 122 status = BT_COMPONENT_STATUS_ERROR;
706a18f9
PP
123 goto end;
124 }
125
126end:
65300d60 127 bt_object_put_ref(priv_conn);
bf55043c 128 return status;
706a18f9
PP
129}
130
131static
132enum bt_component_status colander_consume(
133 struct bt_private_component *priv_comp)
134{
135 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
136 enum bt_notification_iterator_status notif_iter_status;
706a18f9
PP
137 struct colander_data *colander_data =
138 bt_private_component_get_user_data(priv_comp);
d4393e08 139 bt_notification_array notifs;
706a18f9 140
f6ccaed9 141 BT_ASSERT(colander_data);
706a18f9
PP
142
143 if (!colander_data->notif_iter) {
144 BT_LOGW("Trying to consume without an upstream notification iterator: "
145 "comp-addr=%p", priv_comp);
146 goto end;
147 }
148
07245ac2 149 notif_iter_status = bt_private_connection_notification_iterator_next(
d4393e08 150 colander_data->notif_iter, &notifs, colander_data->count_addr);
706a18f9
PP
151 switch (notif_iter_status) {
152 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
153 status = BT_COMPONENT_STATUS_OK;
154 goto end;
155 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
156 status = BT_COMPONENT_STATUS_AGAIN;
157 goto end;
158 case BT_NOTIFICATION_ITERATOR_STATUS_END:
159 status = BT_COMPONENT_STATUS_END;
160 goto end;
161 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
d4393e08
PP
162 /* Move notifications to user (count already set) */
163 memcpy(colander_data->notifs, notifs,
164 sizeof(*notifs) * *colander_data->count_addr);
706a18f9
PP
165 break;
166 default:
167 status = BT_COMPONENT_STATUS_ERROR;
168 goto end;
169 }
170
706a18f9
PP
171end:
172 /* Move notification to user's pointer, even if NULL. */
706a18f9
PP
173 return status;
174}
175
176struct bt_component_class *bt_component_class_sink_colander_get(void)
177{
178 if (colander_comp_cls) {
179 goto end;
180 }
181
182 colander_comp_cls = bt_component_class_sink_create("colander",
183 colander_consume);
184 if (!colander_comp_cls) {
185 BT_LOGE_STR("Cannot create sink colander component class.");
186 goto end;
187 }
188
189 (void) bt_component_class_set_init_method(colander_comp_cls,
190 colander_init);
191 (void) bt_component_class_set_finalize_method(colander_comp_cls,
192 colander_finalize);
193 (void) bt_component_class_set_port_connected_method(colander_comp_cls,
194 colander_port_connected);
195 (void) bt_component_class_freeze(colander_comp_cls);
196
197end:
65300d60 198 return bt_object_get_ref(colander_comp_cls);
706a18f9
PP
199}
200
201__attribute__((destructor)) static
202void put_colander(void) {
65300d60 203 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
706a18f9 204}
This page took 0.034 seconds and 4 git commands to generate.