lib: create input port msg iterator from self {msg iterator, sink comp.}
[babeltrace.git] / src / plugins / utils / dummy / dummy.c
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_component_class_init_method_status dummy_init(
52 bt_self_component_sink *component,
53 const bt_value *params,
54 __attribute__((unused)) void *init_method_data)
55 {
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;
59 struct dummy *dummy = g_new0(struct dummy, 1);
60
61 if (!dummy) {
62 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
63 goto end;
64 }
65
66 add_port_status = bt_self_component_sink_add_input_port(component,
67 "in", NULL, NULL);
68 switch (add_port_status) {
69 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
70 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
71 goto error;
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;
77 }
78
79 bt_self_component_set_data(
80 bt_self_component_sink_as_self_component(component), dummy);
81 goto end;
82
83 error:
84 destroy_private_dummy_data(dummy);
85
86 end:
87 return status;
88 }
89
90 BT_HIDDEN
91 bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
92 bt_self_component_sink *comp)
93 {
94 bt_component_class_sink_graph_is_configured_method_status status =
95 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
96 struct dummy *dummy;
97 bt_self_component_port_input_message_iterator *iterator;
98
99 dummy = bt_self_component_get_data(
100 bt_self_component_sink_as_self_component(comp));
101 BT_ASSERT(dummy);
102 iterator = bt_self_component_port_input_message_iterator_create_from_sink_component(
103 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
104 in_port_name));
105 if (!iterator) {
106 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR;
107 goto end;
108 }
109
110 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
111 dummy->msg_iter, iterator);
112
113 end:
114 return status;
115 }
116
117 BT_HIDDEN
118 bt_component_class_sink_consume_method_status dummy_consume(
119 bt_self_component_sink *component)
120 {
121 bt_component_class_sink_consume_method_status status =
122 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
123 bt_message_array_const msgs;
124 uint64_t count;
125 struct dummy *dummy;
126 bt_message_iterator_next_status next_status;
127 uint64_t i;
128
129 dummy = bt_self_component_get_data(
130 bt_self_component_sink_as_self_component(component));
131 BT_ASSERT(dummy);
132
133 if (G_UNLIKELY(!dummy->msg_iter)) {
134 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
135 goto end;
136 }
137
138 /* Consume one message */
139 next_status = bt_self_component_port_input_message_iterator_next(
140 dummy->msg_iter, &msgs, &count);
141 switch (next_status) {
142 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
143 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
144
145 for (i = 0; i < count; i++) {
146 bt_message_put_ref(msgs[i]);
147 }
148
149 break;
150 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
151 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
152 goto end;
153 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
154 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
155 goto end;
156 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
157 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
158 goto end;
159 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
160 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
161 goto end;
162 default:
163 break;
164 }
165
166 end:
167 return status;
168 }
This page took 0.035202 seconds and 5 git commands to generate.