From: Anders Carlsson Date: Fri, 24 Jan 2003 23:51:59 +0000 (+0000) Subject: 2003-01-25 Anders Carlsson X-Git-Tag: dbus-0.3~54 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a16e83f45d33ae5f3bd5966416c57c8ad4448ae8;p=platform%2Fupstream%2Fdbus.git 2003-01-25 Anders Carlsson * bus/connection.c: (bus_connection_foreach): * bus/connection.h: Add new bus_connection_foreach function. * bus/driver.c: (send_one_message), (bus_driver_broadcast_message): Add function that broadcasts a message to all clients. (bus_driver_send_service_created), (bus_driver_handle_hello), (bus_driver_send_welcome_message), (bus_driver_handle_list_services), (bus_driver_message_handler): Implement functions that take care of listing services, and notifying clients when new services are created. * bus/services.c: (bus_services_list): * bus/services.h: Add new function that returns an array of strings with the currently registered services. * glib/dbus-glib.h: * glib/dbus-gmain.c: Update copyright year. --- diff --git a/ChangeLog b/ChangeLog index f89fe30..3961e97 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,29 @@ 2003-01-25 Anders Carlsson + * bus/connection.c: (bus_connection_foreach): + * bus/connection.h: + Add new bus_connection_foreach function. + + * bus/driver.c: (send_one_message), (bus_driver_broadcast_message): + Add function that broadcasts a message to all clients. + + (bus_driver_send_service_created), (bus_driver_handle_hello), + (bus_driver_send_welcome_message), + (bus_driver_handle_list_services), (bus_driver_message_handler): + Implement functions that take care of listing services, and notifying + clients when new services are created. + + * bus/services.c: (bus_services_list): + * bus/services.h: + Add new function that returns an array of strings with the currently + registered services. + + * glib/dbus-glib.h: + * glib/dbus-gmain.c: + Update copyright year. + +2003-01-25 Anders Carlsson + * dbus/dbus-connection.c: (dbus_connection_send_message): Unlock the message in case it was sent earlier. diff --git a/bus/connection.c b/bus/connection.c index 4a80573..0fcfdbe 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -224,3 +224,10 @@ bus_connection_get_name (DBusConnection *connection) return d->name; } + +void +bus_connection_foreach (BusConnectionForeachFunction function, + void *data) +{ + _dbus_list_foreach (&connections, function, data); +} diff --git a/bus/connection.h b/bus/connection.h index 1a23867..8a6b664 100644 --- a/bus/connection.h +++ b/bus/connection.h @@ -27,6 +27,9 @@ #include #include "services.h" +typedef void (* BusConnectionForeachFunction) (DBusConnection *connection, + void *data); + dbus_bool_t bus_connection_init (void); dbus_bool_t bus_connection_setup (DBusConnection *connection); @@ -38,9 +41,11 @@ void bus_connection_remove_owned_service (DBusConnection *connection, BusService *service); /* called by driver.c */ -dbus_bool_t bus_connection_set_name (DBusConnection *connection, - const DBusString *name); -const char *bus_connection_get_name (DBusConnection *connection); +dbus_bool_t bus_connection_set_name (DBusConnection *connection, + const DBusString *name); +const char *bus_connection_get_name (DBusConnection *connection); +void bus_connection_foreach (BusConnectionForeachFunction function, + void *data); #endif /* BUS_CONNECTION_H */ diff --git a/bus/driver.c b/bus/driver.c index 41c98c9..da167bb 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -24,6 +24,7 @@ #include "connection.h" #include "driver.h" #include "services.h" +#include #include #include #include @@ -31,10 +32,52 @@ #define BUS_DRIVER_SERVICE_NAME "org.freedesktop.DBus" #define BUS_DRIVER_HELLO_NAME "org.freedesktop.DBus.Hello" #define BUS_DRIVER_WELCOME_NAME "org.freedesktop.DBus.Welcome" +#define BUS_DRIVER_LIST_SERVICES_NAME "org.freedesktop.DBus.ListServices" +#define BUS_DRIVER_SERVICES_NAME "org.freedesktop.DBus.Services" + +#define BUS_DRIVER_SERVICE_CREATED_NAME "org.freedesktop.DBus.ServiceCreated" +#define BUS_DRIVER_SERVICE_DELETED_NAME "org.freedesktop.DBus.ServiceDeleted" static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection, DBusMessage *hello_message); +static void +send_one_message (DBusConnection *connection, void *data) +{ + dbus_connection_send_message (connection, data, NULL, NULL); +} + +static void +bus_driver_broadcast_message (DBusMessage *message) +{ + bus_connection_foreach (send_one_message, message); +} + +static dbus_bool_t +bus_driver_send_service_created (DBusConnection *connection, const char *name) +{ + DBusMessage *message; + + message = dbus_message_new (NULL, BUS_DRIVER_SERVICE_CREATED_NAME); + + if (!message) + return FALSE; + + if (!dbus_message_append_fields (message, + DBUS_TYPE_STRING, name, + 0)) + { + dbus_message_unref (message); + return FALSE; + } + + dbus_message_set_sender (message, BUS_DRIVER_SERVICE_NAME); + bus_driver_broadcast_message (message); + dbus_message_unref (message); + + return TRUE; +} + static dbus_bool_t create_unique_client_name (const char *name, DBusString *str) @@ -98,8 +141,8 @@ create_unique_client_name (const char *name, } static dbus_bool_t -bus_driver_handle_hello_message (DBusConnection *connection, - DBusMessage *message) +bus_driver_handle_hello (DBusConnection *connection, + DBusMessage *message) { DBusResultCode result; char *name; @@ -139,13 +182,19 @@ bus_driver_handle_hello_message (DBusConnection *connection, bus_connection_set_name (connection, &unique_name); /* We need to assign the sender to the message here */ - _dbus_message_set_sender (message, - bus_connection_get_name (connection)); + dbus_message_set_sender (message, + bus_connection_get_name (connection)); _dbus_string_free (&unique_name); retval = bus_driver_send_welcome_message (connection, message); + if (!retval) + return FALSE; + + /* Broadcast a ServiceCreated message */ + retval = bus_driver_send_service_created (connection, bus_connection_get_name (connection)); + return retval; } @@ -167,7 +216,7 @@ bus_driver_send_welcome_message (DBusConnection *connection, return FALSE; /* FIXME: Return value */ - _dbus_message_set_sender (welcome, BUS_DRIVER_SERVICE_NAME); + dbus_message_set_sender (welcome, BUS_DRIVER_SERVICE_NAME); if (!dbus_message_append_fields (welcome, DBUS_TYPE_STRING, name, @@ -183,6 +232,39 @@ bus_driver_send_welcome_message (DBusConnection *connection, return retval; } +static void +bus_driver_handle_list_services (DBusConnection *connection, + DBusMessage *message) +{ + DBusMessage *reply; + int len, i; + char **services; + + reply = dbus_message_new_reply (BUS_DRIVER_SERVICES_NAME, message); + + if (reply == NULL) + return; + + services = bus_services_list (&len); + + if (!services) + return; + + if (!dbus_message_append_fields (reply, + DBUS_TYPE_STRING_ARRAY, services, len, + 0)) + goto error; + + if (!dbus_connection_send_message (connection, reply, NULL, NULL)) + goto error; + + error: + dbus_message_unref (reply); + for (i = 0; i < len; i++) + dbus_free (services[i]); + dbus_free (services); +} + /* This is where all the magic occurs */ static DBusHandlerResult bus_driver_message_handler (DBusMessageHandler *handler, @@ -195,13 +277,15 @@ bus_driver_message_handler (DBusMessageHandler *handler, service = dbus_message_get_service (message); name = dbus_message_get_name (message); - _dbus_message_set_sender (message, - bus_connection_get_name (connection)); + dbus_message_set_sender (message, + bus_connection_get_name (connection)); if (strcmp (service, BUS_DRIVER_SERVICE_NAME) == 0) { if (strcmp (name, BUS_DRIVER_HELLO_NAME) == 0) - bus_driver_handle_hello_message (connection, message); + bus_driver_handle_hello (connection, message); + else if (strcmp (name, BUS_DRIVER_LIST_SERVICES_NAME) == 0) + bus_driver_handle_list_services (connection, message); } else { diff --git a/bus/services.c b/bus/services.c index c07b20f..0be56e7 100644 --- a/bus/services.c +++ b/bus/services.c @@ -163,3 +163,42 @@ bus_service_foreach (BusServiceForeachFunction function, (* function) (service, data); } } + +char ** +bus_services_list (int *array_len) +{ + int i, j, len; + char **retval; + DBusHashIter iter; + + len = _dbus_hash_table_get_n_entries (service_hash); + retval = dbus_new (char *, len); + + if (retval == NULL) + return NULL; + + _dbus_hash_iter_init (service_hash, &iter); + i = 0; + while (_dbus_hash_iter_next (&iter)) + { + BusService *service = _dbus_hash_iter_get_value (&iter); + + retval[i] = _dbus_strdup (service->name); + if (retval[i] == NULL) + goto error; + + i++; + } + + if (array_len) + *array_len = len; + + return retval; + + error: + for (j = 0; j < i; j++) + dbus_free (retval[i]); + dbus_free (retval); + + return NULL; +} diff --git a/bus/services.h b/bus/services.h index 8c2344a..3dd2b0f 100644 --- a/bus/services.h +++ b/bus/services.h @@ -50,4 +50,6 @@ const char* bus_service_get_name (BusService *servic void bus_service_foreach (BusServiceForeachFunction function, void *data); +char **bus_services_list (int *array_len); + #endif /* BUS_SERVICES_H */ diff --git a/glib/dbus-glib.h b/glib/dbus-glib.h index 3d72e49..b280e1b 100644 --- a/glib/dbus-glib.h +++ b/glib/dbus-glib.h @@ -1,7 +1,7 @@ /* -*- mode: C; c-file-style: "gnu" -*- */ /* dbus-glib.h GLib integration * - * Copyright (C) 2002 CodeFactory AB + * Copyright (C) 2002, 2003 CodeFactory AB * * Licensed under the Academic Free License version 1.2 * diff --git a/glib/dbus-gmain.c b/glib/dbus-gmain.c index 6e267cd..3256d06 100644 --- a/glib/dbus-gmain.c +++ b/glib/dbus-gmain.c @@ -1,7 +1,7 @@ /* -*- mode: C; c-file-style: "gnu" -*- */ /* dbus-gmain.c GLib main loop integration * - * Copyright (C) 2002 CodeFactory AB + * Copyright (C) 2002, 2003 CodeFactory AB * * Licensed under the Academic Free License version 1.2 *