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