From f97c14e2606c64df0ae23c81a8a4066d27367091 Mon Sep 17 00:00:00 2001 From: Alok Barsode Date: Fri, 3 Feb 2012 20:26:01 +0200 Subject: ntp: Add timeserver switch logic 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 | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/ntp.c') diff --git a/src/ntp.c b/src/ntp.c index 4d3cd888..cbe5a3be 100644 --- 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; -- cgit v1.2.3