* doc/TODO:
[platform/upstream/dbus.git] / test / test-service.c
1
2 #include "test-utils.h"
3
4 static DBusLoop *loop;
5 static dbus_bool_t already_quit = FALSE;
6 static dbus_bool_t hello_from_self_reply_recived = FALSE;
7
8 static void
9 quit (void)
10 {
11   if (!already_quit)
12     {
13       _dbus_loop_quit (loop);
14       already_quit = TRUE;
15     }
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 = NULL;
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_reply,
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   else
111      _dbus_assert_not_reached ("Unexpected message recived\n");
112
113   hello_from_self_reply_recived = TRUE;
114   
115   dbus_message_unref (reply);
116   dbus_message_unref (echo_message);
117   dbus_pending_call_unref (pcall);
118   dbus_connection_unref (connection);
119 }
120
121 static DBusHandlerResult
122 handle_run_hello_from_self (DBusConnection     *connection,
123                                                DBusMessage        *message)
124 {
125   DBusError error;
126   DBusMessage *reply, *self_message;
127   DBusPendingCall *pcall;
128   char *s;
129
130   _dbus_verbose ("sending reply to Echo method\n");
131   
132   dbus_error_init (&error);
133   
134   if (!dbus_message_get_args (message,
135                               &error,
136                               DBUS_TYPE_STRING, &s,
137                               DBUS_TYPE_INVALID))
138     {
139       reply = dbus_message_new_error (message,
140                                       error.name,
141                                       error.message);
142
143       if (reply == NULL)
144         die ("No memory\n");
145
146       if (!dbus_connection_send (connection, reply, NULL))
147         die ("No memory\n");
148
149       dbus_message_unref (reply);
150
151       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152     }
153     printf ("Sending HelloFromSelf\n");
154
155  _dbus_verbose ("*** Sending message to self\n");
156  self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
157                                           "/org/freedesktop/TestSuite",
158                                           "org.freedesktop.TestSuite",
159                                           "HelloFromSelf");
160   
161   if (self_message == NULL)
162     die ("No memory");
163   
164   if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
165     die("No memory");
166   
167   dbus_message_ref (message);
168   if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
169     die("No memory");
170     
171   printf ("Sent HelloFromSelf\n");
172   return DBUS_HANDLER_RESULT_HANDLED;
173 }
174
175 static DBusHandlerResult
176 handle_echo (DBusConnection     *connection,
177              DBusMessage        *message)
178 {
179   DBusError error;
180   DBusMessage *reply;
181   char *s;
182
183   _dbus_verbose ("sending reply to Echo method\n");
184   
185   dbus_error_init (&error);
186   
187   if (!dbus_message_get_args (message,
188                               &error,
189                               DBUS_TYPE_STRING, &s,
190                               DBUS_TYPE_INVALID))
191     {
192       reply = dbus_message_new_error (message,
193                                       error.name,
194                                       error.message);
195
196       if (reply == NULL)
197         die ("No memory\n");
198
199       if (!dbus_connection_send (connection, reply, NULL))
200         die ("No memory\n");
201
202       dbus_message_unref (reply);
203
204       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205     }
206
207   reply = dbus_message_new_method_return (message);
208   if (reply == NULL)
209     die ("No memory\n");
210   
211   if (!dbus_message_append_args (reply,
212                                  DBUS_TYPE_STRING, &s,
213                                  DBUS_TYPE_INVALID))
214     die ("No memory");
215   
216   if (!dbus_connection_send (connection, reply, NULL))
217     die ("No memory\n");
218
219   fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
220   
221   dbus_message_unref (reply);
222     
223   return DBUS_HANDLER_RESULT_HANDLED;
224 }
225
226 static void
227 path_unregistered_func (DBusConnection  *connection,
228                         void            *user_data)
229 {
230   /* connection was finalized */
231 }
232
233 static DBusHandlerResult
234 path_message_func (DBusConnection  *connection,
235                    DBusMessage     *message,
236                    void            *user_data)
237 {
238   if (dbus_message_is_method_call (message,
239                                    "org.freedesktop.TestSuite",
240                                    "Echo"))
241     return handle_echo (connection, message);
242   else if (dbus_message_is_method_call (message,
243                                         "org.freedesktop.TestSuite",
244                                         "Exit"))
245     {
246       dbus_connection_unref (connection);
247       quit ();
248       return DBUS_HANDLER_RESULT_HANDLED;
249     }
250   else if (dbus_message_is_method_call (message,
251                                         "org.freedesktop.TestSuite",
252                                         "EmitFoo"))
253     {
254       /* Emit the Foo signal */
255       DBusMessage *signal;
256       double v_DOUBLE;
257
258       _dbus_verbose ("emitting signal Foo\n");
259       
260       signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
261                                         "org.freedesktop.TestSuite",
262                                         "Foo");
263       if (signal == NULL)
264         die ("No memory\n");
265
266       v_DOUBLE = 42.6;
267       if (!dbus_message_append_args (signal,
268                                      DBUS_TYPE_DOUBLE, &v_DOUBLE,
269                                      DBUS_TYPE_INVALID))
270         die ("No memory");
271   
272       if (!dbus_connection_send (connection, signal, NULL))
273         die ("No memory\n");
274       
275       return DBUS_HANDLER_RESULT_HANDLED;
276     }
277     
278   else if (dbus_message_is_method_call (message,
279                                    "org.freedesktop.TestSuite",
280                                    "RunHelloFromSelf"))
281     {
282       return handle_run_hello_from_self (connection, message);
283     }
284   else if (dbus_message_is_method_call (message,
285                                         "org.freedesktop.TestSuite",
286                                         "HelloFromSelf"))
287     {
288         DBusMessage *reply;
289         printf ("Recived the HelloFromSelf message\n");
290         
291         reply = dbus_message_new_method_return (message);
292         if (reply == NULL)
293           die ("No memory");
294         
295         if (!dbus_connection_send (connection, reply, NULL))
296           die ("No memory");
297
298         return DBUS_HANDLER_RESULT_HANDLED;
299     }
300   else
301     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
302 }
303
304 static DBusObjectPathVTable
305 echo_vtable = {
306   path_unregistered_func,
307   path_message_func,
308   NULL,
309 };
310
311
312 static const char* echo_path = "/org/freedesktop/TestSuite" ;
313
314 static DBusHandlerResult
315 filter_func (DBusConnection     *connection,
316              DBusMessage        *message,
317              void               *user_data)
318 {
319   if (dbus_message_is_signal (message,
320                               DBUS_INTERFACE_LOCAL,
321                               "Disconnected"))
322     {
323       quit ();
324       return DBUS_HANDLER_RESULT_HANDLED;
325     }
326   else
327     {
328       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
329     }
330 }
331
332 int
333 main (int    argc,
334       char **argv)
335 {
336   DBusError error;
337   int result;
338   DBusConnection *connection;
339   
340   dbus_error_init (&error);
341   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
342   if (connection == NULL)
343     {
344       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
345                error.message);
346       dbus_error_free (&error);
347       return 1;
348     }
349
350   loop = _dbus_loop_new ();
351   if (loop == NULL)
352     die ("No memory\n");
353   
354   if (!test_connection_setup (loop, connection))
355     die ("No memory\n");
356
357   if (!dbus_connection_add_filter (connection,
358                                    filter_func, NULL, NULL))
359     die ("No memory");
360
361   if (!dbus_connection_register_object_path (connection,
362                                              echo_path,
363                                              &echo_vtable,
364                                              (void*) 0xdeadbeef))
365     die ("No memory");
366
367   {
368     void *d;
369     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
370       die ("No memory");
371     if (d != (void*) 0xdeadbeef)
372       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
373   }
374   
375   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
376                                   0, &error);
377   if (dbus_error_is_set (&error))
378     {
379       fprintf (stderr, "Error %s\n", error.message);
380       _dbus_verbose ("*** Failed to acquire service: %s\n",
381                      error.message);
382       dbus_error_free (&error);
383       exit (1);
384     }
385   
386   _dbus_verbose ("*** Test service entering main loop\n");
387   _dbus_loop_run (loop);
388   
389   test_connection_shutdown (loop, connection);
390
391   dbus_connection_remove_filter (connection, filter_func, NULL);
392   
393   dbus_connection_unref (connection);
394
395   _dbus_loop_unref (loop);
396   loop = NULL;
397   
398   dbus_shutdown ();
399
400   _dbus_verbose ("*** Test service exiting\n");
401   
402   return 0;
403 }