1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* connection.c Client connections
4 * Copyright (C) 2003 Red Hat, Inc.
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
23 #include "connection.h"
27 #include <dbus/dbus-list.h>
29 static int connection_data_slot;
30 static DBusList *connections = NULL;
34 DBusList *services_owned;
39 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
42 connection_disconnect_handler (DBusConnection *connection,
48 _dbus_warn ("Disconnected\n");
50 d = BUS_CONNECTION_DATA (connection);
51 _dbus_assert (d != NULL);
53 /* Drop any service ownership */
54 while ((service = _dbus_list_get_last (&d->services_owned)))
55 bus_service_remove_owner (service, connection);
57 bus_dispatch_remove_connection (connection);
59 /* no more watching */
60 dbus_connection_set_watch_functions (connection,
65 dbus_connection_set_data (connection,
69 _dbus_list_remove (&connections, connection);
70 dbus_connection_unref (connection);
74 connection_watch_callback (DBusWatch *watch,
75 unsigned int condition,
78 DBusConnection *connection = data;
80 dbus_connection_ref (connection);
82 dbus_connection_handle_watch (connection, watch, condition);
84 while (dbus_connection_dispatch_message (connection));
85 dbus_connection_unref (connection);
89 add_connection_watch (DBusWatch *watch,
90 DBusConnection *connection)
92 bus_loop_add_watch (watch, connection_watch_callback, connection,
97 remove_connection_watch (DBusWatch *watch,
98 DBusConnection *connection)
100 bus_loop_remove_watch (watch, connection_watch_callback, connection);
104 free_connection_data (void *data)
106 BusConnectionData *d = data;
108 /* services_owned should be NULL since we should be disconnected */
109 _dbus_assert (d->services_owned == NULL);
117 bus_connection_init (void)
119 connection_data_slot = dbus_connection_allocate_data_slot ();
121 if (connection_data_slot < 0)
128 bus_connection_setup (DBusConnection *connection)
130 BusConnectionData *d;
132 d = dbus_new0 (BusConnectionData, 1);
137 if (!dbus_connection_set_data (connection,
138 connection_data_slot,
139 d, free_connection_data))
145 if (!_dbus_list_append (&connections, connection))
147 /* this will free our data when connection gets finalized */
148 dbus_connection_disconnect (connection);
152 dbus_connection_ref (connection);
154 dbus_connection_set_watch_functions (connection,
155 (DBusAddWatchFunction) add_connection_watch,
156 (DBusRemoveWatchFunction) remove_connection_watch,
160 dbus_connection_set_disconnect_function (connection,
161 connection_disconnect_handler,
164 /* Setup the connection with the dispatcher */
165 if (!bus_dispatch_add_connection (connection))
172 bus_connection_add_owned_service (DBusConnection *connection,
175 BusConnectionData *d;
177 d = BUS_CONNECTION_DATA (connection);
178 _dbus_assert (d != NULL);
180 if (!_dbus_list_append (&d->services_owned,
188 bus_connection_remove_owned_service (DBusConnection *connection,
191 BusConnectionData *d;
193 d = BUS_CONNECTION_DATA (connection);
194 _dbus_assert (d != NULL);
196 _dbus_list_remove_last (&d->services_owned, service);
200 bus_connection_set_name (DBusConnection *connection,
201 const DBusString *name)
204 BusConnectionData *d;
206 d = BUS_CONNECTION_DATA (connection);
207 _dbus_assert (d != NULL);
208 _dbus_assert (d->name == NULL);
210 _dbus_string_get_const_data (name, &c_name);
212 d->name = _dbus_strdup (c_name);
221 bus_connection_get_name (DBusConnection *connection)
223 BusConnectionData *d;
225 d = BUS_CONNECTION_DATA (connection);
226 _dbus_assert (d != NULL);
232 bus_connection_foreach (BusConnectionForeachFunction function,
235 _dbus_list_foreach (&connections, (DBusForeachFunction)function, data);