ntp: Add timeserver switch logic
authorAlok Barsode <alok.barsode@linux.intel.com>
Fri, 3 Feb 2012 18:26:01 +0000 (20:26 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Sat, 4 Feb 2012 00:15:36 +0000 (01:15 +0100)
Add a timeout mechanism to switch to next system timeserver
if the current one does not respond or is a bogus one.

src/ntp.c
src/timeserver.c

index 4d3cd88..cbe5a3b 100644 (file)
--- a/src/ntp.c
+++ b/src/ntp.c
@@ -75,6 +75,7 @@ static int transmit_fd = 0;
 
 static char *timeserver = NULL;
 static gint poll_id = 0;
+static gint timeout_id = 0;
 
 static void send_packet(int fd, const char *server)
 {
@@ -109,6 +110,18 @@ static void send_packet(int fd, const char *server)
        }
 }
 
+static gboolean next_server(gpointer user_data)
+{
+       if (timeserver != NULL) {
+               g_free(timeserver);
+               timeserver = NULL;
+       }
+
+       __connman_timeserver_sync_next();
+
+       return FALSE;
+}
+
 static gboolean next_poll(gpointer user_data)
 {
        if (timeserver == NULL || transmit_fd == 0)
@@ -168,7 +181,11 @@ static void decode_msg(void *base, size_t len, struct timeval *tv)
 
        DBG("offset=%f delay=%f", offset, delay);
 
-       /* Timeserver has responded.
+       /* Remove the timeout, as timeserver has responded */
+       if (timeout_id > 0)
+               g_source_remove(timeout_id);
+
+       /*
         * Now poll the server every transmit_delay seconds
         * for time correction.
         */
@@ -341,6 +358,13 @@ int __connman_ntp_start(char *server)
 
        start_ntp(timeserver);
 
+       /*
+        * Add a fallback timeout , preferably longer, 16 sec here,
+        * to fallback on the next server.
+        */
+
+       timeout_id = g_timeout_add_seconds(16, next_server, NULL);
+
        return 0;
 }
 
@@ -351,6 +375,9 @@ void __connman_ntp_stop()
        if (poll_id > 0)
                g_source_remove(poll_id);
 
+       if (timeout_id > 0)
+               g_source_remove(timeout_id);
+
        if (channel_watch > 0) {
                g_source_remove(channel_watch);
                channel_watch = 0;
index 2af173d..a361433 100644 (file)
@@ -230,6 +230,8 @@ static void resolv_result(GResolvResultStatus status, char **results, gpointer u
 
        DBG("status %d", status);
 
+       __sync_fetch_and_add(&count, 1);
+
        if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
                if (results != NULL) {
                        for (i = 0; results[i]; i++)
@@ -240,7 +242,7 @@ static void resolv_result(GResolvResultStatus status, char **results, gpointer u
                        return;
                }
        }
-       __sync_fetch_and_add(&count, 1);
+
        __connman_timeserver_sync_next();
 }