Commit | Line | Data |
---|---|---|
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 | ||
39 | static | |
40 | void 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 | ||
52 | BT_HIDDEN | |
53 | struct 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); |
79 | end: | |
80 | return connection; | |
81 | } | |
82 | ||
72b913fb PP |
83 | BT_HIDDEN |
84 | void 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 | ||
115 | // TODO: notify graph user: component's port disconnected | |
116 | ||
117 | bt_put(downstream_comp); | |
118 | bt_put(upstream_comp); | |
119 | } | |
120 | ||
121 | struct bt_port *bt_connection_get_upstream_port( | |
784cdc68 JG |
122 | struct bt_connection *connection) |
123 | { | |
72b913fb | 124 | return connection ? bt_get(connection->upstream_port) : NULL; |
784cdc68 JG |
125 | } |
126 | ||
72b913fb | 127 | struct bt_port *bt_connection_get_downstream_port( |
784cdc68 JG |
128 | struct bt_connection *connection) |
129 | { | |
72b913fb | 130 | return connection ? bt_get(connection->downstream_port) : NULL; |
784cdc68 JG |
131 | } |
132 | ||
133 | struct bt_notification_iterator * | |
134 | bt_connection_create_notification_iterator(struct bt_connection *connection) | |
135 | { | |
136 | struct bt_component *upstream_component = NULL; | |
137 | struct bt_notification_iterator *it = NULL; | |
138 | ||
139 | if (!connection) { | |
140 | goto end; | |
141 | } | |
142 | ||
72b913fb PP |
143 | if (!connection->upstream_port || !connection->downstream_port) { |
144 | goto end; | |
145 | } | |
146 | ||
147 | upstream_component = bt_port_get_component(connection->upstream_port); | |
784cdc68 JG |
148 | assert(upstream_component); |
149 | ||
150 | switch (bt_component_get_class_type(upstream_component)) { | |
151 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
152 | it = bt_component_source_create_notification_iterator( | |
153 | upstream_component); | |
154 | break; | |
155 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
156 | it = bt_component_filter_create_notification_iterator( | |
157 | upstream_component); | |
158 | break; | |
159 | default: | |
160 | goto end; | |
161 | } | |
162 | end: | |
163 | bt_put(upstream_component); | |
164 | return it; | |
165 | } |