1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* bus.c Bus client (driver)
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "connection.h"
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-string.h>
31 #define BUS_DRIVER_SERVICE_NAME "org.freedesktop.DBus"
32 #define BUS_DRIVER_HELLO_NAME "org.freedesktop.DBus.Hello"
33 #define BUS_DRIVER_WELCOME_NAME "org.freedesktop.DBus.Welcome"
35 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
36 DBusMessage *hello_message);
39 create_unique_client_name (const char *name, DBusString *str)
43 if (!_dbus_string_init (str, _DBUS_INT_MAX))
46 if (!_dbus_string_append (str, name))
49 len = _dbus_string_get_length (str);
54 if (!_dbus_string_append_int (str, i))
56 _dbus_string_free (str);
60 /* Check if a client with the name exists */
61 if (bus_service_lookup (str, FALSE) == NULL)
64 _dbus_string_set_length (str, len);
73 bus_driver_handle_hello_message (DBusConnection *connection,
76 DBusResultCode result;
78 DBusString unique_name;
82 result = dbus_message_get_fields (message,
83 DBUS_TYPE_STRING, &name,
86 /* FIXME: Handle this in a better way */
87 if (result != DBUS_RESULT_SUCCESS)
90 if (!create_unique_client_name (name, &unique_name))
93 /* Create the service */
94 service = bus_service_lookup (&unique_name, TRUE);
97 _dbus_string_free (&unique_name);
101 /* FIXME: Error checks from this point */
103 /* Add the connection as the owner */
104 bus_service_add_owner (service, connection);
105 bus_connection_set_name (connection, &unique_name);
107 /* We need to assign the sender to the message here */
108 _dbus_message_set_sender (message,
109 bus_connection_get_name (connection));
111 _dbus_string_free (&unique_name);
113 retval = bus_driver_send_welcome_message (connection, message);
119 bus_driver_send_welcome_message (DBusConnection *connection,
120 DBusMessage *hello_message)
122 DBusMessage *welcome;
127 name = bus_connection_get_name (connection);
128 _dbus_assert (name != NULL);
130 welcome = dbus_message_new_reply (BUS_DRIVER_WELCOME_NAME,
135 /* FIXME: Return value */
136 _dbus_message_set_sender (welcome, BUS_DRIVER_SERVICE_NAME);
138 if (!dbus_message_append_fields (welcome,
139 DBUS_TYPE_STRING, name,
142 dbus_message_unref (welcome);
146 retval = dbus_connection_send_message (connection, welcome, NULL, NULL);
147 dbus_message_unref (welcome);
152 /* This is where all the magic occurs */
153 static DBusHandlerResult
154 bus_driver_message_handler (DBusMessageHandler *handler,
155 DBusConnection *connection,
156 DBusMessage *message,
159 const char *service, *name;
161 service = dbus_message_get_service (message);
162 name = dbus_message_get_name (message);
164 _dbus_message_set_sender (message,
165 bus_connection_get_name (connection));
167 if (strcmp (service, BUS_DRIVER_SERVICE_NAME) == 0)
169 if (strcmp (name, BUS_DRIVER_HELLO_NAME) == 0)
170 bus_driver_handle_hello_message (connection, message);
174 /* FIXME: Dispatch the message :-) */
177 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
181 bus_driver_add_connection (DBusConnection *connection)
183 DBusMessageHandler *handler;
185 handler = dbus_message_handler_new (bus_driver_message_handler, NULL, NULL);
187 if (!dbus_connection_add_filter (connection, handler))
189 dbus_message_handler_unref (handler);
194 _dbus_verbose ("D-Bus driver on board...\n");
200 bus_driver_remove_connection (DBusConnection *connection)
203 DBusString service_name;
206 name = bus_connection_get_name (connection);
211 _dbus_string_init_const (&service_name, name);
213 service = bus_service_lookup (&service_name, FALSE);
216 bus_service_free (service);