Merge branch 'gdbus-merge'
[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}as)",
247                                                         interface_name,
248                                                         builder,
249                                                         NULL),
250                                          &local_error);
251           g_assert_no_error (local_error);
252         }
253     }
254   else if (g_strcmp0 (property_name, "ReadingAlwaysThrowsError") == 0)
255     {
256       /* do nothing - they can't read it after all! */
257     }
258   else if (g_strcmp0 (property_name, "WritingAlwaysThrowsError") == 0)
259     {
260       g_set_error (error,
261                    G_IO_ERROR,
262                    G_IO_ERROR_FAILED,
263                    "Hello AGAIN %s. I thought I said writing this property "
264                    "always results in an error. kthxbye",
265                    sender);
266     }
267
268   return *error == NULL;
269 }
270
271
272 /* for now */
273 static const GDBusInterfaceVTable interface_vtable =
274 {
275   handle_method_call,
276   handle_get_property,
277   handle_set_property
278 };
279
280 /* ---------------------------------------------------------------------------------------------------- */
281
282 static gboolean
283 on_timeout_cb (gpointer user_data)
284 {
285   GDBusConnection *connection = G_DBUS_CONNECTION (user_data);
286   GVariantBuilder *builder;
287   GVariantBuilder *invalidated_builder;
288   GError *error;
289
290   swap_a_and_b = !swap_a_and_b;
291
292   error = NULL;
293   builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
294   invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
295   g_variant_builder_add (builder,
296                          "{sv}",
297                          "Foo",
298                          g_variant_new_string (swap_a_and_b ? "Tock" : "Tick"));
299   g_variant_builder_add (builder,
300                          "{sv}",
301                          "Bar",
302                          g_variant_new_string (swap_a_and_b ? "Tick" : "Tock"));
303   g_dbus_connection_emit_signal (connection,
304                                  NULL,
305                                  "/org/gtk/GDBus/TestObject",
306                                  "org.freedesktop.DBus.Properties",
307                                  "PropertiesChanged",
308                                  g_variant_new ("(sa{sv}as)",
309                                                 "org.gtk.GDBus.TestInterface",
310                                                 builder,
311                                                 invalidated_builder),
312                                  &error);
313   g_assert_no_error (error);
314
315
316   return TRUE;
317 }
318
319 /* ---------------------------------------------------------------------------------------------------- */
320
321 static void
322 on_bus_acquired (GDBusConnection *connection,
323                  const gchar     *name,
324                  gpointer         user_data)
325 {
326   guint registration_id;
327
328   registration_id = g_dbus_connection_register_object (connection,
329                                                        "/org/gtk/GDBus/TestObject",
330                                                        introspection_data->interfaces[0],
331                                                        &interface_vtable,
332                                                        NULL,  /* user_data */
333                                                        NULL,  /* user_data_free_func */
334                                                        NULL); /* GError** */
335   g_assert (registration_id > 0);
336
337   /* swap value of properties Foo and Bar every two seconds */
338   g_timeout_add_seconds (2,
339                          on_timeout_cb,
340                          connection);
341 }
342
343 static void
344 on_name_acquired (GDBusConnection *connection,
345                   const gchar     *name,
346                   gpointer         user_data)
347 {
348 }
349
350 static void
351 on_name_lost (GDBusConnection *connection,
352               const gchar     *name,
353               gpointer         user_data)
354 {
355   exit (1);
356 }
357
358 int
359 main (int argc, char *argv[])
360 {
361   guint owner_id;
362   GMainLoop *loop;
363
364   g_type_init ();
365
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.
369    */
370   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
371   g_assert (introspection_data != NULL);
372
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);
381
382   loop = g_main_loop_new (NULL, FALSE);
383   g_main_loop_run (loop);
384
385   g_bus_unown_name (owner_id);
386
387   g_dbus_node_info_unref (introspection_data);
388
389   return 0;
390 }