| 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 <babeltrace2/babeltrace.h> |
| 24 | #include "common/macros.h" |
| 25 | #include "common/assert.h" |
| 26 | #include "dummy.h" |
| 27 | |
| 28 | static |
| 29 | const char * const in_port_name = "in"; |
| 30 | |
| 31 | void destroy_private_dummy_data(struct dummy *dummy) |
| 32 | { |
| 33 | bt_self_component_port_input_message_iterator_put_ref(dummy->msg_iter); |
| 34 | g_free(dummy); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | BT_HIDDEN |
| 39 | void dummy_finalize(bt_self_component_sink *comp) |
| 40 | { |
| 41 | struct dummy *dummy; |
| 42 | |
| 43 | BT_ASSERT(comp); |
| 44 | dummy = bt_self_component_get_data( |
| 45 | bt_self_component_sink_as_self_component(comp)); |
| 46 | BT_ASSERT(dummy); |
| 47 | destroy_private_dummy_data(dummy); |
| 48 | } |
| 49 | |
| 50 | BT_HIDDEN |
| 51 | bt_self_component_status dummy_init( |
| 52 | bt_self_component_sink *component, |
| 53 | const bt_value *params, |
| 54 | __attribute__((unused)) void *init_method_data) |
| 55 | { |
| 56 | bt_self_component_status ret; |
| 57 | struct dummy *dummy = g_new0(struct dummy, 1); |
| 58 | |
| 59 | if (!dummy) { |
| 60 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | ret = bt_self_component_sink_add_input_port(component, |
| 65 | "in", NULL, NULL); |
| 66 | if (ret != BT_SELF_COMPONENT_STATUS_OK) { |
| 67 | goto error; |
| 68 | } |
| 69 | |
| 70 | bt_self_component_set_data( |
| 71 | bt_self_component_sink_as_self_component(component), dummy); |
| 72 | goto end; |
| 73 | |
| 74 | error: |
| 75 | destroy_private_dummy_data(dummy); |
| 76 | |
| 77 | end: |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | BT_HIDDEN |
| 82 | bt_self_component_status dummy_graph_is_configured( |
| 83 | bt_self_component_sink *comp) |
| 84 | { |
| 85 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 86 | struct dummy *dummy; |
| 87 | bt_self_component_port_input_message_iterator *iterator; |
| 88 | |
| 89 | dummy = bt_self_component_get_data( |
| 90 | bt_self_component_sink_as_self_component(comp)); |
| 91 | BT_ASSERT(dummy); |
| 92 | iterator = bt_self_component_port_input_message_iterator_create( |
| 93 | bt_self_component_sink_borrow_input_port_by_name(comp, |
| 94 | in_port_name)); |
| 95 | if (!iterator) { |
| 96 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
| 101 | dummy->msg_iter, iterator); |
| 102 | |
| 103 | end: |
| 104 | return status; |
| 105 | } |
| 106 | |
| 107 | BT_HIDDEN |
| 108 | bt_self_component_status dummy_consume( |
| 109 | bt_self_component_sink *component) |
| 110 | { |
| 111 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
| 112 | bt_message_array_const msgs; |
| 113 | uint64_t count; |
| 114 | struct dummy *dummy; |
| 115 | bt_message_iterator_status it_ret; |
| 116 | uint64_t i; |
| 117 | |
| 118 | dummy = bt_self_component_get_data( |
| 119 | bt_self_component_sink_as_self_component(component)); |
| 120 | BT_ASSERT(dummy); |
| 121 | |
| 122 | if (G_UNLIKELY(!dummy->msg_iter)) { |
| 123 | ret = BT_SELF_COMPONENT_STATUS_END; |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | /* Consume one message */ |
| 128 | it_ret = bt_self_component_port_input_message_iterator_next( |
| 129 | dummy->msg_iter, &msgs, &count); |
| 130 | switch (it_ret) { |
| 131 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 132 | ret = BT_SELF_COMPONENT_STATUS_OK; |
| 133 | |
| 134 | for (i = 0; i < count; i++) { |
| 135 | bt_message_put_ref(msgs[i]); |
| 136 | } |
| 137 | |
| 138 | break; |
| 139 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 140 | ret = BT_SELF_COMPONENT_STATUS_AGAIN; |
| 141 | goto end; |
| 142 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 143 | ret = BT_SELF_COMPONENT_STATUS_END; |
| 144 | goto end; |
| 145 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
| 146 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
| 147 | goto end; |
| 148 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 149 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 150 | goto end; |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | end: |
| 156 | return ret; |
| 157 | } |