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