Merge remote branch 'gvdb/master'
[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 #include <gio/gunixfdlist.h>
6 /* For STDOUT_FILENO */
7 #include <unistd.h>
8 #endif
9
10 /* ---------------------------------------------------------------------------------------------------- */
11
12 static GDBusNodeInfo *introspection_data = NULL;
13
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>";
51
52 /* ---------------------------------------------------------------------------------------------------- */
53
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)
63 {
64   if (g_strcmp0 (method_name, "HelloWorld") == 0)
65     {
66       const gchar *greeting;
67
68       g_variant_get (parameters, "(&s)", &greeting);
69
70       if (g_strcmp0 (greeting, "Return Unregistered") == 0)
71         {
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)");
76         }
77       else if (g_strcmp0 (greeting, "Return Registered") == 0)
78         {
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)");
83         }
84       else if (g_strcmp0 (greeting, "Return Raw") == 0)
85         {
86           g_dbus_method_invocation_return_dbus_error (invocation,
87                                                       "org.gtk.GDBus.SomeErrorName",
88                                                       "As requested, here's a raw D-Bus error");
89         }
90       else
91         {
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);
97         }
98     }
99   else if (g_strcmp0 (method_name, "EmitSignal") == 0)
100     {
101       GError *local_error;
102       gdouble speed_in_mph;
103       gchar *speed_as_string;
104
105       g_variant_get (parameters, "(d)", &speed_in_mph);
106       speed_as_string = g_strdup_printf ("%g mph!", speed_in_mph);
107
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);
120
121       g_dbus_method_invocation_return_value (invocation, NULL);
122     }
123   else if (g_strcmp0 (method_name, "GimmeStdout") == 0)
124     {
125 #ifdef G_OS_UNIX
126       if (g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
127         {
128           GDBusMessage *reply;
129           GUnixFDList *fd_list;
130           GError *error;
131
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);
136
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);
139
140           error = NULL;
141           g_dbus_connection_send_message (connection,
142                                           reply,
143                                           NULL, /* out_serial */
144                                           &error);
145           g_assert_no_error (error);
146
147           g_object_unref (invocation);
148           g_object_unref (fd_list);
149           g_object_unref (reply);
150         }
151       else
152         {
153           g_dbus_method_invocation_return_dbus_error (invocation,
154                                                       "org.gtk.GDBus.Failed",
155                                                       "Your message bus daemon does not support file descriptor passing (need D-Bus >= 1.3.0)");
156         }
157 #else
158       g_dbus_method_invocation_return_dbus_error (invocation,
159                                                   "org.gtk.GDBus.NotOnUnix",
160                                                   "Your OS does not support file descriptor passing");
161 #endif
162     }
163 }
164
165 static gchar *_global_title = NULL;
166
167 static gboolean swap_a_and_b = FALSE;
168
169 static GVariant *
170 handle_get_property (GDBusConnection  *connection,
171                      const gchar      *sender,
172                      const gchar      *object_path,
173                      const gchar      *interface_name,
174                      const gchar      *property_name,
175                      GError          **error,
176                      gpointer          user_data)
177 {
178   GVariant *ret;
179
180   ret = NULL;
181   if (g_strcmp0 (property_name, "FluxCapicitorName") == 0)
182     {
183       ret = g_variant_new_string ("DeLorean");
184     }
185   else if (g_strcmp0 (property_name, "Title") == 0)
186     {
187       if (_global_title == NULL)
188         _global_title = g_strdup ("Back To C!");
189       ret = g_variant_new_string (_global_title);
190     }
191   else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
192     {
193       g_set_error (error,
194                    G_IO_ERROR,
195                    G_IO_ERROR_FAILED,
196                    "Hello %s. I thought I said reading this property "
197                    "always results in an error. kthxbye",
198                    sender);
199     }
200   else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
201     {
202       ret = g_variant_new_string ("There's no home like home");
203     }
204   else if (g_strcmp0 (property_name, "Foo") == 0)
205     {
206       ret = g_variant_new_string (swap_a_and_b ? "Tock" : "Tick");
207     }
208   else if (g_strcmp0 (property_name, "Bar") == 0)
209     {
210       ret = g_variant_new_string (swap_a_and_b ? "Tick" : "Tock");
211     }
212
213   return ret;
214 }
215
216 static gboolean
217 handle_set_property (GDBusConnection  *connection,
218                      const gchar      *sender,
219                      const gchar      *object_path,
220                      const gchar      *interface_name,
221                      const gchar      *property_name,
222                      GVariant         *value,
223                      GError          **error,
224                      gpointer          user_data)
225 {
226   if (g_strcmp0 (property_name, "Title") == 0)
227     {
228       if (g_strcmp0 (_global_title, g_variant_get_string (value, NULL)) != 0)
229         {
230           GVariantBuilder *builder;
231           GError *local_error;
232
233           g_free (_global_title);
234           _global_title = g_variant_dup_string (value, NULL);
235
236           local_error = NULL;
237           builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
238           g_variant_builder_add (builder,
239                                  "{sv}",
240                                  "Title",
241                                  g_variant_new_string (_global_title));
242           g_dbus_connection_emit_signal (connection,
243                                          NULL,
244                                          object_path,
245                                          "org.freedesktop.DBus.Properties",
246                                          "PropertiesChanged",
247                                          g_variant_new ("(sa{sv}as)",
248                                                         interface_name,
249                                                         builder,
250                                                         NULL),
251                                          &local_error);
252           g_assert_no_error (local_error);
253         }
254     }
255   else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
256     {
257       /* do nothing - they can't read it after all! */
258     }
259   else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
260     {
261       g_set_error (error,
262                    G_IO_ERROR,
263                    G_IO_ERROR_FAILED,
264                    "Hello AGAIN %s. I thought I said writing this property "
265                    "always results in an error. kthxbye",
266                    sender);
267     }
268
269   return *error == NULL;
270 }
271
272
273 /* for now */
274 static const GDBusInterfaceVTable interface_vtable =
275 {
276   handle_method_call,
277   handle_get_property,
278   handle_set_property
279 };
280
281 /* ---------------------------------------------------------------------------------------------------- */
282
283 static gboolean
284 on_timeout_cb (gpointer user_data)
285 {
286   GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
287   GVariantBuilder *builder;
288   GVariantBuilder *invalidated_builder;
289   GError *error;
290
291   swap_a_and_b = !swap_a_and_b;
292
293   error = NULL;
294   builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
295   invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
296   g_variant_builder_add (builder,
297                          "{sv}",
298                          "Foo",
299                          g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
300   g_variant_builder_add (builder,
301                          "{sv}",
302                          "Bar",
303                          g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
304   g_dbus_connection_emit_signal (connection,
305                                  NULL,
306                                  "/org/gtk/GDBus/TestObject",
307                                  "org.freedesktop.DBus.Properties",
308                                  "PropertiesChanged",
309                                  g_variant_new ("(sa{sv}as)",
310                                                 "org.gtk.GDBus.TestInterface",
311                                                 builder,
312                                                 invalidated_builder),
313                                  &error);
314   g_assert_no_error (error);
315
316
317   return TRUE;
318 }
319
320 /* ---------------------------------------------------------------------------------------------------- */
321
322 static void
323 on_bus_acquired (GDBusConnection *connection,
324                  const gchar     *name,
325                  gpointer         user_data)
326 {
327   guint registration_id;
328
329   registration_id = g_dbus_connection_register_object (connection,
330                                                        "/org/gtk/GDBus/TestObject",
331                                                        introspection_data->interfaces[0],
332                                                        &interface_vtable,
333                                                        NULL,  /* user_data */
334                                                        NULL,  /* user_data_free_func */
335                                                        NULL); /* GError** */
336   g_assert (registration_id > 0);
337
338   /* swap value of properties Foo and Bar every two seconds */
339   g_timeout_add_seconds (2,
340                          on_timeout_cb,
341                          connection);
342 }
343
344 static void
345 on_name_acquired (GDBusConnection *connection,
346                   const gchar     *name,
347                   gpointer         user_data)
348 {
349 }
350
351 static void
352 on_name_lost (GDBusConnection *connection,
353               const gchar     *name,
354               gpointer         user_data)
355 {
356   exit (1);
357 }
358
359 int
360 main (int argc, char *argv[])
361 {
362   guint owner_id;
363   GMainLoop *loop;
364
365   g_type_init ();
366
367   /* We are lazy here - we don't want to manually provide
368    * the introspection data structures - so we just build
369    * them from XML.
370    */
371   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
372   g_assert (introspection_data != NULL);
373
374   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
375                              "org.gtk.GDBus.TestServer",
376                              G_BUS_NAME_OWNER_FLAGS_NONE,
377                              on_bus_acquired,
378                              on_name_acquired,
379                              on_name_lost,
380                              NULL,
381                              NULL);
382
383   loop = g_main_loop_new (NULL, FALSE);
384   g_main_loop_run (loop);
385
386   g_bus_unown_name (owner_id);
387
388   g_dbus_node_info_unref (introspection_data);
389
390   return 0;
391 }