13
0

WebSockets: improve JS client message handling code

This commit is contained in:
Luciano Iam 2020-06-01 11:11:01 +02:00 committed by Robin Gareus
parent 176d803a55
commit 8ff4bcfd68
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
6 changed files with 53 additions and 74 deletions

View File

@ -31,10 +31,6 @@
namespace Node
{
const std::string transport_tempo = "transport_tempo";
const std::string transport_time = "transport_time";
const std::string transport_roll = "transport_roll";
const std::string transport_record = "transport_record";
const std::string strip_description = "strip_description";
const std::string strip_meter = "strip_meter";
const std::string strip_gain = "strip_gain";
@ -44,6 +40,10 @@ namespace Node
const std::string strip_plugin_enable = "strip_plugin_enable";
const std::string strip_plugin_param_description = "strip_plugin_param_description";
const std::string strip_plugin_param_value = "strip_plugin_param_value";
const std::string transport_tempo = "transport_tempo";
const std::string transport_time = "transport_time";
const std::string transport_roll = "transport_roll";
const std::string transport_record = "transport_record";
} // namespace Node
typedef std::vector<uint32_t> AddressVector;

View File

@ -19,19 +19,19 @@
export const JSON_INF = 1.0e+128;
export const StateNode = Object.freeze({
STRIP_DESCRIPTION: 'strip_description',
STRIP_METER: 'strip_meter',
STRIP_GAIN: 'strip_gain',
STRIP_PAN: 'strip_pan',
STRIP_MUTE: 'strip_mute',
STRIP_PLUGIN_DESCRIPTION: 'strip_plugin_description',
STRIP_PLUGIN_ENABLE: 'strip_plugin_enable',
STRIP_PLUGIN_PARAM_DESCRIPTION: 'strip_plugin_param_description',
STRIP_PLUGIN_PARAM_VALUE: 'strip_plugin_param_value',
TRANSPORT_TEMPO: 'transport_tempo',
TRANSPORT_TIME: 'transport_time',
TRANSPORT_ROLL: 'transport_roll',
TRANSPORT_RECORD: 'transport_record'
STRIP_DESCRIPTION : 'strip_description',
STRIP_METER : 'strip_meter',
STRIP_GAIN : 'strip_gain',
STRIP_PAN : 'strip_pan',
STRIP_MUTE : 'strip_mute',
STRIP_PLUGIN_DESCRIPTION : 'strip_plugin_description',
STRIP_PLUGIN_ENABLE : 'strip_plugin_enable',
STRIP_PLUGIN_PARAM_DESCRIPTION : 'strip_plugin_param_description',
STRIP_PLUGIN_PARAM_VALUE : 'strip_plugin_param_value',
TRANSPORT_TEMPO : 'transport_tempo',
TRANSPORT_TIME : 'transport_time',
TRANSPORT_ROLL : 'transport_roll',
TRANSPORT_RECORD : 'transport_record'
});
export class Message {
@ -41,13 +41,13 @@ export class Message {
this.addr = addr;
this.val = [];
for (const i in val) {
if (val[i] >= JSON_INF) {
for (const v of val) {
if (v >= JSON_INF) {
this.val.push(Infinity);
} else if (val[i] <= -JSON_INF) {
} else if (v <= -JSON_INF) {
this.val.push(-Infinity);
} else {
this.val.push(val[i]);
this.val.push(v);
}
}
}
@ -64,13 +64,13 @@ export class Message {
toJsonText () {
let val = [];
for (const i in this.val) {
if (this.val[i] == Infinity) {
for (const v of this.val) {
if (v == Infinity) {
val.push(JSON_INF);
} else if (this.val[i] == -Infinity) {
} else if (v == -Infinity) {
val.push(-JSON_INF);
} else {
val.push(this.val[i]);
val.push(v);
}
}

View File

@ -46,20 +46,18 @@ export class Mixer extends RootComponent {
if (node == StateNode.STRIP_DESCRIPTION) {
this._strips[addr] = new Strip(this, addr, val);
this.notifyObservers('strips');
return true;
} else {
const stripAddr = [addr[0]];
if (stripAddr in this._strips) {
this._strips[stripAddr].handle(node, addr, val);
} else {
return false;
return this._strips[stripAddr].handle(node, addr, val);
}
}
return true;
} else {
// all initial strip description messages have been received at this point
if (!this._ready) {
this.updateLocal('ready', true);
// passthrough by allowing to return false
}
}

View File

@ -54,13 +54,12 @@ export class Plugin extends AddressableComponent {
if (node == StateNode.STRIP_PLUGIN_PARAM_DESCRIPTION) {
this._parameters[addr] = new Parameter(this, addr, val);
this.notifyObservers('parameters');
return true;
} else {
if (addr in this._parameters) {
this._parameters[addr].handle(node, addr, val);
return this._parameters[addr].handle(node, addr, val);
}
}
return true;
} else if (node == StateNode.STRIP_PLUGIN_ENABLE) {
this.updateLocal('enable', val[0]);
return true;

View File

@ -20,6 +20,13 @@ import { AddressableComponent } from '../base/component.js';
import { Plugin } from './plugin.js';
import { StateNode } from '../base/protocol.js';
const NodeToProperty = Object.freeze({
[StateNode.STRIP_METER] : 'meter',
[StateNode.STRIP_GAIN] : 'gain',
[StateNode.STRIP_PAN] : 'pan',
[StateNode.STRIP_MUTE] : 'mute'
});
export class Strip extends AddressableComponent {
constructor (parent, addr, desc) {
@ -76,37 +83,17 @@ export class Strip extends AddressableComponent {
handle (node, addr, val) {
if (node.startsWith('strip_plugin')) {
if (node == StateNode.STRIP_PLUGIN_DESCRIPTION) {
this._plugins[addr] = new Plugin(this, addr, val);
this.notifyObservers('plugins');
return true;
} else {
const pluginAddr = [addr[0], addr[1]];
if (pluginAddr in this._plugins) {
this._plugins[pluginAddr].handle(node, addr, val);
} else {
return false;
return this._plugins[pluginAddr].handle(node, addr, val);
}
}
return true;
} else {
switch (node) {
case StateNode.STRIP_METER:
this.updateLocal('meter', val[0]);
break;
case StateNode.STRIP_GAIN:
this.updateLocal('gain', val[0]);
break;
case StateNode.STRIP_PAN:
this.updateLocal('pan', val[0]);
break;
case StateNode.STRIP_MUTE:
this.updateLocal('mute', val[0]);
break;
default:
return false;
}
} else if (node in NodeToProperty) {
this.updateLocal(NodeToProperty[node], val[0]);
return true;
}

View File

@ -19,6 +19,13 @@
import { RootComponent } from '../base/component.js';
import { StateNode } from '../base/protocol.js';
const NodeToProperty = Object.freeze({
[StateNode.TRANSPORT_TEMPO] : 'tempo',
[StateNode.TRANSPORT_TIME] : 'time',
[StateNode.TRANSPORT_ROLL] : 'roll',
[StateNode.TRANSPORT_RECORD] : 'record'
});
export class Transport extends RootComponent {
constructor (channel) {
@ -58,24 +65,12 @@ export class Transport extends RootComponent {
}
handle (node, addr, val) {
switch (node) {
case StateNode.TRANSPORT_TEMPO:
this.updateLocal('tempo', val[0]);
break;
case StateNode.TRANSPORT_TIME:
this.updateLocal('time', val[0]);
break;
case StateNode.TRANSPORT_ROLL:
this.updateLocal('roll', val[0]);
break;
case StateNode.TRANSPORT_RECORD:
this.updateLocal('record', val[0]);
break;
default:
return false;
}
if (node in NodeToProperty) {
this.updateLocal(NodeToProperty[node], val[0]);
return true;
}
return false;
}
}