diff --git a/ReactNativeClient/lib/BaseApplication.js b/ReactNativeClient/lib/BaseApplication.js index cc0bb2419..de4a9beb5 100644 --- a/ReactNativeClient/lib/BaseApplication.js +++ b/ReactNativeClient/lib/BaseApplication.js @@ -666,6 +666,7 @@ class BaseApplication { reg.dispatch = () => {}; BaseService.logger_ = this.logger_; + require('lib/ntpDate').setLogger(reg.logger()); this.dbLogger_.addTarget('file', { path: `${profileDir}/log-database.txt` }); this.dbLogger_.setLevel(initArgs.logLevel); diff --git a/ReactNativeClient/lib/ntpDate.ts b/ReactNativeClient/lib/ntpDate.ts index 1e2e2fb5e..554c4a613 100644 --- a/ReactNativeClient/lib/ntpDate.ts +++ b/ReactNativeClient/lib/ntpDate.ts @@ -1,8 +1,11 @@ const ntpClient = require('lib/vendor/ntp-client'); +const { Logger } = require('lib/logger.js'); const Mutex = require('async-mutex').Mutex; let nextSyncTime = 0; let timeOffset = 0; +let logger = new Logger(); + const fetchingTimeMutex = new Mutex(); const server = { @@ -27,6 +30,10 @@ function shouldSyncTime() { return !nextSyncTime || Date.now() > nextSyncTime; } +export function setLogger(v:any) { + logger = v; +} + export default async function():Promise { if (shouldSyncTime()) { const release = await fetchingTimeMutex.acquire(); @@ -38,7 +45,8 @@ export default async function():Promise { timeOffset = date.getTime() - Date.now(); } } catch (error) { - // Fallback to application time since + logger.warn('Could not get NTP time - falling back to device time:', error); + // Fallback to device time since // most of the time it's actually correct nextSyncTime = Date.now() + 20 * 1000; timeOffset = 0; diff --git a/ReactNativeClient/lib/vendor/ntp-client.js b/ReactNativeClient/lib/vendor/ntp-client.js index 2a1660c38..235d48d7b 100644 --- a/ReactNativeClient/lib/vendor/ntp-client.js +++ b/ReactNativeClient/lib/vendor/ntp-client.js @@ -11,6 +11,8 @@ // it has several bugs and is currently unmaintained // ---------------------------------------------------------------------------------------- +const Buffer = require('buffer').Buffer; + (function (exports) { "use strict"; @@ -89,47 +91,52 @@ errorFired = true; }); - client.send(ntpData, 0, ntpData.length, port, server, function (err) { - if (err) { - clearTimeout(timeout); - if (errorFired) { + // NOTE: To make it work in React Native (Android), a port need to be bound + // before calling client.send() + + // client.bind(5555, '0.0.0.0', function() { + client.send(ntpData, 0, ntpData.length, port, server, function (err) { + if (err) { + clearTimeout(timeout); + if (errorFired) { + return; + } + callback(err, null); + errorFired = true; + closeClient(client); return; } - callback(err, null); - errorFired = true; - closeClient(client); - return; - } - client.once('message', function (msg) { - clearTimeout(timeout); - closeClient(client); + client.once('message', function (msg) { + clearTimeout(timeout); + closeClient(client); - // Offset to get to the "Transmit Timestamp" field (time at which the reply - // departed the server for the client, in 64-bit timestamp format." - var offsetTransmitTime = 40, - intpart = 0, - fractpart = 0; + // Offset to get to the "Transmit Timestamp" field (time at which the reply + // departed the server for the client, in 64-bit timestamp format." + var offsetTransmitTime = 40, + intpart = 0, + fractpart = 0; - // Get the seconds part - for (var i = 0; i <= 3; i++) { - intpart = 256 * intpart + msg[offsetTransmitTime + i]; - } + // Get the seconds part + for (var i = 0; i <= 3; i++) { + intpart = 256 * intpart + msg[offsetTransmitTime + i]; + } - // Get the seconds fraction - for (i = 4; i <= 7; i++) { - fractpart = 256 * fractpart + msg[offsetTransmitTime + i]; - } + // Get the seconds fraction + for (i = 4; i <= 7; i++) { + fractpart = 256 * fractpart + msg[offsetTransmitTime + i]; + } - var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000); + var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000); - // **UTC** time - var date = new Date("Jan 01 1900 GMT"); - date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds); + // **UTC** time + var date = new Date("Jan 01 1900 GMT"); + date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds); - callback(null, date); + callback(null, date); + }); }); - }); + // }); }; exports.demo = function (argv) { diff --git a/ReactNativeClient/root.js b/ReactNativeClient/root.js index 3ebccdbfe..298252148 100644 --- a/ReactNativeClient/root.js +++ b/ReactNativeClient/root.js @@ -66,6 +66,9 @@ const WelcomeUtils = require('lib/WelcomeUtils'); const { themeStyle } = require('lib/components/global-style.js'); const { uuid } = require('lib/uuid.js'); +const ntpClient = require('lib/vendor/ntp-client'); +ntpClient.dgram = require('react-native-udp'); + const { loadKeychainServiceAndSettings } = require('lib/services/SettingUtils'); const KeychainServiceDriverMobile = require('lib/services/keychain/KeychainServiceDriver.mobile').default; @@ -401,6 +404,7 @@ async function initialize(dispatch, messageHandler) { reg.setShowErrorMessageBoxHandler((message) => { alert(message); }); BaseService.logger_ = mainLogger; + require('lib/ntpDate').setLogger(reg.logger()); reg.logger().info('===================================='); reg.logger().info(`Starting application ${Setting.value('appId')} (${Setting.value('env')})`);