GDBus: Add new symbols to gio.symbols
[platform/upstream/glib.git] / gio / tests / gdbus-example-unix-fd-client.c
1 /*
2  * Copyright © 2010 Red Hat, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * See the included COPYING file for more information.
10  *
11  * Author: David Zeuthen <davidz@redhat.com>
12  */
13
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include <time.h>
21
22 #include <gio/gio.h>
23
24 /* see gdbus-example-server.c for the server implementation */
25 static gint
26 get_server_stdout (GDBusConnection  *connection,
27                    const gchar      *name_owner,
28                    GError          **error)
29 {
30   GDBusMessage *method_call_message;
31   GDBusMessage *method_reply_message;
32   GUnixFDList *fd_list;
33   gint fd;
34
35   fd = -1;
36   method_call_message = NULL;
37   method_reply_message = NULL;
38
39   method_call_message = g_dbus_message_new_method_call (name_owner,
40                                                         "/org/gtk/GDBus/TestObject",
41                                                         "org.gtk.GDBus.TestInterface",
42                                                         "GimmeStdout");
43   method_reply_message = g_dbus_connection_send_message_with_reply_sync (connection,
44                                                                          method_call_message,
45                                                                          -1,
46                                                                          NULL, /* out_serial */
47                                                                          NULL, /* cancellable */
48                                                                          error);
49   if (method_reply_message == NULL)
50       goto out;
51
52   if (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_ERROR)
53     {
54       g_dbus_message_to_gerror (method_reply_message, error);
55       goto out;
56     }
57
58   fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
59   fd = g_unix_fd_list_get (fd_list, 0, error);
60
61  out:
62   g_object_unref (method_call_message);
63   g_object_unref (method_reply_message);
64
65   return fd;
66 }
67
68 static void
69 on_name_appeared (GDBusConnection *connection,
70                   const gchar     *name,
71                   const gchar     *name_owner,
72                   gpointer         user_data)
73 {
74   gint fd;
75   GError *error;
76
77   error = NULL;
78   fd = get_server_stdout (connection, name_owner, &error);
79   if (fd == -1)
80     {
81       g_printerr ("Error invoking GimmeStdout(): %s\n",
82                   error->message);
83       g_error_free (error);
84       exit (1);
85     }
86   else
87     {
88       gchar now_buf[256];
89       time_t now;
90       gssize len;
91       gchar *str;
92
93       now = time (NULL);
94       strftime (now_buf,
95                 sizeof now_buf,
96                 "%c",
97                 localtime (&now));
98
99       str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
100                              now_buf,
101                              (gint) getpid ());
102       len = strlen (str);
103       g_warn_if_fail (write (fd, str, len) == len);
104       close (fd);
105
106       g_print ("Wrote the following on server's stdout:\n%s", str);
107
108       g_free (str);
109       exit (0);
110     }
111 }
112
113 static void
114 on_name_vanished (GDBusConnection *connection,
115                   const gchar     *name,
116                   gpointer         user_data)
117 {
118   g_printerr ("Failed to get name owner for %s\n"
119               "Is ./gdbus-example-server running?\n",
120               name);
121   exit (1);
122 }
123
124 int
125 main (int argc, char *argv[])
126 {
127   guint watcher_id;
128   GMainLoop *loop;
129
130   g_type_init ();
131
132   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
133                                  "org.gtk.GDBus.TestServer",
134                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
135                                  on_name_appeared,
136                                  on_name_vanished,
137                                  NULL,
138                                  NULL);
139
140   loop = g_main_loop_new (NULL, FALSE);
141   g_main_loop_run (loop);
142
143   g_bus_unwatch_name (watcher_id);
144   return 0;
145 }