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