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