Host switching

This commit is contained in:
2022-09-26 17:38:27 -04:00
parent 31ffadbae3
commit 1a08acdc7c
6 changed files with 138 additions and 35 deletions

View File

@@ -2,6 +2,8 @@ import vista from './vista.mjs';
import cookie from './cookie.mjs';
import { lab_parse, lab_reparse_results, measurement_parse } from './reportparser.mjs';
const COOKIE_TIME = 45;
function RPCError(type, ...args) {
this.name = type;
this.message = args;
@@ -75,6 +77,10 @@ export function Client(cid, secret) {
this.secret = secret;
this.cid = cid;
this.close = function() {
if(heartbeat) window.clearInterval(heartbeat);
return vista.close(cid);
};
this.call = (method, ...params) => vista.call(cid, method, ...params);
this.callctx = (context, method, ...params) => vista.callctx(cid, context, method, ...params);
this.heartbeat = async function(interval=null) {
@@ -124,13 +130,16 @@ Client.fromScratch = async function(secret, host='vista.northport.med.va.gov', p
if(data.result) return Client.fromID(data.result, secret);
};
Client.fromCookie = async function(secret, host='vista.northport.med.va.gov', port=19209) {
Client.fromCookie = async function(secret, defaulthost='vista.northport.med.va.gov:19209') {
if(!secret) secret = cookie.get('secret');
if(secret) {
var host = cookie.get('host');
host = (host || defaulthost).split(':');
if(secret != cookie.get('secret')) {
console.log('Using new secret', secret);
var client = await Client.fromScratch(secret, host, port);
var client = await Client.fromScratch(secret, host[0], host[1]);
if(client) {
cookie.set('host', host.join(':'), COOKIE_TIME);
cookie.set('secret', secret);
cookie.set('cid', client.cid);
console.log('Established connection', client.cid);
@@ -143,8 +152,9 @@ Client.fromCookie = async function(secret, host='vista.northport.med.va.gov', po
}
} else if(!cookie.get('cid')) {
console.log('Using saved secret', secret);
var client = await Client.fromScratch(secret, host, port);
var client = await Client.fromScratch(secret, host[0], host[1]);
if(client) {
cookie.set('host', host.join(':'), COOKIE_TIME);
cookie.set('secret', secret);
cookie.set('cid', client.cid);
console.log('Established connection', client.cid);
@@ -161,11 +171,13 @@ Client.fromCookie = async function(secret, host='vista.northport.med.va.gov', po
var client = Client.fromID(cid, secret);
if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') {
var server = await client.serverinfo();
if((host == server.result.host) && (port == server.result.port)) return client;
else console.log('Rejecting previous connection to different server', server);
if((host[0] == server.result.host) && (host[1] == server.result.port)) {
cookie.set('host', host.join(':'), COOKIE_TIME);
return client;
} else console.log('Rejecting previous connection to different server', server);
}
cookie.reset('cid');
return await Client.fromCookie(secret, host, port);
return await Client.fromCookie(secret, host.join(':'));
}
}
};