Commit | Line | Data |
---|---|---|
47e5a032 JG |
1 | /* |
2 | * iterator.c | |
3 | * | |
4 | * Babeltrace Notification Iterator | |
5 | * | |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3230ee6b | 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
47e5a032 JG |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
5af447e5 PP |
28 | #define BT_LOG_TAG "NOTIF-ITER" |
29 | #include <babeltrace/lib-logging-internal.h> | |
30 | ||
3d9990ac | 31 | #include <babeltrace/compiler-internal.h> |
b8a06801 | 32 | #include <babeltrace/ref.h> |
2ec84d26 PP |
33 | #include <babeltrace/ctf-ir/fields.h> |
34 | #include <babeltrace/ctf-ir/field-types.h> | |
35 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
3230ee6b PP |
36 | #include <babeltrace/ctf-ir/event-internal.h> |
37 | #include <babeltrace/ctf-ir/packet-internal.h> | |
38 | #include <babeltrace/ctf-ir/stream-internal.h> | |
73d5c1ad | 39 | #include <babeltrace/graph/connection.h> |
bd14d768 | 40 | #include <babeltrace/graph/connection-internal.h> |
b2e0c907 PP |
41 | #include <babeltrace/graph/component.h> |
42 | #include <babeltrace/graph/component-source-internal.h> | |
43 | #include <babeltrace/graph/component-class-internal.h> | |
8ed535b5 PP |
44 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
45 | #include <babeltrace/graph/component-sink.h> | |
fa054faf | 46 | #include <babeltrace/graph/notification.h> |
b2e0c907 PP |
47 | #include <babeltrace/graph/notification-iterator.h> |
48 | #include <babeltrace/graph/notification-iterator-internal.h> | |
e7fa96c3 | 49 | #include <babeltrace/graph/notification-internal.h> |
3230ee6b PP |
50 | #include <babeltrace/graph/notification-event.h> |
51 | #include <babeltrace/graph/notification-event-internal.h> | |
52 | #include <babeltrace/graph/notification-packet.h> | |
53 | #include <babeltrace/graph/notification-packet-internal.h> | |
54 | #include <babeltrace/graph/notification-stream.h> | |
55 | #include <babeltrace/graph/notification-stream-internal.h> | |
2ec84d26 | 56 | #include <babeltrace/graph/notification-discarded-elements-internal.h> |
3230ee6b | 57 | #include <babeltrace/graph/port.h> |
8ed535b5 | 58 | #include <babeltrace/graph/graph-internal.h> |
c55a9f58 | 59 | #include <babeltrace/types.h> |
f6ccaed9 | 60 | #include <babeltrace/assert-internal.h> |
f42867e2 | 61 | #include <babeltrace/assert-pre-internal.h> |
fa054faf | 62 | #include <stdint.h> |
2ec84d26 | 63 | #include <inttypes.h> |
0fbb9a9f | 64 | #include <stdlib.h> |
3230ee6b | 65 | |
2ec84d26 | 66 | struct discarded_elements_state { |
50842bdc | 67 | struct bt_clock_value *cur_begin; |
2ec84d26 PP |
68 | uint64_t cur_count; |
69 | }; | |
70 | ||
3230ee6b | 71 | struct stream_state { |
50842bdc PP |
72 | struct bt_stream *stream; /* owned by this */ |
73 | struct bt_packet *cur_packet; /* owned by this */ | |
2ec84d26 PP |
74 | struct discarded_elements_state discarded_packets_state; |
75 | struct discarded_elements_state discarded_events_state; | |
f42867e2 | 76 | uint64_t expected_notif_seq_num; |
c55a9f58 | 77 | bt_bool is_ended; |
3230ee6b PP |
78 | }; |
79 | ||
3230ee6b | 80 | static |
3dca2276 | 81 | void stream_destroy_listener(struct bt_stream_common *stream, void *data) |
3230ee6b | 82 | { |
90157d89 | 83 | struct bt_notification_iterator_private_connection *iterator = data; |
3230ee6b PP |
84 | |
85 | /* Remove associated stream state */ | |
86 | g_hash_table_remove(iterator->stream_states, stream); | |
87 | } | |
88 | ||
89 | static | |
90 | void destroy_stream_state(struct stream_state *stream_state) | |
91 | { | |
92 | if (!stream_state) { | |
93 | return; | |
94 | } | |
95 | ||
5af447e5 PP |
96 | BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state); |
97 | BT_LOGV_STR("Putting stream state's current packet."); | |
3230ee6b | 98 | bt_put(stream_state->cur_packet); |
5af447e5 | 99 | BT_LOGV_STR("Putting stream state's stream."); |
3230ee6b | 100 | bt_put(stream_state->stream); |
2ec84d26 PP |
101 | bt_put(stream_state->discarded_packets_state.cur_begin); |
102 | bt_put(stream_state->discarded_events_state.cur_begin); | |
3230ee6b PP |
103 | g_free(stream_state); |
104 | } | |
105 | ||
3230ee6b | 106 | static |
50842bdc | 107 | struct stream_state *create_stream_state(struct bt_stream *stream) |
3230ee6b PP |
108 | { |
109 | struct stream_state *stream_state = g_new0(struct stream_state, 1); | |
110 | ||
111 | if (!stream_state) { | |
5af447e5 | 112 | BT_LOGE_STR("Failed to allocate one stream state."); |
3230ee6b PP |
113 | goto end; |
114 | } | |
115 | ||
126215b4 MD |
116 | /* |
117 | * The packet index is a monotonic counter which may not start | |
118 | * at 0 at the beginning of the stream. We therefore need to | |
119 | * have an internal object initial state of -1ULL to distinguish | |
120 | * between initial state and having seen a packet with | |
f42867e2 | 121 | * the sequence number 0. |
126215b4 MD |
122 | */ |
123 | stream_state->discarded_packets_state.cur_count = -1ULL; | |
124 | ||
3230ee6b | 125 | /* |
f42867e2 | 126 | * We keep a reference to the stream until we know it's ended. |
3230ee6b PP |
127 | */ |
128 | stream_state->stream = bt_get(stream); | |
5af447e5 PP |
129 | BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", " |
130 | "stream-state-addr=%p", | |
50842bdc | 131 | stream, bt_stream_get_name(stream), stream_state); |
3230ee6b PP |
132 | |
133 | end: | |
134 | return stream_state; | |
135 | } | |
47e5a032 | 136 | |
8ed535b5 PP |
137 | static |
138 | void destroy_base_notification_iterator(struct bt_object *obj) | |
139 | { | |
140 | struct bt_notification_iterator *iterator = | |
141 | container_of(obj, struct bt_notification_iterator, base); | |
142 | ||
143 | BT_LOGD_STR("Putting current notification."); | |
144 | bt_put(iterator->current_notification); | |
145 | g_free(iterator); | |
146 | } | |
147 | ||
47e5a032 | 148 | static |
90157d89 | 149 | void bt_private_connection_notification_iterator_destroy(struct bt_object *obj) |
47e5a032 | 150 | { |
90157d89 | 151 | struct bt_notification_iterator_private_connection *iterator; |
8738a040 | 152 | |
f6ccaed9 | 153 | BT_ASSERT(obj); |
d3eb6e8f | 154 | |
bd14d768 PP |
155 | /* |
156 | * The notification iterator's reference count is 0 if we're | |
157 | * here. Increment it to avoid a double-destroy (possibly | |
158 | * infinitely recursive). This could happen for example if the | |
159 | * notification iterator's finalization function does bt_get() | |
160 | * (or anything that causes bt_get() to be called) on itself | |
161 | * (ref. count goes from 0 to 1), and then bt_put(): the | |
162 | * reference count would go from 1 to 0 again and this function | |
163 | * would be called again. | |
164 | */ | |
165 | obj->ref_count.count++; | |
90157d89 | 166 | iterator = (void *) container_of(obj, struct bt_notification_iterator, base); |
8ed535b5 | 167 | BT_LOGD("Destroying private connection notification iterator object: addr=%p", |
5af447e5 | 168 | iterator); |
90157d89 | 169 | bt_private_connection_notification_iterator_finalize(iterator); |
d3eb6e8f | 170 | |
3230ee6b PP |
171 | if (iterator->stream_states) { |
172 | /* | |
173 | * Remove our destroy listener from each stream which | |
174 | * has a state in this iterator. Otherwise the destroy | |
175 | * listener would be called with an invalid/other | |
176 | * notification iterator object. | |
177 | */ | |
178 | GHashTableIter ht_iter; | |
179 | gpointer stream_gptr, stream_state_gptr; | |
180 | ||
181 | g_hash_table_iter_init(&ht_iter, iterator->stream_states); | |
182 | ||
183 | while (g_hash_table_iter_next(&ht_iter, &stream_gptr, &stream_state_gptr)) { | |
f6ccaed9 | 184 | BT_ASSERT(stream_gptr); |
5af447e5 PP |
185 | |
186 | BT_LOGD_STR("Removing stream's destroy listener for notification iterator."); | |
3dca2276 | 187 | bt_stream_common_remove_destroy_listener( |
3230ee6b PP |
188 | (void *) stream_gptr, stream_destroy_listener, |
189 | iterator); | |
190 | } | |
191 | ||
192 | g_hash_table_destroy(iterator->stream_states); | |
193 | } | |
194 | ||
bd14d768 PP |
195 | if (iterator->connection) { |
196 | /* | |
197 | * Remove ourself from the originating connection so | |
198 | * that it does not try to finalize a dangling pointer | |
199 | * later. | |
200 | */ | |
201 | bt_connection_remove_iterator(iterator->connection, iterator); | |
202 | } | |
203 | ||
8ed535b5 | 204 | destroy_base_notification_iterator(obj); |
47e5a032 JG |
205 | } |
206 | ||
bd14d768 | 207 | BT_HIDDEN |
90157d89 PP |
208 | void bt_private_connection_notification_iterator_finalize( |
209 | struct bt_notification_iterator_private_connection *iterator) | |
bd14d768 PP |
210 | { |
211 | struct bt_component_class *comp_class = NULL; | |
212 | bt_component_class_notification_iterator_finalize_method | |
213 | finalize_method = NULL; | |
214 | ||
f6ccaed9 | 215 | BT_ASSERT(iterator); |
bd14d768 PP |
216 | |
217 | switch (iterator->state) { | |
90157d89 | 218 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 PP |
219 | /* Skip user finalization if user initialization failed */ |
220 | BT_LOGD("Not finalizing non-initialized notification iterator: " | |
221 | "addr=%p", iterator); | |
222 | return; | |
90157d89 PP |
223 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED: |
224 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED: | |
bd14d768 | 225 | /* Already finalized */ |
5af447e5 PP |
226 | BT_LOGD("Not finalizing notification iterator: already finalized: " |
227 | "addr=%p", iterator); | |
bd14d768 PP |
228 | return; |
229 | default: | |
230 | break; | |
231 | } | |
232 | ||
5af447e5 PP |
233 | BT_LOGD("Finalizing notification iterator: addr=%p", iterator); |
234 | ||
90157d89 | 235 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) { |
5af447e5 | 236 | BT_LOGD("Updating notification iterator's state: " |
90157d89 PP |
237 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED"); |
238 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED; | |
df14f8af | 239 | } else { |
5af447e5 | 240 | BT_LOGD("Updating notification iterator's state: " |
90157d89 PP |
241 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED"); |
242 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED; | |
df14f8af MD |
243 | } |
244 | ||
f6ccaed9 | 245 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
246 | comp_class = iterator->upstream_component->class; |
247 | ||
248 | /* Call user-defined destroy method */ | |
249 | switch (comp_class->type) { | |
250 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
251 | { | |
252 | struct bt_component_class_source *source_class; | |
253 | ||
254 | source_class = container_of(comp_class, struct bt_component_class_source, parent); | |
255 | finalize_method = source_class->methods.iterator.finalize; | |
256 | break; | |
257 | } | |
258 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
259 | { | |
260 | struct bt_component_class_filter *filter_class; | |
261 | ||
262 | filter_class = container_of(comp_class, struct bt_component_class_filter, parent); | |
263 | finalize_method = filter_class->methods.iterator.finalize; | |
264 | break; | |
265 | } | |
266 | default: | |
267 | /* Unreachable */ | |
0fbb9a9f | 268 | abort(); |
bd14d768 PP |
269 | } |
270 | ||
271 | if (finalize_method) { | |
5af447e5 PP |
272 | BT_LOGD("Calling user's finalization method: addr=%p", |
273 | iterator); | |
bd14d768 | 274 | finalize_method( |
90157d89 | 275 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator)); |
bd14d768 PP |
276 | } |
277 | ||
bd14d768 PP |
278 | iterator->upstream_component = NULL; |
279 | iterator->upstream_port = NULL; | |
5af447e5 | 280 | BT_LOGD("Finalized notification iterator: addr=%p", iterator); |
bd14d768 PP |
281 | } |
282 | ||
283 | BT_HIDDEN | |
90157d89 PP |
284 | void bt_private_connection_notification_iterator_set_connection( |
285 | struct bt_notification_iterator_private_connection *iterator, | |
bd14d768 PP |
286 | struct bt_connection *connection) |
287 | { | |
f6ccaed9 | 288 | BT_ASSERT(iterator); |
bd14d768 | 289 | iterator->connection = connection; |
5af447e5 PP |
290 | BT_LOGV("Set notification iterator's connection: " |
291 | "iter-addr=%p, conn-addr=%p", iterator, connection); | |
bd14d768 PP |
292 | } |
293 | ||
90157d89 PP |
294 | static |
295 | void init_notification_iterator(struct bt_notification_iterator *iterator, | |
296 | enum bt_notification_iterator_type type, | |
297 | bt_object_release_func destroy) | |
298 | { | |
299 | bt_object_init(iterator, destroy); | |
300 | iterator->type = type; | |
301 | } | |
302 | ||
47e5a032 | 303 | BT_HIDDEN |
90157d89 | 304 | enum bt_connection_status bt_private_connection_notification_iterator_create( |
3230ee6b | 305 | struct bt_component *upstream_comp, |
fa054faf | 306 | struct bt_port *upstream_port, |
73d5c1ad | 307 | struct bt_connection *connection, |
90157d89 | 308 | struct bt_notification_iterator_private_connection **user_iterator) |
47e5a032 | 309 | { |
73d5c1ad | 310 | enum bt_connection_status status = BT_CONNECTION_STATUS_OK; |
d3e4dcd8 | 311 | enum bt_component_class_type type; |
90157d89 | 312 | struct bt_notification_iterator_private_connection *iterator = NULL; |
47e5a032 | 313 | |
f6ccaed9 PP |
314 | BT_ASSERT(upstream_comp); |
315 | BT_ASSERT(upstream_port); | |
f6ccaed9 PP |
316 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
317 | BT_ASSERT(user_iterator); | |
8ed535b5 | 318 | BT_LOGD("Creating notification iterator on private connection: " |
5af447e5 PP |
319 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
320 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
321 | "conn-addr=%p", | |
322 | upstream_comp, bt_component_get_name(upstream_comp), | |
323 | upstream_port, bt_port_get_name(upstream_port), | |
324 | connection); | |
3230ee6b | 325 | type = bt_component_get_class_type(upstream_comp); |
f6ccaed9 | 326 | BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
ef2f7566 | 327 | type == BT_COMPONENT_CLASS_TYPE_FILTER); |
90157d89 | 328 | iterator = g_new0(struct bt_notification_iterator_private_connection, 1); |
47e5a032 | 329 | if (!iterator) { |
8ed535b5 | 330 | BT_LOGE_STR("Failed to allocate one private connection notification iterator."); |
73d5c1ad PP |
331 | status = BT_CONNECTION_STATUS_NOMEM; |
332 | goto end; | |
47e5a032 JG |
333 | } |
334 | ||
90157d89 PP |
335 | init_notification_iterator((void *) iterator, |
336 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, | |
337 | bt_private_connection_notification_iterator_destroy); | |
3230ee6b PP |
338 | |
339 | iterator->stream_states = g_hash_table_new_full(g_direct_hash, | |
340 | g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state); | |
341 | if (!iterator->stream_states) { | |
5af447e5 | 342 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
73d5c1ad PP |
343 | status = BT_CONNECTION_STATUS_NOMEM; |
344 | goto end; | |
3230ee6b PP |
345 | } |
346 | ||
bd14d768 PP |
347 | iterator->upstream_component = upstream_comp; |
348 | iterator->upstream_port = upstream_port; | |
349 | iterator->connection = connection; | |
5c563278 | 350 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
90157d89 | 351 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED; |
5af447e5 PP |
352 | BT_LOGD("Created notification iterator: " |
353 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
354 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
355 | "conn-addr=%p, iter-addr=%p", | |
356 | upstream_comp, bt_component_get_name(upstream_comp), | |
357 | upstream_port, bt_port_get_name(upstream_port), | |
358 | connection, iterator); | |
1a6a376a PP |
359 | |
360 | /* Move reference to user */ | |
361 | *user_iterator = iterator; | |
362 | iterator = NULL; | |
3230ee6b | 363 | |
47e5a032 | 364 | end: |
73d5c1ad PP |
365 | bt_put(iterator); |
366 | return status; | |
47e5a032 JG |
367 | } |
368 | ||
90157d89 PP |
369 | void *bt_private_connection_private_notification_iterator_get_user_data( |
370 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
ea8d3e58 | 371 | { |
5c563278 | 372 | struct bt_notification_iterator_private_connection *iterator = (void *) |
6d137876 | 373 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
890882ef | 374 | |
f42867e2 PP |
375 | BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator"); |
376 | return iterator->user_data; | |
ea8d3e58 JG |
377 | } |
378 | ||
379 | enum bt_notification_iterator_status | |
90157d89 PP |
380 | bt_private_connection_private_notification_iterator_set_user_data( |
381 | struct bt_private_connection_private_notification_iterator *private_iterator, | |
890882ef | 382 | void *data) |
ea8d3e58 | 383 | { |
5c563278 | 384 | struct bt_notification_iterator_private_connection *iterator = (void *) |
6d137876 | 385 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
ea8d3e58 | 386 | |
f42867e2 | 387 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
ea8d3e58 | 388 | iterator->user_data = data; |
5af447e5 PP |
389 | BT_LOGV("Set notification iterator's user data: " |
390 | "iter-addr=%p, user-data-addr=%p", iterator, data); | |
f42867e2 | 391 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
8738a040 | 392 | } |
413bc2c4 | 393 | |
5c563278 PP |
394 | struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph( |
395 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
396 | { | |
397 | struct bt_notification_iterator_private_connection *iterator = (void *) | |
398 | bt_private_connection_notification_iterator_borrow_from_private( | |
399 | private_iterator); | |
400 | ||
401 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); | |
402 | return iterator->graph; | |
403 | } | |
404 | ||
094ff7c0 | 405 | struct bt_notification *bt_notification_iterator_borrow_notification( |
53d45b87 JG |
406 | struct bt_notification_iterator *iterator) |
407 | { | |
f42867e2 | 408 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
094ff7c0 | 409 | return bt_notification_iterator_borrow_current_notification(iterator); |
53d45b87 JG |
410 | } |
411 | ||
f42867e2 PP |
412 | BT_ASSERT_PRE_FUNC |
413 | static inline | |
414 | void bt_notification_borrow_packet_stream(struct bt_notification *notif, | |
415 | struct bt_stream **stream, struct bt_packet **packet) | |
fa054faf | 416 | { |
f42867e2 | 417 | BT_ASSERT(notif); |
fa054faf | 418 | |
f42867e2 | 419 | switch (notif->type) { |
fa054faf | 420 | case BT_NOTIFICATION_TYPE_EVENT: |
f42867e2 PP |
421 | *packet = bt_event_borrow_packet( |
422 | bt_notification_event_borrow_event(notif)); | |
423 | *stream = bt_packet_borrow_stream(*packet); | |
fa054faf PP |
424 | break; |
425 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
f42867e2 | 426 | *stream = bt_notification_stream_begin_borrow_stream(notif); |
fa054faf PP |
427 | break; |
428 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
f42867e2 | 429 | *stream = bt_notification_stream_end_borrow_stream(notif); |
fa054faf PP |
430 | break; |
431 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
f42867e2 PP |
432 | *packet = bt_notification_packet_begin_borrow_packet(notif); |
433 | *stream = bt_packet_borrow_stream(*packet); | |
fa054faf PP |
434 | break; |
435 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
f42867e2 PP |
436 | *packet = bt_notification_packet_end_borrow_packet(notif); |
437 | *stream = bt_packet_borrow_stream(*packet); | |
2ec84d26 | 438 | break; |
fa054faf | 439 | default: |
f42867e2 | 440 | break; |
fa054faf | 441 | } |
fa054faf PP |
442 | } |
443 | ||
f42867e2 PP |
444 | BT_ASSERT_PRE_FUNC |
445 | static inline | |
446 | bool validate_notification( | |
90157d89 | 447 | struct bt_notification_iterator_private_connection *iterator, |
f42867e2 | 448 | struct bt_notification *notif) |
3230ee6b | 449 | { |
f42867e2 | 450 | bool is_valid = true; |
3230ee6b | 451 | struct stream_state *stream_state; |
f42867e2 PP |
452 | struct bt_stream *stream = NULL; |
453 | struct bt_packet *packet = NULL; | |
454 | ||
455 | BT_ASSERT(notif); | |
456 | bt_notification_borrow_packet_stream(notif, &stream, &packet); | |
457 | ||
458 | if (!stream) { | |
459 | /* we don't care about notifications not attached to streams */ | |
460 | goto end; | |
461 | } | |
3230ee6b | 462 | |
f42867e2 PP |
463 | stream_state = g_hash_table_lookup(iterator->stream_states, stream); |
464 | if (!stream_state) { | |
3230ee6b | 465 | /* |
f42867e2 PP |
466 | * No stream state for this stream: this notification |
467 | * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification | |
468 | * and its sequence number must be 0. | |
3230ee6b | 469 | */ |
f42867e2 PP |
470 | if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) { |
471 | BT_ASSERT_PRE_MSG("Unexpected notification: missing a " | |
472 | "BT_NOTIFICATION_TYPE_STREAM_BEGIN " | |
473 | "notification prior to this notification: " | |
474 | "%![stream-]+s", stream); | |
475 | is_valid = false; | |
3230ee6b PP |
476 | goto end; |
477 | } | |
478 | ||
f42867e2 PP |
479 | if (notif->seq_num == -1ULL) { |
480 | notif->seq_num = 0; | |
3230ee6b PP |
481 | } |
482 | ||
f42867e2 PP |
483 | if (notif->seq_num != 0) { |
484 | BT_ASSERT_PRE_MSG("Unexpected notification sequence " | |
485 | "number for this notification iterator: " | |
486 | "this is the first notification for this " | |
487 | "stream, expecting sequence number 0: " | |
488 | "seq-num=%" PRIu64 ", %![stream-]+s", | |
489 | notif->seq_num, stream); | |
490 | is_valid = false; | |
3230ee6b | 491 | goto end; |
3230ee6b | 492 | } |
3230ee6b | 493 | |
f42867e2 PP |
494 | stream_state = create_stream_state(stream); |
495 | if (!stream_state) { | |
496 | abort(); | |
497 | } | |
fa054faf | 498 | |
f42867e2 PP |
499 | g_hash_table_insert(iterator->stream_states, stream, |
500 | stream_state); | |
501 | stream_state->expected_notif_seq_num++; | |
502 | goto end; | |
fa054faf PP |
503 | } |
504 | ||
f42867e2 PP |
505 | if (stream_state->is_ended) { |
506 | /* | |
507 | * There's a new notification which has a reference to a | |
508 | * stream which, from this iterator's point of view, is | |
509 | * ended ("end of stream" notification was returned). | |
510 | * This is bad: the API guarantees that it can never | |
511 | * happen. | |
512 | */ | |
513 | BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s", | |
514 | stream); | |
515 | is_valid = false; | |
fa054faf PP |
516 | goto end; |
517 | } | |
518 | ||
f42867e2 PP |
519 | if (notif->seq_num == -1ULL) { |
520 | notif->seq_num = stream_state->expected_notif_seq_num; | |
3230ee6b PP |
521 | } |
522 | ||
f42867e2 PP |
523 | if (notif->seq_num != -1ULL && |
524 | notif->seq_num != stream_state->expected_notif_seq_num) { | |
525 | BT_ASSERT_PRE_MSG("Unexpected notification sequence number: " | |
526 | "seq-num=%" PRIu64 ", " | |
527 | "expected-seq-num=%" PRIu64 ", %![stream-]+s", | |
528 | notif->seq_num, stream_state->expected_notif_seq_num, | |
529 | stream); | |
530 | is_valid = false; | |
fa054faf PP |
531 | goto end; |
532 | } | |
533 | ||
f42867e2 PP |
534 | switch (notif->type) { |
535 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
536 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN " | |
537 | "notification at this point: notif-seq-num=%" PRIu64 ", " | |
538 | "%![stream-]+s", notif->seq_num, stream); | |
539 | is_valid = false; | |
540 | goto end; | |
541 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
542 | if (stream_state->cur_packet) { | |
543 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END " | |
544 | "notification: missing a " | |
545 | "BT_NOTIFICATION_TYPE_PACKET_END notification " | |
546 | "prior to this notification: " | |
547 | "notif-seq-num=%" PRIu64 ", " | |
548 | "%![stream-]+s", notif->seq_num, stream); | |
549 | is_valid = false; | |
550 | goto end; | |
551 | } | |
552 | stream_state->expected_notif_seq_num++; | |
553 | stream_state->is_ended = true; | |
554 | goto end; | |
555 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
556 | if (stream_state->cur_packet) { | |
557 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN " | |
558 | "notification at this point: missing a " | |
559 | "BT_NOTIFICATION_TYPE_PACKET_END notification " | |
560 | "prior to this notification: " | |
561 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
562 | "%![packet-]+a", notif->seq_num, stream, | |
563 | packet); | |
564 | is_valid = false; | |
565 | goto end; | |
566 | } | |
567 | stream_state->expected_notif_seq_num++; | |
568 | stream_state->cur_packet = bt_get(packet); | |
569 | goto end; | |
570 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
571 | if (!stream_state->cur_packet) { | |
572 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END " | |
573 | "notification at this point: missing a " | |
574 | "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification " | |
575 | "prior to this notification: " | |
576 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
577 | "%![packet-]+a", notif->seq_num, stream, | |
578 | packet); | |
579 | is_valid = false; | |
580 | goto end; | |
581 | } | |
582 | stream_state->expected_notif_seq_num++; | |
583 | BT_PUT(stream_state->cur_packet); | |
584 | goto end; | |
585 | case BT_NOTIFICATION_TYPE_EVENT: | |
586 | if (packet != stream_state->cur_packet) { | |
587 | BT_ASSERT_PRE_MSG("Unexpected packet for " | |
588 | "BT_NOTIFICATION_TYPE_EVENT notification: " | |
589 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
590 | "%![notif-packet-]+a, %![expected-packet-]+a", | |
591 | notif->seq_num, stream, | |
592 | stream_state->cur_packet, packet); | |
593 | is_valid = false; | |
594 | goto end; | |
595 | } | |
596 | stream_state->expected_notif_seq_num++; | |
597 | goto end; | |
598 | default: | |
599 | break; | |
3230ee6b PP |
600 | } |
601 | ||
3230ee6b | 602 | end: |
f42867e2 | 603 | return is_valid; |
3230ee6b PP |
604 | } |
605 | ||
f42867e2 PP |
606 | BT_ASSERT_PRE_FUNC |
607 | static inline bool priv_conn_notif_iter_can_end( | |
608 | struct bt_notification_iterator_private_connection *iterator) | |
3230ee6b | 609 | { |
f42867e2 PP |
610 | GHashTableIter iter; |
611 | gpointer stream_key, state_value; | |
612 | bool ret = true; | |
3230ee6b | 613 | |
f42867e2 PP |
614 | /* |
615 | * Verify that this iterator received a | |
616 | * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream | |
617 | * which has a state. | |
618 | */ | |
3230ee6b | 619 | |
f42867e2 | 620 | g_hash_table_iter_init(&iter, iterator->stream_states); |
3230ee6b | 621 | |
f42867e2 PP |
622 | while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) { |
623 | struct stream_state *stream_state = (void *) state_value; | |
3230ee6b | 624 | |
f42867e2 PP |
625 | BT_ASSERT(stream_state); |
626 | BT_ASSERT(stream_key); | |
fa054faf | 627 | |
f42867e2 PP |
628 | if (!stream_state->is_ended) { |
629 | BT_ASSERT_PRE_MSG("Ending notification iterator, " | |
630 | "but stream is not ended: " | |
631 | "%![stream-]s", stream_key); | |
632 | ret = false; | |
633 | goto end; | |
634 | } | |
3230ee6b PP |
635 | } |
636 | ||
3230ee6b | 637 | end: |
3230ee6b PP |
638 | return ret; |
639 | } | |
640 | ||
641 | static | |
f42867e2 PP |
642 | enum bt_notification_iterator_status |
643 | bt_priv_conn_private_notification_iterator_next( | |
644 | struct bt_notification_iterator_private_connection *iterator) | |
3230ee6b | 645 | { |
f42867e2 PP |
646 | struct bt_private_connection_private_notification_iterator *priv_iterator = |
647 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator); | |
648 | bt_component_class_notification_iterator_next_method next_method = NULL; | |
649 | struct bt_notification_iterator_next_method_return next_return = { | |
650 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, | |
651 | .notification = NULL, | |
3230ee6b | 652 | }; |
f42867e2 PP |
653 | enum bt_notification_iterator_status status = |
654 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
3230ee6b | 655 | |
f42867e2 PP |
656 | BT_ASSERT(iterator); |
657 | BT_LIB_LOGD("Getting next notification iterator's notification: %!+i", | |
658 | iterator); | |
659 | BT_ASSERT_PRE(iterator->state == | |
660 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE, | |
661 | "Notification iterator's \"next\" called, but " | |
662 | "iterator is in the wrong state: %!+i", iterator); | |
663 | BT_ASSERT(iterator->upstream_component); | |
664 | BT_ASSERT(iterator->upstream_component->class); | |
d3eb6e8f | 665 | |
3230ee6b PP |
666 | /* Pick the appropriate "next" method */ |
667 | switch (iterator->upstream_component->class->type) { | |
d3eb6e8f PP |
668 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
669 | { | |
670 | struct bt_component_class_source *source_class = | |
3230ee6b | 671 | container_of(iterator->upstream_component->class, |
d3eb6e8f PP |
672 | struct bt_component_class_source, parent); |
673 | ||
f6ccaed9 | 674 | BT_ASSERT(source_class->methods.iterator.next); |
d3eb6e8f PP |
675 | next_method = source_class->methods.iterator.next; |
676 | break; | |
677 | } | |
678 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
679 | { | |
680 | struct bt_component_class_filter *filter_class = | |
3230ee6b | 681 | container_of(iterator->upstream_component->class, |
d3eb6e8f PP |
682 | struct bt_component_class_filter, parent); |
683 | ||
f6ccaed9 | 684 | BT_ASSERT(filter_class->methods.iterator.next); |
d3eb6e8f PP |
685 | next_method = filter_class->methods.iterator.next; |
686 | break; | |
687 | } | |
688 | default: | |
0fbb9a9f | 689 | abort(); |
d3eb6e8f PP |
690 | } |
691 | ||
3230ee6b PP |
692 | /* |
693 | * Call the user's "next" method to get the next notification | |
fa054faf | 694 | * and status. |
3230ee6b | 695 | */ |
f6ccaed9 | 696 | BT_ASSERT(next_method); |
f42867e2 PP |
697 | BT_LOGD_STR("Calling user's \"next\" method."); |
698 | next_return = next_method(priv_iterator); | |
699 | BT_LOGD("User method returned: status=%s", | |
700 | bt_notification_iterator_status_string(next_return.status)); | |
701 | if (next_return.status < 0) { | |
702 | BT_LOGW_STR("User method failed."); | |
703 | status = next_return.status; | |
704 | goto end; | |
705 | } | |
3230ee6b | 706 | |
f42867e2 PP |
707 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED || |
708 | iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) { | |
709 | /* | |
710 | * The user's "next" method, somehow, cancelled its own | |
711 | * notification iterator. This can happen, for example, | |
712 | * when the user's method removes the port on which | |
713 | * there's the connection from which the iterator was | |
714 | * created. In this case, said connection is ended, and | |
715 | * all its notification iterators are finalized. | |
716 | * | |
717 | * Only bt_put() the returned notification if | |
718 | * the status is | |
719 | * BT_NOTIFICATION_ITERATOR_STATUS_OK because | |
720 | * otherwise this field could be garbage. | |
721 | */ | |
722 | if (next_return.status == | |
723 | BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
724 | bt_put(next_return.notification); | |
3230ee6b PP |
725 | } |
726 | ||
f42867e2 PP |
727 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; |
728 | goto end; | |
729 | } | |
8cf27cc5 | 730 | |
f42867e2 PP |
731 | switch (next_return.status) { |
732 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
733 | BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator), | |
734 | "Notification iterator cannot end at this point: " | |
735 | "%!+i", iterator); | |
736 | BT_ASSERT(iterator->state == | |
737 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE); | |
738 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED; | |
739 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
740 | BT_LOGD("Set new status: status=%s", | |
741 | bt_notification_iterator_status_string(status)); | |
742 | goto end; | |
743 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
744 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; | |
745 | goto end; | |
746 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
747 | BT_ASSERT_PRE(next_return.notification, | |
748 | "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: " | |
749 | "%!+i", iterator); | |
750 | BT_ASSERT_PRE(validate_notification(iterator, | |
751 | next_return.notification), | |
752 | "Notification is invalid at this point: " | |
753 | "%![notif-iter-]+i, %![notif-]+n", | |
754 | iterator, next_return.notification); | |
755 | bt_notification_iterator_replace_current_notification( | |
756 | (void *) iterator, next_return.notification); | |
757 | bt_put(next_return.notification); | |
758 | break; | |
759 | default: | |
760 | /* Unknown non-error status */ | |
761 | abort(); | |
41a2b7ae PP |
762 | } |
763 | ||
764 | end: | |
3230ee6b PP |
765 | return status; |
766 | } | |
767 | ||
768 | enum bt_notification_iterator_status | |
769 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) | |
770 | { | |
771 | enum bt_notification_iterator_status status; | |
772 | ||
f42867e2 | 773 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
5af447e5 | 774 | BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator); |
f42867e2 PP |
775 | BT_ASSERT(iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION || |
776 | iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT); | |
5af447e5 | 777 | |
90157d89 PP |
778 | switch (iterator->type) { |
779 | case BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION: | |
780 | { | |
781 | struct bt_notification_iterator_private_connection *priv_conn_iter = | |
782 | (void *) iterator; | |
3230ee6b | 783 | |
90157d89 PP |
784 | /* |
785 | * Make sure that the iterator's queue contains at least | |
786 | * one notification. | |
787 | */ | |
f42867e2 PP |
788 | status = bt_priv_conn_private_notification_iterator_next( |
789 | priv_conn_iter); | |
8ed535b5 PP |
790 | break; |
791 | } | |
792 | case BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT: | |
793 | { | |
794 | struct bt_notification_iterator_output_port *out_port_iter = | |
795 | (void *) iterator; | |
796 | ||
797 | /* | |
798 | * Keep current notification in case there's an error: | |
799 | * restore this notification so that the current | |
800 | * notification is not changed from the user's point of | |
801 | * view. | |
802 | */ | |
803 | struct bt_notification *old_notif = | |
804 | bt_get(bt_notification_iterator_borrow_current_notification(iterator)); | |
805 | enum bt_graph_status graph_status; | |
806 | ||
807 | /* | |
808 | * Put current notification since it's possibly | |
809 | * about to be replaced by a new one by the | |
810 | * colander sink. | |
811 | */ | |
812 | bt_notification_iterator_replace_current_notification( | |
813 | iterator, NULL); | |
814 | graph_status = bt_graph_consume_sink_no_check( | |
f42867e2 | 815 | out_port_iter->graph, out_port_iter->colander); |
8ed535b5 PP |
816 | switch (graph_status) { |
817 | case BT_GRAPH_STATUS_CANCELED: | |
818 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; | |
819 | break; | |
820 | case BT_GRAPH_STATUS_AGAIN: | |
821 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; | |
822 | break; | |
823 | case BT_GRAPH_STATUS_END: | |
824 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
825 | break; | |
826 | case BT_GRAPH_STATUS_NOMEM: | |
827 | status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
828 | break; | |
829 | case BT_GRAPH_STATUS_OK: | |
830 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
f6ccaed9 | 831 | BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator)); |
8ed535b5 PP |
832 | break; |
833 | default: | |
834 | /* Other errors */ | |
835 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
836 | } | |
837 | ||
838 | if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
839 | /* Error/exception: restore old notification */ | |
840 | bt_notification_iterator_replace_current_notification( | |
841 | iterator, old_notif); | |
842 | } | |
843 | ||
844 | bt_put(old_notif); | |
90157d89 PP |
845 | break; |
846 | } | |
847 | default: | |
90157d89 PP |
848 | abort(); |
849 | } | |
3230ee6b | 850 | |
3230ee6b | 851 | return status; |
53d45b87 JG |
852 | } |
853 | ||
90157d89 | 854 | struct bt_component *bt_private_connection_notification_iterator_get_component( |
413bc2c4 JG |
855 | struct bt_notification_iterator *iterator) |
856 | { | |
90157d89 PP |
857 | struct bt_notification_iterator_private_connection *iter_priv_conn; |
858 | ||
f42867e2 PP |
859 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
860 | BT_ASSERT_PRE(iterator->type == | |
861 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, | |
862 | "Notification iterator was not created from a private connection: " | |
863 | "%!+i", iterator); | |
90157d89 | 864 | iter_priv_conn = (void *) iterator; |
f42867e2 | 865 | return bt_get(iter_priv_conn->upstream_component); |
413bc2c4 JG |
866 | } |
867 | ||
91457551 | 868 | struct bt_private_component * |
90157d89 PP |
869 | bt_private_connection_private_notification_iterator_get_private_component( |
870 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
91457551 PP |
871 | { |
872 | return bt_private_component_from_component( | |
90157d89 | 873 | bt_private_connection_notification_iterator_get_component( |
6d137876 | 874 | (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator))); |
91457551 | 875 | } |
8ed535b5 PP |
876 | |
877 | static | |
878 | void bt_output_port_notification_iterator_destroy(struct bt_object *obj) | |
879 | { | |
880 | struct bt_notification_iterator_output_port *iterator = | |
881 | (void *) container_of(obj, struct bt_notification_iterator, base); | |
882 | ||
883 | BT_LOGD("Destroying output port notification iterator object: addr=%p", | |
884 | iterator); | |
885 | BT_LOGD_STR("Putting graph."); | |
886 | bt_put(iterator->graph); | |
887 | BT_LOGD_STR("Putting output port."); | |
888 | bt_put(iterator->output_port); | |
889 | BT_LOGD_STR("Putting colander sink component."); | |
890 | bt_put(iterator->colander); | |
891 | destroy_base_notification_iterator(obj); | |
892 | } | |
893 | ||
894 | struct bt_notification_iterator *bt_output_port_notification_iterator_create( | |
895 | struct bt_port *output_port, | |
f42867e2 | 896 | const char *colander_component_name) |
8ed535b5 PP |
897 | { |
898 | struct bt_notification_iterator *iterator_base = NULL; | |
899 | struct bt_notification_iterator_output_port *iterator = NULL; | |
900 | struct bt_component_class *colander_comp_cls = NULL; | |
901 | struct bt_component *output_port_comp = NULL; | |
902 | struct bt_component *colander_comp; | |
903 | struct bt_graph *graph = NULL; | |
904 | enum bt_graph_status graph_status; | |
905 | const char *colander_comp_name; | |
906 | struct bt_port *colander_in_port = NULL; | |
907 | struct bt_component_class_sink_colander_data colander_data; | |
908 | ||
f42867e2 PP |
909 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
910 | BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT, | |
911 | "Port is not an output port: %!+p", output_port); | |
8ed535b5 | 912 | output_port_comp = bt_port_get_component(output_port); |
f42867e2 PP |
913 | BT_ASSERT_PRE(output_port_comp, |
914 | "Output port has no component: %!+p", output_port); | |
8ed535b5 | 915 | graph = bt_component_get_graph(output_port_comp); |
f6ccaed9 | 916 | BT_ASSERT(graph); |
8ed535b5 PP |
917 | |
918 | /* Create notification iterator */ | |
919 | BT_LOGD("Creating notification iterator on output port: " | |
920 | "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"", | |
921 | output_port_comp, bt_component_get_name(output_port_comp), | |
922 | output_port, bt_port_get_name(output_port)); | |
923 | iterator = g_new0(struct bt_notification_iterator_output_port, 1); | |
924 | if (!iterator) { | |
925 | BT_LOGE_STR("Failed to allocate one output port notification iterator."); | |
926 | goto error; | |
927 | } | |
928 | ||
929 | init_notification_iterator((void *) iterator, | |
930 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, | |
931 | bt_output_port_notification_iterator_destroy); | |
932 | ||
933 | /* Create colander component */ | |
934 | colander_comp_cls = bt_component_class_sink_colander_get(); | |
935 | if (!colander_comp_cls) { | |
936 | BT_LOGW("Cannot get colander sink component class."); | |
937 | goto error; | |
938 | } | |
939 | ||
940 | BT_MOVE(iterator->graph, graph); | |
941 | iterator_base = (void *) iterator; | |
942 | colander_comp_name = | |
943 | colander_component_name ? colander_component_name : "colander"; | |
944 | colander_data.notification = &iterator_base->current_notification; | |
8ed535b5 PP |
945 | graph_status = bt_graph_add_component_with_init_method_data( |
946 | iterator->graph, colander_comp_cls, colander_comp_name, | |
947 | NULL, &colander_data, &iterator->colander); | |
948 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
949 | BT_LOGW("Cannot add colander sink component to graph: " | |
950 | "graph-addr=%p, name=\"%s\", graph-status=%s", | |
951 | iterator->graph, colander_comp_name, | |
952 | bt_graph_status_string(graph_status)); | |
953 | goto error; | |
954 | } | |
955 | ||
956 | /* | |
957 | * Connect provided output port to the colander component's | |
958 | * input port. | |
959 | */ | |
960 | colander_in_port = bt_component_sink_get_input_port_by_index( | |
961 | iterator->colander, 0); | |
f6ccaed9 | 962 | BT_ASSERT(colander_in_port); |
8ed535b5 PP |
963 | graph_status = bt_graph_connect_ports(iterator->graph, |
964 | output_port, colander_in_port, NULL); | |
965 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
966 | BT_LOGW("Cannot add colander sink component to graph: " | |
967 | "graph-addr=%p, name=\"%s\", graph-status=%s", | |
968 | iterator->graph, colander_comp_name, | |
969 | bt_graph_status_string(graph_status)); | |
970 | goto error; | |
971 | } | |
972 | ||
973 | /* | |
974 | * At this point everything went fine. Make the graph | |
975 | * nonconsumable forever so that only this notification iterator | |
976 | * can consume (thanks to bt_graph_consume_sink_no_check()). | |
977 | * This avoids leaking the notification created by the colander | |
978 | * sink and moved to the base notification iterator's current | |
979 | * notification member. | |
980 | */ | |
981 | bt_graph_set_can_consume(iterator->graph, BT_FALSE); | |
982 | goto end; | |
983 | ||
984 | error: | |
985 | if (iterator && iterator->graph && iterator->colander) { | |
986 | int ret; | |
987 | ||
988 | /* Remove created colander component from graph if any */ | |
989 | colander_comp = iterator->colander; | |
990 | BT_PUT(iterator->colander); | |
991 | ||
992 | /* | |
993 | * At this point the colander component's reference | |
994 | * count is 0 because iterator->colander was the only | |
995 | * owner. We also know that it is not connected because | |
996 | * this is the last operation before this function | |
997 | * succeeds. | |
998 | * | |
999 | * Since we honor the preconditions here, | |
1000 | * bt_graph_remove_unconnected_component() always | |
1001 | * succeeds. | |
1002 | */ | |
1003 | ret = bt_graph_remove_unconnected_component(iterator->graph, | |
1004 | colander_comp); | |
f6ccaed9 | 1005 | BT_ASSERT(ret == 0); |
8ed535b5 PP |
1006 | } |
1007 | ||
1008 | BT_PUT(iterator); | |
1009 | ||
1010 | end: | |
1011 | bt_put(colander_in_port); | |
1012 | bt_put(colander_comp_cls); | |
1013 | bt_put(output_port_comp); | |
1014 | bt_put(graph); | |
1015 | return (void *) iterator; | |
1016 | } | |
25b68514 PP |
1017 | |
1018 | struct bt_notification_iterator * | |
5c563278 | 1019 | bt_private_connection_notification_iterator_borrow_from_private( |
25b68514 PP |
1020 | struct bt_private_connection_private_notification_iterator *private_notification_iterator) |
1021 | { | |
5c563278 | 1022 | return (void *) private_notification_iterator; |
25b68514 | 1023 | } |