Merge branch 'gbytes-compare-docs' into 'master'
[glib.git] / gio / tests / gdbus-example-server.c
blob6f1123d85f5e07dbf96ad5ad234b90a55b34c933
1 #include <gio/gio.h>
2 #include <stdlib.h>
4 #ifdef G_OS_UNIX
5 #include <gio/gunixfdlist.h>
6 /* For STDOUT_FILENO */
7 #include <unistd.h>
8 #endif
10 /* ---------------------------------------------------------------------------------------------------- */
12 static GDBusNodeInfo *introspection_data = NULL;
14 /* Introspection data for the service we are exporting */
15 static const gchar introspection_xml[] =
16 "<node>"
17 " <interface name='org.gtk.GDBus.TestInterface'>"
18 " <annotation name='org.gtk.GDBus.Annotation' value='OnInterface'/>"
19 " <annotation name='org.gtk.GDBus.Annotation' value='AlsoOnInterface'/>"
20 " <method name='HelloWorld'>"
21 " <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>"
22 " <arg type='s' name='greeting' direction='in'/>"
23 " <arg type='s' name='response' direction='out'/>"
24 " </method>"
25 " <method name='EmitSignal'>"
26 " <arg type='d' name='speed_in_mph' direction='in'>"
27 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg'/>"
28 " </arg>"
29 " </method>"
30 " <method name='GimmeStdout'/>"
31 " <signal name='VelocityChanged'>"
32 " <annotation name='org.gtk.GDBus.Annotation' value='Onsignal'/>"
33 " <arg type='d' name='speed_in_mph'/>"
34 " <arg type='s' name='speed_as_string'>"
35 " <annotation name='org.gtk.GDBus.Annotation' value='OnArg_NonFirst'/>"
36 " </arg>"
37 " </signal>"
38 " <property type='s' name='FluxCapicitorName' access='read'>"
39 " <annotation name='org.gtk.GDBus.Annotation' value='OnProperty'>"
40 " <annotation name='org.gtk.GDBus.Annotation' value='OnAnnotation_YesThisIsCrazy'/>"
41 " </annotation>"
42 " </property>"
43 " <property type='s' name='Title' access='readwrite'/>"
44 " <property type='s' name='ReadingAlwaysThrowsError' access='read'/>"
45 " <property type='s' name='WritingAlwaysThrowsError' access='readwrite'/>"
46 " <property type='s' name='OnlyWritable' access='write'/>"
47 " <property type='s' name='Foo' access='read'/>"
48 " <property type='s' name='Bar' access='read'/>"
49 " </interface>"
50 "</node>";
52 /* ---------------------------------------------------------------------------------------------------- */
54 static void
55 handle_method_call (GDBusConnection *connection,
56 const gchar *sender,
57 const gchar *object_path,
58 const gchar *interface_name,
59 const gchar *method_name,
60 GVariant *parameters,
61 GDBusMethodInvocation *invocation,
62 gpointer user_data)
64 if (g_strcmp0 (method_name, "HelloWorld") == 0)
66 const gchar *greeting;
68 g_variant_get (parameters, "(&s)", &greeting);
70 if (g_strcmp0 (greeting, "Return Unregistered") == 0)
72 g_dbus_method_invocation_return_error (invocation,
73 G_IO_ERROR,
74 G_IO_ERROR_FAILED_HANDLED,
75 "As requested, here's a GError not registered (G_IO_ERROR_FAILED_HANDLED)");
77 else if (g_strcmp0 (greeting, "Return Registered") == 0)
79 g_dbus_method_invocation_return_error (invocation,
80 G_DBUS_ERROR,
81 G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,
82 "As requested, here's a GError that is registered (G_DBUS_ERROR_MATCH_RULE_NOT_FOUND)");
84 else if (g_strcmp0 (greeting, "Return Raw") == 0)
86 g_dbus_method_invocation_return_dbus_error (invocation,
87 "org.gtk.GDBus.SomeErrorName",
88 "As requested, here's a raw D-Bus error");
90 else
92 gchar *response;
93 response = g_strdup_printf ("You greeted me with '%s'. Thanks!", greeting);
94 g_dbus_method_invocation_return_value (invocation,
95 g_variant_new ("(s)", response));
96 g_free (response);
99 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
101 GError *local_error;
102 gdouble speed_in_mph;
103 gchar *speed_as_string;
105 g_variant_get (parameters, "(d)", &speed_in_mph);
106 speed_as_string = g_strdup_printf ("%g mph!", speed_in_mph);
108 local_error = NULL;
109 g_dbus_connection_emit_signal (connection,
110 NULL,
111 object_path,
112 interface_name,
113 "VelocityChanged",
114 g_variant_new ("(ds)",
115 speed_in_mph,
116 speed_as_string),
117 &local_error);
118 g_assert_no_error (local_error);
119 g_free (speed_as_string);
121 g_dbus_method_invocation_return_value (invocation, NULL);
123 else if (g_strcmp0 (method_name, "GimmeStdout") == 0)
125 #ifdef G_OS_UNIX
126 if (g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
128 GDBusMessage *reply;
129 GUnixFDList *fd_list;
130 GError *error;
132 fd_list = g_unix_fd_list_new ();
133 error = NULL;
134 g_unix_fd_list_append (fd_list, STDOUT_FILENO, &error);
135 g_assert_no_error (error);
137 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
138 g_dbus_message_set_unix_fd_list (reply, fd_list);
140 error = NULL;
141 g_dbus_connection_send_message (connection,
142 reply,
143 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
144 NULL, /* out_serial */
145 &error);
146 g_assert_no_error (error);
148 g_object_unref (invocation);
149 g_object_unref (fd_list);
150 g_object_unref (reply);
152 else
154 g_dbus_method_invocation_return_dbus_error (invocation,
155 "org.gtk.GDBus.Failed",
156 "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
158 #else
159 g_dbus_method_invocation_return_dbus_error (invocation,
160 "org.gtk.GDBus.NotOnUnix",
161 "Your OS does not support file descriptor passing");
162 #endif
166 static gchar *_global_title = NULL;
168 static gboolean swap_a_and_b = FALSE;
170 static GVariant *
171 handle_get_property (GDBusConnection *connection,
172 const gchar *sender,
173 const gchar *object_path,
174 const gchar *interface_name,
175 const gchar *property_name,
176 GError **error,
177 gpointer user_data)
179 GVariant *ret;
181 ret = NULL;
182 if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
184 ret = g_variant_new_string ("DeLorean");
186 else if (g_strcmp0 (property_name, "Title") == 0)
188 if (_global_title == NULL)
189 _global_title = g_strdup ("Back To C!");
190 ret = g_variant_new_string (_global_title);
192 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
194 g_set_error (error,
195 G_IO_ERROR,
196 G_IO_ERROR_FAILED,
197 "Hello %s. I thought I said reading this property "
198 "always results in an error. kthxbye",
199 sender);
201 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
203 ret = g_variant_new_string ("There's no home like home");
205 else if (g_strcmp0 (property_name, "Foo") == 0)
207 ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
209 else if (g_strcmp0 (property_name, "Bar") == 0)
211 ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
214 return ret;
217 static gboolean
218 handle_set_property (GDBusConnection *connection,
219 const gchar *sender,
220 const gchar *object_path,
221 const gchar *interface_name,
222 const gchar *property_name,
223 GVariant *value,
224 GError **error,
225 gpointer user_data)
227 if (g_strcmp0 (property_name, "Title") == 0)
229 if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
231 GVariantBuilder *builder;
232 GError *local_error;
234 g_free (_global_title);
235 _global_title = g_variant_dup_string (value, NULL);
237 local_error = NULL;
238 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
239 g_variant_builder_add (builder,
240 "{sv}",
241 "Title",
242 g_variant_new_string (_global_title));
243 g_dbus_connection_emit_signal (connection,
244 NULL,
245 object_path,
246 "org.freedesktop.DBus.Properties",
247 "PropertiesChanged",
248 g_variant_new ("(sa{sv}as)",
249 interface_name,
250 builder,
251 NULL),
252 &local_error);
253 g_assert_no_error (local_error);
256 else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
258 /* do nothing - they can't read it after all! */
260 else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
262 g_set_error (error,
263 G_IO_ERROR,
264 G_IO_ERROR_FAILED,
265 "Hello AGAIN %s. I thought I said writing this property "
266 "always results in an error. kthxbye",
267 sender);
270 return *error == NULL;
274 /* for now */
275 static const GDBusInterfaceVTable interface_vtable =
277 handle_method_call,
278 handle_get_property,
279 handle_set_property
282 /* ---------------------------------------------------------------------------------------------------- */
284 static gboolean
285 on_timeout_cb (gpointer user_data)
287 GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
288 GVariantBuilder *builder;
289 GVariantBuilder *invalidated_builder;
290 GError *error;
292 swap_a_and_b = !swap_a_and_b;
294 error = NULL;
295 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
296 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
297 g_variant_builder_add (builder,
298 "{sv}",
299 "Foo",
300 g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
301 g_variant_builder_add (builder,
302 "{sv}",
303 "Bar",
304 g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
305 g_dbus_connection_emit_signal (connection,
306 NULL,
307 "/org/gtk/GDBus/TestObject",
308 "org.freedesktop.DBus.Properties",
309 "PropertiesChanged",
310 g_variant_new ("(sa{sv}as)",
311 "org.gtk.GDBus.TestInterface",
312 builder,
313 invalidated_builder),
314 &error);
315 g_assert_no_error (error);
318 return TRUE;
321 /* ---------------------------------------------------------------------------------------------------- */
323 static void
324 on_bus_acquired (GDBusConnection *connection,
325 const gchar *name,
326 gpointer user_data)
328 guint registration_id;
330 registration_id = g_dbus_connection_register_object (connection,
331 "/org/gtk/GDBus/TestObject",
332 introspection_data->interfaces[0],
333 &interface_vtable,
334 NULL, /* user_data */
335 NULL, /* user_data_free_func */
336 NULL); /* GError** */
337 g_assert (registration_id > 0);
339 /* swap value of properties Foo and Bar every two seconds */
340 g_timeout_add_seconds (2,
341 on_timeout_cb,
342 connection);
345 static void
346 on_name_acquired (GDBusConnection *connection,
347 const gchar *name,
348 gpointer user_data)
352 static void
353 on_name_lost (GDBusConnection *connection,
354 const gchar *name,
355 gpointer user_data)
357 exit (1);
361 main (int argc, char *argv[])
363 guint owner_id;
364 GMainLoop *loop;
366 /* We are lazy here - we don't want to manually provide
367 * the introspection data structures - so we just build
368 * them from XML.
370 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
371 g_assert (introspection_data != NULL);
373 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
374 "org.gtk.GDBus.TestServer",
375 G_BUS_NAME_OWNER_FLAGS_NONE,
376 on_bus_acquired,
377 on_name_acquired,
378 on_name_lost,
379 NULL,
380 NULL);
382 loop = g_main_loop_new (NULL, FALSE);
383 g_main_loop_run (loop);
385 g_bus_unown_name (owner_id);
387 g_dbus_node_info_unref (introspection_data);
389 return 0;