jellyfin-web/dashboard-ui/cordova/serverdiscovery.js

179 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-05-06 05:56:26 -07:00
(function (globalScope) {
2015-05-06 20:11:51 -07:00
function stringToArrayBuffer(string) {
// UTF-16LE
var buf = new ArrayBuffer(string.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = string.length; i < strLen; i++) {
bufView[i] = string.charCodeAt(i);
}
return buf;
}
function arrayBufferToString(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
2015-05-16 20:17:23 -07:00
function getResultCode(result) {
if (result != null && result.resultCode != null) {
return result.resultCode;
}
return result;
}
2015-05-26 12:53:12 -07:00
function closeSocket(socketId) {
try {
chrome.sockets.udp.close(socketId);
} catch (err) {
}
}
2015-05-12 21:55:19 -07:00
function findServersInternal(timeoutMs) {
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var deferred = DeferredBuilder.Deferred();
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var servers = [];
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
// Expected server properties
// Name, Id, Address, EndpointAddress (optional)
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var chrome = globalScope.chrome;
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
if (!chrome) {
deferred.resolveWith(null, [servers]);
return deferred.promise();
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
var timeout;
var socketId;
2015-05-06 20:11:51 -07:00
2015-05-27 22:51:48 -07:00
function onTimerExpired() {
deferred.resolveWith(null, [servers]);
2015-05-16 20:17:23 -07:00
2015-05-27 22:51:48 -07:00
if (socketId) {
chrome.sockets.udp.onReceive.removeListener(onReceive);
closeSocket(socketId);
}
}
2015-05-06 20:11:51 -07:00
2015-05-27 22:51:48 -07:00
function startTimer() {
2015-05-06 20:11:51 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('starting udp receive timer with timeout ms: ' + timeoutMs);
2015-05-06 20:11:51 -07:00
2015-05-27 22:51:48 -07:00
timeout = setTimeout(onTimerExpired, timeoutMs);
2015-05-12 21:55:19 -07:00
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
function onReceive(info) {
2015-05-06 20:11:51 -07:00
2015-05-26 12:53:12 -07:00
try {
2015-05-06 20:11:51 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('ServerDiscovery message received');
2015-05-06 20:11:51 -07:00
2015-06-26 20:27:38 -07:00
Logger.log(info);
2015-05-06 20:11:51 -07:00
2015-05-26 12:53:12 -07:00
if (info != null && info.socketId == socketId) {
2015-05-06 20:11:51 -07:00
var json = arrayBufferToString(info.data);
2015-06-26 20:27:38 -07:00
Logger.log('Server discovery json: ' + json);
2015-05-06 20:11:51 -07:00
var server = JSON.parse(json);
server.RemoteAddress = info.remoteAddress;
if (info.remotePort) {
server.RemoteAddress += ':' + info.remotePort;
}
servers.push(server);
}
2015-05-26 12:53:12 -07:00
} catch (err) {
2015-06-26 20:27:38 -07:00
Logger.log('Error receiving server info: ' + err);
2015-05-06 20:11:51 -07:00
}
2015-05-12 21:55:19 -07:00
}
var port = 7359;
2015-06-26 20:27:38 -07:00
Logger.log('chrome.sockets.udp.create');
2015-05-27 22:51:48 -07:00
startTimer();
2015-05-12 21:55:19 -07:00
chrome.sockets.udp.create(function (createInfo) {
2015-05-26 08:31:50 -07:00
if (!createInfo) {
2015-06-26 20:27:38 -07:00
Logger.log('create fail');
2015-05-26 08:31:50 -07:00
return;
}
if (!createInfo.socketId) {
2015-06-26 20:27:38 -07:00
Logger.log('create fail');
2015-05-26 08:31:50 -07:00
return;
}
2015-05-12 21:55:19 -07:00
socketId = createInfo.socketId;
2015-06-26 20:27:38 -07:00
Logger.log('chrome.sockets.udp.bind');
2015-05-26 12:53:12 -07:00
chrome.sockets.udp.bind(createInfo.socketId, '0.0.0.0', 0, function (bindResult) {
2015-05-12 21:55:19 -07:00
2015-05-26 12:53:12 -07:00
if (getResultCode(bindResult) != 0) {
2015-06-26 20:27:38 -07:00
Logger.log('bind fail: ' + bindResult);
2015-05-15 08:46:20 -07:00
return;
}
2015-05-12 21:55:19 -07:00
var data = stringToArrayBuffer('who is EmbyServer?');
2015-05-06 20:11:51 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('chrome.sockets.udp.send');
2015-05-27 22:51:48 -07:00
2015-05-26 12:53:12 -07:00
chrome.sockets.udp.send(createInfo.socketId, data, '255.255.255.255', port, function (sendResult) {
2015-05-06 20:11:51 -07:00
2015-05-26 12:53:12 -07:00
if (getResultCode(sendResult) != 0) {
2015-06-26 20:27:38 -07:00
Logger.log('send fail: ' + sendResult);
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
} else {
chrome.sockets.udp.onReceive.addListener(onReceive);
2015-06-26 20:27:38 -07:00
Logger.log('sendTo: success ' + port);
2015-05-12 21:55:19 -07:00
}
});
});
});
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
return deferred.promise();
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
globalScope.ServerDiscovery = {
findServers: function (timeoutMs) {
var deferred = DeferredBuilder.Deferred();
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
deviceReadyPromise.done(function () {
2015-05-06 20:11:51 -07:00
2015-05-23 13:44:15 -07:00
try {
findServersInternal(timeoutMs).done(function (result) {
2015-05-12 21:55:19 -07:00
2015-05-23 13:44:15 -07:00
deferred.resolveWith(null, [result]);
2015-05-12 21:55:19 -07:00
2015-05-23 13:44:15 -07:00
}).fail(function () {
2015-05-12 21:55:19 -07:00
2015-05-27 22:51:48 -07:00
deferred.resolveWith(null, [[]]);
2015-05-23 13:44:15 -07:00
});
} catch (err) {
2015-05-27 22:51:48 -07:00
deferred.resolveWith(null, [[]]);
2015-05-23 13:44:15 -07:00
}
2015-05-06 20:11:51 -07:00
});
2015-05-06 05:56:26 -07:00
return deferred.promise();
}
};
2015-05-12 21:55:19 -07:00
var deviceReadyDeferred = DeferredBuilder.Deferred();
var deviceReadyPromise = deviceReadyDeferred.promise();
document.addEventListener("deviceready", function () {
deviceReadyDeferred.resolve();
}, false);
2015-05-06 05:56:26 -07:00
})(window);