2020-04-11 11:30:15 -04:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Luciano Iam <lucianito@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
import { MessageChannel } from './base/channel.js';
|
|
|
|
import { StateNode } from './base/protocol.js';
|
|
|
|
import { Mixer } from './components/mixer.js';
|
|
|
|
import { Transport } from './components/transport.js';
|
2020-04-11 11:30:15 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
export class ArdourClient {
|
2020-04-14 03:53:25 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
constructor (handlers, options) {
|
|
|
|
this._options = options || {};
|
|
|
|
this._components = [];
|
2020-04-14 16:58:44 -04:00
|
|
|
this._connected = false;
|
2020-04-13 03:05:30 -04:00
|
|
|
|
2020-05-31 07:53:26 -04:00
|
|
|
this._channel = new MessageChannel(this._getOption('host', location.host));
|
2020-04-13 03:05:30 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
this._channel.onMessage = (msg, inbound) => {
|
|
|
|
this._handleMessage(msg, inbound);
|
2020-04-13 03:05:30 -04:00
|
|
|
};
|
2020-05-29 05:37:34 -04:00
|
|
|
|
2020-05-31 07:53:26 -04:00
|
|
|
if (this._getOption('components', true)) {
|
2020-05-29 05:37:34 -04:00
|
|
|
this._mixer = new Mixer(this._channel);
|
|
|
|
this._transport = new Transport(this._channel);
|
|
|
|
this._components.push(this._mixer, this._transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlers = handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
set handlers (handlers) {
|
|
|
|
this._handlers = handlers || {};
|
2020-06-14 05:41:50 -04:00
|
|
|
this._channel.onError = this._handlers.onError || console.log;
|
2020-05-29 05:37:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Access to the object-oriented API (enabled by default)
|
|
|
|
|
|
|
|
get mixer () {
|
|
|
|
return this._mixer;
|
2020-04-13 03:05:30 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
get transport () {
|
|
|
|
return this._transport;
|
2020-04-11 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
// Low level control messages flow through a WebSocket
|
|
|
|
|
2020-05-31 07:53:26 -04:00
|
|
|
async connect () {
|
2020-04-14 16:58:44 -04:00
|
|
|
this._channel.onClose = async () => {
|
|
|
|
if (this._connected) {
|
2020-05-29 05:37:34 -04:00
|
|
|
this._setConnected(false);
|
2020-04-14 16:58:44 -04:00
|
|
|
}
|
|
|
|
|
2020-05-31 07:53:26 -04:00
|
|
|
if (this._getOption('autoReconnect', true)) {
|
2020-04-14 16:58:44 -04:00
|
|
|
await this._sleep(1000);
|
|
|
|
await this._connect();
|
|
|
|
}
|
2020-04-12 09:36:51 -04:00
|
|
|
};
|
|
|
|
|
2020-04-22 05:35:23 -04:00
|
|
|
await this._connect();
|
2020-04-11 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 16:58:44 -04:00
|
|
|
disconnect () {
|
2020-04-13 03:05:30 -04:00
|
|
|
this._channel.onClose = () => {};
|
2020-04-12 09:36:51 -04:00
|
|
|
this._channel.close();
|
2020-04-14 16:58:44 -04:00
|
|
|
this._connected = false;
|
2020-04-12 09:36:51 -04:00
|
|
|
}
|
2020-04-12 09:49:54 -04:00
|
|
|
|
|
|
|
send (msg) {
|
|
|
|
this._channel.send(msg);
|
|
|
|
}
|
2020-04-12 08:56:09 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
async sendAndReceive (msg) {
|
|
|
|
return await this._channel.sendAndReceive(msg);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
// Surface metadata API goes over HTTP
|
2020-04-14 16:58:44 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
async getAvailableSurfaces () {
|
|
|
|
const response = await fetch('/surfaces.json');
|
|
|
|
|
|
|
|
if (response.status == 200) {
|
|
|
|
return await response.json();
|
2020-04-12 08:56:09 -04:00
|
|
|
} else {
|
2020-05-29 05:37:34 -04:00
|
|
|
throw this._fetchResponseStatusError(response.status);
|
2020-04-13 03:05:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
async getSurfaceManifest () {
|
|
|
|
const response = await fetch('manifest.xml');
|
2020-04-13 03:05:30 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
if (response.status == 200) {
|
|
|
|
const manifest = {};
|
|
|
|
const xmlText = await response.text();
|
|
|
|
const xmlDoc = new DOMParser().parseFromString(xmlText, 'text/xml');
|
|
|
|
|
|
|
|
for (const child of xmlDoc.children[0].children) {
|
|
|
|
manifest[child.tagName.toLowerCase()] = child.getAttribute('value');
|
2020-04-13 03:05:30 -04:00
|
|
|
}
|
2020-05-29 05:37:34 -04:00
|
|
|
|
|
|
|
return manifest;
|
|
|
|
} else {
|
|
|
|
throw this._fetchResponseStatusError(response.status);
|
2020-04-12 08:44:58 -04:00
|
|
|
}
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
// Private methods
|
|
|
|
|
|
|
|
async _sleep (t) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, t));
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
2020-04-11 11:30:15 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
async _connect () {
|
|
|
|
await this._channel.open();
|
|
|
|
this._setConnected(true);
|
2020-04-14 16:58:44 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
_setConnected (connected) {
|
|
|
|
this._connected = connected;
|
|
|
|
|
2020-06-14 05:41:50 -04:00
|
|
|
if (this._handlers.onConnected) {
|
|
|
|
this._handlers.onConnected(this._connected);
|
2020-05-29 05:37:34 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 03:53:25 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
_handleMessage (msg, inbound) {
|
2020-06-14 05:41:50 -04:00
|
|
|
if (this._handlers.onMessage) {
|
|
|
|
this._handlers.onMessage(msg, inbound);
|
2020-05-29 05:37:34 -04:00
|
|
|
}
|
2020-04-14 03:53:25 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
if (inbound) {
|
|
|
|
for (const component of this._components) {
|
|
|
|
if (component.handleMessage(msg)) {
|
|
|
|
break;
|
|
|
|
}
|
2020-04-14 03:53:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 05:37:34 -04:00
|
|
|
|
|
|
|
_fetchResponseStatusError (status) {
|
|
|
|
return new Error(`HTTP response status ${status}`);
|
|
|
|
}
|
|
|
|
|
2020-05-31 07:53:26 -04:00
|
|
|
_getOption (key, defaultValue) {
|
|
|
|
return key in this._options ? this._options[key] : defaultValue;
|
|
|
|
}
|
|
|
|
|
2020-04-14 03:53:25 -04:00
|
|
|
}
|