From 96315b3a890cc9ff219c3e6ee9a23dce848a7676 Mon Sep 17 00:00:00 2001 From: Amarnath Valluri Date: Fri, 1 Nov 2013 13:03:49 +0200 Subject: [PATCH] lib: Fix to handling duplicate local ports. messageport_register_local_port() now returns the Id of the port by updating message_cb, in case if its already registered with the same port_name. --- lib/msgport-manager.c | 34 ++++++++++++++++++++++++++++++++++ lib/msgport-service.c | 8 ++++++++ lib/msgport-service.h | 3 +++ 3 files changed, 45 insertions(+) diff --git a/lib/msgport-manager.c b/lib/msgport-manager.c index ff2b905..47ccddc 100644 --- a/lib/msgport-manager.c +++ b/lib/msgport-manager.c @@ -186,16 +186,50 @@ _create_and_cache_service (MsgPortManager *manager, gchar *object_path, messagep return MESSAGEPORT_ERROR_NONE; } +typedef struct { + const gchar *name; + gboolean is_trusted; +} FindServiceData ; + +static gboolean +_find_service (gpointer key, gpointer value, gpointer data) +{ + FindServiceData *service_data = (FindServiceData*)data; + MsgPortService *service = (MsgPortService *)value; + + return g_strcmp0 (msgport_service_name (service), service_data->name) == 0 + && msgport_service_is_trusted (service) == service_data->is_trusted; +} + + messageport_error_e msgport_manager_register_service (MsgPortManager *manager, const gchar *port_name, gboolean is_trusted, messageport_message_cb message_cb, int *service_id) { GError *error = NULL; gchar *object_path = NULL; + FindServiceData service_data; + MsgPortService *service = NULL; g_return_val_if_fail (manager && MSGPORT_IS_MANAGER (manager), MESSAGEPORT_ERROR_IO_ERROR); g_return_val_if_fail (manager->proxy, MESSAGEPORT_ERROR_IO_ERROR); g_return_val_if_fail (service_id && port_name && message_cb, MESSAGEPORT_ERROR_INVALID_PARAMETER); + /* first check in cached services if found any */ + service_data.name = port_name; + service_data.is_trusted = is_trusted; + service = g_hash_table_find (manager->services, _find_service, &service_data); + + if (service) { + int id = msgport_service_id (service); + DBG ("Cached local port found for name '%s:%d' with ID : %d", port_name, is_trusted, id); + + /* update message handler */ + msgport_service_set_message_handler (service, message_cb); + *service_id = id; + + return MESSAGEPORT_ERROR_NONE; + } + msgport_dbus_glue_manager_call_register_service_sync (manager->proxy, port_name, is_trusted, &object_path, NULL, &error); diff --git a/lib/msgport-service.c b/lib/msgport-service.c index a2075d5..f9da082 100644 --- a/lib/msgport-service.c +++ b/lib/msgport-service.c @@ -141,6 +141,14 @@ msgport_service_is_trusted (MsgPortService *service) return msgport_dbus_glue_service_get_is_trusted (service->proxy); } +void +msgport_service_set_message_handler (MsgPortService *service, messageport_message_cb handler) +{ + g_return_if_fail (service && MSGPORT_IS_SERVICE (service)); + + service->client_cb = handler; +} + gboolean msgport_service_unregister (MsgPortService *service) { diff --git a/lib/msgport-service.h b/lib/msgport-service.h index c5b15b4..604ea1b 100644 --- a/lib/msgport-service.h +++ b/lib/msgport-service.h @@ -59,6 +59,9 @@ msgport_service_is_trusted (MsgPortService *service); guint msgport_service_id (MsgPortService *service); +void +msgport_service_set_message_handler (MsgPortService *service, messageport_message_cb handler); + gboolean msgport_service_unregister (MsgPortService *service); -- 2.7.4