mirror of
https://github.com/moonlight-stream/moonlight-chrome.git
synced 2025-08-17 16:46:31 +00:00
Centralize polling algorithm
This commit is contained in:
parent
c308e4034c
commit
47868d7c57
@ -58,37 +58,52 @@ function restoreUiAfterNaClLoad() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function beginBackgroundPollingOfHost(host) {
|
function beginBackgroundPollingOfHost(host) {
|
||||||
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
if (host.online) {
|
||||||
// for each host, first assume it's inactive.
|
|
||||||
|
|
||||||
host.initialPing(function () { // initial attempt was a success
|
|
||||||
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
||||||
|
|
||||||
|
// The host was already online. Just start polling in the background now.
|
||||||
activePolls[host.serverUid] = window.setInterval(function() {
|
activePolls[host.serverUid] = window.setInterval(function() {
|
||||||
if (api && activePolls[api.serverUid] != null) {
|
if (api && activePolls[api.serverUid] != null) {
|
||||||
stopBackgroundPollingOfHost(api);
|
stopBackgroundPollingOfHost(api);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// every 5 seconds, poll at the address we know it was live at
|
// every 5 seconds, poll at the address we know it was live at
|
||||||
host.refreshServerInfoAtAddress(host.address).then(function (onSuccess){
|
host.pollServer(function () {
|
||||||
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
if (host.online) {
|
||||||
}, function (onFailure) {
|
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
||||||
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
} else {
|
||||||
|
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}, function () { // initial attempt was a failure
|
} else {
|
||||||
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
||||||
activePolls[host.serverUid] = window.setInterval(function() {
|
|
||||||
if (api && activePolls[api.serverUid] != null) {
|
// The host was offline, so poll immediately.
|
||||||
stopBackgroundPollingOfHost(api);
|
host.pollServer(function () {
|
||||||
return;
|
if (host.online) {
|
||||||
}
|
|
||||||
if(host.refreshServerInfoAtAddress(host.address)) {
|
|
||||||
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
||||||
} else {
|
} else {
|
||||||
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
||||||
}
|
}
|
||||||
}, 5000);
|
|
||||||
});
|
// Now start background polling
|
||||||
|
activePolls[host.serverUid] = window.setInterval(function() {
|
||||||
|
if (api && activePolls[api.serverUid] != null) {
|
||||||
|
stopBackgroundPollingOfHost(api);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// every 5 seconds, poll at the address we know it was live at
|
||||||
|
host.pollServer(function () {
|
||||||
|
if (host.online) {
|
||||||
|
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
|
||||||
|
} else {
|
||||||
|
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 5000);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopBackgroundPollingOfHost(host) {
|
function stopBackgroundPollingOfHost(host) {
|
||||||
@ -221,26 +236,17 @@ function hostChosen(sourceEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
api = hosts[serverUid];
|
api = hosts[serverUid];
|
||||||
|
if (!api.online) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
stopBackgroundPollingOfHost(api);
|
stopBackgroundPollingOfHost(api);
|
||||||
|
|
||||||
// Use the cached state of this host from the last poll
|
if (!api.paired) {
|
||||||
if(!api.paired) {
|
// Still not paired; go to the pairing flow
|
||||||
// It doesn't think we're paired. Refresh our serverinfo to make sure.
|
pairTo(api, function(){ showApps(api); saveHosts();}, function(){});
|
||||||
api.refreshServerInfo().then(function (ret) {
|
|
||||||
if (!api.paired) {
|
|
||||||
// Still not paired; go to the pairing flow
|
|
||||||
pairTo(api, function(){ showApps(api); saveHosts();}, function(){});
|
|
||||||
} else {
|
|
||||||
// When we queried again, it was paired, so show apps.
|
|
||||||
showApps(api);
|
|
||||||
}
|
|
||||||
}, function (failedRefreshInfo) {
|
|
||||||
snackbarLog('Failed to connect to ' + api.address + '! Are you sure the host is on?');
|
|
||||||
console.log('Returned error was: ' + failedRefreshInfo);
|
|
||||||
console.log('failed API object: ');
|
|
||||||
console.log(api.toString());
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
|
// When we queried again, it was paired, so show apps.
|
||||||
showApps(api);
|
showApps(api);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -743,7 +749,7 @@ function onWindowLoad(){
|
|||||||
var mDnsDiscoveredHost = new NvHTTP(ip, myUniqueid);
|
var mDnsDiscoveredHost = new NvHTTP(ip, myUniqueid);
|
||||||
if(hosts[mDnsDiscoveredHost.serverUid] != null) {
|
if(hosts[mDnsDiscoveredHost.serverUid] != null) {
|
||||||
// if we're seeing a host we've already seen before, update it for the current local IP.
|
// if we're seeing a host we've already seen before, update it for the current local IP.
|
||||||
hosts[mDnsDiscoveredHost.serverUid].localIp = mDnsDiscoveredHost.localIp;
|
hosts[mDnsDiscoveredHost.serverUid].address = mDnsDiscoveredHost.address;
|
||||||
} else {
|
} else {
|
||||||
addHostToGrid(mDnsDiscoveredHost);
|
addHostToGrid(mDnsDiscoveredHost);
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,10 @@ function NvHTTP(address, clientUid, userEnteredAddress = '') {
|
|||||||
this.currentGame = 0;
|
this.currentGame = 0;
|
||||||
this.serverMajorVersion = 0;
|
this.serverMajorVersion = 0;
|
||||||
this.clientUid = clientUid;
|
this.clientUid = clientUid;
|
||||||
this._baseUrlHttps = 'https://' + address + ':47984';
|
|
||||||
this._baseUrlHttp = 'http://' + address + ':47989';
|
|
||||||
this._memCachedBoxArtArray = {};
|
this._memCachedBoxArtArray = {};
|
||||||
|
this._pollCount = 0;
|
||||||
|
this._consecutivePollFailures = 0;
|
||||||
|
this.online = false;
|
||||||
|
|
||||||
this.userEnteredAddress = userEnteredAddress; // if the user entered an address, we keep it on hand to try when polling
|
this.userEnteredAddress = userEnteredAddress; // if the user entered an address, we keep it on hand to try when polling
|
||||||
this.serverUid = '';
|
this.serverUid = '';
|
||||||
@ -100,22 +101,52 @@ NvHTTP.prototype = {
|
|||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// called every few seconds to poll the server for updated info
|
||||||
|
pollServer: function(onComplete) {
|
||||||
|
this.selectServerAddress(function(successfulAddress) {
|
||||||
|
// Successfully determined server address. Update base URL.
|
||||||
|
this.address = successfulAddress;
|
||||||
|
this._baseUrlHttps = 'https://' + successfulAddress + ':47984';
|
||||||
|
this._baseUrlHttp = 'http://' + successfulAddress + ':47989';
|
||||||
|
|
||||||
|
// Poll for the app list every 10 successful serverinfo polls.
|
||||||
|
// Not including the first one to avoid PCs taking a while to show
|
||||||
|
// as online initially
|
||||||
|
if (++this._pollCount % 10 == 0) {
|
||||||
|
this.getAppListWithCacheFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
this._consecutivePollFailures = 0;
|
||||||
|
this.online = true;
|
||||||
|
|
||||||
|
onComplete();
|
||||||
|
}.bind(this), function() {
|
||||||
|
if (++this._consecutivePollFailures >= 3) {
|
||||||
|
this.online = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
onComplete();
|
||||||
|
}.bind(this));
|
||||||
|
},
|
||||||
|
|
||||||
// initially pings the server to try and figure out if it's routable by any means.
|
// initially pings the server to try and figure out if it's routable by any means.
|
||||||
initialPing: function(onSuccess, onFailure) {
|
selectServerAddress: function(onSuccess, onFailure) {
|
||||||
this.refreshServerInfoAtAddress(this.hostname + '.local').then(function(successLocal) {
|
// TODO: Deduplicate the addresses
|
||||||
this.address = this.hostname + '.local';
|
this.refreshServerInfoAtAddress(this.address).then(function(successPrevAddr) {
|
||||||
onSuccess();
|
onSuccess(this.address);
|
||||||
}.bind(this), function(failureLocal) {
|
}.bind(this), function(successPrevAddr) {
|
||||||
this.refreshServerInfoAtAddress(this.externalIP).then(function(successExternal) {
|
this.refreshServerInfoAtAddress(this.hostname + '.local').then(function(successLocal) {
|
||||||
this.address = this.externalIP;
|
onSuccess(this.hostname + '.local');
|
||||||
onSuccess();
|
}.bind(this), function(failureLocal) {
|
||||||
}.bind(this), function(failureExternal) {
|
this.refreshServerInfoAtAddress(this.externalIP).then(function(successExternal) {
|
||||||
this.refreshServerInfoAtAddress(this.userEnteredAddress).then(function(successUserEntered) {
|
onSuccess(this.externalIP);
|
||||||
this.address = this.userEnteredAddress;
|
}.bind(this), function(failureExternal) {
|
||||||
onSuccess();
|
this.refreshServerInfoAtAddress(this.userEnteredAddress).then(function(successUserEntered) {
|
||||||
}.bind(this), function(failureUserEntered) {
|
onSuccess(this.userEnteredAddress);
|
||||||
console.log('WARN! Failed to contact host: ' + this.hostname + '\r\n' + this.toString());
|
}.bind(this), function(failureUserEntered) {
|
||||||
onFailure();
|
console.log('WARN! Failed to contact host: ' + this.hostname + '\r\n' + this.toString());
|
||||||
|
onFailure();
|
||||||
|
}.bind(this));
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user