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