lib: rename "notification" -> "message"
[babeltrace.git] / lib / graph / component-class-sink-colander.c
CommitLineData
361ac4a6 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
361ac4a6
PP
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
8c6884d9
PP
26#include <babeltrace/assert-internal.h>
27#include <babeltrace/assert-pre-internal.h>
28#include <babeltrace/object-internal.h>
7b53201c 29#include <babeltrace/graph/component-class-sink.h>
834e9996
PP
30#include <babeltrace/graph/self-component-sink.h>
31#include <babeltrace/graph/self-component-port.h>
b09a5592 32#include <babeltrace/graph/self-component-port-input-message-iterator.h>
834e9996 33#include <babeltrace/graph/self-component.h>
c3ac0193 34#include <babeltrace/graph/component-class-sink-colander-internal.h>
361ac4a6 35#include <glib.h>
361ac4a6
PP
36
37static
7b53201c 38struct bt_component_class_sink *colander_comp_cls;
361ac4a6
PP
39
40struct colander_data {
b09a5592 41 bt_message_array_const msgs;
3fd7b79d 42 uint64_t *count_addr;
b09a5592 43 struct bt_self_component_port_input_message_iterator *msg_iter;
361ac4a6
PP
44};
45
46static
834e9996
PP
47enum bt_self_component_status colander_init(
48 struct bt_self_component_sink *self_comp,
ce141536 49 const struct bt_value *params, void *init_method_data)
361ac4a6 50{
834e9996 51 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
361ac4a6 52 struct colander_data *colander_data = NULL;
c3ac0193
PP
53 struct bt_component_class_sink_colander_data *user_provided_data =
54 init_method_data;
361ac4a6
PP
55
56 if (!init_method_data) {
57 BT_LOGW_STR("Component initialization method data is NULL.");
834e9996 58 status = BT_SELF_COMPONENT_STATUS_ERROR;
361ac4a6
PP
59 goto end;
60 }
61
62 colander_data = g_new0(struct colander_data, 1);
63 if (!colander_data) {
64 BT_LOGE_STR("Failed to allocate colander data.");
834e9996 65 status = BT_SELF_COMPONENT_STATUS_NOMEM;
361ac4a6
PP
66 goto end;
67 }
68
b09a5592 69 colander_data->msgs = user_provided_data->msgs;
3fd7b79d 70 colander_data->count_addr = user_provided_data->count_addr;
834e9996
PP
71 status = bt_self_component_sink_add_input_port(self_comp, "in",
72 NULL, NULL);
73 if (status != BT_SELF_COMPONENT_STATUS_OK) {
361ac4a6
PP
74 BT_LOGE_STR("Cannot add input port.");
75 goto end;
76 }
77
834e9996 78 bt_self_component_set_data(
bb61965b 79 bt_self_component_sink_as_self_component(self_comp),
834e9996 80 colander_data);
361ac4a6
PP
81
82end:
83 return status;
84}
85
86static
834e9996 87void colander_finalize(struct bt_self_component_sink *self_comp)
361ac4a6
PP
88{
89 struct colander_data *colander_data =
834e9996 90 bt_self_component_get_data(
bb61965b 91 bt_self_component_sink_as_self_component(self_comp));
361ac4a6
PP
92
93 if (!colander_data) {
94 return;
95 }
96
b09a5592 97 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
361ac4a6
PP
98 g_free(colander_data);
99}
100
101static
834e9996
PP
102enum bt_self_component_status colander_input_port_connected(
103 struct bt_self_component_sink *self_comp,
104 struct bt_self_component_port_input *self_port,
7b53201c 105 const struct bt_port_output *other_port)
361ac4a6 106{
834e9996 107 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
361ac4a6 108 struct colander_data *colander_data =
834e9996 109 bt_self_component_get_data(
bb61965b 110 bt_self_component_sink_as_self_component(self_comp));
361ac4a6 111
8b45963b 112 BT_ASSERT(colander_data);
b09a5592
PP
113 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
114 colander_data->msg_iter =
115 bt_self_component_port_input_message_iterator_create(
834e9996 116 self_port);
b09a5592
PP
117 if (!colander_data->msg_iter) {
118 BT_LIB_LOGE("Cannot create message iterator on "
834e9996
PP
119 "self component input port: %![port-]+p",
120 self_port);
121 status = BT_SELF_COMPONENT_STATUS_NOMEM;
361ac4a6
PP
122 goto end;
123 }
124
125end:
634f394c 126 return status;
361ac4a6
PP
127}
128
129static
834e9996
PP
130enum bt_self_component_status colander_consume(
131 struct bt_self_component_sink *self_comp)
361ac4a6 132{
834e9996 133 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
b09a5592 134 enum bt_message_iterator_status msg_iter_status;
361ac4a6 135 struct colander_data *colander_data =
834e9996 136 bt_self_component_get_data(
bb61965b 137 bt_self_component_sink_as_self_component(self_comp));
b09a5592 138 bt_message_array_const msgs;
361ac4a6 139
8b45963b 140 BT_ASSERT(colander_data);
361ac4a6 141
b09a5592 142 if (!colander_data->msg_iter) {
834e9996 143 BT_LIB_LOGW("Trying to consume without an "
b09a5592 144 "upstream message iterator: %![comp-]+c",
834e9996 145 self_comp);
361ac4a6
PP
146 goto end;
147 }
148
b09a5592
PP
149 msg_iter_status =
150 bt_self_component_port_input_message_iterator_next(
151 colander_data->msg_iter, &msgs,
834e9996 152 colander_data->count_addr);
b09a5592
PP
153 switch (msg_iter_status) {
154 case BT_MESSAGE_ITERATOR_STATUS_CANCELED:
834e9996 155 status = BT_SELF_COMPONENT_STATUS_OK;
361ac4a6 156 goto end;
b09a5592 157 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
834e9996 158 status = BT_SELF_COMPONENT_STATUS_AGAIN;
361ac4a6 159 goto end;
b09a5592 160 case BT_MESSAGE_ITERATOR_STATUS_END:
834e9996 161 status = BT_SELF_COMPONENT_STATUS_END;
361ac4a6 162 goto end;
b09a5592
PP
163 case BT_MESSAGE_ITERATOR_STATUS_OK:
164 /* Move messages to user (count already set) */
165 memcpy(colander_data->msgs, msgs,
166 sizeof(*msgs) * *colander_data->count_addr);
361ac4a6
PP
167 break;
168 default:
834e9996 169 status = BT_SELF_COMPONENT_STATUS_ERROR;
361ac4a6
PP
170 goto end;
171 }
172
361ac4a6 173end:
361ac4a6
PP
174 return status;
175}
176
834e9996 177struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
361ac4a6
PP
178{
179 if (colander_comp_cls) {
180 goto end;
181 }
182
7b53201c
PP
183 colander_comp_cls = bt_component_class_sink_create("colander",
184 colander_consume);
361ac4a6
PP
185 if (!colander_comp_cls) {
186 BT_LOGE_STR("Cannot create sink colander component class.");
187 goto end;
188 }
189
7b53201c 190 (void) bt_component_class_sink_set_init_method(
834e9996 191 colander_comp_cls, colander_init);
7b53201c 192 (void) bt_component_class_sink_set_finalize_method(
834e9996 193 colander_comp_cls, colander_finalize);
7b53201c 194 (void) bt_component_class_sink_set_input_port_connected_method(
834e9996 195 colander_comp_cls, colander_input_port_connected);
361ac4a6
PP
196
197end:
4b70020d
PP
198 bt_object_get_ref(colander_comp_cls);
199 return (void *) colander_comp_cls;
361ac4a6
PP
200}
201
202__attribute__((destructor)) static
203void put_colander(void) {
8138bfe1 204 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
361ac4a6 205}
This page took 0.036333 seconds and 4 git commands to generate.