From 20f77cc2119f389468f9ad836ca80e4322e0d60c Mon Sep 17 00:00:00 2001 From: Javier Fernandez Date: Mon, 23 Aug 2010 13:56:38 +0200 Subject: [PATCH] Implementation of a provider selection algorithm. The selection will be done among the providers meeting the user requirements. First, it checks for the availability of the provider. If it's available, it will compare such provider to the currently selected as the best one, using for that the 'gc_master_provider_compare' method. --- src/client.c | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/client.c b/src/client.c index d4650e8..e0183f5 100644 --- a/src/client.c +++ b/src/client.c @@ -327,11 +327,18 @@ gc_master_client_get_best_provider (GcMasterClient *client, GList **provider_list, GcInterfaceFlags iface) { + GcMasterClientPrivate *priv = GET_PRIVATE (client); + GcMasterProvider *best_provider = NULL; + GcInterfaceAccuracy *min_accuracy = NULL; GList *l = *provider_list; /* TODO: should maybe choose a acquiring provider if better ones are are not available */ g_debug ("client: choosing best provider"); + min_accuracy = g_new0(GcInterfaceAccuracy, 1); + min_accuracy->interface = iface; + min_accuracy->accuracy_level = priv->min_accuracy; + while (l) { GcMasterProvider *provider = l->data; @@ -348,20 +355,44 @@ gc_master_client_get_best_provider (GcMasterClient *client, continue; } /* provider did not need to be started */ - - /* TODO: currently returning even providers that are worse than priv->min_accuracy, - * if nothing else is available */ - if (gc_master_provider_get_status (provider) == GEOCLUE_STATUS_AVAILABLE) { - /* unsubscribe from all providers worse than this */ - gc_master_client_unsubscribe_providers (client, l->next, iface); - return provider; + + /* Avoid evaluating twice the same provider. */ + if (best_provider == provider) { + l = l->next; + continue; } - l = l->next; + + /* Check if provider is available. */ + if (!(gc_master_provider_get_status (provider) == GEOCLUE_STATUS_AVAILABLE)) { + goto continue_searching; + } + + /* First provider evaluated, so its the best. */ + if (best_provider == NULL) { + best_provider = provider; + l = l->next; + continue; + } + + /* Check if its the best provider. */ + if (0 > gc_master_provider_compare (provider, best_provider, min_accuracy)) { + gc_master_provider_unsubscribe (best_provider, client, iface); + best_provider = provider; + l = l->next; + continue; + } else { + goto continue_searching; + } + + continue_searching: + gc_master_provider_unsubscribe (provider, client, iface); + l = l->next; } - - /* no provider found */ - gc_master_client_unsubscribe_providers (client, *provider_list, iface); - return NULL; + + /* Free */ + g_free (min_accuracy); + + return best_provider; } static void -- 1.6.3.3