2 #include "test-utils.h"
5 static dbus_bool_t already_quit = FALSE;
6 static dbus_bool_t hello_from_self_reply_recived = 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 recived\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 recived\n");
113 hello_from_self_reply_recived = TRUE;
115 dbus_message_unref (reply);
116 dbus_message_unref (echo_message);
117 dbus_pending_call_unref (pcall);
120 static DBusHandlerResult
121 handle_run_hello_from_self (DBusConnection *connection,
122 DBusMessage *message)
125 DBusMessage *reply, *self_message;
126 DBusPendingCall *pcall;
129 _dbus_verbose ("sending reply to Echo method\n");
131 dbus_error_init (&error);
133 if (!dbus_message_get_args (message,
135 DBUS_TYPE_STRING, &s,
138 reply = dbus_message_new_error (message,
145 if (!dbus_connection_send (connection, reply, NULL))
148 dbus_message_unref (reply);
150 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152 printf ("Sending HelloFromSelf\n");
154 _dbus_verbose ("*** Sending message to self\n");
155 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
156 "/org/freedesktop/TestSuite",
157 "org.freedesktop.TestSuite",
160 if (self_message == NULL)
163 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
166 dbus_message_ref (message);
167 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
170 printf ("Sent HelloFromSelf\n");
171 return DBUS_HANDLER_RESULT_HANDLED;
174 static DBusHandlerResult
175 handle_echo (DBusConnection *connection,
176 DBusMessage *message)
182 _dbus_verbose ("sending reply to Echo method\n");
184 dbus_error_init (&error);
186 if (!dbus_message_get_args (message,
188 DBUS_TYPE_STRING, &s,
191 reply = dbus_message_new_error (message,
198 if (!dbus_connection_send (connection, reply, NULL))
201 dbus_message_unref (reply);
203 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
206 reply = dbus_message_new_method_return (message);
210 if (!dbus_message_append_args (reply,
211 DBUS_TYPE_STRING, &s,
215 if (!dbus_connection_send (connection, reply, NULL))
218 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
220 dbus_message_unref (reply);
222 return DBUS_HANDLER_RESULT_HANDLED;
226 path_unregistered_func (DBusConnection *connection,
229 /* connection was finalized */
232 static DBusHandlerResult
233 path_message_func (DBusConnection *connection,
234 DBusMessage *message,
237 if (dbus_message_is_method_call (message,
238 "org.freedesktop.TestSuite",
240 return handle_echo (connection, message);
241 else if (dbus_message_is_method_call (message,
242 "org.freedesktop.TestSuite",
245 dbus_connection_close (connection);
247 return DBUS_HANDLER_RESULT_HANDLED;
249 else if (dbus_message_is_method_call (message,
250 "org.freedesktop.TestSuite",
253 /* Emit the Foo signal */
257 _dbus_verbose ("emitting signal Foo\n");
259 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
260 "org.freedesktop.TestSuite",
266 if (!dbus_message_append_args (signal,
267 DBUS_TYPE_DOUBLE, &v_DOUBLE,
271 if (!dbus_connection_send (connection, signal, NULL))
274 return DBUS_HANDLER_RESULT_HANDLED;
277 else if (dbus_message_is_method_call (message,
278 "org.freedesktop.TestSuite",
281 return handle_run_hello_from_self (connection, message);
283 else if (dbus_message_is_method_call (message,
284 "org.freedesktop.TestSuite",
288 printf ("Recived the HelloFromSelf message\n");
290 reply = dbus_message_new_method_return (message);
294 if (!dbus_connection_send (connection, reply, NULL))
298 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
301 static DBusObjectPathVTable
303 path_unregistered_func,
309 static const char* echo_path = "/org/freedesktop/TestSuite" ;
311 static DBusHandlerResult
312 filter_func (DBusConnection *connection,
313 DBusMessage *message,
316 if (dbus_message_is_signal (message,
317 DBUS_INTERFACE_LOCAL,
320 dbus_connection_close (connection);
322 return DBUS_HANDLER_RESULT_HANDLED;
326 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
336 DBusConnection *connection;
338 dbus_error_init (&error);
339 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
340 if (connection == NULL)
342 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
344 dbus_error_free (&error);
348 loop = _dbus_loop_new ();
352 if (!test_connection_setup (loop, connection))
355 if (!dbus_connection_add_filter (connection,
356 filter_func, NULL, NULL))
359 if (!dbus_connection_register_object_path (connection,
367 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
369 if (d != (void*) 0xdeadbeef)
370 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
373 result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
375 if (dbus_error_is_set (&error))
377 fprintf (stderr, "Error %s\n", error.message);
378 _dbus_verbose ("*** Failed to acquire service: %s\n",
380 dbus_error_free (&error);
384 _dbus_verbose ("*** Test service entering main loop\n");
385 _dbus_loop_run (loop);
387 test_connection_shutdown (loop, connection);
389 dbus_connection_remove_filter (connection, filter_func, NULL);
391 dbus_connection_unref (connection);
393 _dbus_loop_unref (loop);
398 _dbus_verbose ("*** Test service exiting\n");