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