Debian Codegraph @

examples/dbus.c

raw symbols SCIP
1 /*
2 * dbus.c
3 *
4 * Copyright 2025 Red Hat, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <libdex.h>
25
26
27 #include "dex-dbus-ping-pong.h"
28
29 struct _DexPingPong
30 {
31 DexDbusPingPongSkeleton parent_instance;
32 };
33
34 struct DexPingPongClass
35 {
36 DexDbusPingPongSkeletonClass parent_class;
37 };
38
39 static void dex_dbus_ping_pong_iface_init (DexDbusPingPongIface *iface);
40
41 #define DEX_TYPE_PING_PONG (dex_ping_pong_get_type ())
42 G_DECLARE_FINAL_TYPE (DexPingPong,
43 dex_ping_pong,
44 DEX, PING_PONG,
45 DexDbusPingPongSkeleton)
46
47 G_DEFINE_TYPE_WITH_CODE (DexPingPong,
48 dex_ping_pong,
49 DEX_DBUS_TYPE_PING_PONG_SKELETON,
50 G_IMPLEMENT_INTERFACE (DEX_DBUS_TYPE_PING_PONG,
51 dex_dbus_ping_pong_iface_init));
52
53 static gboolean
54 handle_ping (DexDbusPingPong *object,
55 GDBusMethodInvocation *invocation,
56 const char *ping)
57 {
58 g_print ("service: %s\n", ping);
59
60 dex_await (dex_timeout_new_seconds (1), NULL);
61 dex_dbus_ping_pong_complete_ping (object, invocation, "pong");
62
63 return G_DBUS_METHOD_INVOCATION_HANDLED;
64 }
65
66 static void
67 dex_dbus_ping_pong_iface_init (DexDbusPingPongIface *iface)
68 {
69 iface->handle_ping = handle_ping;
70 }
71
72 static void
73 dex_ping_pong_init (DexPingPong *pp G_GNUC_UNUSED)
74 {
75 }
76
77 static void
78 dex_ping_pong_class_init (DexPingPongClass *klass G_GNUC_UNUSED)
79 {
80 }
81
82 static DexFuture *
83 emit_reloading_signals_fiber (gpointer user_data)
84 {
85 DexDbusPingPong *pp = DEX_DBUS_PING_PONG (user_data);
86
87 while (TRUE)
88 {
89 g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
90 g_autoptr(GError) error = NULL;
91
92 g_variant_builder_add (&builder, "{sv}", "test",
93 g_variant_new_int32 (g_random_int_range (0, 100)));
94
95 dex_dbus_ping_pong_emit_reloading (pp, TRUE, g_variant_builder_end (&builder));
96
97 if (!dex_await (dex_timeout_new_seconds (g_random_int_range (1, 4)), &error) &&
98 !g_error_matches (error, DEX_ERROR, DEX_ERROR_TIMED_OUT))
99 return dex_future_new_for_error (g_steal_pointer (&error));
100 }
101 g_assert_not_reached ();
102 }
103
104 static DexFuture *
105 run_service_fiber (gpointer user_data G_GNUC_UNUSED)
106 {
107 g_autoptr(GDBusConnection) connection = NULL;
108 g_autoptr(DexFuture) name_acquired = NULL;
109 g_autoptr(DexFuture) name_lost = NULL;
110 g_autoptr(DexPingPong) pp = NULL;
111 g_autoptr(GError) error = NULL;
112
113 connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SESSION), &error);
114 if (!connection)
115 return dex_future_new_for_error (g_steal_pointer (&error));
116
117 dex_bus_own_name_on_connection (connection,
118 "org.example.PingPong",
119 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
120 G_BUS_NAME_OWNER_FLAGS_REPLACE,
121 &name_acquired,
122 &name_lost);
123
124 if (!dex_await (g_steal_pointer (&name_acquired), &error))
125 return dex_future_new_for_error (g_steal_pointer (&error));
126
127 pp = g_object_new (DEX_TYPE_PING_PONG, NULL);
128 dex_dbus_interface_skeleton_set_flags (DEX_DBUS_INTERFACE_SKELETON (pp),
129 DEX_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_FIBER);
130
131 if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (pp),
132 connection,
133 "/org/example/pingpong",
134 &error))
135 return dex_future_new_for_error (g_steal_pointer (&error));
136
137
138
139 dex_await (dex_future_first (g_steal_pointer (&name_lost),
140 dex_scheduler_spawn (NULL, 0,
141 emit_reloading_signals_fiber,
142 g_object_ref (pp),
143 g_object_unref),
144 NULL),
145 NULL);
146
147 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (pp));
148 dex_dbus_interface_skeleton_cancel (DEX_DBUS_INTERFACE_SKELETON (pp));
149
150 return dex_future_new_true ();
151 }
152
153 static DexFuture *
154 ping_fiber (gpointer user_data)
155 {
156 g_autoptr(DexDbusPingPong) pp = g_object_ref (user_data);
157
158 while (TRUE)
159 {
160 g_autoptr(DexDbusPingPongPingResult) res = NULL;
161 g_autoptr(GError) error = NULL;
162
163 dex_await (dex_timeout_new_seconds (1), NULL);
164
165 res = dex_await_boxed (dex_dbus_ping_pong_call_ping_future (pp, "ping"), &error);
166 if (!res)
167 return dex_future_new_for_error (g_steal_pointer (&error));
168
169 g_print ("client: %s\n", res->pong);
170 }
171 g_assert_not_reached ();
172 }
173
174 static DexFuture *
175 wait_for_reload_fiber (gpointer user_data)
176 {
177 g_autoptr(DexDbusPingPong) pp = user_data;
178 g_autoptr(DexDbusPingPongSignalMonitor) signal_monitor =
179 dex_dbus_ping_pong_signal_monitor_new (pp, DEX_DBUS_PING_PONG_SIGNAL_RELOADING);
180
181 while (TRUE)
182 {
183 g_autoptr(DexDbusPingPongReloadingSignal) reloading = NULL;
184 g_autoptr(GError) error = NULL;
185 int test;
186
187 reloading = dex_await_boxed (dex_future_first (dex_dbus_ping_pong_signal_monitor_next_reloading (signal_monitor),
188 dex_timeout_new_seconds (10),
189 NULL),
190 &error);
191 if (!reloading)
192 return dex_future_new_for_error (g_steal_pointer (&error));
193
194 g_variant_lookup (reloading->options, "test", "i", &test);
195 g_print ("signal: received reloading, active = %d, options[test] = %d\n",
196 reloading->active,
197 test);
198 }
199 g_assert_not_reached ();
200 }
201
202 static DexFuture *
203 run_client_fiber (gpointer user_data G_GNUC_UNUSED)
204 {
205 g_autoptr(DexDbusPingPong) pp = NULL;
206
207 {
208 g_autoptr(GDBusConnection) connection = NULL;
209 g_autoptr(GError) error = NULL;
210
211 connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SESSION), &error);
212 if (!connection)
213 return dex_future_new_for_error (g_steal_pointer (&error));
214
215 pp = dex_await_object (dex_dbus_ping_pong_proxy_new_future (connection,
216 G_DBUS_PROXY_FLAGS_NONE,
217 "org.example.PingPong",
218 "/org/example/pingpong"),
219 &error);
220 if (!pp)
221 return dex_future_new_for_error (g_steal_pointer (&error));
222 }
223
224 {
225 g_autoptr(GError) error = NULL;
226
227 if (!dex_await (dex_future_all (dex_scheduler_spawn (NULL, 0,
228 ping_fiber,
229 pp, NULL),
230 dex_scheduler_spawn (NULL, 0,
231 wait_for_reload_fiber,
232 pp, NULL),
233 NULL),
234 &error))
235 return dex_future_new_for_error (g_steal_pointer (&error));
236 }
237
238 return dex_future_new_true ();
239 }
240
241 static DexFuture *
242 quit_cb (DexFuture *future,
243 gpointer user_data)
244 {
245 GMainLoop *main_loop = user_data;
246 g_autoptr (GError) error = NULL;
247
248 if (!dex_await (dex_ref (future), &error))
249 g_printerr ("%s\n", error->message);
250
251 while (g_main_context_iteration (g_main_loop_get_context (main_loop), FALSE));
252 g_main_loop_quit (main_loop);
253
254 return NULL;
255 }
256
257 int
258 main (int argc G_GNUC_UNUSED,
259 char **argv G_GNUC_UNUSED)
260 {
261 g_autoptr(GMainLoop) main_loop = NULL;
262 g_autoptr(DexScheduler) thread_pool = NULL;
263 g_autoptr(DexFuture) future = NULL;
264
265 dex_init ();
266
267 main_loop = g_main_loop_new (NULL, FALSE);
268 thread_pool = dex_thread_pool_scheduler_new ();
269
270 future = dex_future_first (dex_scheduler_spawn (NULL, 0,
271 run_service_fiber,
272 NULL, NULL),
273 dex_scheduler_spawn (NULL, 0,
274 run_client_fiber,
275 NULL, NULL),
276 dex_unix_signal_new (SIGINT),
277 NULL);
278 future = dex_future_finally (future, quit_cb, main_loop, NULL);
279
280 g_main_loop_run (main_loop);
281
282 return EXIT_SUCCESS;
283 }