Git init
[external/dbus-glib.git] / test / test-service.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <dbus/dbus.h>
5
6 static dbus_bool_t already_quit = FALSE;
7 static dbus_bool_t hello_from_self_reply_recived = FALSE;
8
9 static void
10 quit (void)
11 {
12   if (!already_quit)
13     already_quit = TRUE;
14 }
15
16 static void
17 die (const char *message)
18 {
19   fprintf (stderr, "*** test-service: %s", message);
20   exit (1);
21 }
22
23 static void
24 check_hello_from_self_reply (DBusPendingCall *pcall, 
25                              void *user_data)
26 {
27   DBusMessage *reply;
28   DBusMessage *echo_message, *echo_reply;
29   DBusError error;
30   DBusConnection *connection;
31   
32   int type;
33   
34   dbus_error_init (&error);
35  
36   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
37   if (connection == NULL)
38     {
39       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
40                error.message);
41       dbus_error_free (&error);
42       die("no memory");
43     }
44
45   
46   echo_message = (DBusMessage *)user_data;
47     
48   reply = dbus_pending_call_steal_reply (pcall);
49     
50   type = dbus_message_get_type (reply);
51     
52   if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
53     {
54       const char *s;
55       printf ("Reply from HelloFromSelf recived\n");
56      
57       if (!dbus_message_get_args (echo_message,
58                               &error,
59                               DBUS_TYPE_STRING, &s,
60                               DBUS_TYPE_INVALID))
61         {
62             echo_reply = dbus_message_new_error (echo_message,
63                                       error.name,
64                                       error.message);
65
66             if (echo_reply == NULL)
67               die ("No memory\n");
68
69         } 
70       else
71         {  
72           echo_reply = dbus_message_new_method_return (echo_message);
73           if (echo_reply == NULL)
74             die ("No memory\n");
75   
76           if (!dbus_message_append_args (echo_reply,
77                                  DBUS_TYPE_STRING, &s,
78                                  DBUS_TYPE_INVALID))
79             die ("No memory");
80         }
81         
82       if (!dbus_connection_send (connection, echo_reply, NULL))
83         die ("No memory\n");
84       
85       dbus_message_unref (echo_reply);
86     }
87   else if (type == DBUS_MESSAGE_TYPE_ERROR)
88     {
89       dbus_set_error_from_message (&error, reply);
90       printf ("Error type in reply: %s\n", error.message);
91
92       if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
93         {
94             echo_reply = dbus_message_new_error (echo_message,
95                                       error.name,
96                                       error.message);
97
98             if (echo_reply == NULL)
99               die ("No memory\n");
100
101             if (!dbus_connection_send (connection, echo_reply, NULL))
102               die ("No memory\n");
103
104             dbus_message_unref (echo_reply);
105         }
106       dbus_error_free (&error);
107     }
108   
109   hello_from_self_reply_recived = TRUE;
110   
111   dbus_message_unref (reply);
112   dbus_message_unref (echo_message);
113   dbus_pending_call_unref (pcall);
114 }
115
116 static DBusHandlerResult
117 handle_run_hello_from_self (DBusConnection     *connection,
118                                                DBusMessage        *message)
119 {
120   DBusError error;
121   DBusMessage *reply, *self_message;
122   DBusPendingCall *pcall;
123   char *s;
124
125   dbus_error_init (&error);
126   
127   if (!dbus_message_get_args (message,
128                               &error,
129                               DBUS_TYPE_STRING, &s,
130                               DBUS_TYPE_INVALID))
131     {
132       reply = dbus_message_new_error (message,
133                                       error.name,
134                                       error.message);
135
136       if (reply == NULL)
137         die ("No memory\n");
138
139       if (!dbus_connection_send (connection, reply, NULL))
140         die ("No memory\n");
141
142       dbus_message_unref (reply);
143
144       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
145     }
146     printf ("Sending HelloFromSelf\n");
147
148  self_message = dbus_message_new_method_call ("org.freedesktop.DBus.GLib.TestEchoService",
149                                           "/org/freedesktop/DBus/GLib/TestSuite",
150                                           "org.freedesktop.DBus.GLib.TestSuite",
151                                           "HelloFromSelf");
152   
153   if (self_message == NULL)
154     die ("No memory");
155   
156   if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
157     die("No memory");
158   
159   dbus_message_ref (message);
160   if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
161     die("No memory");
162     
163   printf ("Sent HelloFromSelf\n");
164   return DBUS_HANDLER_RESULT_HANDLED;
165 }
166
167 static DBusHandlerResult
168 handle_echo (DBusConnection     *connection,
169              DBusMessage        *message)
170 {
171   DBusError error;
172   DBusMessage *reply;
173   char *s;
174
175   dbus_error_init (&error);
176   
177   if (!dbus_message_get_args (message,
178                               &error,
179                               DBUS_TYPE_STRING, &s,
180                               DBUS_TYPE_INVALID))
181     {
182       reply = dbus_message_new_error (message,
183                                       error.name,
184                                       error.message);
185
186       if (reply == NULL)
187         die ("No memory\n");
188
189       if (!dbus_connection_send (connection, reply, NULL))
190         die ("No memory\n");
191
192       dbus_message_unref (reply);
193
194       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
195     }
196
197   reply = dbus_message_new_method_return (message);
198   if (reply == NULL)
199     die ("No memory\n");
200   
201   if (!dbus_message_append_args (reply,
202                                  DBUS_TYPE_STRING, &s,
203                                  DBUS_TYPE_INVALID))
204     die ("No memory");
205   
206   if (!dbus_connection_send (connection, reply, NULL))
207     die ("No memory\n");
208
209   fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
210   
211   dbus_message_unref (reply);
212     
213   return DBUS_HANDLER_RESULT_HANDLED;
214 }
215
216 static void
217 path_unregistered_func (DBusConnection  *connection,
218                         void            *user_data)
219 {
220   /* connection was finalized */
221 }
222
223 static DBusHandlerResult
224 path_message_func (DBusConnection  *connection,
225                    DBusMessage     *message,
226                    void            *user_data)
227 {
228   if (dbus_message_is_method_call (message,
229                                    "org.freedesktop.DBus.GLib.TestSuite",
230                                    "Echo"))
231     return handle_echo (connection, message);
232   else if (dbus_message_is_method_call (message,
233                                         "org.freedesktop.DBus.GLib.TestSuite",
234                                         "Exit"))
235     {
236       dbus_connection_close (connection);
237       quit ();
238       return DBUS_HANDLER_RESULT_HANDLED;
239     }
240   else if (dbus_message_is_method_call (message,
241                                         "org.freedesktop.DBus.GLib.TestSuite",
242                                         "EmitFoo"))
243     {
244       /* Emit the Foo signal */
245       DBusMessage *signal;
246       double v_DOUBLE;
247
248       signal = dbus_message_new_signal ("/org/freedesktop/DBus/GLib/TestSuite",
249                                         "org.freedesktop.DBus.GLib.TestSuite",
250                                         "Foo");
251       if (signal == NULL)
252         die ("No memory\n");
253
254       v_DOUBLE = 42.6;
255       if (!dbus_message_append_args (signal,
256                                      DBUS_TYPE_DOUBLE, &v_DOUBLE,
257                                      DBUS_TYPE_INVALID))
258         die ("No memory");
259   
260       if (!dbus_connection_send (connection, signal, NULL))
261         die ("No memory\n");
262       
263       return DBUS_HANDLER_RESULT_HANDLED;
264     }
265     
266   else if (dbus_message_is_method_call (message,
267                                    "org.freedesktop.DBus.GLib.TestSuite",
268                                    "RunHelloFromSelf"))
269     {
270       return handle_run_hello_from_self (connection, message);
271     }
272   else if (dbus_message_is_method_call (message,
273                                         "org.freedesktop.DBus.GLib.TestSuite",
274                                         "HelloFromSelf"))
275     {
276         DBusMessage *reply;
277         printf ("Recived the HelloFromSelf message\n");
278         
279         reply = dbus_message_new_method_return (message);
280         if (reply == NULL)
281           die ("No memory");
282         
283         if (!dbus_connection_send (connection, reply, NULL))
284           die ("No memory");
285     }
286   
287     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288 }
289
290 static DBusObjectPathVTable
291 echo_vtable = {
292   path_unregistered_func,
293   path_message_func,
294   NULL,
295 };
296
297
298 static const char* echo_path = "/org/freedesktop/DBus/GLib/TestSuite" ;
299
300 static DBusHandlerResult
301 filter_func (DBusConnection     *connection,
302              DBusMessage        *message,
303              void               *user_data)
304 {
305   if (dbus_message_is_signal (message,
306                               DBUS_INTERFACE_LOCAL,
307                               "Disconnected"))
308     {
309       dbus_connection_close (connection);
310       quit ();
311       return DBUS_HANDLER_RESULT_HANDLED;
312     }
313   else
314     {
315       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
316     }
317 }
318
319 int
320 main (int    argc,
321       char **argv)
322 {
323   DBusError error;
324   int result;
325   DBusConnection *connection;
326   
327   dbus_error_init (&error);
328   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
329   if (connection == NULL)
330     {
331       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
332                error.message);
333       dbus_error_free (&error);
334       return 1;
335     }
336
337   if (!dbus_connection_add_filter (connection,
338                                    filter_func, NULL, NULL))
339     die ("No memory");
340
341   if (!dbus_connection_register_object_path (connection,
342                                              echo_path,
343                                              &echo_vtable,
344                                              (void*) 0xdeadbeef))
345     die ("No memory");
346
347   {
348     void *d;
349     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
350       die ("No memory");
351     if (d != (void*) 0xdeadbeef)
352       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
353   }
354   
355   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.GLib.TestEchoService",
356                                   0, &error);
357   if (dbus_error_is_set (&error))
358     {
359       fprintf (stderr, "Error %s\n", error.message);
360       dbus_error_free (&error);
361       exit (1);
362     }
363   
364   while (dbus_connection_read_write_dispatch (connection, -1) && !already_quit)
365     ;  
366
367   dbus_connection_remove_filter (connection, filter_func, NULL);
368   
369   dbus_connection_unref (connection);
370
371   dbus_shutdown ();
372
373   return 0;
374 }