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