...
 
Commits (2)
  • Zeeshan Ali's avatar
    nmea-source: Ensure 1st service remains 1st in list · 4476d3c5
    Zeeshan Ali authored
    Currently if a second service is discovered that has the same accuracy
    as the first one in the list, g_list_insert_sorted() can (and in
    practice does) insert it in the beginning since our comparison function
    declares them both equal. Because of this, we end up switching to the
    new service even if its accuracy level is the same as the one we are
    already using.
    
    Let's solve this by also taking time of discovery into account in our
    comparison function.
    4476d3c5
  • Zeeshan Ali's avatar
    tmp · 158a6f20
    Zeeshan Ali authored
    158a6f20
......@@ -68,6 +68,7 @@ struct AvahiServiceInfo {
char *host_name;
guint16 port;
GClueAccuracyLevel accuracy;
guint64 timestamp;
};
static void
......@@ -86,12 +87,16 @@ avahi_service_new (const char *identifier,
guint16 port,
GClueAccuracyLevel accuracy)
{
GTimeVal tv;
AvahiServiceInfo *service = g_slice_new0 (AvahiServiceInfo);
service->identifier = g_strdup (identifier);
service->host_name = g_strdup (host_name);
service->port = port;
service->accuracy = accuracy;
g_get_current_time (&tv);
service->timestamp = tv.tv_sec;
return service;
}
......@@ -109,8 +114,8 @@ compare_avahi_service_by_identifier (gconstpointer a,
}
static gint
compare_avahi_service_by_accuracy (gconstpointer a,
gconstpointer b)
compare_avahi_service_by_accuracy_n_time (gconstpointer a,
gconstpointer b)
{
AvahiServiceInfo *first, *second;
gint diff;
......@@ -120,8 +125,8 @@ compare_avahi_service_by_accuracy (gconstpointer a,
diff = second->accuracy - first->accuracy;
if (diff == 0)
return 0;
if (diff != 0)
return first->timestamp - second->timestamp;
return diff;
}
......@@ -148,6 +153,7 @@ reconnect_service (GClueNMEASource *source)
if (!reconnection_required (source))
return;
g_print ("reconnecting\n");
disconnect_from_service (source);
connect_to_service (source);
}
......@@ -155,10 +161,11 @@ reconnect_service (GClueNMEASource *source)
static void
refresh_accuracy_level (GClueNMEASource *source)
{
GClueLocationSource *location_source = GCLUE_LOCATION_SOURCE (source);
GClueAccuracyLevel new, existing;
existing = gclue_location_source_get_available_accuracy_level
(GCLUE_LOCATION_SOURCE (source));
(location_source);
if (source->priv->all_services != NULL) {
AvahiServiceInfo *service;
......@@ -231,7 +238,7 @@ CREATE_SERVICE:
source->priv->all_services = g_list_insert_sorted
(source->priv->all_services,
service,
compare_avahi_service_by_accuracy);
compare_avahi_service_by_accuracy_n_time);
refresh_accuracy_level (source);
reconnect_service (source);
......@@ -534,6 +541,7 @@ connect_to_service (GClueNMEASource *source)
priv->cancellable,
on_connection_to_location_server,
source);
g_print ("\nstarted connecting\n");
}
static void
......@@ -556,6 +564,7 @@ disconnect_from_service (GClueNMEASource *source)
g_clear_object (&priv->connection);
g_clear_object (&priv->client);
priv->active_service = NULL;
g_print ("\ndisconnected\n");
}
static void
......