2003-01-06 01:08:14 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu" -*- */
|
|
|
|
|
/* connection.c Client connections
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2003 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Academic Free License version 1.2
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "connection.h"
|
2003-01-25 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
* bus/connection.c: (connection_disconnect_handler),
(connection_watch_callback), (bus_connection_setup):
* bus/dispatch.c: (send_one_message),
(bus_dispatch_broadcast_message), (bus_dispatch_message_handler),
(bus_dispatch_add_connection), (bus_dispatch_remove_connection):
* bus/dispatch.h:
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services), (bus_driver_remove_connection),
(bus_driver_handle_message):
* bus/driver.h:
Refactor code, put the message dispatching in its own file. Use
_DBUS_HANDLE_OOM. Also send ServiceDeleted messages when a client
is disconnected.
2003-01-25 20:53:53 +00:00
|
|
|
#include "dispatch.h"
|
2003-01-06 01:08:14 +00:00
|
|
|
#include "loop.h"
|
|
|
|
|
#include "services.h"
|
|
|
|
|
#include <dbus/dbus-list.h>
|
|
|
|
|
|
|
|
|
|
static int connection_data_slot;
|
|
|
|
|
static DBusList *connections = NULL;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
DBusList *services_owned;
|
|
|
|
|
|
2003-01-21 12:42:33 +00:00
|
|
|
char *name;
|
2003-01-06 01:08:14 +00:00
|
|
|
} BusConnectionData;
|
|
|
|
|
|
|
|
|
|
#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
|
|
|
|
|
|
|
|
|
|
static void
|
2003-01-18 17:40:42 +00:00
|
|
|
connection_disconnect_handler (DBusConnection *connection,
|
|
|
|
|
void *data)
|
2003-01-06 01:08:14 +00:00
|
|
|
{
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
BusService *service;
|
2003-01-14 11:19:06 +00:00
|
|
|
|
2003-01-18 17:40:42 +00:00
|
|
|
_dbus_warn ("Disconnected\n");
|
2003-01-06 01:08:14 +00:00
|
|
|
|
|
|
|
|
d = BUS_CONNECTION_DATA (connection);
|
2003-01-18 17:40:42 +00:00
|
|
|
_dbus_assert (d != NULL);
|
2003-01-06 01:08:14 +00:00
|
|
|
|
|
|
|
|
/* Drop any service ownership */
|
|
|
|
|
while ((service = _dbus_list_get_last (&d->services_owned)))
|
|
|
|
|
bus_service_remove_owner (service, connection);
|
|
|
|
|
|
2003-01-25 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
* bus/connection.c: (connection_disconnect_handler),
(connection_watch_callback), (bus_connection_setup):
* bus/dispatch.c: (send_one_message),
(bus_dispatch_broadcast_message), (bus_dispatch_message_handler),
(bus_dispatch_add_connection), (bus_dispatch_remove_connection):
* bus/dispatch.h:
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services), (bus_driver_remove_connection),
(bus_driver_handle_message):
* bus/driver.h:
Refactor code, put the message dispatching in its own file. Use
_DBUS_HANDLE_OOM. Also send ServiceDeleted messages when a client
is disconnected.
2003-01-25 20:53:53 +00:00
|
|
|
bus_dispatch_remove_connection (connection);
|
2003-01-21 12:42:33 +00:00
|
|
|
|
2003-01-06 01:08:14 +00:00
|
|
|
/* no more watching */
|
|
|
|
|
dbus_connection_set_watch_functions (connection,
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
connection,
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
dbus_connection_set_data (connection,
|
|
|
|
|
connection_data_slot,
|
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
|
|
_dbus_list_remove (&connections, connection);
|
|
|
|
|
dbus_connection_unref (connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
connection_watch_callback (DBusWatch *watch,
|
|
|
|
|
unsigned int condition,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
|
|
|
|
DBusConnection *connection = data;
|
|
|
|
|
|
2003-01-25 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
* bus/connection.c: (connection_disconnect_handler),
(connection_watch_callback), (bus_connection_setup):
* bus/dispatch.c: (send_one_message),
(bus_dispatch_broadcast_message), (bus_dispatch_message_handler),
(bus_dispatch_add_connection), (bus_dispatch_remove_connection):
* bus/dispatch.h:
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services), (bus_driver_remove_connection),
(bus_driver_handle_message):
* bus/driver.h:
Refactor code, put the message dispatching in its own file. Use
_DBUS_HANDLE_OOM. Also send ServiceDeleted messages when a client
is disconnected.
2003-01-25 20:53:53 +00:00
|
|
|
dbus_connection_ref (connection);
|
|
|
|
|
|
2003-01-06 01:08:14 +00:00
|
|
|
dbus_connection_handle_watch (connection, watch, condition);
|
2003-01-21 12:42:33 +00:00
|
|
|
|
|
|
|
|
while (dbus_connection_dispatch_message (connection));
|
2003-01-25 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
* bus/connection.c: (connection_disconnect_handler),
(connection_watch_callback), (bus_connection_setup):
* bus/dispatch.c: (send_one_message),
(bus_dispatch_broadcast_message), (bus_dispatch_message_handler),
(bus_dispatch_add_connection), (bus_dispatch_remove_connection):
* bus/dispatch.h:
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services), (bus_driver_remove_connection),
(bus_driver_handle_message):
* bus/driver.h:
Refactor code, put the message dispatching in its own file. Use
_DBUS_HANDLE_OOM. Also send ServiceDeleted messages when a client
is disconnected.
2003-01-25 20:53:53 +00:00
|
|
|
dbus_connection_unref (connection);
|
2003-01-06 01:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_connection_watch (DBusWatch *watch,
|
|
|
|
|
DBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
bus_loop_add_watch (watch, connection_watch_callback, connection,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
remove_connection_watch (DBusWatch *watch,
|
|
|
|
|
DBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
bus_loop_remove_watch (watch, connection_watch_callback, connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_connection_data (void *data)
|
|
|
|
|
{
|
|
|
|
|
BusConnectionData *d = data;
|
|
|
|
|
|
|
|
|
|
/* services_owned should be NULL since we should be disconnected */
|
|
|
|
|
_dbus_assert (d->services_owned == NULL);
|
2003-01-21 12:42:33 +00:00
|
|
|
|
|
|
|
|
dbus_free (d->name);
|
2003-01-06 01:08:14 +00:00
|
|
|
|
|
|
|
|
dbus_free (d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
|
bus_connection_init (void)
|
|
|
|
|
{
|
|
|
|
|
connection_data_slot = dbus_connection_allocate_data_slot ();
|
|
|
|
|
|
|
|
|
|
if (connection_data_slot < 0)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
|
bus_connection_setup (DBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
|
2003-01-14 11:19:06 +00:00
|
|
|
d = dbus_new0 (BusConnectionData, 1);
|
|
|
|
|
|
2003-01-06 01:08:14 +00:00
|
|
|
if (d == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (!dbus_connection_set_data (connection,
|
|
|
|
|
connection_data_slot,
|
|
|
|
|
d, free_connection_data))
|
|
|
|
|
{
|
|
|
|
|
dbus_free (d);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_dbus_list_append (&connections, connection))
|
|
|
|
|
{
|
|
|
|
|
/* this will free our data when connection gets finalized */
|
|
|
|
|
dbus_connection_disconnect (connection);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_connection_ref (connection);
|
|
|
|
|
|
|
|
|
|
dbus_connection_set_watch_functions (connection,
|
|
|
|
|
(DBusAddWatchFunction) add_connection_watch,
|
|
|
|
|
(DBusRemoveWatchFunction) remove_connection_watch,
|
|
|
|
|
connection,
|
|
|
|
|
NULL);
|
|
|
|
|
|
2003-01-18 17:40:42 +00:00
|
|
|
dbus_connection_set_disconnect_function (connection,
|
|
|
|
|
connection_disconnect_handler,
|
|
|
|
|
NULL, NULL);
|
2003-01-06 01:08:14 +00:00
|
|
|
|
2003-01-25 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
* bus/connection.c: (connection_disconnect_handler),
(connection_watch_callback), (bus_connection_setup):
* bus/dispatch.c: (send_one_message),
(bus_dispatch_broadcast_message), (bus_dispatch_message_handler),
(bus_dispatch_add_connection), (bus_dispatch_remove_connection):
* bus/dispatch.h:
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services), (bus_driver_remove_connection),
(bus_driver_handle_message):
* bus/driver.h:
Refactor code, put the message dispatching in its own file. Use
_DBUS_HANDLE_OOM. Also send ServiceDeleted messages when a client
is disconnected.
2003-01-25 20:53:53 +00:00
|
|
|
/* Setup the connection with the dispatcher */
|
|
|
|
|
if (!bus_dispatch_add_connection (connection))
|
2003-01-21 12:42:33 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
2003-01-06 01:08:14 +00:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
|
bus_connection_add_owned_service (DBusConnection *connection,
|
|
|
|
|
BusService *service)
|
|
|
|
|
{
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
|
|
|
|
|
d = BUS_CONNECTION_DATA (connection);
|
|
|
|
|
_dbus_assert (d != NULL);
|
|
|
|
|
|
|
|
|
|
if (!_dbus_list_append (&d->services_owned,
|
|
|
|
|
service))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
bus_connection_remove_owned_service (DBusConnection *connection,
|
|
|
|
|
BusService *service)
|
|
|
|
|
{
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
|
|
|
|
|
d = BUS_CONNECTION_DATA (connection);
|
|
|
|
|
_dbus_assert (d != NULL);
|
|
|
|
|
|
|
|
|
|
_dbus_list_remove_last (&d->services_owned, service);
|
|
|
|
|
}
|
2003-01-21 12:42:33 +00:00
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
|
bus_connection_set_name (DBusConnection *connection,
|
|
|
|
|
const DBusString *name)
|
|
|
|
|
{
|
|
|
|
|
const char *c_name;
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
|
|
|
|
|
d = BUS_CONNECTION_DATA (connection);
|
|
|
|
|
_dbus_assert (d != NULL);
|
|
|
|
|
_dbus_assert (d->name == NULL);
|
|
|
|
|
|
|
|
|
|
_dbus_string_get_const_data (name, &c_name);
|
|
|
|
|
|
|
|
|
|
d->name = _dbus_strdup (c_name);
|
|
|
|
|
|
|
|
|
|
if (d->name == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
bus_connection_get_name (DBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
BusConnectionData *d;
|
|
|
|
|
|
|
|
|
|
d = BUS_CONNECTION_DATA (connection);
|
|
|
|
|
_dbus_assert (d != NULL);
|
|
|
|
|
|
|
|
|
|
return d->name;
|
|
|
|
|
}
|
2003-01-24 23:51:59 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
bus_connection_foreach (BusConnectionForeachFunction function,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
2003-02-13 Anders Carlsson <andersca@codefactory.se>
* bus/Makefile.am:
Add utils.[ch]
* bus/connection.c: (bus_connection_foreach):
Fix a warning.
* bus/desktop-file.c: (grow_lines_in_section), (grow_sections),
(unescape_string), (new_section), (parse_section_start),
(parse_key_value), (report_error), (bus_desktop_file_load),
(bus_desktop_file_get_string):
* bus/desktop-file.h:
Use DBusError for error reporting.
* bus/dispatch.c: (send_one_message),
(bus_dispatch_message_handler):
* bus/driver.c: (bus_driver_send_service_deleted),
(bus_driver_send_service_created), (bus_driver_send_service_lost),
(bus_driver_send_service_acquired), (bus_driver_handle_hello),
(bus_driver_send_welcome_message),
(bus_driver_handle_list_services),
(bus_driver_handle_acquire_service),
(bus_driver_handle_service_exists):
* bus/loop.c: (bus_loop_run):
* bus/main.c:
Use BUS_HANDLE_OOM instead of _DBUS_HANDLE_OOM.
* bus/utils.c: (bus_wait_for_memory):
* bus/utils.h:
New files with general utility functions.
* dbus/dbus-internals.h:
Remove _DBUS_HANDLE_OOM.
2003-02-13 19:06:42 +00:00
|
|
|
_dbus_list_foreach (&connections, (DBusForeachFunction)function, data);
|
2003-01-24 23:51:59 +00:00
|
|
|
}
|