diff options
author | Havoc Pennington <hp@redhat.com> | 2003-04-07 01:07:13 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2003-04-07 01:07:13 +0000 |
commit | 98572905e60cac7ea971aa0830e2a629bfff208d (patch) | |
tree | 2e3ebcfe8670fc403ac4262c96fa714b7e574f83 /test | |
parent | df008ef97deacd5bb00ac335e5d8671798fa09dd (diff) |
2003-04-06 Havoc Pennington <hp@pobox.com>
* test/Makefile.am: remove a lot of stuff that isn't immediately
useful, it's in CVS history if we want it.
* test/test-service.c: use dbus-mainloop instead of that
watch.[hc] crack
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.am | 26 | ||||
-rw-r--r-- | test/bus-test-loop.c | 120 | ||||
-rw-r--r-- | test/bus-test-loop.h | 10 | ||||
-rw-r--r-- | test/bus-test.c | 250 | ||||
-rw-r--r-- | test/debug-thread.c | 161 | ||||
-rw-r--r-- | test/debug-thread.h | 29 | ||||
-rw-r--r-- | test/echo-client.c | 44 | ||||
-rw-r--r-- | test/echo-server.c | 53 | ||||
-rw-r--r-- | test/test-service.c | 20 | ||||
-rw-r--r-- | test/watch.c | 376 | ||||
-rw-r--r-- | test/watch.h | 15 |
11 files changed, 17 insertions, 1087 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 95d87308..69a447df 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,7 +2,7 @@ INCLUDES=-I$(top_srcdir) $(DBUS_TEST_CFLAGS) if DBUS_BUILD_TESTS -TEST_BINARIES=test-service echo-client echo-server unbase64 break-loader spawn-test test-segfault test-exit test-sleep-forever +TEST_BINARIES=test-service unbase64 break-loader spawn-test test-segfault test-exit test-sleep-forever else TEST_BINARIES= endif @@ -15,31 +15,14 @@ endif noinst_PROGRAMS= $(TEST_BINARIES) $(GCOV_BINARIES) -echo_client_SOURCES= \ - echo-client.c \ - watch.c \ - watch.h - -echo_server_SOURCES= \ - echo-server.c \ - watch.c \ - watch.h - test_service_SOURCES= \ test-service.c \ - watch.c \ - watch.h + test-utils.c \ + test-utils.h unbase64_SOURCES= \ unbase64.c -# bus_test_SOURCES = \ -# debug-thread.c \ -# debug-thread.h \ -# bus-test.c \ -# bus-test-loop.c \ -# bus-test-loop.h - break_loader_SOURCES= \ break-loader.c @@ -60,12 +43,9 @@ decode_gcov_SOURCES= \ TEST_LIBS=$(DBUS_TEST_LIBS) $(top_builddir)/dbus/libdbus-convenience.la -echo_client_LDADD=$(TEST_LIBS) -echo_server_LDADD=$(TEST_LIBS) test_service_LDADD=$(TEST_LIBS) unbase64_LDADD=$(TEST_LIBS) break_loader_LDADD= $(TEST_LIBS) -#bus_test_LDADD=$(TEST_LIBS) $(top_builddir)/bus/libdbus-daemon.la spawn_test_LDADD=$(TEST_LIBS) decode_gcov_LDADD=$(TEST_LIBS) diff --git a/test/bus-test-loop.c b/test/bus-test-loop.c deleted file mode 100644 index aa9e4fcc..00000000 --- a/test/bus-test-loop.c +++ /dev/null @@ -1,120 +0,0 @@ -#include "bus-test-loop.h" -#include <sys/time.h> -#include <stdio.h> - -#define DBUS_COMPILATION /* cheat and use DBusList */ -#include <dbus/dbus-list.h> -#undef DBUS_COMPILATION - -typedef struct -{ - long time; - DBusTimeout *timeout; - -} LoopTimeout; - -static DBusList *timeouts; - -static long -get_time (void) -{ - struct timeval r; - long time; - - /* Can't use dbus-sysdeps here since that isn't - * available outside of libdbus. - */ - gettimeofday (&r, NULL); - - time = r.tv_sec * 1000; - time += r.tv_usec / 1000; - - return time; -} - -static void -add_timeout (DBusTimeout *timeout, - void *data) -{ - LoopTimeout *lt; - - lt = dbus_new (LoopTimeout, 1); - lt->time = get_time () + dbus_timeout_get_interval (timeout); - lt->timeout = timeout; - - _dbus_list_append (&timeouts, lt); -} - -static void -remove_timeout (DBusTimeout *timeout, - void *data) -{ - DBusList *link; - - link = _dbus_list_get_first_link (&timeouts); - while (link != NULL) - { - LoopTimeout *lt = link->data; - if (lt->timeout == timeout) - { - _dbus_list_remove (&timeouts, lt); - return; - } - link = _dbus_list_get_next_link (&timeouts, link); - } -} - -static dbus_bool_t running_loop; - - -void -bus_test_loop_quit (void) -{ - running_loop = FALSE; -} - -void -bus_test_loop_run (void) -{ - running_loop = TRUE; - - /* Horribly inefficient main loop */ - while (running_loop) - { - DBusList *link, *list_copy; - long time; - - time = get_time (); - - _dbus_list_copy (&timeouts, &list_copy); - - link = _dbus_list_get_first_link (&list_copy); - while (link != NULL) - { - LoopTimeout *lt = link->data; - if (lt->time <= time) - { - dbus_timeout_handle (lt->timeout); - _dbus_list_remove (&timeouts, lt); - } - link = _dbus_list_get_next_link (&list_copy, link); - } - } -} - - -void -bus_test_loop_hookup_with_server (DBusServer *server) -{ - dbus_server_set_timeout_functions (server, - add_timeout, remove_timeout, - NULL, NULL); -} - -void -bus_test_loop_hookup_with_connection (DBusConnection *connection) -{ - dbus_connection_set_timeout_functions (connection, - add_timeout, remove_timeout, - NULL, NULL); -} diff --git a/test/bus-test-loop.h b/test/bus-test-loop.h deleted file mode 100644 index ac77b29f..00000000 --- a/test/bus-test-loop.h +++ /dev/null @@ -1,10 +0,0 @@ -#include <dbus/dbus.h> - -void bus_test_loop_hookup_with_server (DBusServer *server); -void bus_test_loop_hookup_with_connection (DBusConnection *connection); - -void bus_test_loop_quit (void); -void bus_test_loop_run (void); - - - diff --git a/test/bus-test.c b/test/bus-test.c deleted file mode 100644 index e059e6cb..00000000 --- a/test/bus-test.c +++ /dev/null @@ -1,250 +0,0 @@ -#include <dbus/dbus.h> -#include <stdio.h> -#include <stdlib.h> - -#define DBUS_COMPILATION /* cheat and use DBusList */ -#include <dbus/dbus-list.h> -#include <bus/connection.h> - -#undef DBUS_COMPILATION - -#include "debug-thread.h" -#include "bus-test-loop.h" - - -static DBusHandlerResult -message_handler (DBusMessageHandler *handler, - DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - printf ("client got a message!: %s\n", - dbus_message_get_name (message)); - return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; -} - -static void -new_connection_callback (DBusServer *server, - DBusConnection *new_connection, - void *data) -{ - if (!bus_connection_setup (new_connection)) - return; - - bus_test_loop_hookup_with_connection (new_connection); - - dbus_connection_ref (new_connection); -} - - -static void -die (const char *failure) -{ - fprintf (stderr, "Unit test failed: %s\n", failure); - exit (1); -} - -/* Here are the tests */ -static dbus_bool_t test_hello_succeeding = TRUE; -static char *client1_name, *client2_name; -static int client1_stage = 0, client2_stage = 0; - -#define TEST_HELLO_HANDLE_FAIL(x) do { if (!(x)) { printf ("failed at line %d\n", __LINE__); test_hello_succeeding = FALSE; goto out; } } while (0) - - -static DBusHandlerResult -test_hello_client1_handler (DBusMessageHandler *handler, - DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - char *tmp = NULL; - - if (!test_hello_succeeding) - goto out; - -#if 1 - printf ("In stage %d got message %s\n", - client1_stage, dbus_message_get_name (message)); -#endif - - if (dbus_message_name_is (message, DBUS_MESSAGE_HELLO)) - { - TEST_HELLO_HANDLE_FAIL (client1_stage == 0); - - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &client1_name, - 0)); - - client1_stage += 1; - } - else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED)) - { - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &tmp, - 0)); - -#if 0 - printf ("ServiceCreated is %s\n", tmp); -#endif - - TEST_HELLO_HANDLE_FAIL (client1_stage == 1 || client1_stage == 3); - - if (client1_stage == 1) - TEST_HELLO_HANDLE_FAIL (strcmp (client1_name, tmp) == 0); - else - TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0); - - client1_stage += 1; - - if (client1_stage == 4) - bus_test_loop_quit (); - } - else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_ACQUIRED)) - { - TEST_HELLO_HANDLE_FAIL (client1_stage == 2); - - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &tmp, - 0)); - TEST_HELLO_HANDLE_FAIL (strcmp (client1_name, tmp) == 0); - - client1_stage += 1; - } - else - { - printf ("client1 received unexpected message %s in stage %d\n", - dbus_message_get_name (message), client1_stage); - - test_hello_succeeding = FALSE; - goto out; - } - - out: - if (tmp) - dbus_free (tmp); - - return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; -} - -static DBusHandlerResult -test_hello_client2_handler (DBusMessageHandler *handler, - DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - char *tmp = NULL; - - if (!test_hello_succeeding) - goto out; - - if (dbus_message_name_is (message, DBUS_MESSAGE_HELLO)) - { - TEST_HELLO_HANDLE_FAIL (client2_stage == 0); - - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &client2_name, - 0)); - - client2_stage += 1; - } - else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED)) - { - TEST_HELLO_HANDLE_FAIL (client2_stage == 1); - - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &tmp, - 0)); - TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0); - - client2_stage += 1; - } - else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_ACQUIRED)) - { - TEST_HELLO_HANDLE_FAIL (client2_stage == 2); - - TEST_HELLO_HANDLE_FAIL (dbus_message_get_args (message, NULL, - DBUS_TYPE_STRING, &tmp, - 0)); - TEST_HELLO_HANDLE_FAIL (strcmp (client2_name, tmp) == 0); - - client2_stage += 1; - } - else - { - test_hello_succeeding = FALSE; - goto out; - } - - out: - if (tmp) - dbus_free (tmp); - - return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; -} - -static dbus_bool_t -test_hello_replies (void) -{ - DBusConnection *connection; - DBusMessage *message; - DBusMessageHandler *handler; - DBusResultCode result; - - /* First start client 1 */ - connection = dbus_connection_open ("debug:name=test-server", &result); - bus_test_loop_hookup_with_connection (connection); - message = dbus_message_new (DBUS_SERVICE_DBUS, - DBUS_MESSAGE_HELLO); - handler = dbus_message_handler_new (test_hello_client1_handler, NULL, NULL); - dbus_connection_add_filter (connection, handler); - if (!dbus_connection_send (connection, message, NULL)) - die ("no memory to send message"); - dbus_message_unref (message); - - /* Then start client 2 */ - connection = dbus_connection_open ("debug:name=test-server", &result); - bus_test_loop_hookup_with_connection (connection); - message = dbus_message_new (DBUS_SERVICE_DBUS, - DBUS_MESSAGE_HELLO); - handler = dbus_message_handler_new (test_hello_client2_handler, NULL, NULL); - dbus_connection_add_filter (connection, handler); - if (!dbus_connection_send (connection, message, NULL)) - die ("no memory to send message"); - dbus_message_unref (message); - - bus_test_loop_run (); - - return test_hello_succeeding; -} - -int -main (int argc, - char **argv) -{ - DBusServer *server; - DBusResultCode result; - - debug_threads_init (); - - bus_connection_init (); - - server = dbus_server_listen ("debug:name=test-server", &result); - dbus_server_set_new_connection_function (server, - new_connection_callback, - NULL, NULL); - bus_test_loop_hookup_with_server (server); - if (server == NULL) - { - fprintf (stderr, "Failed to start server: %s\n", - dbus_result_to_string (result)); - return 1; - } - - if (!test_hello_replies ()) - die ("hello with replies"); - - printf ("all tests succeeded\n"); - - return 0; -} diff --git a/test/debug-thread.c b/test/debug-thread.c deleted file mode 100644 index 5ef3ba44..00000000 --- a/test/debug-thread.c +++ /dev/null @@ -1,161 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* dbus-test.c Program to run all tests - * - * Copyright (C) 2002 Red Hat Inc. - * - * Licensed under the Academic Free License version 1.2 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#include <stdlib.h> -#include <dbus/dbus.h> -#include "debug-thread.h" - -#define DBUS_COMPILATION -#include <dbus/dbus-internals.h> -#undef DBUS_COMPILATION - - -static DBusMutex * tmutex_new (void); -static void tmutex_free (DBusMutex *mutex); -static dbus_bool_t tmutex_lock (DBusMutex *mutex); -static dbus_bool_t tmutex_unlock (DBusMutex *mutex); - -static DBusCondVar*tcondvar_new (void); -static void tcondvar_free (DBusCondVar *cond); -static void tcondvar_wait (DBusCondVar *cond, - DBusMutex *mutex); -static dbus_bool_t tcondvar_wait_timeout (DBusCondVar *cond, - DBusMutex *mutex, - int timeout_msec); -static void tcondvar_wake_one (DBusCondVar *cond); -static void tcondvar_wake_all (DBusCondVar *cond); - -static const DBusThreadFunctions functions = -{ - DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK | - DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK | - DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK | - DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK | - DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK | - DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK | - DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK | - DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK | - DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK| - DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK, - tmutex_new, - tmutex_free, - tmutex_lock, - tmutex_unlock, - tcondvar_new, - tcondvar_free, - tcondvar_wait, - tcondvar_wait_timeout, - tcondvar_wake_one, - tcondvar_wake_all -}; - -static DBusMutex * -tmutex_new (void) -{ - int *tmutex; - - tmutex = malloc (sizeof (int*)); - *tmutex = 0; - - return (DBusMutex *)tmutex; -} - -static void -tmutex_free (DBusMutex *mutex) -{ - free (mutex); -} - -static dbus_bool_t -tmutex_lock (DBusMutex *mutex) -{ - int *tmutex = (int *)mutex; - - _dbus_assert (*tmutex == 0); - - *tmutex = 1; - - return TRUE; -} - -static dbus_bool_t -tmutex_unlock (DBusMutex *mutex) -{ - int *tmutex = (int *)mutex; - - _dbus_assert (*tmutex == 1); - - *tmutex = 0; - - return TRUE; -} - -static DBusCondVar* -tcondvar_new (void) -{ - return (DBusCondVar*)0xcafebabe; -} - -static void -tcondvar_free (DBusCondVar *cond) -{ -} - -static void -tcondvar_wait (DBusCondVar *cond, - DBusMutex *mutex) -{ - int *tmutex = (int *)mutex; - - _dbus_assert (*tmutex == 1); -} - -static dbus_bool_t -tcondvar_wait_timeout (DBusCondVar *cond, - DBusMutex *mutex, - int timeout_msec) -{ - int *tmutex = (int *)mutex; - - _dbus_assert (*tmutex == 1); - - return TRUE; -} - - -static void -tcondvar_wake_one (DBusCondVar *cond) -{ -} - -static void -tcondvar_wake_all (DBusCondVar *cond) -{ -} - -void -debug_threads_init (void) -{ - dbus_threads_init (&functions); -} - diff --git a/test/debug-thread.h b/test/debug-thread.h deleted file mode 100644 index 57adff88..00000000 --- a/test/debug-thread.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* dbus-test.c Program to run all tests - * - * Copyright (C) 2002 Red Hat Inc. - * - * Licensed under the Academic Free License version 1.2 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#ifndef DEBUG_THREAD_H -#define DEBUG_THREAD_H - -void debug_threads_init (void); - -#endif diff --git a/test/echo-client.c b/test/echo-client.c deleted file mode 100644 index b6f68fd7..00000000 --- a/test/echo-client.c +++ /dev/null @@ -1,44 +0,0 @@ -#include <dbus/dbus.h> -#include <stdio.h> -#include "watch.h" - -int -main (int argc, - char **argv) -{ - DBusConnection *connection; - DBusError error; - DBusMessage *message; - - if (argc < 2) - { - fprintf (stderr, "Give the server address as an argument\n"); - return 1; - } - - dbus_error_init (&error); - connection = dbus_connection_open (argv[1], &error); - if (connection == NULL) - { - fprintf (stderr, "Failed to open connection to %s: %s\n", - argv[1], error.message); - dbus_error_free (&error); - return 1; - } - - setup_connection (connection); - - /* Send a message to get things going */ - message = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test"); - if (!dbus_connection_send (connection, - message, - NULL)) - fprintf (stderr, "No memory to send reply\n"); - dbus_message_unref (message); - - do_mainloop (); - - dbus_connection_unref (connection); - - return 0; -} diff --git a/test/echo-server.c b/test/echo-server.c deleted file mode 100644 index 9cfcd307..00000000 --- a/test/echo-server.c +++ /dev/null @@ -1,53 +0,0 @@ -#include <dbus/dbus.h> -#include <stdio.h> -#include "watch.h" - -static void -new_connection_callback (DBusServer *server, - DBusConnection *new_connection, - void *data) -{ - printf ("Got new connection\n"); - - dbus_connection_set_max_live_messages_size (new_connection, - 10); - - setup_connection (new_connection); -} - -int -main (int argc, - char **argv) -{ - DBusServer *server; - DBusError error; - - if (argc < 2) - { - fprintf (stderr, "Give the server address as an argument\n"); - return 1; - } - - dbus_error_init (&error); - server = dbus_server_listen (argv[1], &error); - if (server == NULL) - { - fprintf (stderr, "Failed to start server on %s: %s\n", - argv[1], error.message); - dbus_error_free (&error); - return 1; - } - - setup_server (server); - - dbus_server_set_new_connection_function (server, - new_connection_callback, - NULL, NULL); - - do_mainloop (); - - dbus_server_disconnect (server); - dbus_server_unref (server); - - return 0; -} diff --git a/test/test-service.c b/test/test-service.c index 49048f66..9e06b604 100644 --- a/test/test-service.c +++ b/test/test-service.c @@ -1,7 +1,7 @@ -#include <dbus/dbus.h> -#include <stdio.h> -#include <stdlib.h> -#include "watch.h" + +#include "test-utils.h" + +static DBusLoop *loop; static void die (const char *message) @@ -79,7 +79,12 @@ main (int argc, return 1; } - setup_connection (connection); + loop = _dbus_loop_new (); + if (loop == NULL) + die ("No memory\n"); + + if (!test_connection_setup (loop, connection)) + die ("No memory\n"); handler = dbus_message_handler_new (echo_handler, NULL, NULL); if (handler == NULL) @@ -98,11 +103,14 @@ main (int argc, return 1; } - do_mainloop (); + _dbus_loop_run (loop); dbus_connection_unref (connection); dbus_message_handler_unref (handler); + + _dbus_loop_unref (loop); + loop = NULL; dbus_shutdown (); diff --git a/test/watch.c b/test/watch.c deleted file mode 100644 index bdb18306..00000000 --- a/test/watch.c +++ /dev/null @@ -1,376 +0,0 @@ -#include "watch.h" -#include <stdio.h> - -#define DBUS_COMPILATION /* cheat and use DBusList */ -#include <dbus/dbus-list.h> -#undef DBUS_COMPILATION - -#include <sys/types.h> -#include <unistd.h> - -/* Cheesy main loop used in test programs. Any real app would use the - * GLib or Qt or other non-sucky main loops. - */ - -#undef MAX -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) - -static int watch_list_serial = 0; -static DBusList *watches = NULL; -static dbus_bool_t exited = FALSE; -static DBusList *connections = NULL; - -typedef enum -{ - WATCH_CONNECTION, - WATCH_SERVER -} WatchType; - -typedef struct -{ - WatchType type; - void *data; -} WatchData; - -static void -free_watch_data (void *data) -{ - WatchData *wd = data; - - if (wd->type == WATCH_CONNECTION) - dbus_connection_unref (wd->data); - else if (wd->type == WATCH_SERVER) - dbus_server_unref (wd->data); - - dbus_free (wd); -} - -static dbus_bool_t -add_connection_watch (DBusWatch *watch, - DBusConnection *connection) -{ - WatchData *wd; - - if (!_dbus_list_append (&watches, watch)) - return FALSE; - - wd = dbus_new0 (WatchData, 1); - if (wd == NULL) - { - _dbus_list_remove_last (&watches, watch); - return FALSE; - } - wd->type = WATCH_CONNECTION; - wd->data = connection; - - dbus_connection_ref (connection); - - dbus_watch_set_data (watch, wd, free_watch_data); - - watch_list_serial += 1; - -#if 0 - printf ("Added connection %swatch for fd %d\n", - dbus_watch_get_flags (watch) & DBUS_WATCH_WRITABLE ? "write " : "", - dbus_watch_get_fd (watch)); -#endif - - return TRUE; - } - -static void -remove_connection_watch (DBusWatch *watch, - DBusConnection *connection) -{ - if (!_dbus_list_remove (&watches, watch)) - _dbus_assert_not_reached ("removed nonexistent watch"); - - dbus_watch_set_data (watch, NULL, NULL); - - watch_list_serial += 1; - -#if 0 - printf ("Removed connection watch for fd %d\n", - dbus_watch_get_fd (watch)); -#endif -} - -static dbus_bool_t -add_server_watch (DBusWatch *watch, - DBusServer *server) -{ - WatchData *wd; - - if (!_dbus_list_append (&watches, watch)) - return FALSE; - - wd = dbus_new0 (WatchData, 1); - if (wd == NULL) - { - _dbus_list_remove_last (&watches, watch); - return FALSE; - } - - wd->type = WATCH_SERVER; - wd->data = server; - - dbus_server_ref (server); - - dbus_watch_set_data (watch, wd, free_watch_data); - - watch_list_serial += 1; - -#if 0 - printf ("Added server %swatch for fd %d\n", - dbus_watch_get_flags (watch) & DBUS_WATCH_WRITABLE ? "write " : "", - dbus_watch_get_fd (watch)); -#endif - - return TRUE; -} - -static void -remove_server_watch (DBusWatch *watch, - DBusServer *server) -{ - if (!_dbus_list_remove (&watches, watch)) - _dbus_assert_not_reached ("removed nonexistent server watch"); - - dbus_watch_set_data (watch, NULL, NULL); - - watch_list_serial += 1; - -#if 0 - printf ("Removed server watch for fd %d\n", - dbus_watch_get_fd (watch)); -#endif -} - -static int count = 0; - -static void -disconnect (DBusConnection *connection) -{ - fprintf (stderr, "Disconnected\n"); - - _dbus_list_remove (&connections, connection); - dbus_connection_unref (connection); - quit_mainloop (); -} - - -static void -check_messages (void) -{ - DBusList *link; - - link = _dbus_list_get_first_link (&connections); - while (link != NULL) - { - DBusList *next = _dbus_list_get_next_link (&connections, link); - DBusConnection *connection = link->data; - DBusMessage *message; - const char *name; - - while ((message = dbus_connection_pop_message (connection))) - { - DBusMessage *reply; - - name = dbus_message_get_name (message); - if (name && strcmp (name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0) - { - disconnect (connection); - } - else - { - fprintf (stderr, "Received message %d, sending reply\n", count); - - reply = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test"); - if (!dbus_connection_send (connection, - reply, - NULL)) - fprintf (stderr, "No memory to send reply\n"); - dbus_message_unref (reply); - - dbus_message_unref (message); - - count += 1; - if (count > 100) - { - printf ("Saw %d messages, exiting\n", count); - quit_mainloop (); - } - } - } - - link = next; - } -} - -void -do_mainloop (void) -{ - /* Of course with any real app you'd use GMainLoop or - * QSocketNotifier and not have to see all this crap. - */ - while (!exited && watches != NULL) - { - fd_set read_set; - fd_set write_set; - fd_set err_set; - int max_fd; - DBusList *link; - int initial_watch_serial; - - check_messages (); - - if (exited) - break; - - FD_ZERO (&read_set); - FD_ZERO (&write_set); - FD_ZERO (&err_set); - - max_fd = -1; - - link = _dbus_list_get_first_link (&watches); - while (link != NULL) - { - DBusList *next = _dbus_list_get_next_link (&watches, link); - DBusWatch *watch; - - watch = link->data; - - if (dbus_watch_get_enabled (watch)) - { - int fd; - unsigned int flags; - - fd = dbus_watch_get_fd (watch); - flags = dbus_watch_get_flags (watch); - - max_fd = MAX (max_fd, fd); - - if (flags & DBUS_WATCH_READABLE) - FD_SET (fd, &read_set); - - if (flags & DBUS_WATCH_WRITABLE) - FD_SET (fd, &write_set); - - FD_SET (fd, &err_set); - } - - link = next; - } - - select (max_fd + 1, &read_set, &write_set, &err_set, NULL); - - initial_watch_serial = watch_list_serial; - link = _dbus_list_get_first_link (&watches); - while (link != NULL) - { - DBusList *next = _dbus_list_get_next_link (&watches, link); - DBusWatch *watch; - - if (initial_watch_serial != watch_list_serial) - { - /* Watches were added/removed, - * hosing our list; break out of here - */ - /* A more elegant solution might be to ref - * all watches, then check which have fd >= 0 - * as we iterate over them, since removed - * watches have their fd invalidated. - */ - printf ("Aborting watch iteration due to serial increment\n"); - break; - } - - watch = link->data; - - if (dbus_watch_get_enabled (watch)) - { - int fd; - unsigned int flags; - unsigned int condition; - - - fd = dbus_watch_get_fd (watch); - flags = dbus_watch_get_flags (watch); - - condition = 0; - - if ((flags & DBUS_WATCH_READABLE) && - FD_ISSET (fd, &read_set)) - condition |= DBUS_WATCH_READABLE; - - if ((flags & DBUS_WATCH_WRITABLE) && - FD_ISSET (fd, &write_set)) - condition |= DBUS_WATCH_WRITABLE; - - if (FD_ISSET (fd, &err_set)) - condition |= DBUS_WATCH_ERROR; - - if (condition != 0) - { - WatchData *wd; - - wd = dbus_watch_get_data (watch); - - if (wd->type == WATCH_CONNECTION) - { - DBusConnection *connection = wd->data; - - dbus_connection_handle_watch (connection, - watch, - condition); - } - else if (wd->type == WATCH_SERVER) - { - DBusServer *server = wd->data; - - dbus_server_handle_watch (server, - watch, - condition); - } - } - } - - link = next; - } - } -} - -void -quit_mainloop (void) -{ - exited = TRUE; -} - - -void -setup_connection (DBusConnection *connection) -{ - if (!dbus_connection_set_watch_functions (connection, - (DBusAddWatchFunction) add_connection_watch, - (DBusRemoveWatchFunction) remove_connection_watch, - NULL, - connection, - NULL)) - _dbus_assert_not_reached ("not enough memory"); - - dbus_connection_ref (connection); - _dbus_list_append (&connections, connection); -} - -void -setup_server (DBusServer *server) -{ - if (!dbus_server_set_watch_functions (server, - (DBusAddWatchFunction) add_server_watch, - (DBusRemoveWatchFunction) remove_server_watch, - NULL, - server, - NULL)) - _dbus_assert_not_reached ("not enough memory"); -} diff --git a/test/watch.h b/test/watch.h deleted file mode 100644 index a9ad0834..00000000 --- a/test/watch.h +++ /dev/null @@ -1,15 +0,0 @@ -/* Cheesy main loop thingy used by the test programs */ - -#ifndef WATCH_H -#define WATCH_H - -#include <dbus/dbus.h> - -void do_mainloop (void); - -void quit_mainloop (void); - -void setup_connection (DBusConnection *connection); -void setup_server (DBusServer *server); - -#endif |