mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-26 18:58:21 +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 = () => {};
|
reg.dispatch = () => {};
|
||||||
|
|
||||||
BaseService.logger_ = this.logger_;
|
BaseService.logger_ = this.logger_;
|
||||||
|
require('lib/ntpDate').setLogger(reg.logger());
|
||||||
|
|
||||||
this.dbLogger_.addTarget('file', { path: `${profileDir}/log-database.txt` });
|
this.dbLogger_.addTarget('file', { path: `${profileDir}/log-database.txt` });
|
||||||
this.dbLogger_.setLevel(initArgs.logLevel);
|
this.dbLogger_.setLevel(initArgs.logLevel);
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
const ntpClient = require('lib/vendor/ntp-client');
|
const ntpClient = require('lib/vendor/ntp-client');
|
||||||
|
const { Logger } = require('lib/logger.js');
|
||||||
const Mutex = require('async-mutex').Mutex;
|
const Mutex = require('async-mutex').Mutex;
|
||||||
|
|
||||||
let nextSyncTime = 0;
|
let nextSyncTime = 0;
|
||||||
let timeOffset = 0;
|
let timeOffset = 0;
|
||||||
|
let logger = new Logger();
|
||||||
|
|
||||||
const fetchingTimeMutex = new Mutex();
|
const fetchingTimeMutex = new Mutex();
|
||||||
|
|
||||||
const server = {
|
const server = {
|
||||||
@ -27,6 +30,10 @@ function shouldSyncTime() {
|
|||||||
return !nextSyncTime || Date.now() > nextSyncTime;
|
return !nextSyncTime || Date.now() > nextSyncTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setLogger(v:any) {
|
||||||
|
logger = v;
|
||||||
|
}
|
||||||
|
|
||||||
export default async function():Promise<Date> {
|
export default async function():Promise<Date> {
|
||||||
if (shouldSyncTime()) {
|
if (shouldSyncTime()) {
|
||||||
const release = await fetchingTimeMutex.acquire();
|
const release = await fetchingTimeMutex.acquire();
|
||||||
@ -38,7 +45,8 @@ export default async function():Promise<Date> {
|
|||||||
timeOffset = date.getTime() - Date.now();
|
timeOffset = date.getTime() - Date.now();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} 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
|
// most of the time it's actually correct
|
||||||
nextSyncTime = Date.now() + 20 * 1000;
|
nextSyncTime = Date.now() + 20 * 1000;
|
||||||
timeOffset = 0;
|
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
|
// it has several bugs and is currently unmaintained
|
||||||
// ----------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const Buffer = require('buffer').Buffer;
|
||||||
|
|
||||||
(function (exports) {
|
(function (exports) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
@ -89,47 +91,52 @@
|
|||||||
errorFired = true;
|
errorFired = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
client.send(ntpData, 0, ntpData.length, port, server, function (err) {
|
// NOTE: To make it work in React Native (Android), a port need to be bound
|
||||||
if (err) {
|
// before calling client.send()
|
||||||
clearTimeout(timeout);
|
|
||||||
if (errorFired) {
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
callback(err, null);
|
|
||||||
errorFired = true;
|
|
||||||
closeClient(client);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.once('message', function (msg) {
|
client.once('message', function (msg) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
closeClient(client);
|
closeClient(client);
|
||||||
|
|
||||||
// Offset to get to the "Transmit Timestamp" field (time at which the reply
|
// Offset to get to the "Transmit Timestamp" field (time at which the reply
|
||||||
// departed the server for the client, in 64-bit timestamp format."
|
// departed the server for the client, in 64-bit timestamp format."
|
||||||
var offsetTransmitTime = 40,
|
var offsetTransmitTime = 40,
|
||||||
intpart = 0,
|
intpart = 0,
|
||||||
fractpart = 0;
|
fractpart = 0;
|
||||||
|
|
||||||
// Get the seconds part
|
// Get the seconds part
|
||||||
for (var i = 0; i <= 3; i++) {
|
for (var i = 0; i <= 3; i++) {
|
||||||
intpart = 256 * intpart + msg[offsetTransmitTime + i];
|
intpart = 256 * intpart + msg[offsetTransmitTime + i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the seconds fraction
|
// Get the seconds fraction
|
||||||
for (i = 4; i <= 7; i++) {
|
for (i = 4; i <= 7; i++) {
|
||||||
fractpart = 256 * fractpart + msg[offsetTransmitTime + i];
|
fractpart = 256 * fractpart + msg[offsetTransmitTime + i];
|
||||||
}
|
}
|
||||||
|
|
||||||
var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000);
|
var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000);
|
||||||
|
|
||||||
// **UTC** time
|
// **UTC** time
|
||||||
var date = new Date("Jan 01 1900 GMT");
|
var date = new Date("Jan 01 1900 GMT");
|
||||||
date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds);
|
date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds);
|
||||||
|
|
||||||
callback(null, date);
|
callback(null, date);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
// });
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.demo = function (argv) {
|
exports.demo = function (argv) {
|
||||||
|
@ -66,6 +66,9 @@ const WelcomeUtils = require('lib/WelcomeUtils');
|
|||||||
const { themeStyle } = require('lib/components/global-style.js');
|
const { themeStyle } = require('lib/components/global-style.js');
|
||||||
const { uuid } = require('lib/uuid.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 { loadKeychainServiceAndSettings } = require('lib/services/SettingUtils');
|
||||||
const KeychainServiceDriverMobile = require('lib/services/keychain/KeychainServiceDriver.mobile').default;
|
const KeychainServiceDriverMobile = require('lib/services/keychain/KeychainServiceDriver.mobile').default;
|
||||||
|
|
||||||
@ -401,6 +404,7 @@ async function initialize(dispatch, messageHandler) {
|
|||||||
reg.setShowErrorMessageBoxHandler((message) => { alert(message); });
|
reg.setShowErrorMessageBoxHandler((message) => { alert(message); });
|
||||||
|
|
||||||
BaseService.logger_ = mainLogger;
|
BaseService.logger_ = mainLogger;
|
||||||
|
require('lib/ntpDate').setLogger(reg.logger());
|
||||||
|
|
||||||
reg.logger().info('====================================');
|
reg.logger().info('====================================');
|
||||||
reg.logger().info(`Starting application ${Setting.value('appId')} (${Setting.value('env')})`);
|
reg.logger().info(`Starting application ${Setting.value('appId')} (${Setting.value('env')})`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user