| 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 | #include <babeltrace/plugin/plugin-dev.h> |
| 24 | #include <babeltrace/graph/component.h> |
| 25 | #include <babeltrace/graph/private-component.h> |
| 26 | #include <babeltrace/graph/private-port.h> |
| 27 | #include <babeltrace/graph/port.h> |
| 28 | #include <babeltrace/graph/private-connection.h> |
| 29 | #include <babeltrace/graph/component-sink.h> |
| 30 | #include <babeltrace/graph/notification-iterator.h> |
| 31 | #include <babeltrace/graph/notification.h> |
| 32 | #include <babeltrace/babeltrace-internal.h> |
| 33 | #include <plugins-common.h> |
| 34 | #include <assert.h> |
| 35 | #include "dummy.h" |
| 36 | |
| 37 | static |
| 38 | void destroy_private_dummy_data(struct dummy *dummy) |
| 39 | { |
| 40 | if (dummy->iterators) { |
| 41 | g_ptr_array_free(dummy->iterators, TRUE); |
| 42 | } |
| 43 | g_free(dummy); |
| 44 | } |
| 45 | |
| 46 | void dummy_finalize(struct bt_private_component *component) |
| 47 | { |
| 48 | struct dummy *dummy; |
| 49 | |
| 50 | assert(component); |
| 51 | dummy = bt_private_component_get_user_data(component); |
| 52 | assert(dummy); |
| 53 | destroy_private_dummy_data(dummy); |
| 54 | } |
| 55 | |
| 56 | enum bt_component_status dummy_init(struct bt_private_component *component, |
| 57 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
| 58 | { |
| 59 | enum bt_component_status ret; |
| 60 | struct dummy *dummy = g_new0(struct dummy, 1); |
| 61 | |
| 62 | if (!dummy) { |
| 63 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | dummy->iterators = g_ptr_array_new_with_free_func( |
| 68 | (GDestroyNotify) bt_put); |
| 69 | if (!dummy->iterators) { |
| 70 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | ret = bt_private_component_set_user_data(component, dummy); |
| 75 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 76 | goto error; |
| 77 | } |
| 78 | end: |
| 79 | return ret; |
| 80 | error: |
| 81 | destroy_private_dummy_data(dummy); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | void dummy_port_connected( |
| 86 | struct bt_private_component *component, |
| 87 | struct bt_private_port *self_port, |
| 88 | struct bt_port *other_port) |
| 89 | { |
| 90 | struct dummy *dummy; |
| 91 | struct bt_notification_iterator *iterator; |
| 92 | struct bt_private_connection *connection; |
| 93 | |
| 94 | dummy = bt_private_component_get_user_data(component); |
| 95 | assert(dummy); |
| 96 | connection = bt_private_port_get_private_connection(self_port); |
| 97 | assert(connection); |
| 98 | iterator = bt_private_connection_create_notification_iterator( |
| 99 | connection); |
| 100 | if (!iterator) { |
| 101 | dummy->error = true; |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | g_ptr_array_add(dummy->iterators, iterator); |
| 106 | |
| 107 | end: |
| 108 | bt_put(connection); |
| 109 | } |
| 110 | |
| 111 | enum bt_component_status dummy_consume(struct bt_private_component *component) |
| 112 | { |
| 113 | enum bt_component_status ret; |
| 114 | struct bt_notification *notif = NULL; |
| 115 | size_t i; |
| 116 | struct dummy *dummy; |
| 117 | |
| 118 | dummy = bt_private_component_get_user_data(component); |
| 119 | assert(dummy); |
| 120 | |
| 121 | if (unlikely(dummy->error)) { |
| 122 | ret = BT_COMPONENT_STATUS_ERROR; |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | /* Consume one notification from each iterator. */ |
| 127 | for (i = 0; i < dummy->iterators->len; i++) { |
| 128 | struct bt_notification_iterator *it; |
| 129 | enum bt_notification_iterator_status it_ret; |
| 130 | |
| 131 | it = g_ptr_array_index(dummy->iterators, i); |
| 132 | |
| 133 | it_ret = bt_notification_iterator_next(it); |
| 134 | switch (it_ret) { |
| 135 | case BT_NOTIFICATION_ITERATOR_STATUS_ERROR: |
| 136 | ret = BT_COMPONENT_STATUS_ERROR; |
| 137 | goto end; |
| 138 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 139 | ret = BT_COMPONENT_STATUS_END; |
| 140 | g_ptr_array_remove_index(dummy->iterators, i); |
| 141 | i--; |
| 142 | continue; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | notif = bt_notification_iterator_get_notification(it); |
| 148 | if (!notif) { |
| 149 | ret = BT_COMPONENT_STATUS_ERROR; |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Dummy! I'm doing nothing with this notification, |
| 155 | * NOTHING. |
| 156 | */ |
| 157 | BT_PUT(notif); |
| 158 | } |
| 159 | |
| 160 | if (dummy->iterators->len == 0) { |
| 161 | ret = BT_COMPONENT_STATUS_END; |
| 162 | } |
| 163 | end: |
| 164 | bt_put(notif); |
| 165 | return ret; |
| 166 | } |