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-06-14 06:34:27 -04:00
|
|
|
import { Component } from './base/component.js';
|
2020-05-29 05:37:34 -04:00
|
|
|
import { StateNode } from './base/protocol.js';
|
2020-06-14 06:10:30 -04:00
|
|
|
import MessageChannel from './base/channel.js';
|
|
|
|
import Mixer from './components/mixer.js';
|
|
|
|
import Transport from './components/transport.js';
|
2020-04-11 11:30:15 -04:00
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
function getOption (options, key, defaultValue) {
|
|
|
|
return options ? (key in options ? options[key] : defaultValue) : defaultValue;
|
|
|
|
}
|
2020-04-13 03:05:30 -04:00
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
export default class ArdourClient extends Component {
|
2020-04-13 03:05:30 -04:00
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
constructor (options) {
|
|
|
|
super(new MessageChannel(getOption(options, 'host', location.host)));
|
2020-05-29 05:37:34 -04:00
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
if (getOption(options, 'components', true)) {
|
2020-06-14 07:04:43 -04:00
|
|
|
this._mixer = new Mixer(this);
|
|
|
|
this._transport = new Transport(this);
|
2020-06-14 06:34:27 -04:00
|
|
|
this._components = [this._mixer, this._transport];
|
|
|
|
} else {
|
|
|
|
this._components = [];
|
2020-05-29 05:37:34 -04:00
|
|
|
}
|
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
this._autoReconnect = getOption(options, 'autoReconnect', true);
|
|
|
|
this._connected = false;
|
2020-05-29 05:37:34 -04:00
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
this.channel.onMessage = (msg, inbound) => this._handleMessage(msg, inbound);
|
|
|
|
this.channel.onError = (err) => this.notifyObservers('error', err);
|
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-06-14 06:34:27 -04:00
|
|
|
this.channel.onClose = async () => {
|
2020-04-14 16:58:44 -04:00
|
|
|
if (this._connected) {
|
2020-05-29 05:37:34 -04:00
|
|
|
this._setConnected(false);
|
2020-04-14 16:58:44 -04:00
|
|
|
}
|
|
|
|
|
2020-06-14 06:34:27 -04:00
|
|
|
if (this._autoReconnect) {
|
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-06-14 06:34:27 -04:00
|
|
|
this.channel.onClose = () => {};
|
|
|
|
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) {
|
2020-06-14 06:34:27 -04:00
|
|
|
this.channel.send(msg);
|
2020-04-12 09:49:54 -04:00
|
|
|
}
|
2020-04-12 08:56:09 -04:00
|
|
|
|
2020-05-29 05:37:34 -04:00
|
|
|
async sendAndReceive (msg) {
|
2020-06-14 06:34:27 -04:00
|
|
|
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 () {
|
2020-06-14 06:34:27 -04:00
|
|
|
await this.channel.open();
|
2020-05-29 05:37:34 -04:00
|
|
|
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 06:34:27 -04:00
|
|
|
this.notifyPropertyChanged('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 06:34:27 -04:00
|
|
|
this.notifyObservers('message', msg, inbound);
|
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-04-14 03:53:25 -04:00
|
|
|
}
|