lib: create input port msg iterator from self {msg iterator, sink comp.}
[babeltrace.git] / src / 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
b03487ab 23#define BT_LOG_TAG "LIB/COLANDER"
1633ef46 24#include "lib/logging.h"
361ac4a6 25
57952005
MJ
26#include "common/assert.h"
27#include "lib/assert-pre.h"
28#include "lib/object.h"
71c5da58
MJ
29#include <babeltrace2/graph/component-class-sink.h>
30#include <babeltrace2/graph/self-component-sink.h>
31#include <babeltrace2/graph/self-component-port.h>
32#include <babeltrace2/graph/self-component-port-input-message-iterator.h>
33#include <babeltrace2/graph/self-component.h>
361ac4a6 34#include <glib.h>
361ac4a6 35
57952005 36#include "component-class-sink-colander.h"
fb25b9e3 37#include "lib/func-status.h"
57952005 38
361ac4a6 39static
7b53201c 40struct bt_component_class_sink *colander_comp_cls;
361ac4a6 41
361ac4a6 42static
fb25b9e3 43enum bt_component_class_init_method_status colander_init(
834e9996 44 struct bt_self_component_sink *self_comp,
ce141536 45 const struct bt_value *params, void *init_method_data)
361ac4a6 46{
621b4e7e 47 int status = BT_FUNC_STATUS_OK;
15a52f66 48 struct bt_component_class_sink_colander_priv_data *colander_data = NULL;
c3ac0193
PP
49 struct bt_component_class_sink_colander_data *user_provided_data =
50 init_method_data;
361ac4a6 51
a684a357 52 BT_ASSERT(init_method_data);
15a52f66
PP
53 colander_data = g_new0(
54 struct bt_component_class_sink_colander_priv_data, 1);
361ac4a6 55 if (!colander_data) {
a8f90e5d
PP
56 BT_LIB_LOGE_APPEND_CAUSE(
57 "Failed to allocate colander sink data.");
fb25b9e3 58 status = BT_FUNC_STATUS_MEMORY_ERROR;
361ac4a6
PP
59 goto end;
60 }
61
b09a5592 62 colander_data->msgs = user_provided_data->msgs;
3fd7b79d 63 colander_data->count_addr = user_provided_data->count_addr;
834e9996
PP
64 status = bt_self_component_sink_add_input_port(self_comp, "in",
65 NULL, NULL);
fb25b9e3 66 if (status != BT_FUNC_STATUS_OK) {
a8f90e5d
PP
67 BT_LIB_LOGE_APPEND_CAUSE(
68 "Cannot add input port to colander sink.");
361ac4a6
PP
69 goto end;
70 }
71
834e9996 72 bt_self_component_set_data(
bb61965b 73 bt_self_component_sink_as_self_component(self_comp),
834e9996 74 colander_data);
361ac4a6
PP
75
76end:
77 return status;
78}
79
80static
834e9996 81void colander_finalize(struct bt_self_component_sink *self_comp)
361ac4a6 82{
15a52f66 83 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 84 bt_self_component_get_data(
bb61965b 85 bt_self_component_sink_as_self_component(self_comp));
361ac4a6
PP
86
87 if (!colander_data) {
88 return;
89 }
90
b09a5592 91 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
361ac4a6
PP
92 g_free(colander_data);
93}
94
95static
fb25b9e3
PP
96enum bt_component_class_sink_graph_is_configured_method_status
97colander_graph_is_configured(
ddcfe154 98 bt_self_component_sink *self_comp)
361ac4a6 99{
fb25b9e3
PP
100 enum bt_component_class_sink_graph_is_configured_method_status status =
101 BT_FUNC_STATUS_OK;
15a52f66 102 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 103 bt_self_component_get_data(
bb61965b 104 bt_self_component_sink_as_self_component(self_comp));
361ac4a6 105
ddcfe154
SM
106 struct bt_self_component_port_input *self_port =
107 bt_self_component_sink_borrow_input_port_by_name(self_comp, "in");
108 BT_ASSERT(self_port);
109
8b45963b 110 BT_ASSERT(colander_data);
b09a5592
PP
111 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
112 colander_data->msg_iter =
692f1a01
PP
113 bt_self_component_port_input_message_iterator_create_from_sink_component(
114 self_comp, self_port);
b09a5592 115 if (!colander_data->msg_iter) {
a8f90e5d 116 BT_LIB_LOGE_APPEND_CAUSE("Cannot create message iterator on "
834e9996
PP
117 "self component input port: %![port-]+p",
118 self_port);
fb25b9e3 119 status = BT_FUNC_STATUS_MEMORY_ERROR;
361ac4a6
PP
120 goto end;
121 }
122
123end:
634f394c 124 return status;
361ac4a6
PP
125}
126
127static
fb25b9e3 128enum bt_component_class_sink_consume_method_status colander_consume(
834e9996 129 struct bt_self_component_sink *self_comp)
361ac4a6 130{
fb25b9e3
PP
131 enum bt_component_class_sink_consume_method_status status =
132 BT_FUNC_STATUS_OK;
133 enum bt_message_iterator_next_status next_status;
15a52f66 134 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 135 bt_self_component_get_data(
bb61965b 136 bt_self_component_sink_as_self_component(self_comp));
b09a5592 137 bt_message_array_const msgs;
361ac4a6 138
8b45963b 139 BT_ASSERT(colander_data);
a684a357 140 BT_ASSERT(colander_data->msg_iter);
fb25b9e3
PP
141 next_status = bt_self_component_port_input_message_iterator_next(
142 colander_data->msg_iter, &msgs,
143 colander_data->count_addr);
144 switch (next_status) {
145 case BT_FUNC_STATUS_AGAIN:
146 status = BT_FUNC_STATUS_AGAIN;
361ac4a6 147 goto end;
fb25b9e3
PP
148 case BT_FUNC_STATUS_END:
149 status = BT_FUNC_STATUS_END;
361ac4a6 150 goto end;
fb25b9e3 151 case BT_FUNC_STATUS_OK:
b09a5592
PP
152 /* Move messages to user (count already set) */
153 memcpy(colander_data->msgs, msgs,
154 sizeof(*msgs) * *colander_data->count_addr);
361ac4a6
PP
155 break;
156 default:
fb25b9e3 157 status = BT_FUNC_STATUS_ERROR;
361ac4a6
PP
158 goto end;
159 }
160
361ac4a6 161end:
361ac4a6
PP
162 return status;
163}
164
834e9996 165struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
361ac4a6
PP
166{
167 if (colander_comp_cls) {
168 goto end;
169 }
170
7b53201c
PP
171 colander_comp_cls = bt_component_class_sink_create("colander",
172 colander_consume);
361ac4a6 173 if (!colander_comp_cls) {
a8f90e5d
PP
174 BT_LIB_LOGE_APPEND_CAUSE(
175 "Cannot create sink colander component class.");
361ac4a6
PP
176 goto end;
177 }
178
7b53201c 179 (void) bt_component_class_sink_set_init_method(
834e9996 180 colander_comp_cls, colander_init);
7b53201c 181 (void) bt_component_class_sink_set_finalize_method(
834e9996 182 colander_comp_cls, colander_finalize);
ddcfe154
SM
183 (void) bt_component_class_sink_set_graph_is_configured_method(
184 colander_comp_cls, colander_graph_is_configured);
361ac4a6
PP
185
186end:
4b70020d 187 bt_object_get_ref(colander_comp_cls);
88cbd22e 188 return colander_comp_cls;
361ac4a6
PP
189}
190
191__attribute__((destructor)) static
192void put_colander(void) {
8138bfe1 193 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
361ac4a6 194}
This page took 0.076069 seconds and 4 git commands to generate.