4 * Babeltrace Connection
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/component/component-internal.h>
30 #include <babeltrace/component/component-source-internal.h>
31 #include <babeltrace/component/component-filter-internal.h>
32 #include <babeltrace/component/connection-internal.h>
33 #include <babeltrace/component/graph-internal.h>
34 #include <babeltrace/component/port-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/compiler.h>
40 void bt_connection_destroy(struct bt_object
*obj
)
42 struct bt_connection
*connection
= container_of(obj
,
43 struct bt_connection
, base
);
46 * No bt_put on ports as a connection only holds _weak_ references
53 struct bt_connection
*bt_connection_create(
54 struct bt_graph
*graph
,
55 struct bt_port
*upstream_port
,
56 struct bt_port
*downstream_port
)
58 struct bt_connection
*connection
= NULL
;
60 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
63 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
67 connection
= g_new0(struct bt_connection
, 1);
72 bt_object_init(connection
, bt_connection_destroy
);
73 /* Weak references are taken, see comment in header. */
74 connection
->upstream_port
= upstream_port
;
75 connection
->downstream_port
= downstream_port
;
76 bt_port_set_connection(upstream_port
, connection
);
77 bt_port_set_connection(downstream_port
, connection
);
78 bt_object_set_parent(connection
, &graph
->base
);
84 void bt_connection_disconnect_ports(struct bt_connection
*conn
,
85 struct bt_component
*acting_comp
)
87 struct bt_component
*downstream_comp
= NULL
;
88 struct bt_component
*upstream_comp
= NULL
;
89 struct bt_port
*downstream_port
= conn
->downstream_port
;
90 struct bt_port
*upstream_port
= conn
->upstream_port
;
92 if (downstream_port
) {
93 downstream_comp
= bt_port_get_component(downstream_port
);
94 bt_port_set_connection(downstream_port
, NULL
);
95 conn
->downstream_port
= NULL
;
99 upstream_comp
= bt_port_get_component(upstream_port
);
100 bt_port_set_connection(upstream_port
, NULL
);
101 conn
->upstream_port
= NULL
;
104 if (downstream_comp
&& downstream_comp
!= acting_comp
&&
105 downstream_comp
->class->methods
.port_disconnected
) {
106 bt_component_port_disconnected(downstream_comp
,
110 if (upstream_comp
&& upstream_comp
!= acting_comp
&&
111 upstream_comp
->class->methods
.port_disconnected
) {
112 bt_component_port_disconnected(upstream_comp
, upstream_port
);
115 // TODO: notify graph user: component's port disconnected
117 bt_put(downstream_comp
);
118 bt_put(upstream_comp
);
121 struct bt_port
*bt_connection_get_upstream_port(
122 struct bt_connection
*connection
)
124 return connection
? bt_get(connection
->upstream_port
) : NULL
;
127 struct bt_port
*bt_connection_get_downstream_port(
128 struct bt_connection
*connection
)
130 return connection
? bt_get(connection
->downstream_port
) : NULL
;
133 struct bt_notification_iterator
*
134 bt_connection_create_notification_iterator(struct bt_connection
*connection
)
136 struct bt_component
*upstream_component
= NULL
;
137 struct bt_notification_iterator
*it
= NULL
;
143 if (!connection
->upstream_port
|| !connection
->downstream_port
) {
147 upstream_component
= bt_port_get_component(connection
->upstream_port
);
148 assert(upstream_component
);
150 switch (bt_component_get_class_type(upstream_component
)) {
151 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
152 it
= bt_component_source_create_notification_iterator(
155 case BT_COMPONENT_CLASS_TYPE_FILTER
:
156 it
= bt_component_filter_create_notification_iterator(
163 bt_put(upstream_component
);