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