Add bt_connection_is_ended()
[babeltrace.git] / lib / graph / graph.c
CommitLineData
c0418dd9 1/*
7d55361f 2 * graph.c
c0418dd9
JG
3 *
4 * Babeltrace Plugin Component Graph
5 *
f60c8b34 6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
262e5473 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
c0418dd9
JG
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
262e5473
PP
28#define BT_LOG_TAG "GRAPH"
29#include <babeltrace/lib-logging-internal.h>
30
b2e0c907
PP
31#include <babeltrace/graph/component-internal.h>
32#include <babeltrace/graph/graph-internal.h>
33#include <babeltrace/graph/connection-internal.h>
34#include <babeltrace/graph/component-sink-internal.h>
35#include <babeltrace/graph/component-source.h>
36#include <babeltrace/graph/component-filter.h>
37#include <babeltrace/graph/port.h>
3d9990ac 38#include <babeltrace/compiler-internal.h>
c55a9f58 39#include <babeltrace/types.h>
f60c8b34 40#include <unistd.h>
1bf957a0
PP
41#include <glib.h>
42
43struct bt_graph_listener {
44 void *func;
45 void *data;
46};
c0418dd9 47
f60c8b34
JG
48static
49void bt_graph_destroy(struct bt_object *obj)
c0418dd9 50{
f60c8b34
JG
51 struct bt_graph *graph = container_of(obj,
52 struct bt_graph, base);
c0418dd9 53
bd14d768
PP
54 /*
55 * The graph's reference count is 0 if we're here. Increment
56 * it to avoid a double-destroy (possibly infinitely recursive)
57 * in this situation:
58 *
59 * 1. We put and destroy a connection.
60 * 2. This connection's destructor finalizes its active
61 * notification iterators.
62 * 3. A notification iterator's finalization function gets a
63 * new reference on its component (reference count goes from
64 * 0 to 1).
65 * 4. Since this component's reference count goes to 1, it takes
66 * a reference on its parent (this graph). This graph's
67 * reference count goes from 0 to 1.
68 * 5. The notification iterator's finalization function puts its
69 * component reference (reference count goes from 1 to 0).
70 * 6. Since this component's reference count goes from 1 to 0,
71 * it puts its parent (this graph). This graph's reference
72 * count goes from 1 to 0.
73 * 7. Since this graph's reference count goes from 1 to 0,
74 * its destructor is called (this function).
75 *
76 * With the incrementation below, the graph's reference count at
77 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
78 * ensures that this function is not called two times.
79 */
262e5473 80 BT_LOGD("Destroying graph: addr=%p", graph);
bd14d768
PP
81 obj->ref_count.count++;
82
c0418dd9 83 if (graph->connections) {
262e5473 84 BT_LOGD_STR("Destroying connections.");
c0418dd9
JG
85 g_ptr_array_free(graph->connections, TRUE);
86 }
bd14d768 87 if (graph->components) {
262e5473 88 BT_LOGD_STR("Destroying components.");
bd14d768
PP
89 g_ptr_array_free(graph->components, TRUE);
90 }
f60c8b34
JG
91 if (graph->sinks_to_consume) {
92 g_queue_free(graph->sinks_to_consume);
c0418dd9 93 }
1bf957a0
PP
94
95 if (graph->listeners.port_added) {
96 g_array_free(graph->listeners.port_added, TRUE);
97 }
98
99 if (graph->listeners.port_removed) {
100 g_array_free(graph->listeners.port_removed, TRUE);
101 }
102
f345f8bb
PP
103 if (graph->listeners.ports_connected) {
104 g_array_free(graph->listeners.ports_connected, TRUE);
1bf957a0
PP
105 }
106
f345f8bb
PP
107 if (graph->listeners.ports_disconnected) {
108 g_array_free(graph->listeners.ports_disconnected, TRUE);
1bf957a0
PP
109 }
110
c0418dd9
JG
111 g_free(graph);
112}
113
1bf957a0
PP
114static
115int init_listeners_array(GArray **listeners)
116{
117 int ret = 0;
118
119 assert(listeners);
120 *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener));
121 if (!*listeners) {
262e5473 122 BT_LOGE_STR("Failed to allocate one GArray.");
1bf957a0
PP
123 ret = -1;
124 goto end;
125 }
126
127end:
128 return ret;
129}
130
f60c8b34 131struct bt_graph *bt_graph_create(void)
c0418dd9 132{
f60c8b34 133 struct bt_graph *graph;
1bf957a0 134 int ret;
c0418dd9 135
262e5473 136 BT_LOGD_STR("Creating graph object.");
f60c8b34 137 graph = g_new0(struct bt_graph, 1);
c0418dd9 138 if (!graph) {
262e5473 139 BT_LOGE_STR("Failed to allocate one graph.");
c0418dd9
JG
140 goto end;
141 }
142
f60c8b34 143 bt_object_init(graph, bt_graph_destroy);
c0418dd9 144
f60c8b34 145 graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
c0418dd9 146 if (!graph->connections) {
262e5473 147 BT_LOGE_STR("Failed to allocate one GPtrArray.");
c0418dd9
JG
148 goto error;
149 }
f60c8b34
JG
150 graph->components = g_ptr_array_new_with_free_func(bt_object_release);
151 if (!graph->components) {
262e5473 152 BT_LOGE_STR("Failed to allocate one GPtrArray.");
f60c8b34
JG
153 goto error;
154 }
155 graph->sinks_to_consume = g_queue_new();
156 if (!graph->sinks_to_consume) {
262e5473 157 BT_LOGE_STR("Failed to allocate one GQueue.");
c0418dd9
JG
158 goto error;
159 }
1bf957a0
PP
160
161 ret = init_listeners_array(&graph->listeners.port_added);
162 if (ret) {
262e5473 163 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
1bf957a0
PP
164 goto error;
165 }
166
167 ret = init_listeners_array(&graph->listeners.port_removed);
168 if (ret) {
262e5473 169 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
1bf957a0
PP
170 goto error;
171 }
172
f345f8bb 173 ret = init_listeners_array(&graph->listeners.ports_connected);
1bf957a0 174 if (ret) {
262e5473 175 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
1bf957a0
PP
176 goto error;
177 }
178
f345f8bb 179 ret = init_listeners_array(&graph->listeners.ports_disconnected);
1bf957a0 180 if (ret) {
262e5473 181 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
1bf957a0
PP
182 goto error;
183 }
184
262e5473
PP
185 BT_LOGD("Created graph object: addr=%p", graph);
186
c0418dd9
JG
187end:
188 return graph;
189error:
190 BT_PUT(graph);
191 goto end;
192}
193
77a54f99 194struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
f60c8b34
JG
195 struct bt_port *upstream_port,
196 struct bt_port *downstream_port)
197{
198 struct bt_connection *connection = NULL;
199 struct bt_graph *upstream_graph = NULL;
200 struct bt_graph *downstream_graph = NULL;
201 struct bt_component *upstream_component = NULL;
202 struct bt_component *downstream_component = NULL;
203 enum bt_component_status component_status;
c55a9f58
PP
204 bt_bool upstream_was_already_in_graph;
205 bt_bool downstream_was_already_in_graph;
f60c8b34 206
262e5473
PP
207 if (!graph) {
208 BT_LOGW_STR("Invalid parameter: graph is NULL.");
209 goto end;
210 }
211
212 if (!upstream_port) {
213 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
214 goto end;
215 }
216
217 if (!downstream_port) {
218 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
f60c8b34
JG
219 goto end;
220 }
221
262e5473
PP
222 BT_LOGD("Connecting component ports within graph: "
223 "graph-addr=%p, "
224 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
225 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
226 graph, upstream_port, bt_port_get_name(upstream_port),
227 downstream_port, bt_port_get_name(downstream_port));
228
202a3a13 229 if (graph->canceled) {
262e5473 230 BT_LOGW_STR("Invalid parameter: graph is canceled.");
202a3a13
PP
231 goto end;
232 }
233
72b913fb 234 /* Ensure appropriate types for upstream and downstream ports. */
f60c8b34 235 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
262e5473 236 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
f60c8b34
JG
237 goto end;
238 }
239 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
262e5473 240 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
f60c8b34
JG
241 goto end;
242 }
243
72b913fb 244 /* Ensure that both ports are currently unconnected. */
0d8b4d8e 245 if (bt_port_is_connected(upstream_port)) {
262e5473 246 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
72b913fb
PP
247 goto end;
248 }
249
0d8b4d8e 250 if (bt_port_is_connected(downstream_port)) {
262e5473 251 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
72b913fb
PP
252 goto end;
253 }
254
255 /*
256 * Ensure that both ports are still attached to their creating
257 * component.
258 */
f60c8b34 259 upstream_component = bt_port_get_component(upstream_port);
72b913fb 260 if (!upstream_component) {
262e5473 261 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
72b913fb
PP
262 goto end;
263 }
264
265 downstream_component = bt_port_get_component(downstream_port);
266 if (!downstream_component) {
262e5473 267 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
72b913fb
PP
268 goto end;
269 }
270
262e5473
PP
271 BT_LOGD("Connecting component ports: "
272 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
273 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
274 upstream_component, bt_component_get_name(upstream_component),
275 downstream_component, bt_component_get_name(downstream_component));
276
72b913fb 277 /* Ensure the components are not already part of another graph. */
f60c8b34
JG
278 upstream_graph = bt_component_get_graph(upstream_component);
279 if (upstream_graph && (graph != upstream_graph)) {
262e5473
PP
280 BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: "
281 "other-graph-addr=%p", upstream_graph);
f60c8b34
JG
282 goto error;
283 }
ffeb0eed 284 upstream_was_already_in_graph = (graph == upstream_graph);
f60c8b34
JG
285 downstream_graph = bt_component_get_graph(downstream_component);
286 if (downstream_graph && (graph != downstream_graph)) {
262e5473
PP
287 BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: "
288 "other-graph-addr=%p", downstream_graph);
f60c8b34
JG
289 goto error;
290 }
ffeb0eed 291 downstream_was_already_in_graph = (graph == downstream_graph);
f60c8b34 292
0d8b4d8e
PP
293 /*
294 * At this point the ports are not connected yet. Both
295 * components need to accept an eventual connection to their
296 * port by the other port before we continue.
297 */
262e5473 298 BT_LOGD_STR("Asking upstream component to accept the connection.");
0d8b4d8e
PP
299 component_status = bt_component_accept_port_connection(
300 upstream_component, upstream_port, downstream_port);
301 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
302 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
303 BT_LOGD_STR("Upstream component refused the connection.");
304 } else {
305 BT_LOGW("Cannot ask upstream component to accept the connection: "
306 "status=%s", bt_component_status_string(component_status));
307 }
308
0d8b4d8e
PP
309 goto error;
310 }
262e5473
PP
311
312 BT_LOGD_STR("Asking downstream component to accept the connection.");
0d8b4d8e
PP
313 component_status = bt_component_accept_port_connection(
314 downstream_component, downstream_port, upstream_port);
315 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
316 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
317 BT_LOGD_STR("Downstream component refused the connection.");
318 } else {
319 BT_LOGW("Cannot ask downstream component to accept the connection: "
320 "status=%s", bt_component_status_string(component_status));
321 }
322
0d8b4d8e
PP
323 goto error;
324 }
325
262e5473 326 BT_LOGD_STR("Creating connection.");
f60c8b34
JG
327 connection = bt_connection_create(graph, upstream_port,
328 downstream_port);
329 if (!connection) {
262e5473 330 BT_LOGW("Cannot create connection object.");
f60c8b34
JG
331 goto error;
332 }
333
262e5473
PP
334 BT_LOGD("Connection object created: conn-addr=%p", connection);
335
f60c8b34 336 /*
72b913fb
PP
337 * Ownership of upstream_component/downstream_component and of
338 * the connection object is transferred to the graph.
f60c8b34
JG
339 */
340 g_ptr_array_add(graph->connections, connection);
ffeb0eed
JG
341
342 if (!upstream_was_already_in_graph) {
343 g_ptr_array_add(graph->components, upstream_component);
344 bt_component_set_graph(upstream_component, graph);
345 }
346 if (!downstream_was_already_in_graph) {
347 g_ptr_array_add(graph->components, downstream_component);
348 bt_component_set_graph(downstream_component, graph);
349 if (bt_component_get_class_type(downstream_component) ==
350 BT_COMPONENT_CLASS_TYPE_SINK) {
351 g_queue_push_tail(graph->sinks_to_consume,
352 downstream_component);
353 }
f60c8b34
JG
354 }
355
356 /*
0d8b4d8e
PP
357 * The graph is now the parent of these components which
358 * garantees their existence for the duration of the graph's
359 * lifetime.
f60c8b34 360 */
f60c8b34
JG
361
362 /*
0d8b4d8e 363 * Notify both components that their port is connected.
f60c8b34 364 */
262e5473 365 BT_LOGD_STR("Notifying upstream component that its port is connected.");
0d8b4d8e
PP
366 bt_component_port_connected(upstream_component, upstream_port,
367 downstream_port);
262e5473 368 BT_LOGD_STR("Notifying downstream component that its port is connected.");
0d8b4d8e
PP
369 bt_component_port_connected(downstream_component, downstream_port,
370 upstream_port);
1bf957a0
PP
371
372 /*
0d8b4d8e 373 * Notify the graph's creator that both ports are connected.
1bf957a0 374 */
262e5473 375 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
f345f8bb 376 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
262e5473
PP
377 BT_LOGD("Connected component ports within graph: "
378 "graph-addr=%p, "
379 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
380 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
381 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
382 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
383 graph,
384 upstream_component, bt_component_get_name(upstream_component),
385 downstream_component, bt_component_get_name(downstream_component),
386 upstream_port, bt_port_get_name(upstream_port),
387 downstream_port, bt_port_get_name(downstream_port));
1bf957a0 388
f60c8b34
JG
389end:
390 bt_put(upstream_graph);
391 bt_put(downstream_graph);
3eeacbb9
JG
392 bt_put(upstream_component);
393 bt_put(downstream_component);
f60c8b34 394 return connection;
3eeacbb9 395
3eeacbb9
JG
396error:
397 BT_PUT(upstream_component);
398 BT_PUT(downstream_component);
f60c8b34
JG
399 goto end;
400}
401
72b913fb 402enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
c0418dd9 403{
f60c8b34 404 struct bt_component *sink;
72b913fb
PP
405 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
406 enum bt_component_status comp_status;
f60c8b34
JG
407 GList *current_node;
408
409 if (!graph) {
262e5473 410 BT_LOGW_STR("Invalid parameter: graph is NULL.");
72b913fb 411 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
412 goto end;
413 }
414
262e5473
PP
415 BT_LOGV("Making sink consume: addr=%p", graph);
416
202a3a13 417 if (graph->canceled) {
262e5473 418 BT_LOGW_STR("Invalid parameter: graph is canceled.");
202a3a13
PP
419 status = BT_GRAPH_STATUS_CANCELED;
420 goto end;
421 }
422
f60c8b34 423 if (g_queue_is_empty(graph->sinks_to_consume)) {
262e5473 424 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
72b913fb 425 status = BT_GRAPH_STATUS_END;
f60c8b34
JG
426 goto end;
427 }
428
429 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
430 sink = current_node->data;
262e5473
PP
431 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
432 sink, bt_component_get_name(sink));
72b913fb 433 comp_status = bt_component_sink_consume(sink);
262e5473
PP
434 BT_LOGV("Consumed from sink: status=%s",
435 bt_component_status_string(comp_status));
72b913fb
PP
436 switch (comp_status) {
437 case BT_COMPONENT_STATUS_OK:
438 break;
439 case BT_COMPONENT_STATUS_END:
440 status = BT_GRAPH_STATUS_END;
441 break;
442 case BT_COMPONENT_STATUS_AGAIN:
443 status = BT_GRAPH_STATUS_AGAIN;
444 break;
445 case BT_COMPONENT_STATUS_INVALID:
446 status = BT_GRAPH_STATUS_INVALID;
447 break;
448 default:
449 status = BT_GRAPH_STATUS_ERROR;
450 break;
451 }
452
453 if (status != BT_GRAPH_STATUS_END) {
f60c8b34
JG
454 g_queue_push_tail_link(graph->sinks_to_consume, current_node);
455 goto end;
456 }
457
458 /* End reached, the node is not added back to the queue and free'd. */
459 g_queue_delete_link(graph->sinks_to_consume, current_node);
460
461 /* Don't forward an END status if there are sinks left to consume. */
462 if (!g_queue_is_empty(graph->sinks_to_consume)) {
463 status = BT_GRAPH_STATUS_OK;
464 goto end;
465 }
466end:
262e5473 467 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status));
f60c8b34 468 return status;
c0418dd9
JG
469}
470
72b913fb 471enum bt_graph_status bt_graph_run(struct bt_graph *graph)
f60c8b34 472{
72b913fb 473 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
474
475 if (!graph) {
262e5473 476 BT_LOGW_STR("Invalid parameter: graph is NULL.");
72b913fb 477 status = BT_GRAPH_STATUS_INVALID;
202a3a13 478 goto end;
f60c8b34
JG
479 }
480
262e5473
PP
481 BT_LOGV("Running graph: addr=%p", graph);
482
f60c8b34 483 do {
72b913fb
PP
484 status = bt_graph_consume(graph);
485 if (status == BT_GRAPH_STATUS_AGAIN) {
f60c8b34 486 /*
202a3a13
PP
487 * If AGAIN is received and there are multiple
488 * sinks, go ahead and consume from the next
489 * sink.
f60c8b34 490 *
202a3a13
PP
491 * However, in the case where a single sink is
492 * left, the caller can decide to busy-wait and
493 * call bt_graph_run() continuously until the
494 * source is ready or it can decide to sleep for
495 * an arbitrary amount of time.
f60c8b34
JG
496 */
497 if (graph->sinks_to_consume->length > 1) {
72b913fb 498 status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
499 }
500 }
72b913fb 501 } while (status == BT_GRAPH_STATUS_OK);
f60c8b34
JG
502
503 if (g_queue_is_empty(graph->sinks_to_consume)) {
72b913fb 504 status = BT_GRAPH_STATUS_END;
f60c8b34 505 }
262e5473 506
202a3a13 507end:
262e5473 508 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
72b913fb 509 return status;
f60c8b34 510}
1bf957a0
PP
511
512static
0d107cdd 513int add_listener(GArray *listeners, void *func, void *data)
1bf957a0
PP
514{
515 struct bt_graph_listener listener = {
516 .func = func,
517 .data = data,
518 };
519
520 g_array_append_val(listeners, listener);
0d107cdd 521 return listeners->len - 1;
1bf957a0
PP
522}
523
0d107cdd 524int bt_graph_add_port_added_listener(
1bf957a0
PP
525 struct bt_graph *graph,
526 bt_graph_port_added_listener listener, void *data)
527{
0d107cdd 528 int ret;
1bf957a0 529
262e5473
PP
530 if (!graph) {
531 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 532 ret = -1;
262e5473
PP
533 goto end;
534 }
535
536 if (!listener) {
537 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 538 ret = -1;
1bf957a0
PP
539 goto end;
540 }
541
0d107cdd 542 ret = add_listener(graph->listeners.port_added, listener, data);
262e5473 543 BT_LOGV("Added \"port added\" listener to graph: "
0d107cdd
PP
544 "graph-addr=%p, listener-addr=%p, pos=%d",
545 graph, listener, ret);
1bf957a0
PP
546
547end:
0d107cdd 548 return ret;
1bf957a0
PP
549}
550
0d107cdd 551int bt_graph_add_port_removed_listener(
1bf957a0
PP
552 struct bt_graph *graph,
553 bt_graph_port_removed_listener listener, void *data)
554{
0d107cdd 555 int ret;
1bf957a0 556
262e5473
PP
557 if (!graph) {
558 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 559 ret = -1;
262e5473
PP
560 goto end;
561 }
562
563 if (!listener) {
564 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 565 ret = -1;
1bf957a0
PP
566 goto end;
567 }
568
0d107cdd 569 ret = add_listener(graph->listeners.port_removed, listener, data);
262e5473 570 BT_LOGV("Added \"port removed\" listener to graph: "
0d107cdd
PP
571 "graph-addr=%p, listener-addr=%p, pos=%d",
572 graph, listener, ret);
1bf957a0
PP
573
574end:
0d107cdd 575 return ret;
1bf957a0
PP
576}
577
0d107cdd 578int bt_graph_add_ports_connected_listener(
1bf957a0 579 struct bt_graph *graph,
f345f8bb 580 bt_graph_ports_connected_listener listener, void *data)
1bf957a0 581{
0d107cdd 582 int ret;
1bf957a0 583
262e5473
PP
584 if (!graph) {
585 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 586 ret = -1;
262e5473
PP
587 goto end;
588 }
589
590 if (!listener) {
591 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 592 ret = -1;
1bf957a0
PP
593 goto end;
594 }
595
0d107cdd 596 ret = add_listener(graph->listeners.ports_connected, listener, data);
262e5473 597 BT_LOGV("Added \"port connected\" listener to graph: "
0d107cdd
PP
598 "graph-addr=%p, listener-addr=%p, pos=%d",
599 graph, listener, ret);
1bf957a0
PP
600
601end:
0d107cdd 602 return ret;
1bf957a0
PP
603}
604
0d107cdd 605int bt_graph_add_ports_disconnected_listener(
1bf957a0 606 struct bt_graph *graph,
f345f8bb 607 bt_graph_ports_disconnected_listener listener, void *data)
1bf957a0 608{
0d107cdd 609 int ret;
1bf957a0 610
262e5473
PP
611 if (!graph) {
612 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 613 ret = -1;
262e5473
PP
614 goto end;
615 }
616
617 if (!listener) {
618 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 619 ret = -1;
1bf957a0
PP
620 goto end;
621 }
622
0d107cdd 623 ret = add_listener(graph->listeners.ports_disconnected, listener, data);
262e5473 624 BT_LOGV("Added \"port disconnected\" listener to graph: "
0d107cdd
PP
625 "graph-addr=%p, listener-addr=%p, pos=%d",
626 graph, listener, ret);
1bf957a0
PP
627
628end:
0d107cdd 629 return ret;
1bf957a0
PP
630}
631
632BT_HIDDEN
633void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
634{
635 size_t i;
636
262e5473
PP
637 BT_LOGV("Notifying graph listeners that a port was added: "
638 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
639 graph, port, bt_port_get_name(port));
640
1bf957a0
PP
641 for (i = 0; i < graph->listeners.port_added->len; i++) {
642 struct bt_graph_listener listener =
643 g_array_index(graph->listeners.port_added,
644 struct bt_graph_listener, i);
645 bt_graph_port_added_listener func = listener.func;
646
647 assert(func);
648 func(port, listener.data);
649 }
650}
651
652BT_HIDDEN
653void bt_graph_notify_port_removed(struct bt_graph *graph,
654 struct bt_component *comp, struct bt_port *port)
655{
656 size_t i;
657
262e5473
PP
658 BT_LOGV("Notifying graph listeners that a port was removed: "
659 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
660 graph, port, bt_port_get_name(port));
661
1bf957a0
PP
662 for (i = 0; i < graph->listeners.port_removed->len; i++) {
663 struct bt_graph_listener listener =
664 g_array_index(graph->listeners.port_removed,
665 struct bt_graph_listener, i);
666 bt_graph_port_removed_listener func = listener.func;
667
668 assert(func);
669 func(comp, port, listener.data);
670 }
671}
672
673BT_HIDDEN
f345f8bb
PP
674void bt_graph_notify_ports_connected(struct bt_graph *graph,
675 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0
PP
676{
677 size_t i;
678
262e5473
PP
679 BT_LOGV("Notifying graph listeners that two ports were connected: "
680 "graph-addr=%p, "
681 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
682 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
683 graph, upstream_port, bt_port_get_name(upstream_port),
684 downstream_port, bt_port_get_name(downstream_port));
685
f345f8bb 686 for (i = 0; i < graph->listeners.ports_connected->len; i++) {
1bf957a0 687 struct bt_graph_listener listener =
f345f8bb 688 g_array_index(graph->listeners.ports_connected,
1bf957a0 689 struct bt_graph_listener, i);
f345f8bb 690 bt_graph_ports_connected_listener func = listener.func;
1bf957a0
PP
691
692 assert(func);
f345f8bb 693 func(upstream_port, downstream_port, listener.data);
1bf957a0
PP
694 }
695}
696
697BT_HIDDEN
f345f8bb
PP
698void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
699 struct bt_component *upstream_comp,
700 struct bt_component *downstream_comp,
701 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0
PP
702{
703 size_t i;
704
262e5473
PP
705 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
706 "graph-addr=%p, "
707 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
708 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
709 graph, upstream_port, bt_port_get_name(upstream_port),
710 downstream_port, bt_port_get_name(downstream_port));
711
f345f8bb 712 for (i = 0; i < graph->listeners.ports_disconnected->len; i++) {
1bf957a0 713 struct bt_graph_listener listener =
f345f8bb 714 g_array_index(graph->listeners.ports_disconnected,
1bf957a0 715 struct bt_graph_listener, i);
f345f8bb 716 bt_graph_ports_disconnected_listener func = listener.func;
1bf957a0
PP
717
718 assert(func);
f345f8bb
PP
719 func(upstream_comp, downstream_comp, upstream_port,
720 downstream_port, listener.data);
1bf957a0
PP
721 }
722}
202a3a13
PP
723
724extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph)
725{
726 enum bt_graph_status ret = BT_GRAPH_STATUS_OK;
727
728 if (!graph) {
262e5473 729 BT_LOGW_STR("Invalid parameter: graph is NULL.");
202a3a13
PP
730 ret = BT_GRAPH_STATUS_INVALID;
731 goto end;
732 }
733
734 graph->canceled = BT_TRUE;
262e5473 735 BT_LOGV("Canceled graph: addr=%p", graph);
202a3a13
PP
736
737end:
738 return ret;
739}
740
741extern bt_bool bt_graph_is_canceled(struct bt_graph *graph)
742{
743 return graph ? graph->canceled : BT_FALSE;
744}
f167d3c0
PP
745
746BT_HIDDEN
747void bt_graph_remove_connection(struct bt_graph *graph,
748 struct bt_connection *connection)
749{
750 assert(graph);
751 assert(connection);
262e5473
PP
752 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
753 graph, connection);
f167d3c0
PP
754 g_ptr_array_remove(graph->connections, connection);
755}
This page took 0.064432 seconds and 4 git commands to generate.