From 6128e8dca48c3c5e39d2a4f8d4770d0e6faaec3b Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 3 Jun 2013 16:06:15 +0100 Subject: [PATCH] Stop using handle qdata The handles now live as long as the connection, so there's no point in trying to bind to their lifetime; just keep a hash table. --- rakia/base-connection.c | 52 ++++++++++++++++++++++++++++++++++++++++++ rakia/base-connection.h | 3 +++ rakia/connection-aliasing.c | 5 ++-- rakia/handles.c | 46 ++----------------------------------- rakia/handles.h | 1 - src/sip-connection-helpers.c | 3 ++- src/sip-connection.c | 7 +++++- 7 files changed, 67 insertions(+), 50 deletions(-) diff --git a/rakia/base-connection.c b/rakia/base-connection.c index 1c68f02..5de4f18 100644 --- a/rakia/base-connection.c +++ b/rakia/base-connection.c @@ -31,11 +31,15 @@ #include #include +#include #include +#include struct _RakiaBaseConnectionPrivate { su_root_t *sofia_root; + /* guint: handle => owned url_t */ + GHashTable *uris; unsigned dispose_has_run:1; unsigned :0; }; @@ -67,6 +71,8 @@ rakia_base_connection_init (RakiaBaseConnection *self) self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, RAKIA_TYPE_BASE_CONNECTION, RakiaBaseConnectionPrivate); + self->priv->uris = g_hash_table_new_full (NULL, NULL, NULL, free); + tp_contacts_mixin_init (object, G_STRUCT_OFFSET (RakiaBaseConnection, contacts_mixin)); @@ -96,6 +102,10 @@ rakia_base_connection_dispose(GObject *object) static void rakia_base_connection_finalize(GObject *object) { + RakiaBaseConnection *self = RAKIA_BASE_CONNECTION (object); + + tp_clear_pointer (&self->priv->uris, g_hash_table_unref); + G_OBJECT_CLASS(rakia_base_connection_parent_class)->finalize(object); } @@ -141,12 +151,22 @@ rakia_base_connection_get_property (GObject *object, } } +static void +rakia_base_connection_disconnected (TpBaseConnection *base) +{ + RakiaBaseConnection *self = RAKIA_BASE_CONNECTION (base); + + /* handles are no longer meaningful */ + g_hash_table_remove_all (self->priv->uris); +} + /* -------------------------------------------------------------------------- */ static void rakia_base_connection_class_init (RakiaBaseConnectionClass *klass) { GObjectClass *object_class = (GObjectClass *) klass; + TpBaseConnectionClass *conn_class = (TpBaseConnectionClass *) klass; g_type_class_add_private (klass, sizeof (RakiaBaseConnectionPrivate)); @@ -155,6 +175,7 @@ rakia_base_connection_class_init (RakiaBaseConnectionClass *klass) object_class->finalize = rakia_base_connection_finalize; object_class->get_property = rakia_base_connection_get_property; object_class->set_property = rakia_base_connection_set_property; + conn_class->disconnected = rakia_base_connection_disconnected; g_object_class_install_property (object_class, PROP_SOFIA_ROOT, @@ -203,3 +224,34 @@ rakia_base_connection_save_event (RakiaBaseConnection *self, nua_save_event (nua, ret_saved); } + +const url_t* +rakia_base_connection_handle_to_uri (RakiaBaseConnection *self, + TpHandle handle) +{ + TpHandleRepoIface *repo; + url_t *url; + GError *error = NULL; + + repo = tp_base_connection_get_handles (TP_BASE_CONNECTION (self), + TP_HANDLE_TYPE_CONTACT); + + if (!tp_handle_is_valid (repo, handle, &error)) + { + DEBUG ("invalid handle %u: %s", handle, error->message); + g_error_free (error); + return NULL; + } + + url = g_hash_table_lookup (self->priv->uris, GUINT_TO_POINTER (handle)); + + if (url == NULL) + { + url = url_make (NULL, tp_handle_inspect (repo, handle)); + + g_hash_table_replace (self->priv->uris, GUINT_TO_POINTER (handle), + url); + } + + return url; +} diff --git a/rakia/base-connection.h b/rakia/base-connection.h index 0a2a047..2d2d526 100644 --- a/rakia/base-connection.h +++ b/rakia/base-connection.h @@ -87,6 +87,9 @@ void rakia_base_connection_sofia_callback (nua_event_t event, sip_t const *sip, tagi_t tags[]); +const url_t *rakia_base_connection_handle_to_uri ( + RakiaBaseConnection *self, TpHandle handle); + G_END_DECLS #endif /* #ifndef __RAKIA_BASE_CONNECTION_H__*/ diff --git a/rakia/connection-aliasing.c b/rakia/connection-aliasing.c index 87c696d..aec89fa 100644 --- a/rakia/connection-aliasing.c +++ b/rakia/connection-aliasing.c @@ -111,9 +111,8 @@ conn_get_default_alias (TpBaseConnection *base, const url_t *url; gchar *alias = NULL; - /* TODO: create our custom handle repo to be able to get the URL off it. - * Then we can reuse the contact_handles parameter */ - url = rakia_handle_inspect_uri (base, handle); + url = rakia_base_connection_handle_to_uri (RAKIA_BASE_CONNECTION (base), + handle); switch (url->url_type) { diff --git a/rakia/handles.c b/rakia/handles.c index baa4812..1295ca7 100644 --- a/rakia/handles.c +++ b/rakia/handles.c @@ -28,49 +28,6 @@ #define DEBUG_FLAG RAKIA_DEBUG_CONNECTION #include "rakia/debug.h" -static GQuark -rakia_handle_url_quark (void) -{ - static GQuark quark = 0; - - if (G_UNLIKELY (quark == 0)) - quark = g_quark_from_static_string ("rakia-handle-url"); - - return quark; -} - -const url_t* -rakia_handle_inspect_uri (TpBaseConnection *base, - TpHandle handle) -{ - TpHandleRepoIface *repo; - GQuark url_quark; - url_t *url; - GError *error; - - repo = tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT); - - if (!tp_handle_is_valid (repo, handle, &error)) - { - DEBUG("invalid handle %u: %s", handle, error->message); - g_error_free (error); - return NULL; - } - - url_quark = rakia_handle_url_quark (); - - url = tp_handle_get_qdata (repo, handle, url_quark); - - if (url == NULL) - { - url = url_make (NULL, tp_handle_inspect (repo, handle)); - - tp_handle_set_qdata (repo, handle, url_quark, url, free); - } - - return url; -} - TpHandle rakia_handle_ensure (TpBaseConnection *conn, url_t const *uri, @@ -91,7 +48,8 @@ rakia_handle_ensure (TpBaseConnection *conn, su_free (NULL, str); - /* TODO: set qdata for the alias */ + /* TODO: store the alias somehow (probably by moving this code + * into RakiaBaseConnection and using a hash table in priv) */ return handle; } diff --git a/rakia/handles.h b/rakia/handles.h index ac9d2e7..da71e2b 100644 --- a/rakia/handles.h +++ b/rakia/handles.h @@ -29,7 +29,6 @@ G_BEGIN_DECLS TpHandle rakia_handle_ensure (TpBaseConnection *, url_t const *, char const *); TpHandle rakia_handle_by_requestor (TpBaseConnection *, sip_t const *sip); char const *rakia_handle_inspect (TpBaseConnection *, TpHandle handle); -const url_t *rakia_handle_inspect_uri (TpBaseConnection *, TpHandle handle); gchar * rakia_handle_normalize (TpHandleRepoIface *repo, const gchar *sipuri, diff --git a/src/sip-connection-helpers.c b/src/sip-connection-helpers.c index e1294eb..74524f7 100644 --- a/src/sip-connection-helpers.c +++ b/src/sip-connection-helpers.c @@ -64,7 +64,8 @@ priv_sip_to_url_make (RakiaConnection *conn, { const url_t *url; - url = rakia_handle_inspect_uri (TP_BASE_CONNECTION (conn), contact); + url = rakia_base_connection_handle_to_uri ( + (RakiaBaseConnection *) conn, contact); return sip_to_create (home, (const url_string_t *) url); } diff --git a/src/sip-connection.c b/src/sip-connection.c index 02c9c3e..91b0e8e 100644 --- a/src/sip-connection.c +++ b/src/sip-connection.c @@ -1006,6 +1006,7 @@ rakia_connection_start_connecting (TpBaseConnection *base, GError **error) { RakiaConnection *self = RAKIA_CONNECTION (base); + RakiaBaseConnection *rbase = RAKIA_BASE_CONNECTION (self); RakiaConnectionPrivate *priv = RAKIA_CONNECTION_GET_PRIVATE (self); TpHandleRepoIface *contact_repo; const gchar *sip_address; @@ -1031,7 +1032,8 @@ rakia_connection_start_connecting (TpBaseConnection *base, DEBUG("self_handle = %d, sip_address = %s", base->self_handle, sip_address); - priv->account_url = rakia_handle_inspect_uri (base, base->self_handle); + priv->account_url = rakia_base_connection_handle_to_uri (rbase, + tp_base_connection_get_self_handle (base)); if (priv->account_url == NULL) { g_set_error (error, TP_ERROR, TP_ERROR_NOT_AVAILABLE, @@ -1130,4 +1132,7 @@ rakia_connection_disconnected (TpBaseConnection *base) nua_handle_unref (priv->register_op); priv->register_op = NULL; } + + /* we know that RakiaBaseConnection does implement this */ + TP_BASE_CONNECTION_CLASS (rakia_connection_parent_class)->disconnected (base); } -- 1.7.10.4