| 1 | /* |
| 2 | * component-connection.c |
| 3 | * |
| 4 | * Babeltrace Connection |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/component/component-connection-internal.h> |
| 30 | #include <babeltrace/component/component-graph-internal.h> |
| 31 | #include <babeltrace/component/component-port-internal.h> |
| 32 | #include <babeltrace/component/component-source-internal.h> |
| 33 | #include <babeltrace/component/component-filter-internal.h> |
| 34 | #include <babeltrace/object-internal.h> |
| 35 | #include <babeltrace/compiler.h> |
| 36 | #include <glib.h> |
| 37 | |
| 38 | static |
| 39 | void bt_connection_destroy(struct bt_object *obj) |
| 40 | { |
| 41 | struct bt_connection *connection = container_of(obj, |
| 42 | struct bt_connection, base); |
| 43 | |
| 44 | /* |
| 45 | * No bt_put on ports as a connection only holds _weak_ references |
| 46 | * to them. |
| 47 | */ |
| 48 | g_free(connection); |
| 49 | } |
| 50 | |
| 51 | BT_HIDDEN |
| 52 | struct bt_connection *bt_connection_create( |
| 53 | struct bt_graph *graph, |
| 54 | struct bt_port *upstream, struct bt_port *downstream) |
| 55 | { |
| 56 | struct bt_connection *connection = NULL; |
| 57 | |
| 58 | if (bt_port_get_type(upstream) != BT_PORT_TYPE_OUTPUT) { |
| 59 | goto end; |
| 60 | } |
| 61 | if (bt_port_get_type(downstream) != BT_PORT_TYPE_INPUT) { |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | connection = g_new0(struct bt_connection, 1); |
| 66 | if (!connection) { |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | bt_object_init(connection, bt_connection_destroy); |
| 71 | /* Weak references are taken, see comment in header. */ |
| 72 | connection->output_port = upstream; |
| 73 | connection->input_port = downstream; |
| 74 | bt_port_add_connection(upstream, connection); |
| 75 | bt_port_add_connection(downstream, connection); |
| 76 | bt_object_set_parent(connection, &graph->base); |
| 77 | end: |
| 78 | return connection; |
| 79 | } |
| 80 | |
| 81 | struct bt_port *bt_connection_get_input_port( |
| 82 | struct bt_connection *connection) |
| 83 | { |
| 84 | return connection ? connection->input_port : NULL; |
| 85 | } |
| 86 | |
| 87 | struct bt_port *bt_connection_get_output_port( |
| 88 | struct bt_connection *connection) |
| 89 | { |
| 90 | return connection ? connection->output_port : NULL; |
| 91 | } |
| 92 | |
| 93 | struct bt_notification_iterator * |
| 94 | bt_connection_create_notification_iterator(struct bt_connection *connection) |
| 95 | { |
| 96 | struct bt_component *upstream_component = NULL; |
| 97 | struct bt_notification_iterator *it = NULL; |
| 98 | |
| 99 | if (!connection) { |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | upstream_component = bt_port_get_component(connection->output_port); |
| 104 | assert(upstream_component); |
| 105 | |
| 106 | switch (bt_component_get_class_type(upstream_component)) { |
| 107 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 108 | it = bt_component_source_create_notification_iterator( |
| 109 | upstream_component); |
| 110 | break; |
| 111 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 112 | it = bt_component_filter_create_notification_iterator( |
| 113 | upstream_component); |
| 114 | break; |
| 115 | default: |
| 116 | goto end; |
| 117 | } |
| 118 | end: |
| 119 | bt_put(upstream_component); |
| 120 | return it; |
| 121 | } |