summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@codefactory.se>2003-02-16 21:50:32 +0000
committerAnders Carlsson <andersca@codefactory.se>2003-02-16 21:50:32 +0000
commit6cf6f82faae928a9142d4aa465b5add381960e29 (patch)
tree4275653da460d9f96639e7c246f82dd1c6e233a9
parentb2fce4c69b9fd75c54bb370e043ea166b83083f6 (diff)
2003-02-16 Anders Carlsson <andersca@codefactory.se>
* bus/dispatch.c: (send_one_message): Only send broadcast messages to registered connections. * dbus/dbus-message.c: (dbus_message_name_is): * dbus/dbus-message.h: New convenience function. * dbus/dbus-transport-debug.c: (do_reading): Only dispatch one message per run. * test/Makefile.am: * test/bus-test.c: (new_connection_callback), (die), (test_hello_client1_handler), (test_hello_client2_handler), (test_hello_replies), (main): * test/bus-test-loop.[ch]: Add these.
-rw-r--r--ChangeLog20
-rw-r--r--bus/dispatch.c4
-rw-r--r--dbus/dbus-message.c11
-rw-r--r--dbus/dbus-message.h3
-rw-r--r--dbus/dbus-transport-debug.c15
-rw-r--r--test/Makefile.am4
-rw-r--r--test/bus-test-loop.c120
-rw-r--r--test/bus-test-loop.h10
-rw-r--r--test/bus-test.c283
9 files changed, 350 insertions, 120 deletions
diff --git a/ChangeLog b/ChangeLog
index 655afb0c..bffbefea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2003-02-16 Anders Carlsson <andersca@codefactory.se>
+
+ * bus/dispatch.c: (send_one_message):
+ Only send broadcast messages to registered connections.
+
+ * dbus/dbus-message.c: (dbus_message_name_is):
+ * dbus/dbus-message.h:
+ New convenience function.
+
+ * dbus/dbus-transport-debug.c: (do_reading):
+ Only dispatch one message per run.
+
+ * test/Makefile.am:
+ * test/bus-test.c: (new_connection_callback), (die),
+ (test_hello_client1_handler), (test_hello_client2_handler),
+ (test_hello_replies), (main):
+
+ * test/bus-test-loop.[ch]:
+ Add these.
+
2003-02-16 Havoc Pennington <hp@pobox.com>
* dbus/dbus-connection.c (dbus_connection_dispatch_message): fix
diff --git a/bus/dispatch.c b/bus/dispatch.c
index 2ce26ae1..76e10a9f 100644
--- a/bus/dispatch.c
+++ b/bus/dispatch.c
@@ -33,6 +33,10 @@ static int message_handler_slot;
static void
send_one_message (DBusConnection *connection, void *data)
{
+ /* Only send messages to registered connections */
+ if (bus_connection_get_name (connection) == NULL)
+ return;
+
BUS_HANDLE_OOM (dbus_connection_send_message (connection, data, NULL, NULL));
}
diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c
index fced9c72..893e45f1 100644
--- a/dbus/dbus-message.c
+++ b/dbus/dbus-message.c
@@ -1735,6 +1735,17 @@ dbus_message_get_sender (DBusMessage *message)
return get_string_field (message, FIELD_SENDER, NULL);
}
+dbus_bool_t
+dbus_message_name_is (DBusMessage *message,
+ const char *name)
+{
+ if (dbus_message_get_name (message) &&
+ strcmp (dbus_message_get_name (message), name) == 0)
+ return TRUE;
+ else
+ return FALSE;
+}
+
/** @} */
/**
diff --git a/dbus/dbus-message.h b/dbus/dbus-message.h
index 2038978b..a4b93843 100644
--- a/dbus/dbus-message.h
+++ b/dbus/dbus-message.h
@@ -56,6 +56,9 @@ void dbus_message_set_is_error (DBusMessage *message,
dbus_bool_t is_error_reply);
dbus_bool_t dbus_message_get_is_error (DBusMessage *message);
+dbus_bool_t dbus_message_name_is (DBusMessage *message,
+ const char *name);
+
dbus_bool_t dbus_message_append_args (DBusMessage *message,
int first_arg_type,
...);
diff --git a/dbus/dbus-transport-debug.c b/dbus/dbus-transport-debug.c
index f829fc29..e0a90644 100644
--- a/dbus/dbus-transport-debug.c
+++ b/dbus/dbus-transport-debug.c
@@ -76,11 +76,24 @@ debug_finalize (DBusTransport *transport)
static void
do_reading (DBusTransport *transport)
{
+ DBusTransportDebug *debug_transport = (DBusTransportDebug*) transport;
+
if (transport->disconnected)
return;
/* Now dispatch the messages */
- while (dbus_connection_dispatch_message (transport->connection));
+ if (dbus_connection_dispatch_message (transport->connection))
+ {
+ debug_transport->read_timeout =
+ _dbus_timeout_new (DEFAULT_INTERVAL, (DBusTimeoutHandler)do_reading,
+ transport, NULL);
+ if (!_dbus_connection_add_timeout (transport->connection,
+ debug_transport->read_timeout))
+ {
+ _dbus_timeout_unref (debug_transport->read_timeout);
+ debug_transport->read_timeout = NULL;
+ }
+ }
}
static void
diff --git a/test/Makefile.am b/test/Makefile.am
index 604fd3f8..aeb623fb 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -26,7 +26,9 @@ unbase64_SOURCES= \
bus_test_SOURCES = \
debug-thread.c \
debug-thread.h \
- bus-test.c
+ bus-test.c \
+ bus-test-loop.c \
+ bus-test-loop.h
break_loader_SOURCES= \
break-loader.c
diff --git a/test/bus-test-loop.c b/test/bus-test-loop.c
new file mode 100644
index 00000000..aa9e4fcc
--- /dev/null
+++ b/test/bus-test-loop.c
@@ -0,0 +1,120 @@
+#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
new file mode 100644
index 00000000..ac77b29f
--- /dev/null
+++ b/test/bus-test-loop.h
@@ -0,0 +1,10 @@
+#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
index 38742805..342e806b 100644
--- a/test/bus-test.c
+++ b/test/bus-test.c
@@ -1,6 +1,6 @@
#include <dbus/dbus.h>
#include <stdio.h>
-#include <sys/time.h>
+#include <stdlib.h>
#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
@@ -9,54 +9,8 @@
#undef DBUS_COMPILATION
#include "debug-thread.h"
+#include "bus-test-loop.h"
-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);
- printf ("add timeout!\n");
-}
-
-static void
-remove_timeout (DBusTimeout *timeout,
- void *data)
-{
- printf ("remove timeout!\n");
-
-}
static DBusHandlerResult
message_handler (DBusMessageHandler *handler,
@@ -77,80 +31,197 @@ new_connection_callback (DBusServer *server,
if (!bus_connection_setup (new_connection))
return;
- dbus_connection_set_timeout_functions (new_connection,
- add_timeout, remove_timeout,
- NULL, NULL);
+ 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);
+}
-static dbus_bool_t running_loop;
+/* 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 void
-loop_quit (void)
+
+static DBusHandlerResult
+test_hello_client1_handler (DBusMessageHandler *handler,
+ DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
{
- running_loop = FALSE;
+ char *tmp = NULL;
+
+ if (!test_hello_succeeding)
+ goto out;
+
+ 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,
+ DBUS_TYPE_STRING, &client1_name,
+ 0) == DBUS_RESULT_SUCCESS));
+
+ client1_stage += 1;
+ }
+ else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
+ {
+ TEST_HELLO_HANDLE_FAIL (client1_stage == 1 || client1_stage == 3);
+
+ TEST_HELLO_HANDLE_FAIL ((dbus_message_get_args (message,
+ DBUS_TYPE_STRING, &tmp,
+ 0) == DBUS_RESULT_SUCCESS));
+ 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,
+ DBUS_TYPE_STRING, &tmp,
+ 0) == DBUS_RESULT_SUCCESS));
+ 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 void
-loop_run (void)
+static DBusHandlerResult
+test_hello_client2_handler (DBusMessageHandler *handler,
+ DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
{
- long start_time = get_time ();
- running_loop = TRUE;
+ 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);
- /* Horribly inefficient main loop */
- while (running_loop)
+ TEST_HELLO_HANDLE_FAIL ((dbus_message_get_args (message,
+ DBUS_TYPE_STRING, &client2_name,
+ 0) == DBUS_RESULT_SUCCESS));
+
+ client2_stage += 1;
+ }
+ else if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
{
- 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);
- }
-
- if (get_time () - start_time > 1000)
- loop_quit ();
+ TEST_HELLO_HANDLE_FAIL (client2_stage == 1);
+
+ TEST_HELLO_HANDLE_FAIL ((dbus_message_get_args (message,
+ DBUS_TYPE_STRING, &tmp,
+ 0) == DBUS_RESULT_SUCCESS));
+ 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,
+ DBUS_TYPE_STRING, &tmp,
+ 0) == DBUS_RESULT_SUCCESS));
+ 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;
+ DBusResultCode result;
+ DBusMessage *message;
+ DBusMessageHandler *handler;
+
+ /* 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);
+ dbus_connection_send_message (connection, message, NULL, NULL);
+ 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);
+ dbus_connection_send_message (connection, message, NULL, NULL);
+ dbus_message_unref (message);
+ bus_test_loop_run ();
+
+ return test_hello_succeeding;
+}
int
main (int argc,
char **argv)
{
DBusServer *server;
- DBusConnection *connection;
DBusResultCode result;
- DBusMessage *message;
- DBusMessageHandler *handler;
-
+
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);
- dbus_server_set_timeout_functions (server,
- add_timeout, remove_timeout,
- NULL, NULL);
+ bus_test_loop_hookup_with_server (server);
if (server == NULL)
{
fprintf (stderr, "Failed to start server: %s\n",
@@ -158,34 +229,10 @@ main (int argc,
return 1;
}
- connection = dbus_connection_open ("debug:name=test-server", &result);
- dbus_connection_set_timeout_functions (connection,
- add_timeout, remove_timeout,
- NULL, NULL);
- if (connection == NULL)
- {
- fprintf (stderr, "Failed to connect to server: %s\n",
- dbus_result_to_string (result));
- return 1;
- }
-
- message = dbus_message_new (DBUS_SERVICE_DBUS,
- DBUS_MESSAGE_HELLO);
- dbus_message_append_args (message,
- DBUS_TYPE_STRING, "test",
- 0);
-
- handler = dbus_message_handler_new (message_handler, NULL, NULL);
- dbus_connection_add_filter (connection, handler);
-
- dbus_connection_send_message (connection, message, NULL, NULL);
- dbus_message_unref (message);
-
- bus_connection_init ();
-
- loop_run ();
-
- dbus_connection_disconnect (connection);
+ if (!test_hello_replies ())
+ die ("hello with replies");
+ printf ("all tests succeeded\n");
+
return 0;
}