diff options
author | Havoc Pennington <hp@redhat.com> | 2002-11-25 05:13:09 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2002-11-25 05:13:09 +0000 |
commit | 041b0767b284034aee09e9a0de2a3844b8cc546a (patch) | |
tree | 8ef9cce16d743350971696ff6333ce43686c7ac0 /test | |
parent | 576cdb6e0b1274e9fa5276e01337aef330dd4e8c (diff) |
2002-11-24 Havoc Pennington <hp@pobox.com>
* test/echo-client.c, test/echo-server.c: cheesy test
clients.
* configure.in (AC_CHECK_FUNCS): check for writev
* dbus/dbus-message.c (_dbus_message_get_network_data): new
function
* dbus/dbus-list.c (_dbus_list_foreach): new function
* dbus/dbus-internals.c (_dbus_verbose): new function
* dbus/dbus-server.c, dbus/dbus-server.h: public object
representing a server that listens for connections.
* dbus/.cvsignore: create
* dbus/dbus-errors.h, dbus/dbus-errors.c:
public API for reporting errors
* dbus/dbus-connection.h, dbus/dbus-connection.c:
public object representing a connection that
sends/receives messages. (Same object used for
both client and server.)
* dbus/dbus-transport.h, dbus/dbus-transport.c:
Basic abstraction for different kinds of stream
that we might read/write messages from.
Diffstat (limited to 'test')
-rw-r--r-- | test/.cvsignore | 9 | ||||
-rw-r--r-- | test/Makefile.am | 19 | ||||
-rw-r--r-- | test/echo-client.c | 41 | ||||
-rw-r--r-- | test/echo-server.c | 48 | ||||
-rw-r--r-- | test/watch.c | 268 | ||||
-rw-r--r-- | test/watch.h | 15 |
6 files changed, 400 insertions, 0 deletions
diff --git a/test/.cvsignore b/test/.cvsignore new file mode 100644 index 00000000..53012e2b --- /dev/null +++ b/test/.cvsignore @@ -0,0 +1,9 @@ +.deps +.libs +Makefile +Makefile.in +*.lo +*.la +*.o +echo-server +echo-client diff --git a/test/Makefile.am b/test/Makefile.am index 6e42b47b..8f7d8e1d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,23 @@ if DBUS_BUILD_TESTS +INCLUDES=$(DBUS_TEST_CFLAGS) + +noinst_PROGRAMS= echo-client echo-server + +echo_client_SOURCES= \ + echo-client.c \ + watch.c \ + watch.h + +echo_server_SOURCES= \ + echo-server.c \ + watch.c \ + watch.h + +TEST_LIBS=$(DBUS_TEST_LIBS) $(top_builddir)/dbus/libdbus-convenience.la $(top_builddir)/dbus/libdbus-1.la + +echo_client_LDADD=$(TEST_LIBS) +echo_server_LDADD=$(TEST_LIBS) + endif diff --git a/test/echo-client.c b/test/echo-client.c new file mode 100644 index 00000000..5c6ee425 --- /dev/null +++ b/test/echo-client.c @@ -0,0 +1,41 @@ +#include <dbus/dbus.h> +#include <stdio.h> +#include "watch.h" + +int +main (int argc, + char **argv) +{ + DBusConnection *connection; + DBusResultCode result; + DBusMessage *message; + + if (argc < 2) + { + fprintf (stderr, "Give the server address as an argument\n"); + return 1; + } + + connection = dbus_connection_open (argv[1], &result); + if (connection == NULL) + { + fprintf (stderr, "Failed to open connection to %s: %s\n", + argv[1], dbus_result_to_string (result)); + return 1; + } + + setup_connection (connection); + + /* Send a message to get things going */ + message = dbus_message_new (); + dbus_connection_send_message (connection, + message, + NULL); + dbus_message_unref (message); + + do_mainloop (); + + dbus_connection_unref (connection); + + return 0; +} diff --git a/test/echo-server.c b/test/echo-server.c new file mode 100644 index 00000000..99f97ffd --- /dev/null +++ b/test/echo-server.c @@ -0,0 +1,48 @@ +#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"); + + setup_connection (new_connection); +} + +int +main (int argc, + char **argv) +{ + DBusServer *server; + DBusResultCode result; + + if (argc < 2) + { + fprintf (stderr, "Give the server address as an argument\n"); + return 1; + } + + server = dbus_server_listen (argv[1], &result); + if (server == NULL) + { + fprintf (stderr, "Failed to start server on %s: %s\n", + argv[1], dbus_result_to_string (result)); + 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/watch.c b/test/watch.c new file mode 100644 index 00000000..df26855c --- /dev/null +++ b/test/watch.c @@ -0,0 +1,268 @@ +#include "watch.h" +#include <stdio.h> + +#define DBUS_COMPILATION /* cheat and use DBusList */ +#include <dbus/dbus-list.h> +#undef DBUS_COMPILATION + +/* 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 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 +add_connection_watch (DBusWatch *watch, + DBusConnection *connection) +{ + WatchData *wd; + + wd = dbus_new0 (WatchData, 1); + wd->type = WATCH_CONNECTION; + wd->data = connection; + + _dbus_list_append (&watches, watch); + dbus_watch_set_data (watch, wd, dbus_free); +} + +static void +remove_connection_watch (DBusWatch *watch, + DBusConnection *connection) +{ + _dbus_list_remove (&watches, watch); + dbus_watch_set_data (watch, NULL, NULL); +} + +static void +add_server_watch (DBusWatch *watch, + DBusServer *server) +{ + WatchData *wd; + + wd = dbus_new0 (WatchData, 1); + wd->type = WATCH_SERVER; + wd->data = server; + + _dbus_list_append (&watches, watch); + + dbus_watch_set_data (watch, wd, dbus_free); +} + +static void +remove_server_watch (DBusWatch *watch, + DBusServer *server) +{ + _dbus_list_remove (&watches, watch); + dbus_watch_set_data (watch, NULL, NULL); +} + +static int count = 0; + +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; + + while ((message = dbus_connection_pop_message (connection))) + { + DBusMessage *reply; + + printf ("Received message %d, sending reply\n", count); + + reply = dbus_message_new (); + dbus_connection_send_message (connection, + reply, + NULL); + 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; + + check_messages (); + + 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); + int fd; + DBusWatch *watch; + unsigned int flags; + + watch = link->data; + + 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); + + link = _dbus_list_get_first_link (&watches); + while (link != NULL) + { + DBusList *next = _dbus_list_get_next_link (&watches, link); + int fd; + DBusWatch *watch; + unsigned int flags; + unsigned int condition; + + watch = link->data; + + 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; +} + +static void +error_handler (DBusConnection *connection, + DBusResultCode error_code, + void *data) +{ + fprintf (stderr, + "Error on connection: %s\n", + dbus_result_to_string (error_code)); + + _dbus_list_remove (&connections, connection); + dbus_connection_unref (connection); + quit_mainloop (); +} + +void +setup_connection (DBusConnection *connection) +{ + dbus_connection_set_watch_functions (connection, + (DBusAddWatchFunction) add_connection_watch, + (DBusRemoveWatchFunction) remove_connection_watch, + connection, + NULL); + + dbus_connection_set_error_function (connection, + error_handler, + NULL, NULL); + + dbus_connection_ref (connection); + _dbus_list_append (&connections, connection); +} + +void +setup_server (DBusServer *server) +{ + dbus_server_set_watch_functions (server, + (DBusAddWatchFunction) add_server_watch, + (DBusRemoveWatchFunction) remove_server_watch, + server, + NULL); +} diff --git a/test/watch.h b/test/watch.h new file mode 100644 index 00000000..a9ad0834 --- /dev/null +++ b/test/watch.h @@ -0,0 +1,15 @@ +/* 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 |