mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Get NTP time working on Android
This commit is contained in:
parent
f41ba67e15
commit
c9adccad4a
@ -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);
|
||||
|
@ -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<Date> {
|
||||
if (shouldSyncTime()) {
|
||||
const release = await fetchingTimeMutex.acquire();
|
||||
@ -38,7 +45,8 @@ export default async function():Promise<Date> {
|
||||
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;
|
||||
|
69
ReactNativeClient/lib/vendor/ntp-client.js
vendored
69
ReactNativeClient/lib/vendor/ntp-client.js
vendored
@ -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) {
|
||||
|
@ -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')})`);
|
||||
|
Loading…
Reference in New Issue
Block a user