Debian Codegraph @

src/ws/cockpitflow.c

raw
1 /*
2 * Copyright (C) 2018 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 /**
7 * CockpitFlow:
8 *
9 * An interface representing a bidirectional flow. Implementors are
10 * CockpitPipe and CockpitStream. Currently the interface functionality
11 * is limited to flow control.
12 *
13 * - Its input can be throttled, it can listen to a "pressure" signal
14 * from another object passed into cockpit_pipe_throttle()
15 * - It can optionally control another flow, by emitting a "pressure" signal
16 * when it's output queue is too large
17 */
18
19 #include "config.h"
20
21 /* This gets logged as part of the (more verbose) protocol logging */
22 #ifdef G_LOG_DOMAIN
23 #undef G_LOG_DOMAIN
24 #endif
25 #define G_LOG_DOMAIN "cockpit-protocol"
26
27 #include "cockpitflow.h"
28
29 G_DEFINE_INTERFACE (CockpitFlow, cockpit_flow, 0);
30
31 static guint cockpit_flow_signal_pressure = 0;
32
33 static void
34 cockpit_flow_default_init (CockpitFlowInterface *iface)
35 {
36 /**
37 * CockpitFlow::pressure:
38 * @throttle: Pressure on or off
39 *
40 * Emitted when the pipe wants to give back-pressure to other feeding
41 * streams. It does this when its output queue is too long and should
42 * slow down.
43 */
44 cockpit_flow_signal_pressure = g_signal_new ("pressure", COCKPIT_TYPE_FLOW, G_SIGNAL_RUN_FIRST,
45 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
46 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
47 }
48
49 /**
50 * cockpit_flow_throttle:
51 * @flow: The flow to have input throttled
52 * @controlling: A controlling flow that throttles this one
53 *
54 * When the @controlling flow has pressure, it will slow input on this
55 * flow. If @controlling is NULL
56 */
57 void
58 cockpit_flow_throttle (CockpitFlow *flow,
59 CockpitFlow *controlling)
60 {
61 CockpitFlowInterface *iface;
62
63 g_return_if_fail (COCKPIT_IS_FLOW (flow));
64 g_return_if_fail (controlling == NULL || COCKPIT_IS_FLOW (controlling));
65
66 iface = COCKPIT_FLOW_GET_IFACE (flow);
67 g_return_if_fail (iface->throttle != NULL);
68 (iface->throttle) (flow, controlling);
69 }
70
71 /**
72 * cockpit_flow_emit_pressure:
73 * @flow: The flow
74 * @ressure: Whether to emit back-pressure or release it.
75 *
76 * Emit a "pressure" signal, which indicates back-pressure or
77 * releases it. Used by implementations of CockpitFlow
78 *
79 * This is used to throttle another flow's input if this
80 * flow is the controlling flow.
81 */
82 void
83 cockpit_flow_emit_pressure (CockpitFlow *flow,
84 gboolean pressure)
85 {
86 g_return_if_fail (COCKPIT_IS_FLOW (flow));
87 g_signal_emit (flow, cockpit_flow_signal_pressure, 0, pressure);
88 }