...
 
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 { ...@@ -68,6 +68,7 @@ struct AvahiServiceInfo {
char *host_name; char *host_name;
guint16 port; guint16 port;
GClueAccuracyLevel accuracy; GClueAccuracyLevel accuracy;
guint64 timestamp;
}; };
static void static void
...@@ -86,12 +87,16 @@ avahi_service_new (const char *identifier, ...@@ -86,12 +87,16 @@ avahi_service_new (const char *identifier,
guint16 port, guint16 port,
GClueAccuracyLevel accuracy) GClueAccuracyLevel accuracy)
{ {
GTimeVal tv;
AvahiServiceInfo *service = g_slice_new0 (AvahiServiceInfo); AvahiServiceInfo *service = g_slice_new0 (AvahiServiceInfo);
service->identifier = g_strdup (identifier); service->identifier = g_strdup (identifier);
service->host_name = g_strdup (host_name); service->host_name = g_strdup (host_name);
service->port = port; service->port = port;
service->accuracy = accuracy; service->accuracy = accuracy;
g_get_current_time (&tv);
service->timestamp = tv.tv_sec;
return service; return service;
} }
...@@ -109,8 +114,8 @@ compare_avahi_service_by_identifier (gconstpointer a, ...@@ -109,8 +114,8 @@ compare_avahi_service_by_identifier (gconstpointer a,
} }
static gint static gint
compare_avahi_service_by_accuracy (gconstpointer a, compare_avahi_service_by_accuracy_n_time (gconstpointer a,
gconstpointer b) gconstpointer b)
{ {
AvahiServiceInfo *first, *second; AvahiServiceInfo *first, *second;
gint diff; gint diff;
...@@ -120,8 +125,8 @@ compare_avahi_service_by_accuracy (gconstpointer a, ...@@ -120,8 +125,8 @@ compare_avahi_service_by_accuracy (gconstpointer a,
diff = second->accuracy - first->accuracy; diff = second->accuracy - first->accuracy;
if (diff == 0) if (diff != 0)
return 0; return first->timestamp - second->timestamp;
return diff; return diff;
} }
...@@ -148,6 +153,7 @@ reconnect_service (GClueNMEASource *source) ...@@ -148,6 +153,7 @@ reconnect_service (GClueNMEASource *source)
if (!reconnection_required (source)) if (!reconnection_required (source))
return; return;
g_print ("reconnecting\n");
disconnect_from_service (source); disconnect_from_service (source);
connect_to_service (source); connect_to_service (source);
} }
...@@ -155,10 +161,11 @@ reconnect_service (GClueNMEASource *source) ...@@ -155,10 +161,11 @@ reconnect_service (GClueNMEASource *source)
static void static void
refresh_accuracy_level (GClueNMEASource *source) refresh_accuracy_level (GClueNMEASource *source)
{ {
GClueLocationSource *location_source = GCLUE_LOCATION_SOURCE (source);
GClueAccuracyLevel new, existing; GClueAccuracyLevel new, existing;
existing = gclue_location_source_get_available_accuracy_level existing = gclue_location_source_get_available_accuracy_level
(GCLUE_LOCATION_SOURCE (source)); (location_source);
if (source->priv->all_services != NULL) { if (source->priv->all_services != NULL) {
AvahiServiceInfo *service; AvahiServiceInfo *service;
...@@ -231,7 +238,7 @@ CREATE_SERVICE: ...@@ -231,7 +238,7 @@ CREATE_SERVICE:
source->priv->all_services = g_list_insert_sorted source->priv->all_services = g_list_insert_sorted
(source->priv->all_services, (source->priv->all_services,
service, service,
compare_avahi_service_by_accuracy); compare_avahi_service_by_accuracy_n_time);
refresh_accuracy_level (source); refresh_accuracy_level (source);
reconnect_service (source); reconnect_service (source);
...@@ -534,6 +541,7 @@ connect_to_service (GClueNMEASource *source) ...@@ -534,6 +541,7 @@ connect_to_service (GClueNMEASource *source)
priv->cancellable, priv->cancellable,
on_connection_to_location_server, on_connection_to_location_server,
source); source);
g_print ("\nstarted connecting\n");
} }
static void static void
...@@ -556,6 +564,7 @@ disconnect_from_service (GClueNMEASource *source) ...@@ -556,6 +564,7 @@ disconnect_from_service (GClueNMEASource *source)
g_clear_object (&priv->connection); g_clear_object (&priv->connection);
g_clear_object (&priv->client); g_clear_object (&priv->client);
priv->active_service = NULL; priv->active_service = NULL;
g_print ("\ndisconnected\n");
} }
static void static void
......