lint; server code lint
This commit is contained in:
parent
08dcb99b2e
commit
c9250a2906
14 changed files with 357 additions and 352 deletions
|
|
@ -2,99 +2,96 @@
|
|||
// (electron-rebuild breaks it for node-only mode)
|
||||
require('vmodule')('serialport', {}, {global: true})
|
||||
|
||||
var osc = require('osc'),
|
||||
ipc = require('../server').ipc,
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
oscInPort = settings.read('oscInPort') || settings.read('httpPort'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
debug = settings.read('debug'),
|
||||
vm = require('vm'),
|
||||
fs = require('fs'),
|
||||
var ipc = require('../server').ipc,
|
||||
settings = require('../settings'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
debug = settings.read('debug'),
|
||||
vm = require('vm'),
|
||||
fs = require('fs'),
|
||||
midi = settings.read('midi') ? require('../midi') : false,
|
||||
oscUDPServer = require('./udp'),
|
||||
oscTCPServer = require('./tcp')
|
||||
oscUDPServer = require('./udp'),
|
||||
oscTCPServer = require('./tcp')
|
||||
|
||||
class OscServer {
|
||||
|
||||
constructor(){
|
||||
|
||||
this.customModule = (()=>{
|
||||
this.customModule = (()=>{
|
||||
|
||||
if (!settings.read('customModule')) return false
|
||||
if (!settings.read('customModule')) return false
|
||||
|
||||
var file = (()=>{
|
||||
try {
|
||||
return fs.readFileSync(settings.read('customModule'),'utf8')
|
||||
} catch(err) {
|
||||
console.log('CustomModule Error: File not found: ' + settings.read('customModule'))
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
mod,
|
||||
context = {
|
||||
console,
|
||||
sendOsc: this.sendOsc.bind(this),
|
||||
receiveOsc: this.receiveOsc.bind(this),
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
settings
|
||||
}
|
||||
var file = (()=>{
|
||||
try {
|
||||
return fs.readFileSync(settings.read('customModule'),'utf8')
|
||||
} catch(err) {
|
||||
console.log('CustomModule Error: File not found: ' + settings.read('customModule'))
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
mod,
|
||||
context = {
|
||||
console,
|
||||
sendOsc: this.sendOsc.bind(this),
|
||||
receiveOsc: this.receiveOsc.bind(this),
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
settings
|
||||
}
|
||||
|
||||
try {
|
||||
mod = vm.runInContext(file, vm.createContext(context))
|
||||
} catch(err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
try {
|
||||
mod = vm.runInContext(file, vm.createContext(context))
|
||||
} catch(err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
return mod
|
||||
return mod
|
||||
|
||||
})()
|
||||
})()
|
||||
|
||||
|
||||
}
|
||||
|
||||
parseType(type){
|
||||
var t = type[0].match(/[bhtdscrmifTFNI]/)
|
||||
var t = type[0].match(/[bhtdscrmifTFNI]/)
|
||||
|
||||
if (t!==null) {
|
||||
return t[0]
|
||||
} else {
|
||||
return 's'
|
||||
}
|
||||
if (t!==null) {
|
||||
return t[0]
|
||||
} else {
|
||||
return 's'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parseArg(arg, precision){
|
||||
if (arg==null) return null
|
||||
switch (typeof arg) {
|
||||
case 'number':
|
||||
var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null,
|
||||
typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1],
|
||||
precisionValue = parseFloat(precision) || 0
|
||||
if (arg==null) return null
|
||||
switch (typeof arg) {
|
||||
case 'number':
|
||||
var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null,
|
||||
typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1],
|
||||
precisionValue = parseFloat(precision) || 0
|
||||
|
||||
return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))}
|
||||
case 'boolean':
|
||||
return {type: arg ? 'T' : 'F', value: arg}
|
||||
case 'object':
|
||||
if (arg.type) {
|
||||
return {type: parseType(arg.type), value: arg.value}
|
||||
} else {
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))}
|
||||
case 'boolean':
|
||||
return {type: arg ? 'T' : 'F', value: arg}
|
||||
case 'object':
|
||||
if (arg.type) {
|
||||
return {type: parseType(arg.type), value: arg.value}
|
||||
} else {
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
case 'string':
|
||||
return {type: 's', value:arg}
|
||||
default:
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
default:
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
}
|
||||
|
||||
sendOsc(data){
|
||||
|
||||
if (!data) return
|
||||
if (!data) return
|
||||
|
||||
if (data.host == 'midi') {
|
||||
|
||||
|
|
@ -102,24 +99,24 @@ class OscServer {
|
|||
|
||||
} else {
|
||||
|
||||
if (typeof data.address !== 'string' || data.address[0] !== '/') {
|
||||
console.error('OSC error: malformed address: "' + data.address + "")
|
||||
return
|
||||
}
|
||||
if (typeof data.address !== 'string' || data.address[0] !== '/') {
|
||||
console.error('OSC error: malformed address: "' + data.address + '')
|
||||
return
|
||||
}
|
||||
|
||||
oscUDPServer.send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
}, data.host, data.port)
|
||||
oscUDPServer.send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
}, data.host, data.port)
|
||||
|
||||
if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) {
|
||||
oscTCPServer.clients[data.host][data.port].send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
})
|
||||
}
|
||||
if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) {
|
||||
oscTCPServer.clients[data.host][data.port].send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
})
|
||||
}
|
||||
|
||||
if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port)
|
||||
if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port)
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -128,51 +125,51 @@ class OscServer {
|
|||
|
||||
receiveOsc(data, info){
|
||||
|
||||
if (!data) return
|
||||
if (!data) return
|
||||
|
||||
for (i in data.args) {
|
||||
data.args[i] = data.args[i].value
|
||||
}
|
||||
|
||||
if (data.args.length==1) data.args = data.args[0]
|
||||
if (data.args.length==1) data.args = data.args[0]
|
||||
|
||||
ipc.send('receiveOsc',data)
|
||||
ipc.send('receiveOsc',data)
|
||||
|
||||
if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port)
|
||||
if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port)
|
||||
|
||||
}
|
||||
|
||||
|
||||
oscInFilter(data){
|
||||
if (this.customModule.oscInFilter) {
|
||||
return this.customModule.oscInFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
if (this.customModule.oscInFilter) {
|
||||
return this.customModule.oscInFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
oscOutFilter(data){
|
||||
if (this.customModule.oscOutFilter) {
|
||||
return this.customModule.oscOutFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
if (this.customModule.oscOutFilter) {
|
||||
return this.customModule.oscOutFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
oscInHandler(msg, timetag, info){
|
||||
var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port})
|
||||
var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port})
|
||||
if (!data) return
|
||||
this.receiveOsc(data)
|
||||
this.receiveOsc(data)
|
||||
}
|
||||
|
||||
oscInHandlerWrapper(msg, timetag, info) {
|
||||
var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0
|
||||
if (delay) {
|
||||
setTimeout(()=>{
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}, delay)
|
||||
} else {
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}
|
||||
var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0
|
||||
if (delay) {
|
||||
setTimeout(()=>{
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}, delay)
|
||||
} else {
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -181,7 +178,7 @@ class OscServer {
|
|||
|
||||
if (midi) midi.init((data)=>{
|
||||
|
||||
var data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port})
|
||||
data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port})
|
||||
|
||||
if (!data) return
|
||||
|
||||
|
|
@ -189,17 +186,17 @@ class OscServer {
|
|||
|
||||
})
|
||||
|
||||
oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscUDPServer.open()
|
||||
oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscUDPServer.open()
|
||||
|
||||
if (settings.read('tcpInPort')) {
|
||||
oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscTCPServer.open()
|
||||
}
|
||||
if (settings.read('tcpInPort')) {
|
||||
oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscTCPServer.open()
|
||||
}
|
||||
|
||||
if (this.customModule.init) {
|
||||
this.customModule.init()
|
||||
}
|
||||
if (this.customModule.init) {
|
||||
this.customModule.init()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -212,22 +209,22 @@ oscServer.init()
|
|||
|
||||
module.exports = {
|
||||
|
||||
send: function(host,port,address,args,precision) {
|
||||
send: function(host,port,address,args,precision) {
|
||||
|
||||
var message = []
|
||||
var message = []
|
||||
|
||||
if (!Array.isArray(args)) args = [args]
|
||||
if (typeof args=='object' && args!==null) {
|
||||
for (var i in args) {
|
||||
var arg = oscServer.parseArg(args[i],precision)
|
||||
if (arg!=null) message.push(arg)
|
||||
}
|
||||
}
|
||||
if (typeof args=='object' && args!==null) {
|
||||
for (var i in args) {
|
||||
var arg = oscServer.parseArg(args[i],precision)
|
||||
if (arg!=null) message.push(arg)
|
||||
}
|
||||
}
|
||||
|
||||
var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port})
|
||||
var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port})
|
||||
|
||||
oscServer.sendOsc(data)
|
||||
oscServer.sendOsc(data)
|
||||
|
||||
},
|
||||
},
|
||||
midi:midi
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
var osc = require('osc'),
|
||||
EventEmitter = require('events').EventEmitter
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
settings = require('../settings'),
|
||||
debug = settings.read('debug'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
tcpTargets = settings.read('tcpTargets'),
|
||||
|
|
@ -9,7 +8,7 @@ var osc = require('osc'),
|
|||
|
||||
class OscTCPClient extends EventEmitter {
|
||||
|
||||
constructor(options) {
|
||||
constructor(options) {
|
||||
|
||||
super()
|
||||
|
||||
|
|
@ -47,7 +46,7 @@ class OscTCPClient extends EventEmitter {
|
|||
console.log(`TCP: connection established (${this.remoteAddress}:${this.remotePort})`)
|
||||
})
|
||||
|
||||
if (!this.options.socket) {
|
||||
if (!this.options.socket) {
|
||||
|
||||
this.port.open()
|
||||
|
||||
|
|
@ -67,7 +66,7 @@ class OscTCPClient extends EventEmitter {
|
|||
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
send(msg) {
|
||||
|
||||
this.port.send(msg)
|
||||
|
||||
|
|
@ -79,7 +78,7 @@ class OscTCPClient extends EventEmitter {
|
|||
|
||||
class OscTCPServer extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
|
|
@ -100,7 +99,7 @@ class OscTCPServer extends EventEmitter {
|
|||
|
||||
addClient(client) {
|
||||
|
||||
this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {}
|
||||
this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {}
|
||||
this.clients[client.remoteAddress][client.remotePort] = client
|
||||
|
||||
client.on('message', (msg, timetag, info)=>{
|
||||
|
|
@ -130,10 +129,10 @@ class OscTCPServer extends EventEmitter {
|
|||
}
|
||||
|
||||
zeroconf.publish({
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
protocol: 'tcp',
|
||||
type: 'osc',
|
||||
port: tcpInPort
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
protocol: 'tcp',
|
||||
type: 'osc',
|
||||
port: tcpInPort
|
||||
}).on('error', (e)=>{
|
||||
console.error(`Error: Zeroconf: ${e.message}`)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
var osc = require('osc'),
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
oscInPort = settings.read('oscInPort') || settings.read('httpPort')
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
oscInPort = settings.read('oscInPort') || settings.read('httpPort')
|
||||
|
||||
var oscUDPServer = new osc.UDPPort({
|
||||
localAddress: "0.0.0.0",
|
||||
localAddress: '0.0.0.0',
|
||||
localPort: oscInPort,
|
||||
metadata: true,
|
||||
broadcast: true
|
||||
metadata: true,
|
||||
broadcast: true
|
||||
})
|
||||
|
||||
oscUDPServer.on('error', function(error) {
|
||||
|
|
@ -15,10 +15,10 @@ oscUDPServer.on('error', function(error) {
|
|||
})
|
||||
|
||||
zeroconf.publish({
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
protocol: 'udp',
|
||||
type: 'osc',
|
||||
port: oscInPort
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
protocol: 'udp',
|
||||
type: 'osc',
|
||||
port: oscInPort
|
||||
}).on('error', (e)=>{
|
||||
console.error(`Error: Zeroconf: ${e.message}`)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue