package: rename browser to client and main to server
This commit is contained in:
parent
fd2f6b8af7
commit
263eb84780
123 changed files with 43 additions and 42 deletions
343
src/server/callbacks.js
Normal file
343
src/server/callbacks.js
Normal file
|
|
@ -0,0 +1,343 @@
|
|||
var path = require('path'),
|
||||
fs = require('fs'),
|
||||
settings = require('./settings'),
|
||||
osc = require('./osc'),
|
||||
{ipc} = require('./server'),
|
||||
{deepCopy} = require('./utils'),
|
||||
theme = require('./theme'),
|
||||
chokidar = require('chokidar')
|
||||
|
||||
var openedSessions = {},
|
||||
widgetHashTable = {},
|
||||
lastSavingClient,
|
||||
sessionBackups = {}
|
||||
|
||||
module.exports = {
|
||||
|
||||
ready: function(data,clientId) {
|
||||
ipc.send('connected')
|
||||
|
||||
if (settings.read('readOnly')) {
|
||||
ipc.send('readOnly')
|
||||
}
|
||||
|
||||
if (data.backupId && sessionBackups[data.backupId]) {
|
||||
ipc.send('loadBackup', sessionBackups[data.backupId])
|
||||
return
|
||||
}
|
||||
|
||||
if (settings.read('newSession')) {
|
||||
ipc.send('sessionNew')
|
||||
return
|
||||
}
|
||||
|
||||
if (settings.read('sessionFile')) return this.sessionOpen({path:settings.read('sessionFile')},clientId)
|
||||
|
||||
var recentSessions = settings.read('recentSessions')
|
||||
|
||||
if (settings.read('examples')) {
|
||||
var dir = path.resolve(__dirname + '/../examples')
|
||||
recentSessions = fs.readdirSync(dir)
|
||||
recentSessions = recentSessions.map(function(file){return dir + '/' + file})
|
||||
}
|
||||
|
||||
ipc.send('sessionList',recentSessions,clientId)
|
||||
},
|
||||
|
||||
sessionAddToHistory: function(data) {
|
||||
var sessionlist = settings.read('recentSessions')
|
||||
|
||||
fs.lstat(data,(err, stats)=>{
|
||||
|
||||
if (err || !stats.isFile()) return
|
||||
|
||||
// add session to history
|
||||
sessionlist.unshift(path.resolve(data))
|
||||
// remove doubles from history
|
||||
sessionlist = sessionlist.filter(function(elem, index, self) {
|
||||
return index == self.indexOf(elem)
|
||||
})
|
||||
// save history
|
||||
settings.write('recentSessions',sessionlist)
|
||||
|
||||
})
|
||||
},
|
||||
|
||||
sessionRemoveFromHistory: function(data) {
|
||||
var sessionlist = settings.read('recentSessions')
|
||||
if (sessionlist.indexOf(data) > -1) {
|
||||
sessionlist.splice(sessionlist.indexOf(data),1)
|
||||
settings.write('recentSessions',sessionlist)
|
||||
}
|
||||
},
|
||||
|
||||
sessionOpen: function(data,clientId) {
|
||||
|
||||
var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(),
|
||||
error = file === false && data.path ? 'Session file "' + data.path + '" not found.' : false,
|
||||
session
|
||||
|
||||
try {
|
||||
session = JSON.parse(file)
|
||||
} catch(err) {
|
||||
error = err
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
|
||||
ipc.send('sessionOpen', session,clientId)
|
||||
|
||||
|
||||
for (var i in openedSessions) {
|
||||
if (openedSessions[i].indexOf(clientId) != -1) {
|
||||
openedSessions[i].splice(openedSessions[i].indexOf(clientId), 1)
|
||||
}
|
||||
}
|
||||
|
||||
if (data.path) {
|
||||
|
||||
if (!settings.read('readOnly')) this.sessionAddToHistory(data.path)
|
||||
|
||||
fs.lstat(data.path, (err, stats)=>{
|
||||
|
||||
if (err || !stats.isFile()) return
|
||||
|
||||
|
||||
if (!openedSessions[data.path]) {
|
||||
|
||||
openedSessions[data.path] = []
|
||||
|
||||
var watchFile = ()=>{
|
||||
var watcher = chokidar.watch(data.path)
|
||||
watcher.on('change',()=>{
|
||||
var openedSessionsClone = deepCopy(openedSessions[data.path])
|
||||
for (var k in openedSessionsClone) {
|
||||
if (openedSessionsClone[k] != lastSavingClient) {
|
||||
module.exports.sessionOpen({path:data.path}, openedSessionsClone[k])
|
||||
}
|
||||
}
|
||||
watcher.close()
|
||||
watchFile()
|
||||
})
|
||||
}
|
||||
|
||||
watchFile()
|
||||
|
||||
|
||||
}
|
||||
|
||||
openedSessions[data.path].push(clientId)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
ipc.send('error', `Invalid session file (${error})`)
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
sessionOpened: function(data, clientId) {
|
||||
|
||||
if (settings.read('stateFile')) {
|
||||
var send = true
|
||||
for (var id in ipc.clients) {
|
||||
// only make the client send its osc state if there are no other active clients
|
||||
if (id !== clientId && ipc.clients[id].connected()) {
|
||||
send = false
|
||||
}
|
||||
}
|
||||
var state = {
|
||||
state: settings.read('stateFile'),
|
||||
send: send
|
||||
}
|
||||
ipc.send('stateLoad', state, clientId)
|
||||
}
|
||||
|
||||
ipc.send('stateSend', null, null, clientId)
|
||||
|
||||
},
|
||||
|
||||
savingSession: function(data, clientId) {
|
||||
|
||||
lastSavingClient = clientId
|
||||
|
||||
},
|
||||
|
||||
syncOsc: function(shortdata, clientId) {
|
||||
|
||||
if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return
|
||||
|
||||
var value = shortdata.v,
|
||||
data = widgetHashTable[clientId][shortdata.h]
|
||||
|
||||
data = deepCopy(data)
|
||||
for (var k in shortdata) {
|
||||
data[k] = shortdata[k]
|
||||
}
|
||||
|
||||
data.args = data.preArgs ? data.preArgs.concat(value) : [value]
|
||||
|
||||
if (!data.noSync) ipc.send('receiveOsc', data, null, clientId)
|
||||
|
||||
|
||||
},
|
||||
|
||||
sendOsc: function(shortdata, clientId) {
|
||||
|
||||
if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return
|
||||
|
||||
var value = shortdata.v,
|
||||
data = widgetHashTable[clientId][shortdata.h]
|
||||
|
||||
data = deepCopy(data)
|
||||
for (var k in shortdata) {
|
||||
data[k] = shortdata[k]
|
||||
}
|
||||
|
||||
data.args = data.preArgs ? data.preArgs.concat(value) : [value]
|
||||
|
||||
if (data.target) {
|
||||
|
||||
var targets = []
|
||||
|
||||
if (data.target.indexOf(null) === -1 && settings.read('targets') && !shortdata.target) Array.prototype.push.apply(targets, settings.read('targets'))
|
||||
if (data.target) Array.prototype.push.apply(targets, data.target)
|
||||
|
||||
for (var i in targets) {
|
||||
|
||||
if (targets[i] === 'self') {
|
||||
ipc.send('receiveOsc',data,clientId)
|
||||
continue
|
||||
} else if (targets[i] === null) {
|
||||
continue
|
||||
}
|
||||
|
||||
var host = targets[i].split(':')[0],
|
||||
port = targets[i].split(':')[1]
|
||||
|
||||
if (port) {
|
||||
|
||||
if (data.split) {
|
||||
|
||||
for (var j in data.split) {
|
||||
osc.send(host,port,data.split[j],data.args[j],data.precision)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
osc.send(host,port,data.address,data.args,data.precision)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!data.noSync) ipc.send('receiveOsc', data, null, clientId)
|
||||
|
||||
},
|
||||
|
||||
addWidget(data, clientId) {
|
||||
|
||||
if (!widgetHashTable[clientId]) {
|
||||
widgetHashTable[clientId] = {}
|
||||
}
|
||||
|
||||
if (!widgetHashTable[clientId][data.hash]) {
|
||||
widgetHashTable[clientId][data.hash] = {}
|
||||
}
|
||||
|
||||
var cache = widgetHashTable[clientId][data.hash],
|
||||
widgetData = data.data
|
||||
|
||||
for (var k in widgetData) {
|
||||
if ((k === 'target' || k === 'preArgs')) {
|
||||
if (widgetData[k] !== '') {
|
||||
cache[k] = Array.isArray(widgetData[k]) ? widgetData[k] : [widgetData[k]]
|
||||
} else {
|
||||
cache[k] = []
|
||||
}
|
||||
} else {
|
||||
cache[k] = widgetData[k]
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
removeWidget(data, clientId) {
|
||||
|
||||
delete widgetHashTable[clientId][data.hash]
|
||||
|
||||
},
|
||||
|
||||
removeClientWidgets(clientId) {
|
||||
|
||||
if (widgetHashTable[clientId]) {
|
||||
delete widgetHashTable[clientId]
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
stateSave: function(data) {
|
||||
dialog.showSaveDialog(window,{title:'Save current state to preset file',defaultPath:settings.read('presetPath').replace(settings.read('presetPath').split('/').pop(),''),filters: [ { name: 'OSC Preset', extensions: ['preset'] }]},function(file){
|
||||
|
||||
if (file==undefined) {return}
|
||||
settings.write('presetPath',file)
|
||||
|
||||
if (file.indexOf('.preset')==-1){file+='.preset'}
|
||||
fs.writeFile(file,data, function(err, data) {
|
||||
if (err) throw err
|
||||
console.log('The current state was saved in '+file)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
fullscreen: function(data) {
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
},
|
||||
|
||||
reload: function(data) {
|
||||
|
||||
ipc.send('reload')
|
||||
|
||||
},
|
||||
|
||||
storeBackup: function(data) {
|
||||
|
||||
sessionBackups[data.backupId] = data
|
||||
|
||||
},
|
||||
|
||||
deleteBackup: function(data) {
|
||||
|
||||
delete sessionBackups[data]
|
||||
|
||||
},
|
||||
|
||||
reloadCss:function(){
|
||||
theme.load()
|
||||
ipc.send('reloadCss')
|
||||
},
|
||||
|
||||
log: function(data) {
|
||||
console.log(data)
|
||||
},
|
||||
|
||||
error: function(data) {
|
||||
console.error(data)
|
||||
},
|
||||
|
||||
errorLog: function(data) {
|
||||
console.error(data)
|
||||
},
|
||||
|
||||
errorPopup: function(data) {
|
||||
ipc.send('error', data)
|
||||
}
|
||||
|
||||
}
|
||||
110
src/server/electron-app.js
Normal file
110
src/server/electron-app.js
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
var {app, Menu, shell} = require('electron'),
|
||||
settings = require('./settings'),
|
||||
infos = require('../package.json')
|
||||
|
||||
app.commandLine.appendSwitch('--touch-events')
|
||||
|
||||
if (settings.read('noVsync') || (!settings.cli && settings.read('argv')['disable-vsync'])) {
|
||||
app.commandLine.appendSwitch('--disable-gpu-vsync')
|
||||
}
|
||||
|
||||
if (settings.read('noGpu') || (!settings.cli && settings.read('argv')['disable-gpu'])) {
|
||||
app.disableHardwareAcceleration()
|
||||
}
|
||||
|
||||
var template = [{
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
{role: 'undo', accelerator: 'CmdOrCtrl+Z'},
|
||||
{role: 'redo', accelerator: 'Shift+CmdOrCtrl+Z'},
|
||||
{type: 'separator'},
|
||||
{role: 'cut', accelerator: 'CmdOrCtrl+X'},
|
||||
{role: 'copy', accelerator: 'CmdOrCtrl+C'},
|
||||
{role: 'paste', accelerator: 'CmdOrCtrl+V'}
|
||||
]
|
||||
}]
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
// Add app menu (OS X)
|
||||
template.unshift({
|
||||
label: settings.read('appName'),
|
||||
submenu: [
|
||||
{
|
||||
label: 'Hide ' + infos.productName,
|
||||
accelerator: 'Command+H',
|
||||
role: 'hide'
|
||||
},
|
||||
{
|
||||
label: 'Hide Others',
|
||||
accelerator: 'Command+Alt+H',
|
||||
role: 'hideothers'
|
||||
},
|
||||
{
|
||||
label: 'Show All',
|
||||
role: 'unhide'
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Quit',
|
||||
accelerator: 'Command+Q',
|
||||
click: ()=>app.quit()
|
||||
}
|
||||
]
|
||||
})
|
||||
template.push(
|
||||
{
|
||||
label: 'Window',
|
||||
role: 'window',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Minimize',
|
||||
accelerator: 'CmdOrCtrl+M',
|
||||
role: 'minimize'
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Bring All to Front',
|
||||
role: 'front'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Help',
|
||||
role: 'help',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Documentation',
|
||||
click: ()=>shell.openExternal(infos.homepage)
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Report an Issue',
|
||||
click: ()=>shell.openExternal(infos.repository.url + '/issues')
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
app.on('before-quit',()=>{
|
||||
process.exit()
|
||||
})
|
||||
|
||||
app.on('ready',()=>{
|
||||
var menu = Menu.buildFromTemplate(template)
|
||||
Menu.setApplicationMenu(menu)
|
||||
})
|
||||
|
||||
app.on('window-all-closed', function() {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
87
src/server/electron-window.js
Normal file
87
src/server/electron-window.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
var path = require('path'),
|
||||
{BrowserWindow, dialog} = require('electron'),
|
||||
shortcut = require('electron-localshortcut'),
|
||||
settings = require('./settings'),
|
||||
theme = require('./theme')
|
||||
|
||||
|
||||
module.exports = function(options={}) {
|
||||
|
||||
var window
|
||||
|
||||
window = new BrowserWindow({
|
||||
width: options.width || 800,
|
||||
height: options.height || 600,
|
||||
title: options.title || settings.read('appName'),
|
||||
icon: path.resolve(__dirname + '/../client/logo.png'),
|
||||
backgroundColor: options.color || theme.backgroundColor,
|
||||
type: options.type,
|
||||
fullscreen: options.fullscreen,
|
||||
useContentSize: true,
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
nodeIntegration: !!options.node
|
||||
}
|
||||
})
|
||||
|
||||
window.webContents.on('will-prevent-unload', (event)=>{
|
||||
var choice = dialog.showMessageBox(window, {
|
||||
type: 'question',
|
||||
buttons: ['Yes', 'No'],
|
||||
title: 'Are you sure ?',
|
||||
message: 'Unsaved data will be lost. Are you sure you want to quit?',
|
||||
defaultId: 0,
|
||||
cancelId: 1
|
||||
})
|
||||
if (choice === 0) {
|
||||
event.preventDefault()
|
||||
}
|
||||
})
|
||||
|
||||
// window.webContents.on('will-navigate', (event)=>event.preventDefault())
|
||||
window.webContents.on('new-window', (event)=>event.preventDefault())
|
||||
|
||||
if (options.fullscreen) {
|
||||
window.webContents.on('dom-ready', ()=>{
|
||||
window.webContents.executeJavaScript(`
|
||||
window.ELECTRON_FULLSCREEN = true
|
||||
`)
|
||||
})
|
||||
}
|
||||
|
||||
if (options.zoom === false) {
|
||||
window.webContents.setVisualZoomLevelLimits(1, 1)
|
||||
}
|
||||
|
||||
|
||||
window.on('closed', function() {
|
||||
window = null
|
||||
})
|
||||
|
||||
window.setMenu(null)
|
||||
|
||||
window.loadURL(options.address)
|
||||
|
||||
if (options.shortcuts) {
|
||||
|
||||
shortcut.register(window,'CmdOrCtrl+R',function(){
|
||||
window.reload()
|
||||
})
|
||||
|
||||
shortcut.register(window,'F11',function(){
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
})
|
||||
|
||||
shortcut.register(window,'F12',function(){
|
||||
window.toggleDevTools()
|
||||
})
|
||||
|
||||
} else {
|
||||
shortcut.register(window,'CmdOrCtrl+R',function(e){
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
return window
|
||||
|
||||
}
|
||||
145
src/server/index.js
Normal file
145
src/server/index.js
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
var dev = process.argv[0].includes('node_modules'),
|
||||
settings = require('./settings')
|
||||
|
||||
var serverStarted
|
||||
|
||||
function nodeMode() {
|
||||
|
||||
console.warn('Running with node')
|
||||
|
||||
require('source-map-support').install({handleUncaughtExceptions: false})
|
||||
|
||||
if (!settings.read('noGui')) {
|
||||
settings.cli = true
|
||||
settings.write('noGui', true, true)
|
||||
console.warn('Headless mode (--no-gui) enabled automatically')
|
||||
}
|
||||
|
||||
process.on('uncaughtException', (err)=>{
|
||||
console.error('A JavaScript error occurred in the main process:')
|
||||
console.error(err.stack)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if (process.title === 'node' || process.title === 'node.exe') {
|
||||
|
||||
nodeMode()
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
|
||||
require('electron').dialog.showErrorBox = (title, err)=>{
|
||||
console.error(title + ': ' + err)
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
|
||||
nodeMode()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var start = function(readyApp) {
|
||||
|
||||
if (!settings.read('guiOnly') && !serverStarted) {
|
||||
|
||||
var server = require('./server'),
|
||||
osc = require('./osc'),
|
||||
callbacks = require('./callbacks'),
|
||||
zeroconf = require('./zeroconf')
|
||||
|
||||
server.bindCallbacks(callbacks)
|
||||
|
||||
serverStarted = true
|
||||
process.on('exit',()=>{
|
||||
if (osc.midi) osc.midi.stop()
|
||||
zeroconf.unpublishAll()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if (!settings.read('noGui')) {
|
||||
|
||||
var app = require('./electron-app')
|
||||
var address = typeof settings.read('guiOnly')=='string'? 'http://' + settings.read('guiOnly') : settings.read('appAddresses')[0]
|
||||
address += settings.read('urlOptions')
|
||||
|
||||
var launch = ()=>{
|
||||
var win = require('./electron-window')({address:address, shortcuts:true, zoom:false, fullscreen: settings.read('fullScreen')})
|
||||
return win
|
||||
}
|
||||
if (app.isReady()) {
|
||||
return launch()
|
||||
} else {
|
||||
app.on('ready',function(){
|
||||
launch()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (settings.cli) {
|
||||
|
||||
start()
|
||||
|
||||
} else {
|
||||
|
||||
var app = require('./electron-app'),
|
||||
path = require('path'),
|
||||
address = 'file://' + path.resolve(__dirname + '/../launcher/' + 'index.html'),
|
||||
{ipcMain} = require('electron'),
|
||||
launcher
|
||||
|
||||
app.on('ready',function(){
|
||||
global.settings = settings
|
||||
launcher = require('./electron-window')({address:address, shortcuts:dev, width:680, height:(100 + 8*3 + 29 * Object.keys(settings.options).filter(x=>settings.options[x].launcher !== false).length), node:true, color:'#253040'})
|
||||
})
|
||||
|
||||
if (process.log) {
|
||||
process.log = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process, arguments)
|
||||
launcher.webContents.send('stdout', string)
|
||||
}
|
||||
})(process.log)
|
||||
}
|
||||
|
||||
process.stdout.write = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process.stdout, arguments)
|
||||
launcher.webContents.send('stdout', string)
|
||||
}
|
||||
})(process.stdout.write)
|
||||
|
||||
process.stderr.write = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process.stdout, arguments)
|
||||
launcher.webContents.send('stderr', string)
|
||||
}
|
||||
})(process.stderr.write)
|
||||
|
||||
|
||||
ipcMain.on('start',function(e, options){
|
||||
|
||||
var gui = start()
|
||||
|
||||
if (settings.read('guiOnly')) {
|
||||
launcher.hide()
|
||||
gui.on('close',()=>{
|
||||
launcher.close()
|
||||
})
|
||||
} else {
|
||||
launcher.webContents.send('started')
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
119
src/server/ipc/client.js
Normal file
119
src/server/ipc/client.js
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
var EventEmitter = require('events').EventEmitter
|
||||
|
||||
var hearbeatInterval = 25000,
|
||||
hearbeatTimeout = 5000,
|
||||
dieTimeout = 60000
|
||||
|
||||
class Socket extends EventEmitter {
|
||||
|
||||
constructor(ws, id) {
|
||||
|
||||
super()
|
||||
|
||||
this.id = id
|
||||
this.socket = null
|
||||
|
||||
this.queue = []
|
||||
|
||||
this.hearbeat = undefined
|
||||
this.hearbeatTimeout = undefined
|
||||
this.on('pong', ()=>{
|
||||
this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout)
|
||||
})
|
||||
this.on('ping', ()=>{
|
||||
this.socket.send('["pong"]')
|
||||
})
|
||||
|
||||
this.dieTimeout = undefined
|
||||
|
||||
this.open(ws)
|
||||
|
||||
}
|
||||
|
||||
open(ws) {
|
||||
|
||||
this.socket = ws
|
||||
|
||||
this.socket.onmessage = (e)=>{
|
||||
this.receive(e.data)
|
||||
}
|
||||
|
||||
this.socket.onclose = this.socket.onerror = ()=>{
|
||||
this.close()
|
||||
}
|
||||
|
||||
this.dieTimeout = clearTimeout(this.dieTimeout)
|
||||
|
||||
this.hearbeat = setInterval(()=>{
|
||||
if (!this.connected()) return
|
||||
this.socket.send('["ping"]')
|
||||
this.hearbeatTimeout = setTimeout(()=>{
|
||||
if (this.connected()) this.socket.close()
|
||||
}, hearbeatTimeout)
|
||||
}, hearbeatInterval)
|
||||
|
||||
}
|
||||
|
||||
close() {
|
||||
|
||||
this.hearbeat = clearInterval(this.hearbeat)
|
||||
this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout)
|
||||
this.dieTimeout = clearTimeout(this.dieTimeout)
|
||||
|
||||
this.dieTimeout = setTimeout(()=>{
|
||||
|
||||
this.emit('close')
|
||||
|
||||
}, dieTimeout)
|
||||
|
||||
}
|
||||
|
||||
connected() {
|
||||
|
||||
return this.socket.readyState == this.socket.OPEN
|
||||
|
||||
}
|
||||
|
||||
receive(message) {
|
||||
|
||||
if (typeof message == 'string') {
|
||||
|
||||
var packet = JSON.parse(message)
|
||||
|
||||
if (Array.isArray(packet) && typeof packet[0] == 'string') {
|
||||
|
||||
this.emit(packet[0], packet[1])
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
send(event, data) {
|
||||
|
||||
var packet = JSON.stringify([event, data])
|
||||
|
||||
if (this.connected()) {
|
||||
this.socket.send(packet)
|
||||
} else {
|
||||
this.queue.push(packet)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
flush(){
|
||||
|
||||
for (var i in this.queue) {
|
||||
|
||||
if (this.connected()) this.socket.send(this.queue[i])
|
||||
|
||||
}
|
||||
|
||||
this.queue = []
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Socket
|
||||
59
src/server/ipc/server.js
Normal file
59
src/server/ipc/server.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
var EventEmitter = require('events').EventEmitter,
|
||||
WebSocketServer = require('ws').Server,
|
||||
Client = require('./client')
|
||||
|
||||
class Ipc extends EventEmitter {
|
||||
|
||||
constructor(server) {
|
||||
|
||||
super()
|
||||
|
||||
this.clients = {}
|
||||
this.server = null
|
||||
|
||||
this.server = new WebSocketServer({server: server})
|
||||
|
||||
this.server.on('connection', (socket, req)=>{
|
||||
|
||||
var id = req.url.split('/').pop()
|
||||
|
||||
if (!id) return
|
||||
|
||||
if (!this.clients[id]) {
|
||||
|
||||
var client = new Client(socket, id)
|
||||
this.clients[id] = client
|
||||
|
||||
this.emit('connection', client)
|
||||
|
||||
client.on('close', ()=>{
|
||||
delete this.clients[id]
|
||||
})
|
||||
|
||||
} else {
|
||||
|
||||
this.clients[id].open(socket)
|
||||
this.clients[id].flush()
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
send(event, data, id, except) {
|
||||
|
||||
var clients = id ? [this.clients[id]] : this.clients
|
||||
|
||||
for (var k in clients) {
|
||||
if (!except || this.clients[k] != this.clients[except]) {
|
||||
if (clients[k]) clients[k].send(event, data)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = Ipc
|
||||
64
src/server/midi.js
Normal file
64
src/server/midi.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
var {PythonShell} = require('python-shell'),
|
||||
settings = require('./settings')
|
||||
|
||||
class Midi {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.py = new PythonShell('midi.py', {
|
||||
scriptPath:__dirname,
|
||||
mode:'text',
|
||||
args: [
|
||||
settings.read('debug') ? 'debug' : '',
|
||||
...settings.read('midi')
|
||||
]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
send(data) {
|
||||
|
||||
var args = []
|
||||
for (i in data.args) {
|
||||
args.push(data.args[i].value)
|
||||
}
|
||||
|
||||
this.py.send(JSON.stringify([data.port, data.address, ...args]))
|
||||
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
||||
this.py.childProcess.kill()
|
||||
|
||||
}
|
||||
|
||||
init(receiveOsc) {
|
||||
this.py.on('message', function(message) {
|
||||
// console.log(message)
|
||||
var name, data
|
||||
try {
|
||||
[name, data] = JSON.parse(message)
|
||||
} catch (err) {
|
||||
// console.log(err)
|
||||
}
|
||||
if (name == 'log') {
|
||||
if (data.indexOf('ERROR') === 0) {
|
||||
console.error(data)
|
||||
} else {
|
||||
console.log(data)
|
||||
}
|
||||
} else if (name == 'osc') {
|
||||
receiveOsc(data)
|
||||
} else if (name == 'error') {
|
||||
console.error('ERROR: MIDI: ' + data)
|
||||
this.stop()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var midi = new Midi()
|
||||
|
||||
module.exports = midi
|
||||
234
src/server/osc/index.js
Normal file
234
src/server/osc/index.js
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// destroy serialport module
|
||||
// (electron-rebuild breaks it for node-only mode)
|
||||
require('vmodule')('serialport', {}, {global: true})
|
||||
|
||||
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'),
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
|
||||
class OscServer {
|
||||
|
||||
constructor(){
|
||||
|
||||
this.customModuleEventEmitter = new EventEmitter()
|
||||
this.customModule = (()=>{
|
||||
|
||||
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),
|
||||
app: this.customModuleEventEmitter,
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
settings
|
||||
}
|
||||
|
||||
try {
|
||||
mod = vm.runInContext(file, vm.createContext(context))
|
||||
} catch(err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
return mod
|
||||
|
||||
})()
|
||||
|
||||
|
||||
}
|
||||
|
||||
parseType(type){
|
||||
var t = type[0].match(/[bhtdscrmifTFNI]/)
|
||||
|
||||
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
|
||||
|
||||
return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))}
|
||||
case 'boolean':
|
||||
return {type: arg ? 'T' : 'F', value: arg}
|
||||
case 'object':
|
||||
if (arg.type) {
|
||||
return {type: this.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)}
|
||||
}
|
||||
}
|
||||
|
||||
sendOsc(data){
|
||||
|
||||
if (!data) return
|
||||
|
||||
if (data.host == 'midi') {
|
||||
|
||||
if (midi) midi.send(data)
|
||||
|
||||
} else {
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
receiveOsc(data, info){
|
||||
|
||||
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]
|
||||
|
||||
ipc.send('receiveOsc',data)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
oscOutFilter(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})
|
||||
if (!data) return
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
init(){
|
||||
|
||||
if (midi) midi.init((data)=>{
|
||||
|
||||
data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port})
|
||||
|
||||
if (!data) return
|
||||
|
||||
this.receiveOsc(data)
|
||||
|
||||
})
|
||||
|
||||
oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscUDPServer.open()
|
||||
|
||||
if (settings.read('tcpInPort')) {
|
||||
oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscTCPServer.open()
|
||||
}
|
||||
|
||||
if (this.customModule.init) {
|
||||
this.customModule.init()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
var oscServer = new OscServer()
|
||||
oscServer.init()
|
||||
|
||||
module.exports = {
|
||||
server: oscServer,
|
||||
send: function(host,port,address,args,precision) {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port})
|
||||
|
||||
oscServer.sendOsc(data)
|
||||
|
||||
},
|
||||
midi:midi
|
||||
}
|
||||
146
src/server/osc/tcp.js
Normal file
146
src/server/osc/tcp.js
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
var osc = require('osc'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
tcpTargets = settings.read('tcpTargets'),
|
||||
net = require('net')
|
||||
|
||||
class OscTCPClient extends EventEmitter {
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super()
|
||||
|
||||
this.options = options
|
||||
this.remoteAddress = options.address
|
||||
this.remotePort = options.port
|
||||
|
||||
this.open()
|
||||
|
||||
}
|
||||
|
||||
open() {
|
||||
|
||||
this.port = new osc.TCPSocketPort(this.options)
|
||||
|
||||
this.port.on('error', (err)=>{
|
||||
console.error(`TCP: Error: ${err.message}`)
|
||||
})
|
||||
|
||||
this.port.on('close', (hadErr)=>{
|
||||
console.log(`TCP: connection closed (${this.remoteAddress}:${this.remotePort})`)
|
||||
if (!this.options.socket) {
|
||||
console.log(`TCP: attempting reconnection in 5s (${this.remoteAddress}:${this.remotePort})`)
|
||||
this.reconnect()
|
||||
} else {
|
||||
this.emit('die')
|
||||
}
|
||||
})
|
||||
|
||||
this.port.on('message', (msg, timetag)=>{
|
||||
this.emit('message', msg, timetag, {address: this.remoteAddress, port: this.remotePort})
|
||||
})
|
||||
|
||||
this.port.on('ready', ()=>{
|
||||
console.log(`TCP: connection established (${this.remoteAddress}:${this.remotePort})`)
|
||||
})
|
||||
|
||||
if (!this.options.socket) {
|
||||
|
||||
this.port.open()
|
||||
|
||||
} else {
|
||||
|
||||
this.port.emit('ready')
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
reconnect() {
|
||||
|
||||
setTimeout(()=>{
|
||||
this.open()
|
||||
}, 5000)
|
||||
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
|
||||
this.port.send(msg)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class OscTCPServer extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
this.clients = {}
|
||||
|
||||
}
|
||||
|
||||
bindSocket(socket) {
|
||||
|
||||
this.addClient(new OscTCPClient({
|
||||
metadata: true,
|
||||
socket: socket,
|
||||
address: socket.remoteAddress.indexOf('::ffff:') === 0 ? socket.remoteAddress.replace('::ffff:', '') : socket.remoteAddress,
|
||||
port: socket.remotePort
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
addClient(client) {
|
||||
|
||||
this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {}
|
||||
this.clients[client.remoteAddress][client.remotePort] = client
|
||||
|
||||
client.on('message', (msg, timetag, info)=>{
|
||||
this.emit('message', msg, timetag, info)
|
||||
})
|
||||
|
||||
client.on('die', (hadError)=>{
|
||||
if (this.clients[client.remoteAddress]) delete this.clients[client.remoteAddress][client.remotePort]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
open() {
|
||||
|
||||
this.server = net.createServer(this.bindSocket.bind(this))
|
||||
this.server.listen({port: tcpInPort})
|
||||
|
||||
for (var i in tcpTargets) {
|
||||
|
||||
this.addClient(new OscTCPClient({
|
||||
metadata: true,
|
||||
address: tcpTargets[i].split(':')[0],
|
||||
port: tcpTargets[i].split(':')[1]
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
zeroconf.publish({
|
||||
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}`)
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var oscTCPServer = new OscTCPServer()
|
||||
|
||||
module.exports = oscTCPServer
|
||||
26
src/server/osc/udp.js
Normal file
26
src/server/osc/udp.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
var osc = require('osc'),
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
oscInPort = settings.read('oscInPort') || settings.read('httpPort')
|
||||
|
||||
var oscUDPServer = new osc.UDPPort({
|
||||
localAddress: '0.0.0.0',
|
||||
localPort: oscInPort,
|
||||
metadata: true,
|
||||
broadcast: true
|
||||
})
|
||||
|
||||
oscUDPServer.on('error', function(error) {
|
||||
console.error(error)
|
||||
})
|
||||
|
||||
zeroconf.publish({
|
||||
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}`)
|
||||
})
|
||||
|
||||
module.exports = oscUDPServer
|
||||
76
src/server/server.js
Normal file
76
src/server/server.js
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
var express = require('express')(),
|
||||
path = require('path'),
|
||||
http = require('http'),
|
||||
server = http.createServer(express),
|
||||
Ipc = require('./ipc/server'),
|
||||
ipc = new Ipc(server),
|
||||
settings = require('./settings'),
|
||||
theme = require('./theme').init(),
|
||||
zeroconf = require('./zeroconf'),
|
||||
appAddresses = settings.read('appAddresses'),
|
||||
osc = {},
|
||||
clients = {}
|
||||
|
||||
express.get('/', function(req, res){
|
||||
res.sendFile(path.resolve(__dirname + '/../browser/index.html'))
|
||||
})
|
||||
|
||||
express.get('*', function(req, res){
|
||||
if (req.path.indexOf('theme.css') != -1) {
|
||||
res.set('Content-Type', 'text/css')
|
||||
if (settings.read('theme')) {
|
||||
var str = theme.get(),
|
||||
buf = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from(str) : new Buffer(str)
|
||||
res.send(buf)
|
||||
} else {
|
||||
res.send('')
|
||||
}
|
||||
} else if (req.path.indexOf('client/') != -1){
|
||||
res.sendFile(path.resolve(__dirname + '/..' + req.path))
|
||||
} else {
|
||||
res.sendFile(path.resolve(req.path))
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(settings.read('httpPort'))
|
||||
|
||||
zeroconf.publish({
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
type: 'http',
|
||||
port: settings.read('httpPort')
|
||||
}).on('error', (e)=>{
|
||||
console.error(`Error: Zeroconf: ${e.message}`)
|
||||
})
|
||||
|
||||
var bindCallbacks = function(callbacks) {
|
||||
|
||||
ipc.on('connection', function(client) {
|
||||
|
||||
for (let name in callbacks) {
|
||||
client.on(name, (data)=>{
|
||||
if (osc.customModule) {
|
||||
osc.customModuleEventEmitter.emit(name, data, client.id)
|
||||
}
|
||||
callbacks[name](data, client.id)
|
||||
})
|
||||
}
|
||||
|
||||
client.on('close', function() {
|
||||
|
||||
callbacks.removeClientWidgets(client.id)
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
console.log('App available at ' + appAddresses.join(' & '))
|
||||
|
||||
module.exports = {
|
||||
ipc:ipc,
|
||||
bindCallbacks:bindCallbacks,
|
||||
clients:clients
|
||||
}
|
||||
|
||||
osc = require('./osc').server
|
||||
195
src/server/settings.js
Normal file
195
src/server/settings.js
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
// This prevents argv parsing to be breaked when the app is packaged (executed without 'electron' prefix)
|
||||
if (process.argv[1]&&process.argv[1].indexOf('-')==0) process.argv.unshift('')
|
||||
|
||||
var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
|
||||
configPath = require('path').join(baseDir, '.open-stage-control'),
|
||||
fs = require('fs'),
|
||||
ifaces = require('os').networkInterfaces()
|
||||
|
||||
var options = {
|
||||
's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port pairs)',
|
||||
check: (s)=>{
|
||||
return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ?
|
||||
'Targets must be ip:port pairs & port must be >= 1024' : true
|
||||
}
|
||||
},
|
||||
'sync':{type:'array',describe:false,launcher:false,hidden:true,
|
||||
check: (s)=>{
|
||||
if (s) console.error('Warning: --sync is deprecated, use --send instead.')
|
||||
return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ?
|
||||
'Targets must be ip:port pairs & port must be >= 1024' : true
|
||||
}
|
||||
},
|
||||
'l':{alias:'load',type:'string',file:{name:'OSC Session (.json)', extensions: ['json', 'js']},describe:'session file to load'},
|
||||
'state':{type:'string',file:{name:'OSC State (.state)', extensions: ['state']},describe:'state file to load'},
|
||||
'b':{alias:'blank',type:'boolean',describe:'load a blank session and start the editor'},
|
||||
'c':{alias:'custom-module',type:'string',file:{name:'OSC Custom module (.js)', extensions: ['js']},describe:'custom module file to load'},
|
||||
'p':{alias:'port',type:'number',describe:'http port of the server (default to 8080)',
|
||||
check: (p)=>{
|
||||
return (!isNaN(p) && p > 1023 && parseInt(p)===p) ?
|
||||
true : 'Port must be an integer >= 1024'
|
||||
}
|
||||
},
|
||||
'o':{alias:'osc-port',type:'number',describe:'osc input port (default to --port)',
|
||||
check: (o)=>{
|
||||
return (!isNaN(o) && o > 1023 && parseInt(o)===o) ?
|
||||
true : 'Port must be an integer >= 1024'
|
||||
}
|
||||
},
|
||||
'tcp-port':{type:'number', describe:'TCP server input port',
|
||||
check: (t, argv)=>{
|
||||
var h = argv['port'] || 8080
|
||||
if (t===h) return 'TCP input port must different from --port'
|
||||
return (!isNaN(t) && t > 1023 && parseInt(t)===t) ?
|
||||
true : 'Port must be an integer >= 1024'
|
||||
}
|
||||
},
|
||||
'tcp-targets':{type:'array',describe:'TCP servers to connect to (ip:port pairs), does not susbtitute for --send',
|
||||
check: (s, argv)=>{
|
||||
if (!argv['tcp-port']) return '--tcp-port must be set'
|
||||
return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ?
|
||||
'Targets must be ip:port pairs & port must be >= 1024' : true
|
||||
}
|
||||
},
|
||||
'm':{alias:'midi',type:'array',describe:'midi router settings'},
|
||||
'd':{alias:'debug',type:'boolean',describe:'log received osc messages in the console'},
|
||||
'n':{alias:'no-gui',type:'boolean',describe:'disable default gui',
|
||||
check: (n,argv)=>{
|
||||
return (!n || !argv.g) ?
|
||||
true : 'no-gui and gui-only can\'s be enabled simultaneously'
|
||||
}
|
||||
},
|
||||
'g':{alias:'gui-only',type:'string',describe:'app server\'s url. If true, local port (--port) is used',
|
||||
check: (g,argv)=>{
|
||||
return (!g || !argv.n) ?
|
||||
true : 'no-gui and gui-only can\'s be enabled simultaneously'
|
||||
}
|
||||
},
|
||||
't':{alias:'theme',type:'array',describe:'theme name or path (mutliple values allowed)'},
|
||||
'e':{alias:'examples',type:'boolean',describe:'list examples instead of recent sessions',
|
||||
check: (e,argv)=>{
|
||||
return (!e || !argv.l) ?
|
||||
true : 'examples can\'t be listed if --load is set'
|
||||
}
|
||||
},
|
||||
'url-options':{type:'array',describe:'url options (opt=value pairs)',
|
||||
check: (u, argv)=>{
|
||||
return (!u || !argv.n) ?
|
||||
true : 'url options can\'t be passed in no-gui mode'
|
||||
}
|
||||
},
|
||||
'disable-vsync':{type:'boolean',describe:'disable gui\'s vertical synchronization', restart: true},
|
||||
'disable-gpu':{type:'boolean',describe:'disable hardware acceleration', restart: true},
|
||||
'read-only':{type:'boolean',describe:'disable session editing and session history changes',
|
||||
check: (r, argv)=>{
|
||||
return (!r || !argv.b) ?
|
||||
true : 'blank session can\'t be started in read-only mode'
|
||||
}
|
||||
},
|
||||
'instance-name':{type:'string',describe:'used to differenciate multiple instances in a zeroconf network'},
|
||||
'fullscreen':{type:'boolean', describe:'launch the default client gui in fullscreen mode (F11 to exit)'}
|
||||
}
|
||||
|
||||
|
||||
var argv = require('yargs')
|
||||
.help('help').usage('\nUsage:\n $0 [options]').alias('h', 'help')
|
||||
.options(options)
|
||||
.check((argv)=>{
|
||||
var err = []
|
||||
for (key in options) {
|
||||
if (options[key].check && argv[key] != undefined) {
|
||||
var c = options[key].check(argv[key],argv)
|
||||
if (c!==true) {
|
||||
err.push(`-${key}: ${c}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
return err.length ? err.join('\n') : true
|
||||
})
|
||||
.strict()
|
||||
.version().alias('v','version')
|
||||
|
||||
argv = argv.argv
|
||||
|
||||
var cli = false
|
||||
for (i in argv) {
|
||||
if (i != '_' && i != '$0' && (argv[i]!=undefined && argv[i]!==false)) cli = true
|
||||
}
|
||||
|
||||
var configFile = function(){try {return JSON.parse(fs.readFileSync(configPath,'utf-8'))} catch(err) {return {}}}(),
|
||||
config = JSON.parse(JSON.stringify(configFile)),
|
||||
defaultConfig
|
||||
|
||||
|
||||
var makeDefaultConfig = function(argv){
|
||||
defaultConfig = {
|
||||
argv:argv,
|
||||
presetPath : process.cwd(),
|
||||
sessionPath: process.cwd(),
|
||||
recentSessions: [],
|
||||
appName: 'Open Stage Control',
|
||||
instanceName: argv['instance-name'] || false,
|
||||
targets: argv['send'] || argv.sync || false,
|
||||
oscInPort: argv['osc-port'] || 0,
|
||||
httpPort: argv['port'] || 8080,
|
||||
tcpInPort: argv['tcp-port'] || false,
|
||||
tcpTargets: argv['tcp-targets'] || [],
|
||||
debug: argv['debug'] || false,
|
||||
sessionFile: argv['load'] || false,
|
||||
newSession: argv['blank'] || false,
|
||||
customModule: argv['custom-module'] || false,
|
||||
fullScreen: argv['fullscreen'] || false,
|
||||
noGui: argv['no-gui'] || false,
|
||||
guiOnly: typeof argv['gui-only'] == 'string' ? argv['gui-only'].length ? argv['gui-only'] : true : false,
|
||||
urlOptions: argv['url-options'] ? '?' + argv['url-options'].join('&') : '',
|
||||
noVsync: argv['disable-vsync'] || false,
|
||||
noGpu: argv['disable-gpu'] || false,
|
||||
readOnly: argv['read-only'] || false,
|
||||
midi: argv['midi'],
|
||||
stateFile: (function(){
|
||||
if (!argv['state']) return false
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(argv['state'], 'utf8'))
|
||||
} catch(err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
appAddresses:function(){
|
||||
var appAddresses = []
|
||||
|
||||
Object.keys(ifaces).forEach(function(ifname) {
|
||||
for (i=0;i<ifaces[ifname].length;i++) {
|
||||
if (ifaces[ifname][i].family == 'IPv4') {
|
||||
appAddresses.push('http://' + ifaces[ifname][i].address + ':' + (argv['port'] || 8080))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return appAddresses
|
||||
}(),
|
||||
examples: argv['examples'],
|
||||
theme: argv['theme'] || []
|
||||
}
|
||||
}
|
||||
|
||||
makeDefaultConfig(argv)
|
||||
|
||||
module.exports = {
|
||||
argv:argv,
|
||||
options:options,
|
||||
makeDefaultConfig:makeDefaultConfig,
|
||||
read:function(key){
|
||||
var x = config[key] || defaultConfig[key]
|
||||
return x
|
||||
},
|
||||
write:function(key,value,tmp) {
|
||||
config[key] = value
|
||||
if (tmp) return
|
||||
configFile[key] = value
|
||||
fs.writeFile(configPath,JSON.stringify(configFile,null,4), function(err, data) {
|
||||
if (err) throw err
|
||||
})
|
||||
},
|
||||
cli: cli
|
||||
}
|
||||
76
src/server/theme.js
Normal file
76
src/server/theme.js
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
settings = require('./settings')
|
||||
|
||||
class Theme {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.themes = []
|
||||
this.files = []
|
||||
this.css = []
|
||||
|
||||
this.defaultColor = '#191f2a'
|
||||
this.backgroundColor = this.defaultColor
|
||||
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
this.themes = settings.read('theme')
|
||||
this.files = []
|
||||
|
||||
for (let theme of this.themes) {
|
||||
|
||||
if (theme.includes('.css') && fs.existsSync(theme)) {
|
||||
this.files.push(theme)
|
||||
} else if (!theme.includes('.css') && fs.existsSync(path.resolve(__dirname + '/../browser/themes/' + theme + '.css'))) {
|
||||
this.files.push(path.resolve(__dirname + '/../browser/themes/' + theme + '.css'))
|
||||
} else {
|
||||
console.error('Theme error: "' + theme + '" not found.')
|
||||
}
|
||||
}
|
||||
|
||||
this.load()
|
||||
|
||||
return this
|
||||
|
||||
}
|
||||
|
||||
load() {
|
||||
|
||||
this.css = []
|
||||
|
||||
for (let i in this.files) {
|
||||
|
||||
try {
|
||||
this.css.push(fs.readFileSync(this.files[i],'utf-8'))
|
||||
} catch(err) {
|
||||
console.error('Theme error: could not load "' + this.files[i] + '".')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var css = this.get()
|
||||
|
||||
if (css.includes('--color-bg:')) {
|
||||
this.backgroundColor = css.match(/--color-bg:([^;]*);/)[1].trim()
|
||||
} else {
|
||||
this.backgroundColor = this.defaultColor
|
||||
}
|
||||
|
||||
return this
|
||||
|
||||
}
|
||||
|
||||
get() {
|
||||
|
||||
return this.css.join('\n')
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var theme = new Theme()
|
||||
|
||||
module.exports = theme
|
||||
24
src/server/utils.js
Normal file
24
src/server/utils.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
module.exports = {
|
||||
|
||||
deepCopy: function(obj, precision) {
|
||||
|
||||
var copy = obj
|
||||
|
||||
if (obj === null) {
|
||||
return obj
|
||||
}
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
copy = Array.isArray(obj) ? [] : {}
|
||||
for (let key in obj) {
|
||||
copy[key] = module.exports.deepCopy(obj[key], precision)
|
||||
}
|
||||
} else if (typeof obj == 'number') {
|
||||
return precision === undefined ? copy : parseFloat(copy.toFixed(precision))
|
||||
}
|
||||
|
||||
return copy
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
3
src/server/zeroconf.js
Normal file
3
src/server/zeroconf.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var zeroconf = require('bonjour')()
|
||||
|
||||
module.exports = zeroconf
|
||||
Loading…
Add table
Add a link
Reference in a new issue