-struct bt_self_port_output *bt_self_message_iterator_borrow_port(
- struct bt_self_message_iterator *self_iterator)
-{
- struct bt_self_component_port_input_message_iterator *iterator =
- (void *) self_iterator;
-
- BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
- return (void *) iterator->upstream_port;
-}
-
-static
-void bt_port_output_message_iterator_destroy(struct bt_object *obj)
-{
- struct bt_port_output_message_iterator *iterator = (void *) obj;
-
- BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
- iterator);
- BT_LOGD_STR("Putting graph.");
- BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
- BT_LOGD_STR("Putting colander sink component.");
- BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
- destroy_base_message_iterator(obj);
-}
-
-struct bt_port_output_message_iterator *
-bt_port_output_message_iterator_create(struct bt_graph *graph,
- const struct bt_port_output *output_port)
-{
- struct bt_port_output_message_iterator *iterator = NULL;
- struct bt_component_class_sink *colander_comp_cls = NULL;
- struct bt_component *output_port_comp = NULL;
- struct bt_component_sink *colander_comp;
- enum bt_graph_status graph_status;
- struct bt_port_input *colander_in_port = NULL;
- struct bt_component_class_sink_colander_data colander_data;
- int ret;
-
- BT_ASSERT_PRE_NON_NULL(graph, "Graph");
- BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
- output_port_comp = bt_port_borrow_component_inline(
- (const void *) output_port);
- BT_ASSERT_PRE(output_port_comp,
- "Output port has no component: %!+p", output_port);
- BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
- (void *) graph,
- "Output port is not part of graph: %![graph-]+g, %![port-]+p",
- graph, output_port);
- BT_ASSERT_PRE(!graph->has_sink,
- "Graph already has a sink component: %![graph-]+g");
-
- /* Create message iterator */
- BT_LIB_LOGI("Creating message iterator on output port: "
- "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
- iterator = g_new0(struct bt_port_output_message_iterator, 1);
- if (!iterator) {
- BT_LOGE_STR("Failed to allocate one output port message iterator.");
- goto error;
- }
-
- ret = init_message_iterator((void *) iterator,
- BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
- bt_port_output_message_iterator_destroy);
- if (ret) {
- /* init_message_iterator() logs errors */
- BT_OBJECT_PUT_REF_AND_RESET(iterator);
- goto end;
- }
-
- /* Create colander component */
- colander_comp_cls = bt_component_class_sink_colander_get();
- if (!colander_comp_cls) {
- BT_LOGW("Cannot get colander sink component class.");
- goto error;
- }
-
- iterator->graph = graph;
- bt_object_get_no_null_check(iterator->graph);
- colander_data.msgs = (void *) iterator->base.msgs->pdata;
- colander_data.count_addr = &iterator->count;
-
- /* Hope that nobody uses this very unique name */
- graph_status =
- bt_graph_add_sink_component_with_init_method_data(
- (void *) graph, colander_comp_cls,
- "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
- NULL, &colander_data, (void *) &iterator->colander);
- if (graph_status != BT_GRAPH_STATUS_OK) {
- BT_LIB_LOGW("Cannot add colander sink component to graph: "
- "%1[graph-]+g, status=%s", graph,
- bt_graph_status_string(graph_status));
- goto error;
- }
-
- /*
- * Connect provided output port to the colander component's
- * input port.
- */
- colander_in_port =
- (void *) bt_component_sink_borrow_input_port_by_index_const(
- (void *) iterator->colander, 0);
- BT_ASSERT(colander_in_port);
- graph_status = bt_graph_connect_ports(graph,
- output_port, colander_in_port, NULL);
- if (graph_status != BT_GRAPH_STATUS_OK) {
- BT_LIB_LOGW("Cannot add colander sink component to graph: "
- "%![graph-]+g, %![comp-]+c, status=%s", graph,
- iterator->colander,
- bt_graph_status_string(graph_status));
- goto error;
- }
-
- /*
- * At this point everything went fine. Make the graph
- * nonconsumable forever so that only this message iterator
- * can consume (thanks to bt_graph_consume_sink_no_check()).
- * This avoids leaking the message created by the colander
- * sink and moved to the message iterator's message
- * member.
- */
- bt_graph_set_can_consume(iterator->graph, false);
-
- /* Also set the graph as being configured. */
- graph_status = bt_graph_configure(graph);
- if (graph_status != BT_GRAPH_STATUS_OK) {
- BT_LIB_LOGW("Cannot configure graph after having added colander: "
- "%![graph-]+g, status=%s", graph,
- bt_graph_status_string(graph_status));
- goto error;
- }
- goto end;
-
-error:
- if (iterator && iterator->graph && iterator->colander) {
- int ret;
-
- /* Remove created colander component from graph if any */
- colander_comp = iterator->colander;
- BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
-
- /*
- * At this point the colander component's reference
- * count is 0 because iterator->colander was the only
- * owner. We also know that it is not connected because
- * this is the last operation before this function
- * succeeds.
- *
- * Since we honor the preconditions here,
- * bt_graph_remove_unconnected_component() always
- * succeeds.
- */
- ret = bt_graph_remove_unconnected_component(iterator->graph,
- (void *) colander_comp);
- BT_ASSERT(ret == 0);
- }
-
- BT_OBJECT_PUT_REF_AND_RESET(iterator);