| 1 | /* |
| 2 | * Copyright 2017 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 <babeltrace/lib-logging-internal.h> |
| 25 | |
| 26 | #include <babeltrace/ref.h> |
| 27 | #include <babeltrace/graph/connection.h> |
| 28 | #include <babeltrace/graph/component-class-sink.h> |
| 29 | #include <babeltrace/graph/private-component-sink.h> |
| 30 | #include <babeltrace/graph/private-port.h> |
| 31 | #include <babeltrace/graph/private-connection.h> |
| 32 | #include <babeltrace/graph/private-connection-notification-iterator.h> |
| 33 | #include <babeltrace/graph/private-component.h> |
| 34 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
| 35 | #include <babeltrace/assert-internal.h> |
| 36 | #include <glib.h> |
| 37 | |
| 38 | static |
| 39 | struct bt_component_class *colander_comp_cls; |
| 40 | |
| 41 | struct colander_data { |
| 42 | bt_notification_array notifs; |
| 43 | uint64_t *count_addr; |
| 44 | struct bt_notification_iterator *notif_iter; |
| 45 | }; |
| 46 | |
| 47 | static |
| 48 | enum bt_component_status colander_init( |
| 49 | struct bt_private_component *priv_comp, |
| 50 | struct bt_value *params, void *init_method_data) |
| 51 | { |
| 52 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 53 | struct colander_data *colander_data = NULL; |
| 54 | struct bt_component_class_sink_colander_data *user_provided_data = |
| 55 | init_method_data; |
| 56 | |
| 57 | if (!init_method_data) { |
| 58 | BT_LOGW_STR("Component initialization method data is NULL."); |
| 59 | status = BT_COMPONENT_STATUS_INVALID; |
| 60 | goto end; |
| 61 | } |
| 62 | |
| 63 | colander_data = g_new0(struct colander_data, 1); |
| 64 | if (!colander_data) { |
| 65 | BT_LOGE_STR("Failed to allocate colander data."); |
| 66 | status = BT_COMPONENT_STATUS_NOMEM; |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | colander_data->notifs = user_provided_data->notifs; |
| 71 | colander_data->count_addr = user_provided_data->count_addr; |
| 72 | status = bt_private_component_sink_add_input_private_port( |
| 73 | priv_comp, "in", NULL, NULL); |
| 74 | if (status != BT_COMPONENT_STATUS_OK) { |
| 75 | BT_LOGE_STR("Cannot add input port."); |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | (void) bt_private_component_set_user_data(priv_comp, colander_data); |
| 80 | |
| 81 | end: |
| 82 | return status; |
| 83 | } |
| 84 | |
| 85 | static |
| 86 | void colander_finalize(struct bt_private_component *priv_comp) |
| 87 | { |
| 88 | struct colander_data *colander_data = |
| 89 | bt_private_component_get_user_data(priv_comp); |
| 90 | |
| 91 | if (!colander_data) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (colander_data->notif_iter) { |
| 96 | bt_put(colander_data->notif_iter); |
| 97 | } |
| 98 | |
| 99 | g_free(colander_data); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | enum bt_component_status colander_port_connected(struct bt_private_component *priv_comp, |
| 104 | struct bt_private_port *self_priv_port, |
| 105 | struct bt_port *other_port) |
| 106 | { |
| 107 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 108 | enum bt_connection_status conn_status; |
| 109 | struct bt_private_connection *priv_conn = |
| 110 | bt_private_port_get_private_connection(self_priv_port); |
| 111 | struct colander_data *colander_data = |
| 112 | bt_private_component_get_user_data(priv_comp); |
| 113 | |
| 114 | BT_ASSERT(priv_conn); |
| 115 | BT_ASSERT(colander_data); |
| 116 | BT_PUT(colander_data->notif_iter); |
| 117 | conn_status = bt_private_connection_create_notification_iterator( |
| 118 | priv_conn, &colander_data->notif_iter); |
| 119 | if (conn_status) { |
| 120 | BT_LOGE("Cannot create notification iterator from connection: " |
| 121 | "comp-addr=%p, conn-addr=%p", priv_comp, priv_conn); |
| 122 | status = BT_COMPONENT_STATUS_ERROR; |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | end: |
| 127 | bt_put(priv_conn); |
| 128 | return status; |
| 129 | } |
| 130 | |
| 131 | static |
| 132 | enum bt_component_status colander_consume( |
| 133 | struct bt_private_component *priv_comp) |
| 134 | { |
| 135 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 136 | enum bt_notification_iterator_status notif_iter_status; |
| 137 | struct colander_data *colander_data = |
| 138 | bt_private_component_get_user_data(priv_comp); |
| 139 | bt_notification_array notifs; |
| 140 | |
| 141 | BT_ASSERT(colander_data); |
| 142 | |
| 143 | if (!colander_data->notif_iter) { |
| 144 | BT_LOGW("Trying to consume without an upstream notification iterator: " |
| 145 | "comp-addr=%p", priv_comp); |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | notif_iter_status = bt_private_connection_notification_iterator_next( |
| 150 | colander_data->notif_iter, ¬ifs, colander_data->count_addr); |
| 151 | switch (notif_iter_status) { |
| 152 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: |
| 153 | status = BT_COMPONENT_STATUS_OK; |
| 154 | goto end; |
| 155 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 156 | status = BT_COMPONENT_STATUS_AGAIN; |
| 157 | goto end; |
| 158 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 159 | status = BT_COMPONENT_STATUS_END; |
| 160 | goto end; |
| 161 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
| 162 | /* Move notifications to user (count already set) */ |
| 163 | memcpy(colander_data->notifs, notifs, |
| 164 | sizeof(*notifs) * *colander_data->count_addr); |
| 165 | break; |
| 166 | default: |
| 167 | status = BT_COMPONENT_STATUS_ERROR; |
| 168 | goto end; |
| 169 | } |
| 170 | |
| 171 | end: |
| 172 | /* Move notification to user's pointer, even if NULL. */ |
| 173 | return status; |
| 174 | } |
| 175 | |
| 176 | struct bt_component_class *bt_component_class_sink_colander_get(void) |
| 177 | { |
| 178 | if (colander_comp_cls) { |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | colander_comp_cls = bt_component_class_sink_create("colander", |
| 183 | colander_consume); |
| 184 | if (!colander_comp_cls) { |
| 185 | BT_LOGE_STR("Cannot create sink colander component class."); |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | (void) bt_component_class_set_init_method(colander_comp_cls, |
| 190 | colander_init); |
| 191 | (void) bt_component_class_set_finalize_method(colander_comp_cls, |
| 192 | colander_finalize); |
| 193 | (void) bt_component_class_set_port_connected_method(colander_comp_cls, |
| 194 | colander_port_connected); |
| 195 | (void) bt_component_class_freeze(colander_comp_cls); |
| 196 | |
| 197 | end: |
| 198 | return bt_get(colander_comp_cls); |
| 199 | } |
| 200 | |
| 201 | __attribute__((destructor)) static |
| 202 | void put_colander(void) { |
| 203 | BT_PUT(colander_comp_cls); |
| 204 | } |