Commit 96a9f803 authored by Havoc Pennington's avatar Havoc Pennington

2003-01-05 Havoc Pennington <hp@pobox.com>

	* bus/connection.c: implement routines for handling connections,
	first thing is keeping a list of owned services on each connection
	and setting up watches etc.

	* bus/services.c: implement a mapping from service names to lists
	of connections

	* dbus/dbus-hash.c: add DBUS_HASH_POINTER

	* dbus/dbus-threads.c (dbus_static_mutex_lock): add functions
	to use static mutexes for global data

	* dbus/dbus-connection.c (dbus_connection_set_data): add new
	collection of functions to set/get application-specific data
	on the DBusConnection.
parent c92339de
2003-01-05 Havoc Pennington <hp@pobox.com>
* bus/connection.c: implement routines for handling connections,
first thing is keeping a list of owned services on each connection
and setting up watches etc.
* bus/services.c: implement a mapping from service names to lists
of connections
* dbus/dbus-hash.c: add DBUS_HASH_POINTER
* dbus/dbus-threads.c (dbus_static_mutex_lock): add functions
to use static mutexes for global data
* dbus/dbus-connection.c (dbus_connection_set_data): add new
collection of functions to set/get application-specific data
on the DBusConnection.
2003-01-04 Havoc Pennington <hp@pobox.com>
* dbus/dbus-sysdeps.c (_dbus_sleep_milliseconds): new function
......
......@@ -7,9 +7,13 @@ EFENCE=
bin_PROGRAMS=dbus-daemon-1
dbus_daemon_1_SOURCES= \
connection.c \
connection.h \
loop.c \
loop.h \
main.c
main.c \
services.c \
services.h
dbus_daemon_1_LDADD= \
$(EFENCE) \
......
/* -*- 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"
#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;
} BusConnectionData;
#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
static void
connection_error_handler (DBusConnection *connection,
DBusResultCode error_code,
void *data)
{
BusConnectionData *d;
BusService *service;
_dbus_warn ("Error on connection: %s\n",
dbus_result_to_string (error_code));
d = BUS_CONNECTION_DATA (connection);
_dbus_assert (d != NULL);
/* we don't want to be called again since we're dropping the connection */
dbus_connection_set_error_function (connection, NULL, NULL, NULL);
/* Drop any service ownership */
while ((service = _dbus_list_get_last (&d->services_owned)))
bus_service_remove_owner (service, connection);
/* 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;
dbus_connection_handle_watch (connection, watch, condition);
}
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);
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;
d = dbus_new (BusConnectionData, 1);
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);
dbus_connection_set_error_function (connection,
connection_error_handler,
NULL, NULL);
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);
}
/* -*- mode: C; c-file-style: "gnu" -*- */
/* connection.h 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
*
*/
#ifndef BUS_CONNECTION_H
#define BUS_CONNECTION_H
#include <dbus/dbus.h>
#include "services.h"
dbus_bool_t bus_connection_init (void);
dbus_bool_t bus_connection_setup (DBusConnection *connection);
/* called by services.c */
dbus_bool_t bus_connection_add_owned_service (DBusConnection *connection,
BusService *service);
void bus_connection_remove_owned_service (DBusConnection *connection,
BusService *service);
#endif /* BUS_CONNECTION_H */
......@@ -173,7 +173,11 @@ bus_loop_run (void)
initial_serial = watch_list_serial;
i = 0;
while (i < n_fds)
{
{
/* FIXME I think this "restart if we change the watches"
* approach could result in starving watches
* toward the end of the list.
*/
if (initial_serial != watch_list_serial)
goto next_iteration;
......
......@@ -21,35 +21,8 @@
*
*/
#include "loop.h"
#include "connection.h"
#include <dbus/dbus-list.h>
#include <dbus/dbus.h>
static DBusList *connections = NULL;
static void
connection_error_handler (DBusConnection *connection,
DBusResultCode error_code,
void *data)
{
_dbus_warn ("Error on connection: %s\n",
dbus_result_to_string (error_code));
/* we don't want to be called again since we're dropping the connection */
dbus_connection_set_error_function (connection, NULL, 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;
dbus_connection_handle_watch (connection, watch, condition);
}
static void
server_watch_callback (DBusWatch *watch,
......@@ -61,21 +34,6 @@ server_watch_callback (DBusWatch *watch,
dbus_server_handle_watch (server, watch, condition);
}
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
add_server_watch (DBusWatch *watch,
DBusServer *server)
......@@ -91,30 +49,6 @@ remove_server_watch (DBusWatch *watch,
bus_loop_remove_watch (watch, server_watch_callback, server);
}
static dbus_bool_t
setup_connection (DBusConnection *connection)
{
if (!_dbus_list_append (&connections, connection))
{
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);
dbus_connection_set_error_function (connection,
connection_error_handler,
NULL, NULL);
return TRUE;
}
static void
setup_server (DBusServer *server)
{
......@@ -130,7 +64,7 @@ new_connection_callback (DBusServer *server,
DBusConnection *new_connection,
void *data)
{
if (!setup_connection (new_connection))
if (!bus_connection_setup (new_connection))
; /* we won't have ref'd the connection so it will die */
}
......@@ -156,6 +90,8 @@ main (int argc, char **argv)
setup_server (server);
bus_connection_init ();
dbus_server_set_new_connection_function (server,
new_connection_callback,
NULL, NULL);
......
/* -*- mode: C; c-file-style: "gnu" -*- */
/* services.c Service management
*
* 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 "services.h"
#include "connection.h"
#include <dbus/dbus-hash.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-mempool.h>
struct BusService
{
char *name;
DBusList *owners;
};
static DBusHashTable *service_hash = NULL;
static DBusMemPool *service_pool = NULL;
BusService*
bus_service_lookup (const DBusString *service_name,
dbus_bool_t create_if_not_found)
{
const char *c_name;
BusService *service;
if (service_hash == NULL)
{
service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
NULL, NULL);
service_pool = _dbus_mem_pool_new (sizeof (BusService),
TRUE);
if (service_hash == NULL || service_pool == NULL)
{
if (service_hash)
{
_dbus_hash_table_unref (service_hash);
service_hash = NULL;
}
if (service_pool)
{
_dbus_mem_pool_free (service_pool);
service_pool = NULL;
}
return NULL;
}
}
_dbus_string_get_const_data (service_name, &c_name);
service = _dbus_hash_table_lookup_string (service_hash,
c_name);
if (service != NULL)
return service;
if (!create_if_not_found)
return NULL;
service = _dbus_mem_pool_alloc (service_pool);
if (service == NULL)
return NULL;
service->name = _dbus_strdup (c_name);
if (service->name == NULL)
{
_dbus_mem_pool_dealloc (service_pool, service);
return NULL;
}
if (!_dbus_hash_table_insert_string (service_hash,
service->name,
service))
{
dbus_free (service->name);
_dbus_mem_pool_dealloc (service_pool, service);
return NULL;
}
return service;
}
dbus_bool_t
bus_service_add_owner (BusService *service,
DBusConnection *owner)
{
if (!_dbus_list_append (&service->owners,
owner))
return FALSE;
if (!bus_connection_add_owned_service (owner, service))
{
_dbus_list_remove_last (&service->owners, owner);
return FALSE;
}
return TRUE;
}
void
bus_service_remove_owner (BusService *service,
DBusConnection *owner)
{
_dbus_list_remove_last (&service->owners, owner);
bus_connection_remove_owned_service (owner, service);
}
DBusConnection*
bus_service_get_primary_owner (BusService *service)
{
return _dbus_list_get_first (&service->owners);
}
const char*
bus_service_get_name (BusService *service)
{
return service->name;
}
void
bus_service_foreach (BusServiceForeachFunction function,
void *data)
{
DBusHashIter iter;
if (service_hash == NULL)
return;
_dbus_hash_iter_init (service_hash, &iter);
while (_dbus_hash_iter_next (&iter))
{
BusService *service = _dbus_hash_iter_get_value (&iter);
(* function) (service, data);
}
}
/* -*- mode: C; c-file-style: "gnu" -*- */
/* services.h Service management
*
* 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
*
*/
#ifndef BUS_SERVICES_H
#define BUS_SERVICES_H
#include <dbus/dbus.h>
#include <dbus/dbus-string.h>
/* Each service can have multiple owners; one owner is the "real
* owner" and the others are queued up. For example, if I have
* multiple text editors open, one might own the TextEditor service;
* if I close that one, the next in line will become the owner of it.
*/
typedef struct BusService BusService;
typedef void (* BusServiceForeachFunction) (BusService *service,
void *data);
BusService* bus_service_lookup (const DBusString *service_name,
dbus_bool_t create_if_not_found);
dbus_bool_t bus_service_add_owner (BusService *service,
DBusConnection *owner);
void bus_service_remove_owner (BusService *service,
DBusConnection *owner);
DBusConnection* bus_service_get_primary_owner (BusService *service);
const char* bus_service_get_name (BusService *service);
void bus_service_foreach (BusServiceForeachFunction function,
void *data);
#endif /* BUS_SERVICES_H */
......@@ -29,6 +29,7 @@
#include "dbus-list.h"
#include "dbus-hash.h"
#include "dbus-message-internal.h"
#include "dbus-threads.h"
/**
* @defgroup DBusConnection DBusConnection
......@@ -57,6 +58,15 @@
* @{
*/
/** Opaque typedef for DBusDataSlot */
typedef struct DBusDataSlot DBusDataSlot;
/** DBusDataSlot is used to store application data on the connection */
struct DBusDataSlot
{
void *data; /**< The application data */
DBusFreeFunction free_data_func; /**< Free the application data */
};
/**
* Implementation details of DBusConnection. All fields are private.
*/
......@@ -80,8 +90,12 @@ struct DBusConnection
DBusList *filter_list; /**< List of filters. */
int filters_serial; /**< Increments when the list of filters is changed. */
int handlers_serial; /**< Increments when the handler table is changed. */
DBusDataSlot *data_slots; /**< Data slots */
int n_slots; /**< Slots allocated so far. */
};
static void _dbus_connection_free_data_slots (DBusConnection *connection);
/**
* Adds a message to the incoming message queue, returning #FALSE
* if there's insufficient memory to queue the message.
......@@ -313,6 +327,9 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
connection->watches = watch_list;
connection->handler_table = handler_table;
connection->filter_list = NULL;
connection->data_slots = NULL;
connection->n_slots = 0;
_dbus_transport_ref (transport);
_dbus_transport_set_connection (transport, connection);
......@@ -449,13 +466,15 @@ dbus_connection_unref (DBusConnection *connection)
{
DBusHashIter iter;
DBusList *link;
/* free error data as a side effect */
dbus_connection_set_error_function (connection,
NULL, NULL, NULL);
_dbus_watch_list_free (connection->watches);
connection->watches = NULL;
_dbus_connection_free_data_slots (connection);
_dbus_hash_iter_init (connection->handler_table, &iter);
while (_dbus_hash_iter_next (&iter))
......@@ -1059,4 +1078,193 @@ dbus_connection_unregister_handler (DBusConnection *connection,
connection->handlers_serial += 1;
}
static int *allocated_slots = NULL;
static int n_allocated_slots = 0;
static int n_used_slots = 0;
static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
/**
* Allocates an integer ID to be used for storing application-specific
* data on any DBusConnection. The allocated ID may then be used
* with dbus_connection_set_data() and dbus_connection_get_data().