Clean up platform-specific includes
[platform/upstream/glib.git] / gio / tests / gdbus-example-unix-fd-client.c
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 #include <time.h>
8
9 #include <gio/gio.h>
10 #include <gio/gunixfdlist.h>
11
12 /* see gdbus-example-server.c for the server implementation */
13 static gint
14 get_server_stdout (GDBusConnection  *connection,
15                    const gchar      *name_owner,
16                    GError          **error)
17 {
18   GDBusMessage *method_call_message;
19   GDBusMessage *method_reply_message;
20   GUnixFDList *fd_list;
21   gint fd;
22
23   fd = -1;
24   method_call_message = NULL;
25   method_reply_message = NULL;
26
27   method_call_message = g_dbus_message_new_method_call (name_owner,
28                                                         "/org/gtk/GDBus/TestObject",
29                                                         "org.gtk.GDBus.TestInterface",
30                                                         "GimmeStdout");
31   method_reply_message = g_dbus_connection_send_message_with_reply_sync (connection,
32                                                                          method_call_message,
33                                                                          -1,
34                                                                          NULL, /* out_serial */
35                                                                          NULL, /* cancellable */
36                                                                          error);
37   if (method_reply_message == NULL)
38       goto out;
39
40   if (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_ERROR)
41     {
42       g_dbus_message_to_gerror (method_reply_message, error);
43       goto out;
44     }
45
46   fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
47   fd = g_unix_fd_list_get (fd_list, 0, error);
48
49  out:
50   g_object_unref (method_call_message);
51   g_object_unref (method_reply_message);
52
53   return fd;
54 }
55
56 static void
57 on_name_appeared (GDBusConnection *connection,
58                   const gchar     *name,
59                   const gchar     *name_owner,
60                   gpointer         user_data)
61 {
62   gint fd;
63   GError *error;
64
65   error = NULL;
66   fd = get_server_stdout (connection, name_owner, &error);
67   if (fd == -1)
68     {
69       g_printerr ("Error invoking GimmeStdout(): %s\n",
70                   error->message);
71       g_error_free (error);
72       exit (1);
73     }
74   else
75     {
76       gchar now_buf[256];
77       time_t now;
78       gssize len;
79       gchar *str;
80
81       now = time (NULL);
82       strftime (now_buf,
83                 sizeof now_buf,
84                 "%c",
85                 localtime (&now));
86
87       str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
88                              now_buf,
89                              (gint) getpid ());
90       len = strlen (str);
91       g_warn_if_fail (write (fd, str, len) == len);
92       close (fd);
93
94       g_print ("Wrote the following on server's stdout:\n%s", str);
95
96       g_free (str);
97       exit (0);
98     }
99 }
100
101 static void
102 on_name_vanished (GDBusConnection *connection,
103                   const gchar     *name,
104                   gpointer         user_data)
105 {
106   g_printerr ("Failed to get name owner for %s\n"
107               "Is ./gdbus-example-server running?\n",
108               name);
109   exit (1);
110 }
111
112 int
113 main (int argc, char *argv[])
114 {
115   guint watcher_id;
116   GMainLoop *loop;
117
118   g_type_init ();
119
120   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
121                                  "org.gtk.GDBus.TestServer",
122                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
123                                  on_name_appeared,
124                                  on_name_vanished,
125                                  NULL,
126                                  NULL);
127
128   loop = g_main_loop_new (NULL, FALSE);
129   g_main_loop_run (loop);
130
131   g_bus_unwatch_name (watcher_id);
132   return 0;
133 }