From 54b943432c7c947db88066751dd36a372cc9a618 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 18 Jun 2007 19:32:51 +0000 Subject: 2007-06-18 Havoc Pennington * doc/dbus-specification.xml: document org.freedesktop.DBus.GetId() * bus/driver.c (bus_driver_handle_get_id): implement org.freedesktop.DBus.GetId() * bus/bus.c (bus_context_new): generate a unique ID for each bus context * dbus/dbus-connection.c (dbus_connection_get_server_id): new function * dbus/dbus-bus.c (dbus_bus_get_id): new function * dbus/dbus-server.c (dbus_server_get_id): new function --- dbus/dbus-server.c | 61 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'dbus/dbus-server.c') diff --git a/dbus/dbus-server.c b/dbus/dbus-server.c index 9fc5cace..7d18e615 100644 --- a/dbus/dbus-server.c +++ b/dbus/dbus-server.c @@ -798,6 +798,43 @@ dbus_server_get_address (DBusServer *server) return retval; } +/** + * Returns the unique ID of the server, as a newly-allocated + * string which must be freed by the caller. This ID is + * normally used by clients to tell when two #DBusConnection + * would be equivalent (because the server address passed + * to dbus_connection_open() will have the same guid in the + * two cases). dbus_connection_open() can re-use an existing + * connection with the same ID instead of opening a new + * connection. + * + * This is an ID unique to each #DBusServer. Remember that + * a #DBusServer represents only one mode of connecting, + * so e.g. a bus daemon can listen on multiple addresses + * which will mean it has multiple #DBusServer each with + * their own ID. + * + * The ID is not a UUID in the sense of RFC4122; the details + * are explained in the D-Bus specification. + * + * @param server the server + * @returns the id of the server or #NULL if no memory + */ +char* +dbus_server_get_id (DBusServer *server) +{ + char *retval; + + _dbus_return_val_if_fail (server != NULL, NULL); + + SERVER_LOCK (server); + retval = NULL; + _dbus_string_copy_data (&server->guid_hex, &retval); + SERVER_UNLOCK (server); + + return retval; +} + /** * Sets a function to be used for handling new connections. The given * function is passed each new connection as the connection is @@ -1110,6 +1147,7 @@ dbus_server_get_data (DBusServer *server, #ifdef DBUS_BUILD_TESTS #include "dbus-test.h" +#include dbus_bool_t _dbus_server_test (void) @@ -1130,8 +1168,8 @@ _dbus_server_test (void) for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++) { DBusError error; - - /* FIXME um, how are the two tests here different? */ + char *address; + char *id; dbus_error_init (&error); server = dbus_server_listen (valid_addresses[i], &error); @@ -1142,18 +1180,21 @@ _dbus_server_test (void) _dbus_assert_not_reached ("Failed to listen for valid address."); } - dbus_server_disconnect (server); - dbus_server_unref (server); + id = dbus_server_get_id (server); + _dbus_assert (id != NULL); + address = dbus_server_get_address (server); + _dbus_assert (address != NULL); - /* Try disconnecting before unreffing */ - server = dbus_server_listen (valid_addresses[i], &error); - if (server == NULL) + if (strstr (address, id) == NULL) { - _dbus_warn ("server listen error: %s: %s\n", error.name, error.message); - dbus_error_free (&error); - _dbus_assert_not_reached ("Failed to listen for valid address."); + _dbus_warn ("server id '%s' is not in the server address '%s'\n", + id, address); + _dbus_assert_not_reached ("bad server id or address"); } + dbus_free (id); + dbus_free (address); + dbus_server_disconnect (server); dbus_server_unref (server); } -- cgit