[PATCH v2 6/8] agent: Add multi agent support

Jukka Rissanen jukka.rissanen at linux.intel.com
Fri Sep 6 04:12:17 PDT 2013


User is able to register multiple agents to ConnMan. When
ConnMan needs to ask something from the user, it uses
the nearest agent available.
---
 src/agent.c | 454 ++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 303 insertions(+), 151 deletions(-)

diff --git a/src/agent.c b/src/agent.c
index 49825ed..81b3825 100644
--- a/src/agent.c
+++ b/src/agent.c
@@ -33,10 +33,23 @@
 
 #include "connman.h"
 
+#define agent_ref(agent) \
+	agent_ref_debug(agent, __FILE__, __LINE__, __func__)
+#define agent_unref(agent) \
+	agent_unref_debug(agent, __FILE__, __LINE__, __func__)
+
 static DBusConnection *connection = NULL;
-static guint agent_watch = 0;
-static gchar *agent_path = NULL;
-static gchar *agent_sender = NULL;
+static GHashTable *agent_hash = NULL;
+static struct connman_agent *default_agent = NULL;
+
+struct connman_agent {
+	int refcount;
+	char *owner;
+	char *path;
+	struct connman_agent_request *request;
+	GList *pending; /* pending and queued requests for this agent */
+	guint watch;
+};
 
 struct connman_agent_request {
 	void *user_context;
@@ -48,122 +61,141 @@ struct connman_agent_request {
 	struct connman_agent_driver *driver;
 };
 
-static GList *agent_queue = NULL;
-static struct connman_agent_request *agent_request = NULL;
 static GSList *driver_list = NULL;
 
 void *connman_agent_get_info(const char *dbus_sender, const char **sender,
 							const char **path)
 {
-	*sender = agent_sender;
-	*path = agent_path;
+	struct connman_agent *agent;
 
-	return NULL;
+	if (!dbus_sender)
+		agent = default_agent;
+	else {
+		agent = g_hash_table_lookup(agent_hash, dbus_sender);
+		if (!agent)
+			agent = default_agent;
+	}
+
+	if (agent) {
+		if (sender)
+			*sender = agent->owner;
+		if (path)
+			*path = agent->path;
+	} else {
+		if (sender)
+			*sender = NULL;
+		if (path)
+			*path = NULL;
+	}
+
+	return agent;
 }
 
-static void agent_data_free(struct connman_agent_request *data)
+static void agent_request_free(struct connman_agent_request *request)
 {
-	if (!data)
+	if (!request)
 		return;
-	if (data->user_context) {
-		if (data->driver && data->driver->context_unref)
-			data->driver->context_unref(data->user_context);
+
+	if (request->user_context) {
+		if (request->driver && request->driver->context_unref)
+			request->driver->context_unref(request->user_context);
+	}
+
+	if (request->msg)
+		dbus_message_unref(request->msg);
+
+	if (request->call) {
+		dbus_pending_call_cancel(request->call);
+		dbus_pending_call_unref(request->call);
 	}
-	if (data->msg)
-		dbus_message_unref(data->msg);
-	if (data->call)
-		dbus_pending_call_cancel(data->call);
 
-	g_free(data);
+	g_free(request);
 }
 
 static void agent_receive_message(DBusPendingCall *call, void *user_data);
 
-static int agent_send_next_request(void)
+static int agent_send_next_request(struct connman_agent *agent)
 {
-	if (agent_request)
+	if (agent->request)
 		return -EBUSY;
 
-	if (!agent_queue)
+	if (!agent->pending)
 		return 0;
 
-	agent_request = agent_queue->data;
-	agent_queue = g_list_remove(agent_queue, agent_request);
+	agent->request = agent->pending->data;
+	agent->pending = g_list_remove(agent->pending, agent->request);
 
-	if (!dbus_connection_send_with_reply(connection, agent_request->msg,
-						&agent_request->call,
-						agent_request->timeout))
+	if (!dbus_connection_send_with_reply(connection, agent->request->msg,
+						&agent->request->call,
+						agent->request->timeout))
 		goto fail;
 
-	if (!agent_request->call)
+	if (!agent->request->call)
 		goto fail;
 
-	if (!dbus_pending_call_set_notify(agent_request->call,
+	if (!dbus_pending_call_set_notify(agent->request->call,
 						agent_receive_message,
-						agent_request, NULL))
+						agent, NULL))
 		goto fail;
 
-	dbus_message_unref(agent_request->msg);
-	agent_request->msg = NULL;
+	dbus_message_unref(agent->request->msg);
+	agent->request->msg = NULL;
 	return 0;
 
 fail:
-	agent_data_free(agent_request);
-	agent_request = NULL;
+	agent->request->callback(agent->request->msg,
+						agent->request->user_data);
+	agent_request_free(agent->request);
+	agent->request = NULL;
 	return -ESRCH;
 }
 
-static int agent_send_cancel(struct connman_agent_request *agent)
+static int send_cancel_request(struct connman_agent *agent,
+			struct connman_agent_request *request)
 {
 	DBusMessage *message;
 
-	if (!agent_sender || !agent || !agent->driver)
-		return 0;
+	DBG("send cancel req to %s %s", agent->owner, agent->path);
 
-	message = dbus_message_new_method_call(agent_sender, agent_path,
-			agent->driver->interface, "Cancel");
-	if (message) {
-		dbus_message_set_no_reply(message, TRUE);
-		g_dbus_send_message(connection, message);
-		return 0;
+	message = dbus_message_new_method_call(agent->owner,
+					agent->path,
+					request->driver->interface,
+					"Cancel");
+	if (!message) {
+		connman_error("Couldn't allocate D-Bus message");
+		return -ENOMEM;
 	}
 
-	connman_warn("Failed to send Cancel message to agent");
-	return -ESRCH;
+	g_dbus_send_message(connection, message);
+	return 0;
 }
 
 static void agent_receive_message(DBusPendingCall *call, void *user_data)
 {
-	struct connman_agent_request *queue_data = user_data;
+	struct connman_agent *agent = user_data;
 	DBusMessage *reply;
 	int err;
 
-	DBG("waiting for %p received %p", agent_request, queue_data);
-
-	if (agent_request != queue_data) {
-		connman_error("Agent callback expected %p got %p",
-				agent_request, queue_data);
-		return;
-	}
+	DBG("agent %p req %p", agent, agent->request);
 
 	reply = dbus_pending_call_steal_reply(call);
 	dbus_pending_call_unref(call);
-	queue_data->call = NULL;
+	agent->request->call = NULL;
 
 	if (dbus_message_is_error(reply,
 			"org.freedesktop.DBus.Error.Timeout") ||
 			dbus_message_is_error(reply,
 			"org.freedesktop.DBus.Error.TimedOut")) {
-		agent_send_cancel(queue_data->user_context);
+		send_cancel_request(agent, agent->request);
 	}
 
-	queue_data->callback(reply, queue_data->user_data);
+	agent->request->callback(reply, agent->request->user_data);
 	dbus_message_unref(reply);
 
-	agent_data_free(queue_data);
-	agent_request = NULL;
+	agent_request_free(agent->request);
+	agent->request = NULL;
 
-	err = agent_send_next_request();
+	err = agent_send_next_request(agent);
 	if (err < 0)
 		DBG("send next request failed (%s/%d)", strerror(-err), -err);
 }
@@ -180,6 +212,7 @@ int connman_agent_queue_message(void *user_context,
 {
 	struct connman_agent_request *queue_data;
 	struct connman_agent_driver *driver;
+	struct connman_agent *agent = agent_data;
 	int err;
 
 	if (!user_context || !callback)
@@ -202,103 +235,108 @@ int connman_agent_queue_message(void *user_context,
 	queue_data->timeout = timeout;
 	queue_data->callback = callback;
 	queue_data->user_data = user_data;
-	agent_queue = g_list_append(agent_queue, queue_data);
+	agent->pending = g_list_append(agent->pending, queue_data);
 
-	err = agent_send_next_request();
+	err = agent_send_next_request(agent);
 	if (err < 0)
 		DBG("send next request failed (%s/%d)", strerror(-err), -err);
 
 	return err;
 }
 
-void connman_agent_cancel(void *user_context)
+static void set_default_agent(struct connman_agent *agent)
 {
-	GList *item, *next;
-	struct connman_agent_request *queued_req;
-	int err;
+	if (default_agent == agent)
+		return;
+
+	if (agent)
+		DBG("default agent set to %s %s", agent->owner, agent->path);
+	else
+		DBG("default agent cleared");
 
-	DBG("context %p", user_context);
+	default_agent = agent;
+}
 
-	item = agent_queue;
+static struct connman_agent *elect_default_agent(void)
+{
+	GHashTableIter iter;
+	gpointer key, value;
+	struct connman_agent *agent = NULL;
 
-	while (item) {
-		next = g_list_next(item);
-		queued_req = item->data;
+	g_hash_table_iter_init(&iter, agent_hash);
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		agent = value;
 
-		if (queued_req->user_context == user_context ||
-							!user_context) {
-			agent_data_free(queued_req);
-			agent_queue = g_list_delete_link(agent_queue, item);
-		}
+		if (default_agent != agent)
+			break;
 
-		item = next;
+		agent = NULL;
 	}
 
-	if (!agent_request)
-		return;
+	return agent;
+}
 
-	if (agent_request->user_context != user_context &&
-						user_context)
-		return;
+static void agent_disconnect(DBusConnection *conn, void *user_data)
+{
+	struct connman_agent *agent = user_data;
 
-	agent_send_cancel(agent_request);
+	DBG("agent %s disconnected", agent->owner);
 
-	agent_data_free(agent_request);
-	agent_request = NULL;
+	if (agent->watch > 0) {
+		g_dbus_remove_watch(conn, agent->watch);
+		agent->watch = 0;
+	}
 
-	err = agent_send_next_request();
-	if (err < 0)
-		DBG("send next request failed (%s/%d)", strerror(-err), -err);
+	g_hash_table_remove(agent_hash, agent->owner);
 }
 
-static void agent_free(void)
+static struct connman_agent *agent_ref_debug(struct connman_agent *agent,
+				const char *file, int line, const char *caller)
 {
-	if (agent_watch > 0)
-		g_dbus_remove_watch(connection, agent_watch);
-
-	agent_watch = 0;
+	DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount + 1,
+		file, line, caller);
 
-	g_free(agent_sender);
-	agent_sender = NULL;
+	__sync_fetch_and_add(&agent->refcount, 1);
 
-	g_free(agent_path);
-	agent_path = NULL;
-
-	connman_agent_cancel(NULL);
+	return agent;
 }
 
-static void agent_disconnect(DBusConnection *conn, void *data)
+static struct connman_agent *agent_create(const char *name, const char *path)
 {
-	DBG("data %p", data);
-	agent_free();
+	struct connman_agent *agent;
+
+	agent = g_new0(struct connman_agent, 1);
+
+	agent->owner = g_strdup(name);
+	agent->path = g_strdup(path);
+
+	agent->watch = g_dbus_add_disconnect_watch(connection,
+							name, agent_disconnect,
+							agent, NULL);
+
+	return agent_ref(agent);
 }
 
 int connman_agent_register(const char *sender, const char *path)
 {
+	struct connman_agent *agent;
+
 	DBG("sender %s path %s", sender, path);
-	if (agent_path)
+
+	agent = g_hash_table_lookup(agent_hash, sender);
+	if (agent)
 		return -EEXIST;
 
-	agent_sender = g_strdup(sender);
-	agent_path = g_strdup(path);
+	agent = agent_create(sender, path);
+	if (!agent)
+		return -EINVAL;
 
-	agent_watch = g_dbus_add_disconnect_watch(connection, sender,
-						agent_disconnect, NULL, NULL);
+	DBG("agent %s", agent->owner);
 
-	return 0;
-}
+	g_hash_table_replace(agent_hash, agent->owner, agent);
 
-int connman_agent_unregister(const char *sender, const char *path)
-{
-	DBG("sender %s path %s", sender, path);
-
-	if (!agent_path)
-		return -ESRCH;
-
-	if (agent_watch > 0)
-		g_dbus_remove_watch(connection, agent_watch);
-
-	agent_free();
+	if (!default_agent)
+		set_default_agent(agent);
 
 	return 0;
 }
@@ -336,13 +374,18 @@ int connman_agent_report_error(void *user_context, const char *path,
 	DBusMessage *message;
 	DBusMessageIter iter;
 	struct report_error_data *report_error;
+	struct connman_agent *agent;
 	int err;
 
-	if (!user_context || !agent_path || !error ||
-							!callback)
+	agent = connman_agent_get_info(dbus_sender, NULL, NULL);
+
+	DBG("agent %p sender %s context %p path %s", agent,
+		dbus_sender, user_context, agent ? agent->path : "-");
+
+	if (!user_context || !agent || !agent->path || !error || !callback)
 		return -ESRCH;
 
-	message = dbus_message_new_method_call(agent_sender, agent_path,
+	message = dbus_message_new_method_call(agent->owner, agent->path,
 					CONNMAN_AGENT_INTERFACE,
 					"ReportError");
 	if (!message)
@@ -368,7 +411,7 @@ int connman_agent_report_error(void *user_context, const char *path,
 	err = connman_agent_queue_message(user_context, message,
 					connman_timeout_input_request(),
 					report_error_reply, report_error,
-					NULL);
+					agent);
 	if (err < 0 && err != -EBUSY) {
 		DBG("error %d sending error request", err);
 		g_free(report_error);
@@ -381,6 +424,23 @@ int connman_agent_report_error(void *user_context, const char *path,
 	return -EINPROGRESS;
 }
 
+static void agent_release(struct connman_agent *agent, const char *interface)
+{
+	DBusMessage *message;
+
+	DBG("release agent %s %s", agent->owner, agent->path);
+
+	message = dbus_message_new_method_call(agent->owner, agent->path,
+						interface, "Release");
+	if (message == NULL) {
+		connman_error("Couldn't allocate D-Bus message");
+		return;
+	}
+
+	dbus_message_set_no_reply(message, TRUE);
+	g_dbus_send_message(connection, message);
+}
+
 static gint compare_priority(gconstpointer a, gconstpointer b)
 {
 	const struct connman_agent_driver *driver1 = a;
@@ -407,6 +467,94 @@ int connman_agent_driver_register(struct connman_agent_driver *driver)
 	return 0;
 }
 
+static void release_driver(void)
+{
+	connman_agent_driver_unregister(get_driver());
+}
+
+static void cancel_all_requests(struct connman_agent *agent)
+{
+	GList *list;
+
+	DBG("request %p pending %p", agent->request, agent->pending);
+
+	if (agent->request) {
+		agent->pending = g_list_prepend(agent->pending,
+						agent->request);
+		agent->request = NULL;
+	}
+
+	for (list = agent->pending; list; list = list->next) {
+		struct connman_agent_request *request = list->data;
+
+		if (!request)
+			continue;
+
+		DBG("canceling %p", request);
+
+		request->callback(request->msg, request->user_data);
+
+		if (request->call)
+			send_cancel_request(agent, request);
+
+		agent_request_free(request);
+	}
+
+	g_list_free(agent->pending);
+	agent->pending = NULL;
+}
+
+void connman_agent_cancel(void *user_context)
+{
+	GHashTableIter iter;
+	gpointer key, value;
+	struct connman_agent *agent = NULL;
+	int err;
+
+	DBG("context %p", user_context);
+
+	g_hash_table_iter_init(&iter, agent_hash);
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		struct connman_agent *item = value;
+
+		if (item->request && item->request->user_context &&
+				item->request->user_context == user_context) {
+			agent = item;
+			break;
+		}
+	}
+
+	if (!agent)
+		return;
+
+	cancel_all_requests(agent);
+
+	err = agent_send_next_request(agent);
+	if (err < 0)
+		DBG("send next request failed (%s/%d)", strerror(-err), -err);
+}
+
+static void agent_unref_debug(struct connman_agent *agent,
+			const char *file, int line, const char *caller)
+{
+	DBG("%p ref %d by %s:%d:%s()", agent, agent->refcount - 1,
+		file, line, caller);
+
+	if (__sync_fetch_and_sub(&agent->refcount, 1) != 1)
+		return;
+
+	cancel_all_requests(agent);
+	g_list_free(agent->pending);
+
+	g_free(agent->owner);
+	g_free(agent->path);
+
+	if (agent == default_agent)
+		set_default_agent(elect_default_agent());
+
+	g_free(agent);
+}
+
 /**
  * connman_agent_driver_unregister:
  * @driver: Agent driver definition
@@ -422,42 +570,41 @@ void connman_agent_driver_unregister(struct connman_agent_driver *driver)
 
 	DBG("Unregistering driver %p name %s", driver, driver->name);
 
-	if (!agent_sender && !agent_path)
-		goto out;
-
 	for (list = driver_list; list; list = list->next) {
-		DBusMessage *message;
-
 		if (driver != list->data)
 			continue;
 
-		DBG("Sending release to %s path %s iface %s", agent_sender,
-			agent_path, driver->interface);
-
-		message = dbus_message_new_method_call(agent_sender, agent_path,
-				driver->interface, "Release");
-		if (message) {
-			dbus_message_set_no_reply(message, TRUE);
-			g_dbus_send_message(connection, message);
-		}
-
-		agent_free();
-
-		/*
-		 * ATM agent_free() unsets the agent_sender and agent_path
-		 * variables so we can unregister only once.
-		 * This needs proper fix later.
-		 */
+		g_hash_table_remove_all(agent_hash);
 		break;
 	}
 
-out:
 	driver_list = g_slist_remove(driver_list, driver);
 }
 
-static void release_all_agents(void)
+static void agent_destroy(gpointer data)
 {
-	connman_agent_driver_unregister(get_driver());
+	struct connman_agent *agent = data;
+
+	DBG("agent %s req %p", agent->owner, agent->request);
+
+	if (agent->watch > 0) {
+		g_dbus_remove_watch(connection, agent->watch);
+		agent->watch = 0;
+	}
+
+	agent_release(agent, get_driver()->interface);
+
+	agent_unref(agent);
+}
+
+int connman_agent_unregister(const char *sender, const char *path)
+{
+	DBG("sender %s path %s", sender, path);
+
+	if (!g_hash_table_remove(agent_hash, sender))
+		return -ESRCH;
+
+	return 0;
 }
 
 int __connman_agent_init(void)
@@ -466,7 +613,12 @@ int __connman_agent_init(void)
 
 	connection = connman_dbus_get_connection();
 	if (!connection)
-		return -1;
+		return -EINVAL;
+
+	agent_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+						NULL, agent_destroy);
+	if (!agent_hash)
+		return -ENOMEM;
 
 	return 0;
 }
@@ -478,10 +630,10 @@ void __connman_agent_cleanup(void)
 	if (!connection)
 		return;
 
-	if (agent_watch > 0)
-		g_dbus_remove_watch(connection, agent_watch);
+	set_default_agent(NULL);
+	g_hash_table_destroy(agent_hash);
 
-	release_all_agents();
+	release_driver();
 
 	dbus_connection_unref(connection);
 	connection = NULL;
-- 
1.7.11.7




More information about the connman mailing list