instacart / truetime-android

Android NTP time library. Get the true current time impervious to device clock time changes
https://tech.instacart.com/truetime/
Apache License 2.0
1.41k stars 194 forks source link

How can use list of NTP servers? #123

Closed junny-solano closed 4 years ago

junny-solano commented 4 years ago

Hi, is it possible to use a list of NTP servers? I try this, but not work.

private void initRxTrueTime() {

    DisposableSingleObserver<Date> disposable = TrueTimeRx.build()
            .withConnectionTimeout(60)
            .withRetryCount(2)
            .withSharedPreferencesCache(this)
            .withLoggingEnabled(true)
            .initializeRx("time.google.com, 0.us.pool.ntp.org")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableSingleObserver<Date>() {
                @Override
                public void onSuccess(Date date) {
                    Log.d("Pruebas", "Success initialized TrueTime :" + date.toString());
                }

                @Override
                public void onError(Throwable e) {
                    Log.d("Pruebas", "something went wrong when trying to initializeRx TrueTime" + e);
                }
            });
}

Return this Exception:

something went wrong when trying to initializeRx TrueTimejava.net.UnknownHostException: Unable to resolve host "time.google.com, 0.us.pool.ntp.org": No address associated with hostname

junny-solano commented 4 years ago

This is my little solution, I don't know if it is the most efficient and best solution but it worked, I hope it helps.

**Note: I use retrolambda.

private static List getNtpServers(){ return Arrays.asList( "time.google.com", "time.apple.com", "time1.google.com", "time2.google.com", "time3.google.com", "time4.google.com", "0.us.pool.ntp.org", "1.us.pool.ntp.org", "2.us.pool.ntp.org", "3.us.pool.ntp.org"

    );
}

private void initNtpServer() { Observable .just(getNtpServers()) .concatMap((Function<List, Observable>) Observable::fromIterable) .concatMapSingle((Function<String, Single>) this::initTrueTime) .takeWhile(init -> init.equals(false)) .subscribeOn(Schedulers.io()) .subscribe(); }

private Single<Boolean> initTrueTime(String ntpServer){
    return TrueTimeRx
            .build()
            .withConnectionTimeout(30000)
            .withRetryCount(5)
            .withSharedPreferencesCache(this)
            .withLoggingEnabled(true)
            .initializeRx(ntpServer)
            .ignoreElement()
            .toSingleDefault(true)
            .onErrorReturnItem(false);
}