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 | ||
350ad6c1 | 23 | #define BT_LOG_TAG "LIB/COLANDER" |
c2d9d9cf | 24 | #include "lib/logging.h" |
706a18f9 | 25 | |
578e048b MJ |
26 | #include "common/assert.h" |
27 | #include "lib/assert-pre.h" | |
28 | #include "lib/object.h" | |
3fadfbc0 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> | |
706a18f9 | 34 | #include <glib.h> |
706a18f9 | 35 | |
578e048b | 36 | #include "component-class-sink-colander.h" |
d24d5663 | 37 | #include "lib/func-status.h" |
578e048b | 38 | |
706a18f9 | 39 | static |
0d72b8c3 | 40 | struct bt_component_class_sink *colander_comp_cls; |
706a18f9 | 41 | |
706a18f9 | 42 | static |
d24d5663 | 43 | enum bt_component_class_init_method_status colander_init( |
d94d92ac | 44 | struct bt_self_component_sink *self_comp, |
05e21286 | 45 | const struct bt_value *params, void *init_method_data) |
706a18f9 | 46 | { |
9275bef4 | 47 | int status = BT_FUNC_STATUS_OK; |
7474e7d3 | 48 | struct bt_component_class_sink_colander_priv_data *colander_data = NULL; |
8ed535b5 PP |
49 | struct bt_component_class_sink_colander_data *user_provided_data = |
50 | init_method_data; | |
706a18f9 | 51 | |
3f7d4d90 | 52 | BT_ASSERT(init_method_data); |
7474e7d3 PP |
53 | colander_data = g_new0( |
54 | struct bt_component_class_sink_colander_priv_data, 1); | |
706a18f9 PP |
55 | if (!colander_data) { |
56 | BT_LOGE_STR("Failed to allocate colander data."); | |
d24d5663 | 57 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
706a18f9 PP |
58 | goto end; |
59 | } | |
60 | ||
d6e69534 | 61 | colander_data->msgs = user_provided_data->msgs; |
d4393e08 | 62 | colander_data->count_addr = user_provided_data->count_addr; |
d94d92ac PP |
63 | status = bt_self_component_sink_add_input_port(self_comp, "in", |
64 | NULL, NULL); | |
d24d5663 | 65 | if (status != BT_FUNC_STATUS_OK) { |
706a18f9 PP |
66 | BT_LOGE_STR("Cannot add input port."); |
67 | goto end; | |
68 | } | |
69 | ||
d94d92ac | 70 | bt_self_component_set_data( |
707b7d35 | 71 | bt_self_component_sink_as_self_component(self_comp), |
d94d92ac | 72 | colander_data); |
706a18f9 PP |
73 | |
74 | end: | |
75 | return status; | |
76 | } | |
77 | ||
78 | static | |
d94d92ac | 79 | void colander_finalize(struct bt_self_component_sink *self_comp) |
706a18f9 | 80 | { |
7474e7d3 | 81 | struct bt_component_class_sink_colander_priv_data *colander_data = |
d94d92ac | 82 | bt_self_component_get_data( |
707b7d35 | 83 | bt_self_component_sink_as_self_component(self_comp)); |
706a18f9 PP |
84 | |
85 | if (!colander_data) { | |
86 | return; | |
87 | } | |
88 | ||
d6e69534 | 89 | BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter); |
706a18f9 PP |
90 | g_free(colander_data); |
91 | } | |
92 | ||
93 | static | |
d24d5663 PP |
94 | enum bt_component_class_sink_graph_is_configured_method_status |
95 | colander_graph_is_configured( | |
36d1acad | 96 | bt_self_component_sink *self_comp) |
706a18f9 | 97 | { |
d24d5663 PP |
98 | enum bt_component_class_sink_graph_is_configured_method_status status = |
99 | BT_FUNC_STATUS_OK; | |
7474e7d3 | 100 | struct bt_component_class_sink_colander_priv_data *colander_data = |
d94d92ac | 101 | bt_self_component_get_data( |
707b7d35 | 102 | bt_self_component_sink_as_self_component(self_comp)); |
706a18f9 | 103 | |
36d1acad SM |
104 | struct bt_self_component_port_input *self_port = |
105 | bt_self_component_sink_borrow_input_port_by_name(self_comp, "in"); | |
106 | BT_ASSERT(self_port); | |
107 | ||
f6ccaed9 | 108 | BT_ASSERT(colander_data); |
d6e69534 PP |
109 | BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter); |
110 | colander_data->msg_iter = | |
111 | bt_self_component_port_input_message_iterator_create( | |
d94d92ac | 112 | self_port); |
d6e69534 PP |
113 | if (!colander_data->msg_iter) { |
114 | BT_LIB_LOGE("Cannot create message iterator on " | |
d94d92ac PP |
115 | "self component input port: %![port-]+p", |
116 | self_port); | |
d24d5663 | 117 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
706a18f9 PP |
118 | goto end; |
119 | } | |
120 | ||
121 | end: | |
bf55043c | 122 | return status; |
706a18f9 PP |
123 | } |
124 | ||
125 | static | |
d24d5663 | 126 | enum bt_component_class_sink_consume_method_status colander_consume( |
d94d92ac | 127 | struct bt_self_component_sink *self_comp) |
706a18f9 | 128 | { |
d24d5663 PP |
129 | enum bt_component_class_sink_consume_method_status status = |
130 | BT_FUNC_STATUS_OK; | |
131 | enum bt_message_iterator_next_status next_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); |
3f7d4d90 | 138 | BT_ASSERT(colander_data->msg_iter); |
d24d5663 PP |
139 | next_status = bt_self_component_port_input_message_iterator_next( |
140 | colander_data->msg_iter, &msgs, | |
141 | colander_data->count_addr); | |
142 | switch (next_status) { | |
143 | case BT_FUNC_STATUS_AGAIN: | |
144 | status = BT_FUNC_STATUS_AGAIN; | |
706a18f9 | 145 | goto end; |
d24d5663 PP |
146 | case BT_FUNC_STATUS_END: |
147 | status = BT_FUNC_STATUS_END; | |
706a18f9 | 148 | goto end; |
d24d5663 | 149 | case BT_FUNC_STATUS_OK: |
d6e69534 PP |
150 | /* Move messages to user (count already set) */ |
151 | memcpy(colander_data->msgs, msgs, | |
152 | sizeof(*msgs) * *colander_data->count_addr); | |
706a18f9 PP |
153 | break; |
154 | default: | |
d24d5663 | 155 | status = BT_FUNC_STATUS_ERROR; |
706a18f9 PP |
156 | goto end; |
157 | } | |
158 | ||
706a18f9 | 159 | end: |
706a18f9 PP |
160 | return status; |
161 | } | |
162 | ||
d94d92ac | 163 | struct bt_component_class_sink *bt_component_class_sink_colander_get(void) |
706a18f9 PP |
164 | { |
165 | if (colander_comp_cls) { | |
166 | goto end; | |
167 | } | |
168 | ||
0d72b8c3 PP |
169 | colander_comp_cls = bt_component_class_sink_create("colander", |
170 | colander_consume); | |
706a18f9 PP |
171 | if (!colander_comp_cls) { |
172 | BT_LOGE_STR("Cannot create sink colander component class."); | |
173 | goto end; | |
174 | } | |
175 | ||
0d72b8c3 | 176 | (void) bt_component_class_sink_set_init_method( |
d94d92ac | 177 | colander_comp_cls, colander_init); |
0d72b8c3 | 178 | (void) bt_component_class_sink_set_finalize_method( |
d94d92ac | 179 | colander_comp_cls, colander_finalize); |
36d1acad SM |
180 | (void) bt_component_class_sink_set_graph_is_configured_method( |
181 | colander_comp_cls, colander_graph_is_configured); | |
706a18f9 PP |
182 | |
183 | end: | |
398454ed | 184 | bt_object_get_ref(colander_comp_cls); |
5a2b6475 | 185 | return colander_comp_cls; |
706a18f9 PP |
186 | } |
187 | ||
188 | __attribute__((destructor)) static | |
189 | void put_colander(void) { | |
65300d60 | 190 | BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls); |
706a18f9 | 191 | } |