Cancel pending calls when giving up on them
[platform/upstream/at-spi2-core.git] / dbind / dbind.c
1 /*
2  * Copyright 2008-2011 Novell, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <glib.h>
25
26 #include "config.h"
27 #include "dbind/dbind.h"
28 #include "atspi/atspi-gmain.h"
29
30 static int dbind_timeout = -1;
31
32 /*
33  * FIXME: compare types - to ensure they match &
34  *        do dynamic padding of structures etc.
35  */
36
37 /*---------------------------------------------------------------------------*/
38
39 typedef struct _SpiReentrantCallClosure 
40 {
41   DBusMessage *reply;
42 } SpiReentrantCallClosure;
43
44 static void
45 set_reply (DBusPendingCall * pending, void *user_data)
46 {
47   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
48
49   closure->reply = dbus_pending_call_steal_reply (pending);
50   dbus_pending_call_unref (pending);
51 }
52
53 static gint
54 time_elapsed (struct timeval *origin)
55 {
56   struct timeval tv;
57
58   gettimeofday (&tv, NULL);
59   return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
60 }
61
62 DBusMessage *
63 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
64 {
65   DBusPendingCall *pending;
66   SpiReentrantCallClosure *closure;
67   const char *unique_name = dbus_bus_get_unique_name (bus);
68   const char *destination = dbus_message_get_destination (message);
69   struct timeval tv;
70   DBusMessage *ret;
71
72   if (unique_name && destination &&
73       strcmp (destination, unique_name) != 0)
74     return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
75
76   closure = g_new0 (SpiReentrantCallClosure, 1);
77   closure->reply = NULL;
78   atspi_dbus_connection_setup_with_g_main(bus, NULL);
79   if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout)
80       || !pending)
81     {
82       g_free (closure);
83       return NULL;
84     }
85   dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
86
87   closure->reply = NULL;
88   gettimeofday (&tv, NULL);
89   dbus_pending_call_ref (pending);
90   while (!closure->reply)
91     {
92       if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
93         {
94           //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
95           dbus_pending_call_cancel (pending);
96           dbus_pending_call_unref (pending);
97           return NULL;
98         }
99       if (time_elapsed (&tv) > dbind_timeout)
100         {
101           //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
102           dbus_pending_call_cancel (pending);
103           dbus_pending_call_unref (pending);
104           dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
105                                 "timeout from dbind");
106           return NULL;
107         }
108     }
109   
110   ret = closure->reply;
111   dbus_pending_call_unref (pending);
112   return ret;
113 }
114
115 dbus_bool_t
116 dbind_method_call_reentrant_va (DBusConnection *cnx,
117                                 const char     *bus_name,
118                                 const char     *path,
119                                 const char     *interface,
120                                 const char     *method,
121                                 DBusError      *opt_error,
122                                 const char     *arg_types,
123                                 va_list         args)
124 {
125     dbus_bool_t success = FALSE;
126     DBusMessage *msg = NULL, *reply = NULL;
127     DBusMessageIter iter;
128     DBusError *err, real_err;
129     const char *p;
130   va_list args_demarshal;
131
132   dbus_error_init (&real_err);
133
134   va_copy (args_demarshal, args);
135     if (opt_error)
136         err = opt_error;
137     else {
138         err = &real_err;
139     }
140
141     msg = dbus_message_new_method_call (bus_name, path, interface, method);
142     if (!msg)
143         goto out;
144
145     p = arg_types;
146     dbus_message_iter_init_append (msg, &iter);
147     dbind_any_marshal_va (&iter, &p, args);
148
149     reply = dbind_send_and_allow_reentry (cnx, msg, err);
150     if (!reply)
151         goto out;
152
153     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
154     {
155       const char *name = dbus_message_get_error_name (reply);
156       goto out;
157     }
158     /* demarshal */
159     if (p[0] == '=' && p[1] == '>')
160     {
161         DBusMessageIter iter;
162         dbus_message_iter_init (reply, &iter);
163         if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
164         {
165             g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
166                        method, dbus_message_get_signature (reply), p + 2);
167             if (opt_error)
168                 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
169                                 "Call to \"%s\" returned signature %s; expected %s",
170                                 method, dbus_message_get_signature (reply),
171                                 p + 2);
172             goto out;
173         }
174         p = arg_types;
175         dbind_any_demarshal_va (&iter, &p, args_demarshal);
176     }
177
178     success = TRUE;
179 out:
180     if (msg)
181         dbus_message_unref (msg);
182
183     if (reply)
184         dbus_message_unref (reply);
185
186     if (dbus_error_is_set (&real_err))
187         dbus_error_free (&real_err);
188
189     va_end (args_demarshal);
190     return success;
191 }
192
193 /**
194  * dbind_method_call_reentrant:
195  *
196  * @cnx:       A D-Bus Connection used to make the method call.
197  * @bus_name:  The D-Bus bus name of the program where the method call should
198  *             be made.
199  * @path:      The D-Bus object path that should handle the method.
200  * @interface: The D-Bus interface used to scope the method name.
201  * @method:    Method to be invoked.
202  * @opt_error: D-Bus error.
203  * @arg_types: Variable length arguments interleaving D-Bus argument types
204  *             and pointers to argument data.
205  *
206  * Makes a D-Bus method call using the supplied location data, method name and
207  * argument data.This function is re-entrant. It continuously reads from the D-Bus
208  * bus and dispatches messages until a reply has been recieved.
209  **/
210 dbus_bool_t
211 dbind_method_call_reentrant (DBusConnection *cnx,
212                              const char     *bus_name,
213                              const char     *path,
214                              const char     *interface,
215                              const char     *method,
216                              DBusError      *opt_error,
217                              const char     *arg_types,
218                              ...)
219 {
220     dbus_bool_t success = FALSE;
221     va_list args;
222
223     va_start (args, arg_types);
224     success = dbind_method_call_reentrant_va (cnx,
225                                               bus_name,
226                                               path,
227                                               interface,
228                                               method,
229                                               opt_error,
230                                               arg_types,
231                                               args);
232     va_end (args);
233
234     return success;
235 }
236
237 /*---------------------------------------------------------------------------*/
238
239 dbus_bool_t
240 dbind_emit_signal_va (DBusConnection *cnx,
241                       const char     *path,
242                       const char     *interface,
243                       const char     *signal,
244                       DBusError      *opt_error,
245                       const char     *arg_types,
246                       va_list         args)
247 {
248     dbus_bool_t success = FALSE;
249     DBusMessage *msg = NULL;
250     DBusMessageIter iter;
251     DBusError *err, real_err;
252     const char *p;
253
254     dbus_error_init (&real_err);
255
256     if (opt_error)
257         err = opt_error;
258     else {
259         err = &real_err;
260     }
261
262     msg = dbus_message_new_signal (path, interface, signal);
263     if (!msg)
264         goto out;
265
266     p = arg_types;
267     dbus_message_iter_init_append (msg, &iter);
268     dbind_any_marshal_va (&iter, &p, args);
269
270     if (!dbus_connection_send (cnx, msg, NULL))
271        goto out;
272
273     success = TRUE;
274 out:
275
276     if (msg)
277         dbus_message_unref (msg);
278
279     if (dbus_error_is_set (&real_err))
280         dbus_error_free (&real_err);
281
282     return success;
283 }
284
285 /**
286  * dbind_emit_signal:
287  *
288  * @cnx:       A D-Bus Connection used to make the method call.
289  * @path:      The D-Bus object path that this signal is emitted from.
290  * @interface: The D-Bus interface used to scope the method name.
291  * @signal:    Name of signal to emit.
292  * @opt_error: D-Bus error.
293  * @arg_types: Variable length arguments interleaving D-Bus argument types
294  *             and pointers to argument data.
295  *
296  * Emits a D-Bus signal  using the supplied signal name and argument data.
297  **/
298 dbus_bool_t
299 dbind_emit_signal (DBusConnection *cnx,
300                    const char     *path,
301                    const char     *interface,
302                    const char     *signal,
303                    DBusError      *opt_error,
304                    const char     *arg_types,
305                    ...)
306 {
307     dbus_bool_t success = FALSE;
308     va_list args;
309
310     va_start (args, arg_types);
311     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
312     va_end (args);
313
314     return success;
315 }
316 void
317 dbind_set_timeout (int timeout)
318 {
319   dbind_timeout = timeout;
320 }
321
322
323 /*END------------------------------------------------------------------------*/