2003-04-07 01:07:13 +00:00
|
|
|
|
|
|
|
|
#include "test-utils.h"
|
|
|
|
|
|
|
|
|
|
static DBusLoop *loop;
|
2003-04-07 23:28:16 +00:00
|
|
|
static dbus_bool_t already_quit;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
quit (void)
|
|
|
|
|
{
|
|
|
|
|
if (!already_quit)
|
|
|
|
|
{
|
|
|
|
|
_dbus_loop_quit (loop);
|
|
|
|
|
already_quit = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-04-03 05:22:49 +00:00
|
|
|
|
2003-04-03 21:56:22 +00:00
|
|
|
static void
|
|
|
|
|
die (const char *message)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "%s", message);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult
|
2003-04-07 02:01:53 +00:00
|
|
|
handle_echo (DBusConnection *connection,
|
|
|
|
|
DBusMessage *message)
|
2003-04-03 21:56:22 +00:00
|
|
|
{
|
|
|
|
|
DBusError error;
|
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
|
|
dbus_error_init (&error);
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args (message,
|
|
|
|
|
&error,
|
|
|
|
|
DBUS_TYPE_STRING, &s,
|
|
|
|
|
DBUS_TYPE_INVALID))
|
|
|
|
|
{
|
|
|
|
|
reply = dbus_message_new_error_reply (message,
|
|
|
|
|
error.name,
|
|
|
|
|
error.message);
|
|
|
|
|
|
|
|
|
|
if (reply == NULL)
|
|
|
|
|
die ("No memory\n");
|
|
|
|
|
|
|
|
|
|
if (!dbus_connection_send (connection, reply, NULL))
|
|
|
|
|
die ("No memory\n");
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (reply);
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply = dbus_message_new_reply (message);
|
|
|
|
|
if (reply == NULL)
|
|
|
|
|
die ("No memory\n");
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_append_string (reply, s))
|
|
|
|
|
die ("No memory");
|
|
|
|
|
|
|
|
|
|
if (!dbus_connection_send (connection, reply, NULL))
|
|
|
|
|
die ("No memory\n");
|
|
|
|
|
|
|
|
|
|
dbus_free (s);
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (reply);
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-07 02:01:53 +00:00
|
|
|
static DBusHandlerResult
|
|
|
|
|
filter_func (DBusMessageHandler *handler,
|
|
|
|
|
DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
2003-04-07 23:28:16 +00:00
|
|
|
{
|
2003-04-07 02:01:53 +00:00
|
|
|
if (dbus_message_name_is (message, "org.freedesktop.DBus.TestSuiteEcho"))
|
|
|
|
|
return handle_echo (connection, message);
|
2003-04-07 23:28:16 +00:00
|
|
|
else if (dbus_message_name_is (message, "org.freedesktop.DBus.TestSuiteExit") ||
|
|
|
|
|
dbus_message_name_is (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
|
2003-04-07 02:01:53 +00:00
|
|
|
{
|
2003-04-07 23:28:16 +00:00
|
|
|
dbus_connection_disconnect (connection);
|
|
|
|
|
quit ();
|
2003-04-07 02:01:53 +00:00
|
|
|
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-03 05:22:49 +00:00
|
|
|
int
|
|
|
|
|
main (int argc,
|
|
|
|
|
char **argv)
|
|
|
|
|
{
|
|
|
|
|
DBusConnection *connection;
|
|
|
|
|
DBusError error;
|
2003-04-03 21:56:22 +00:00
|
|
|
DBusMessageHandler *handler;
|
2003-04-07 02:01:53 +00:00
|
|
|
const char *to_handle[] = {
|
|
|
|
|
"org.freedesktop.DBus.TestSuiteEcho",
|
2003-04-07 23:28:16 +00:00
|
|
|
"org.freedesktop.DBus.TestSuiteExit",
|
2003-04-07 02:01:53 +00:00
|
|
|
DBUS_MESSAGE_LOCAL_DISCONNECT,
|
|
|
|
|
};
|
2003-04-03 21:56:22 +00:00
|
|
|
int result;
|
|
|
|
|
|
2003-04-03 05:22:49 +00:00
|
|
|
dbus_error_init (&error);
|
|
|
|
|
connection = dbus_bus_get (DBUS_BUS_ACTIVATION, &error);
|
|
|
|
|
if (connection == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Failed to open connection to activating message bus: %s\n",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-07 01:07:13 +00:00
|
|
|
loop = _dbus_loop_new ();
|
|
|
|
|
if (loop == NULL)
|
|
|
|
|
die ("No memory\n");
|
|
|
|
|
|
|
|
|
|
if (!test_connection_setup (loop, connection))
|
|
|
|
|
die ("No memory\n");
|
2003-04-03 21:56:22 +00:00
|
|
|
|
2003-04-07 02:01:53 +00:00
|
|
|
handler = dbus_message_handler_new (filter_func, NULL, NULL);
|
2003-04-03 21:56:22 +00:00
|
|
|
if (handler == NULL)
|
|
|
|
|
die ("No memory");
|
|
|
|
|
|
2003-04-07 02:01:53 +00:00
|
|
|
if (!dbus_connection_register_handler (connection, handler, to_handle,
|
|
|
|
|
_DBUS_N_ELEMENTS (to_handle)))
|
2003-04-03 21:56:22 +00:00
|
|
|
die ("No memory");
|
|
|
|
|
|
|
|
|
|
result = dbus_bus_acquire_service (connection, "org.freedesktop.DBus.TestSuiteEchoService",
|
|
|
|
|
0, &error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Failed to acquire service: %s\n",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free (&error);
|
2003-04-07 23:28:16 +00:00
|
|
|
exit (1);
|
2003-04-03 21:56:22 +00:00
|
|
|
}
|
2003-04-03 05:22:49 +00:00
|
|
|
|
2003-04-07 01:07:13 +00:00
|
|
|
_dbus_loop_run (loop);
|
2003-04-03 05:22:49 +00:00
|
|
|
|
2003-04-07 23:28:16 +00:00
|
|
|
test_connection_shutdown (loop, connection);
|
|
|
|
|
|
2003-04-03 05:22:49 +00:00
|
|
|
dbus_connection_unref (connection);
|
2003-04-07 02:01:53 +00:00
|
|
|
|
2003-04-03 21:56:22 +00:00
|
|
|
dbus_message_handler_unref (handler);
|
2003-04-07 01:07:13 +00:00
|
|
|
|
|
|
|
|
_dbus_loop_unref (loop);
|
|
|
|
|
loop = NULL;
|
2003-04-03 21:56:22 +00:00
|
|
|
|
|
|
|
|
dbus_shutdown ();
|
2003-04-07 23:28:16 +00:00
|
|
|
|
|
|
|
|
printf ("*** Test service exiting\n");
|
2003-04-03 05:22:49 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|