Add graph event listeners
[babeltrace.git] / lib / component / connection.c
CommitLineData
784cdc68 1/*
7d55361f 2 * connection.c
784cdc68
JG
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
72b913fb
PP
29#include <babeltrace/component/component-internal.h>
30#include <babeltrace/component/component-source-internal.h>
31#include <babeltrace/component/component-filter-internal.h>
7d55361f
JG
32#include <babeltrace/component/connection-internal.h>
33#include <babeltrace/component/graph-internal.h>
34#include <babeltrace/component/port-internal.h>
784cdc68
JG
35#include <babeltrace/object-internal.h>
36#include <babeltrace/compiler.h>
37#include <glib.h>
38
39static
40void bt_connection_destroy(struct bt_object *obj)
41{
42 struct bt_connection *connection = container_of(obj,
43 struct bt_connection, base);
44
45 /*
46 * No bt_put on ports as a connection only holds _weak_ references
47 * to them.
48 */
49 g_free(connection);
50}
51
52BT_HIDDEN
53struct bt_connection *bt_connection_create(
54 struct bt_graph *graph,
72b913fb
PP
55 struct bt_port *upstream_port,
56 struct bt_port *downstream_port)
784cdc68
JG
57{
58 struct bt_connection *connection = NULL;
59
72b913fb 60 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
784cdc68
JG
61 goto end;
62 }
72b913fb 63 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
784cdc68
JG
64 goto end;
65 }
66
67 connection = g_new0(struct bt_connection, 1);
68 if (!connection) {
69 goto end;
70 }
71
72 bt_object_init(connection, bt_connection_destroy);
73 /* Weak references are taken, see comment in header. */
72b913fb
PP
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);
784cdc68
JG
78 bt_object_set_parent(connection, &graph->base);
79end:
80 return connection;
81}
82
72b913fb
PP
83BT_HIDDEN
84void bt_connection_disconnect_ports(struct bt_connection *conn,
85 struct bt_component *acting_comp)
86{
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;
91
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;
96 }
97
98 if (upstream_port) {
99 upstream_comp = bt_port_get_component(upstream_port);
100 bt_port_set_connection(upstream_port, NULL);
101 conn->upstream_port = NULL;
102 }
103
104 if (downstream_comp && downstream_comp != acting_comp &&
105 downstream_comp->class->methods.port_disconnected) {
106 bt_component_port_disconnected(downstream_comp,
107 downstream_port);
108 }
109
110 if (upstream_comp && upstream_comp != acting_comp &&
111 upstream_comp->class->methods.port_disconnected) {
112 bt_component_port_disconnected(upstream_comp, upstream_port);
113 }
114
1bf957a0
PP
115 if (upstream_comp) {
116 struct bt_graph *graph = bt_component_get_graph(upstream_comp);
117
118 assert(graph);
119 bt_graph_notify_port_disconnected(graph, upstream_comp,
120 upstream_port);
121 bt_put(graph);
122 }
123
124 if (downstream_comp) {
125 struct bt_graph *graph =
126 bt_component_get_graph(downstream_comp);
127
128 assert(graph);
129 bt_graph_notify_port_disconnected(graph, downstream_comp,
130 downstream_port);
131 bt_put(graph);
132 }
72b913fb
PP
133
134 bt_put(downstream_comp);
135 bt_put(upstream_comp);
136}
137
138struct bt_port *bt_connection_get_upstream_port(
784cdc68
JG
139 struct bt_connection *connection)
140{
72b913fb 141 return connection ? bt_get(connection->upstream_port) : NULL;
784cdc68
JG
142}
143
72b913fb 144struct bt_port *bt_connection_get_downstream_port(
784cdc68
JG
145 struct bt_connection *connection)
146{
72b913fb 147 return connection ? bt_get(connection->downstream_port) : NULL;
784cdc68
JG
148}
149
150struct bt_notification_iterator *
151bt_connection_create_notification_iterator(struct bt_connection *connection)
152{
153 struct bt_component *upstream_component = NULL;
154 struct bt_notification_iterator *it = NULL;
155
156 if (!connection) {
157 goto end;
158 }
159
72b913fb
PP
160 if (!connection->upstream_port || !connection->downstream_port) {
161 goto end;
162 }
163
164 upstream_component = bt_port_get_component(connection->upstream_port);
784cdc68
JG
165 assert(upstream_component);
166
167 switch (bt_component_get_class_type(upstream_component)) {
168 case BT_COMPONENT_CLASS_TYPE_SOURCE:
169 it = bt_component_source_create_notification_iterator(
170 upstream_component);
171 break;
172 case BT_COMPONENT_CLASS_TYPE_FILTER:
173 it = bt_component_filter_create_notification_iterator(
174 upstream_component);
175 break;
176 default:
177 goto end;
178 }
179end:
180 bt_put(upstream_component);
181 return it;
182}
This page took 0.029979 seconds and 4 git commands to generate.