Use graph facilities in dummy sink component class
[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>
24#include <babeltrace/component/component.h>
25#include <babeltrace/component/component-sink.h>
26#include <babeltrace/component/notification/iterator.h>
27#include <babeltrace/component/notification/notification.h>
c2b71c92 28#include <plugins-common.h>
e0dfa761 29#include <assert.h>
c2b71c92
JG
30#include "dummy.h"
31
32static
33void destroy_private_dummy_data(struct dummy *dummy)
34{
35 if (dummy->iterators) {
36 g_ptr_array_free(dummy->iterators, TRUE);
37 }
38 g_free(dummy);
39}
40
41void dummy_destroy(struct bt_component *component)
42{
43 struct dummy *dummy;
44
45 assert(component);
46 dummy = bt_component_get_private_data(component);
47 assert(dummy);
48 destroy_private_dummy_data(dummy);
49}
50
51enum bt_component_status dummy_init(struct bt_component *component,
52 struct bt_value *params, UNUSED_VAR void *init_method_data)
53{
54 enum bt_component_status ret;
55 struct dummy *dummy = g_new0(struct dummy, 1);
56
57 if (!dummy) {
58 ret = BT_COMPONENT_STATUS_NOMEM;
59 goto end;
60 }
61
62 dummy->iterators = g_ptr_array_new_with_free_func(
63 (GDestroyNotify) bt_put);
64 if (!dummy->iterators) {
65 ret = BT_COMPONENT_STATUS_NOMEM;
66 goto end;
67 }
68
69 ret = bt_component_set_private_data(component, dummy);
70 if (ret != BT_COMPONENT_STATUS_OK) {
71 goto error;
72 }
73end:
74 return ret;
75error:
76 destroy_private_dummy_data(dummy);
77 return ret;
78}
79
80enum bt_component_status dummy_new_connection(struct bt_port *own_port,
81 struct bt_connection *connection)
82{
83 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
84 struct bt_component *component;
85 struct dummy *dummy;
86 struct bt_notification_iterator *iterator;
87
88 component = bt_port_get_component(own_port);
89 assert(component);
90
91 dummy = bt_component_get_private_data(component);
92 assert(dummy);
93
94 iterator = bt_connection_create_notification_iterator(connection);
95 if (!iterator) {
96 ret = BT_COMPONENT_STATUS_ERROR;
97 goto end;
98 }
99
100 g_ptr_array_add(dummy->iterators, iterator);
101end:
102 bt_put(component);
103 return ret;
104}
e0dfa761
PP
105
106enum bt_component_status dummy_consume(struct bt_component *component)
107{
108 enum bt_component_status ret;
109 struct bt_notification *notif = NULL;
e0dfa761 110 size_t i;
c2b71c92 111 struct dummy *dummy;
e0dfa761 112
c2b71c92
JG
113 dummy = bt_component_get_private_data(component);
114 assert(dummy);
e0dfa761 115
c2b71c92
JG
116 /* Consume one notification from each iterator. */
117 for (i = 0; i < dummy->iterators->len; i++) {
118 struct bt_notification_iterator *it;
e0dfa761
PP
119 enum bt_notification_iterator_status it_ret;
120
c2b71c92
JG
121 it = g_ptr_array_index(dummy->iterators, i);
122
e0dfa761
PP
123 it_ret = bt_notification_iterator_next(it);
124 switch (it_ret) {
125 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
126 ret = BT_COMPONENT_STATUS_ERROR;
127 goto end;
128 case BT_NOTIFICATION_ITERATOR_STATUS_END:
129 ret = BT_COMPONENT_STATUS_END;
c2b71c92
JG
130 g_ptr_array_remove_index(dummy->iterators, i);
131 i--;
e0dfa761
PP
132 continue;
133 default:
134 break;
135 }
136
137 notif = bt_notification_iterator_get_notification(it);
138 if (!notif) {
139 ret = BT_COMPONENT_STATUS_ERROR;
140 goto end;
141 }
142
143 /*
144 * Dummy! I'm doing nothing with this notification,
145 * NOTHING.
146 */
e0dfa761
PP
147 BT_PUT(notif);
148 }
149
c2b71c92 150 if (dummy->iterators->len == 0) {
e0dfa761
PP
151 ret = BT_COMPONENT_STATUS_END;
152 }
e0dfa761 153end:
e0dfa761
PP
154 bt_put(notif);
155 return ret;
156}
This page took 0.030129 seconds and 4 git commands to generate.