lib: Fix to handling duplicate local ports.
authorAmarnath Valluri <amarnath.valluri@linux.intel.com>
Fri, 1 Nov 2013 11:03:49 +0000 (13:03 +0200)
committerAmarnath Valluri <amarnath.valluri@linux.intel.com>
Wed, 20 Aug 2014 06:45:10 +0000 (09:45 +0300)
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
lib/msgport-service.c
lib/msgport-service.h

index ff2b905..47ccddc 100644 (file)
@@ -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);
 
index a2075d5..f9da082 100644 (file)
@@ -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)
 {
index c5b15b4..604ea1b 100644 (file)
@@ -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);