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