lib: make message iterator creation functions return a status
[babeltrace.git] / src / plugins / utils / dummy / dummy.c
CommitLineData
e0dfa761
PP
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
71c5da58 23#include <babeltrace2/babeltrace.h>
85e7137b 24#include "common/macros.h"
57952005 25#include "common/assert.h"
c2b71c92
JG
26#include "dummy.h"
27
1043fdea
PP
28static
29const char * const in_port_name = "in";
30
c2b71c92
JG
31void destroy_private_dummy_data(struct dummy *dummy)
32{
b09a5592 33 bt_self_component_port_input_message_iterator_put_ref(dummy->msg_iter);
c2b71c92 34 g_free(dummy);
834e9996 35
c2b71c92
JG
36}
37
834e9996 38BT_HIDDEN
8eee8ea2 39void dummy_finalize(bt_self_component_sink *comp)
c2b71c92
JG
40{
41 struct dummy *dummy;
42
834e9996
PP
43 BT_ASSERT(comp);
44 dummy = bt_self_component_get_data(
bb61965b 45 bt_self_component_sink_as_self_component(comp));
8b45963b 46 BT_ASSERT(dummy);
c2b71c92
JG
47 destroy_private_dummy_data(dummy);
48}
49
834e9996 50BT_HIDDEN
fb25b9e3 51bt_component_class_init_method_status dummy_init(
8eee8ea2
PP
52 bt_self_component_sink *component,
53 const bt_value *params,
f807570c 54 __attribute__((unused)) void *init_method_data)
c2b71c92 55{
fb25b9e3
PP
56 bt_component_class_init_method_status status =
57 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
58 bt_self_component_add_port_status add_port_status;
c2b71c92
JG
59 struct dummy *dummy = g_new0(struct dummy, 1);
60
61 if (!dummy) {
fb25b9e3 62 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
c2b71c92
JG
63 goto end;
64 }
65
fb25b9e3 66 add_port_status = bt_self_component_sink_add_input_port(component,
147337a3 67 "in", NULL, NULL);
fb25b9e3
PP
68 switch (add_port_status) {
69 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
70 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
c2b71c92 71 goto error;
fb25b9e3
PP
72 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
73 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
74 goto error;
75 default:
76 break;
c2b71c92 77 }
d83d78e7 78
834e9996 79 bt_self_component_set_data(
bb61965b 80 bt_self_component_sink_as_self_component(component), dummy);
d83d78e7
PP
81 goto end;
82
c2b71c92
JG
83error:
84 destroy_private_dummy_data(dummy);
d83d78e7
PP
85
86end:
fb25b9e3 87 return status;
c2b71c92
JG
88}
89
834e9996 90BT_HIDDEN
fb25b9e3 91bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
1043fdea 92 bt_self_component_sink *comp)
c2b71c92 93{
ab8b2b1b
SM
94 bt_component_class_sink_graph_is_configured_method_status status;
95 bt_self_component_port_input_message_iterator_create_from_sink_component_status
96 msg_iter_status;
c2b71c92 97 struct dummy *dummy;
b09a5592 98 bt_self_component_port_input_message_iterator *iterator;
c2b71c92 99
834e9996 100 dummy = bt_self_component_get_data(
bb61965b 101 bt_self_component_sink_as_self_component(comp));
8b45963b 102 BT_ASSERT(dummy);
ab8b2b1b 103 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
692f1a01 104 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
ab8b2b1b
SM
105 in_port_name), &iterator);
106 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
107 status = (int) msg_iter_status;
c2b71c92
JG
108 goto end;
109 }
110
b09a5592
PP
111 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
112 dummy->msg_iter, iterator);
0d8b4d8e 113
ab8b2b1b
SM
114 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
115
c2b71c92 116end:
634f394c 117 return status;
c2b71c92 118}
e0dfa761 119
834e9996 120BT_HIDDEN
fb25b9e3 121bt_component_class_sink_consume_method_status dummy_consume(
8eee8ea2 122 bt_self_component_sink *component)
e0dfa761 123{
fb25b9e3
PP
124 bt_component_class_sink_consume_method_status status =
125 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
b09a5592 126 bt_message_array_const msgs;
3fd7b79d 127 uint64_t count;
c2b71c92 128 struct dummy *dummy;
fb25b9e3 129 bt_message_iterator_next_status next_status;
3fd7b79d 130 uint64_t i;
e0dfa761 131
834e9996 132 dummy = bt_self_component_get_data(
bb61965b 133 bt_self_component_sink_as_self_component(component));
8b45963b 134 BT_ASSERT(dummy);
e0dfa761 135
85e7137b 136 if (G_UNLIKELY(!dummy->msg_iter)) {
fb25b9e3 137 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d83d78e7 138 goto end;
e0dfa761
PP
139 }
140
b09a5592 141 /* Consume one message */
fb25b9e3 142 next_status = bt_self_component_port_input_message_iterator_next(
b09a5592 143 dummy->msg_iter, &msgs, &count);
fb25b9e3
PP
144 switch (next_status) {
145 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
146 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
3fd7b79d
PP
147
148 for (i = 0; i < count; i++) {
b09a5592 149 bt_message_put_ref(msgs[i]);
3fd7b79d
PP
150 }
151
152 break;
fb25b9e3
PP
153 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
154 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
d83d78e7 155 goto end;
fb25b9e3
PP
156 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
157 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d83d78e7 158 goto end;
fb25b9e3
PP
159 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
160 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
834e9996 161 goto end;
fb25b9e3
PP
162 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
163 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
3fd7b79d 164 goto end;
d83d78e7
PP
165 default:
166 break;
e0dfa761 167 }
d83d78e7 168
e0dfa761 169end:
fb25b9e3 170 return status;
e0dfa761 171}
This page took 0.061236 seconds and 4 git commands to generate.