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