2 #include "test-utils.h"
5 static dbus_bool_t already_quit = FALSE;
6 static dbus_bool_t hello_from_self_reply_received = FALSE;
13 _dbus_loop_quit (loop);
19 die (const char *message)
21 fprintf (stderr, "*** test-service: %s", message);
26 check_hello_from_self_reply (DBusPendingCall *pcall,
30 DBusMessage *echo_message, *echo_reply = NULL;
32 DBusConnection *connection;
36 dbus_error_init (&error);
38 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
39 if (connection == NULL)
41 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
43 dbus_error_free (&error);
48 echo_message = (DBusMessage *)user_data;
50 reply = dbus_pending_call_steal_reply (pcall);
52 type = dbus_message_get_type (reply);
54 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
57 printf ("Reply from HelloFromSelf received\n");
59 if (!dbus_message_get_args (echo_message,
64 echo_reply = dbus_message_new_error (echo_message,
68 if (echo_reply == NULL)
74 echo_reply = dbus_message_new_method_return (echo_message);
75 if (echo_reply == NULL)
78 if (!dbus_message_append_args (echo_reply,
84 if (!dbus_connection_send (connection, echo_reply, NULL))
87 dbus_message_unref (echo_reply);
89 else if (type == DBUS_MESSAGE_TYPE_ERROR)
91 dbus_set_error_from_message (&error, reply);
92 printf ("Error type in reply: %s\n", error.message);
94 if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
96 echo_reply = dbus_message_new_error (echo_reply,
100 if (echo_reply == NULL)
103 if (!dbus_connection_send (connection, echo_reply, NULL))
106 dbus_message_unref (echo_reply);
108 dbus_error_free (&error);
111 _dbus_assert_not_reached ("Unexpected message received\n");
113 hello_from_self_reply_received = TRUE;
115 dbus_message_unref (reply);
116 dbus_message_unref (echo_message);
117 dbus_pending_call_unref (pcall);
118 dbus_connection_unref (connection);
121 static DBusHandlerResult
122 handle_run_hello_from_self (DBusConnection *connection,
123 DBusMessage *message)
126 DBusMessage *reply, *self_message;
127 DBusPendingCall *pcall;
130 _dbus_verbose ("sending reply to Echo method\n");
132 dbus_error_init (&error);
134 if (!dbus_message_get_args (message,
136 DBUS_TYPE_STRING, &s,
139 reply = dbus_message_new_error (message,
146 if (!dbus_connection_send (connection, reply, NULL))
149 dbus_message_unref (reply);
151 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
153 printf ("Sending HelloFromSelf\n");
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",
161 if (self_message == NULL)
164 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
167 dbus_message_ref (message);
168 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
171 printf ("Sent HelloFromSelf\n");
172 return DBUS_HANDLER_RESULT_HANDLED;
175 static DBusHandlerResult
176 handle_echo (DBusConnection *connection,
177 DBusMessage *message)
183 _dbus_verbose ("sending reply to Echo method\n");
185 dbus_error_init (&error);
187 if (!dbus_message_get_args (message,
189 DBUS_TYPE_STRING, &s,
192 reply = dbus_message_new_error (message,
199 if (!dbus_connection_send (connection, reply, NULL))
202 dbus_message_unref (reply);
204 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
207 reply = dbus_message_new_method_return (message);
211 if (!dbus_message_append_args (reply,
212 DBUS_TYPE_STRING, &s,
216 if (!dbus_connection_send (connection, reply, NULL))
219 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
221 dbus_message_unref (reply);
223 return DBUS_HANDLER_RESULT_HANDLED;
226 static DBusHandlerResult
227 handle_delay_echo (DBusConnection *connection,
228 DBusMessage *message)
234 _dbus_verbose ("sleeping for a short time\n");
238 _dbus_verbose ("sending reply to DelayEcho method\n");
240 dbus_error_init (&error);
242 if (!dbus_message_get_args (message,
244 DBUS_TYPE_STRING, &s,
247 reply = dbus_message_new_error (message,
254 if (!dbus_connection_send (connection, reply, NULL))
257 dbus_message_unref (reply);
259 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
262 reply = dbus_message_new_method_return (message);
266 if (!dbus_message_append_args (reply,
267 DBUS_TYPE_STRING, &s,
271 if (!dbus_connection_send (connection, reply, NULL))
274 fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s);
276 dbus_message_unref (reply);
278 return DBUS_HANDLER_RESULT_HANDLED;
283 path_unregistered_func (DBusConnection *connection,
286 /* connection was finalized */
289 static DBusHandlerResult
290 path_message_func (DBusConnection *connection,
291 DBusMessage *message,
294 if (dbus_message_is_method_call (message,
295 "org.freedesktop.TestSuite",
297 return handle_echo (connection, message);
298 else if (dbus_message_is_method_call (message,
299 "org.freedesktop.TestSuite",
301 return handle_delay_echo (connection, message);
302 else if (dbus_message_is_method_call (message,
303 "org.freedesktop.TestSuite",
307 return DBUS_HANDLER_RESULT_HANDLED;
309 else if (dbus_message_is_method_call (message,
310 "org.freedesktop.TestSuite",
313 /* Emit the Foo signal */
317 _dbus_verbose ("emitting signal Foo\n");
319 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
320 "org.freedesktop.TestSuite",
326 if (!dbus_message_append_args (signal,
327 DBUS_TYPE_DOUBLE, &v_DOUBLE,
331 if (!dbus_connection_send (connection, signal, NULL))
334 return DBUS_HANDLER_RESULT_HANDLED;
337 else if (dbus_message_is_method_call (message,
338 "org.freedesktop.TestSuite",
341 return handle_run_hello_from_self (connection, message);
343 else if (dbus_message_is_method_call (message,
344 "org.freedesktop.TestSuite",
348 printf ("Received the HelloFromSelf message\n");
350 reply = dbus_message_new_method_return (message);
354 if (!dbus_connection_send (connection, reply, NULL))
357 return DBUS_HANDLER_RESULT_HANDLED;
360 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
363 static DBusObjectPathVTable
365 path_unregistered_func,
371 static const char* echo_path = "/org/freedesktop/TestSuite" ;
373 static DBusHandlerResult
374 filter_func (DBusConnection *connection,
375 DBusMessage *message,
378 if (dbus_message_is_signal (message,
379 DBUS_INTERFACE_LOCAL,
383 return DBUS_HANDLER_RESULT_HANDLED;
387 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
397 DBusConnection *connection;
399 dbus_error_init (&error);
400 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
401 if (connection == NULL)
403 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
405 dbus_error_free (&error);
409 loop = _dbus_loop_new ();
413 if (!test_connection_setup (loop, connection))
416 if (!dbus_connection_add_filter (connection,
417 filter_func, NULL, NULL))
420 if (!dbus_connection_register_object_path (connection,
428 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
430 if (d != (void*) 0xdeadbeef)
431 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
434 result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
436 if (dbus_error_is_set (&error))
438 fprintf (stderr, "Error %s\n", error.message);
439 _dbus_verbose ("*** Failed to acquire service: %s\n",
441 dbus_error_free (&error);
445 _dbus_verbose ("*** Test service entering main loop\n");
446 _dbus_loop_run (loop);
448 test_connection_shutdown (loop, connection);
450 dbus_connection_remove_filter (connection, filter_func, NULL);
452 dbus_connection_unref (connection);
454 _dbus_loop_unref (loop);
459 _dbus_verbose ("*** Test service exiting\n");