diff --git a/jukebox/children.js b/jukebox/child-process.js similarity index 52% rename from jukebox/children.js rename to jukebox/child-process.js index 214d0ad..6582c01 100644 --- a/jukebox/children.js +++ b/jukebox/child-process.js @@ -4,17 +4,16 @@ // there enough commonalities to warrant a base class? const spawn = require('child_process').spawn; - const EventEmitter = require('events').EventEmitter; -const TAGREADER = 'tagreader'; -const MP3PLAYER = 'mpg321'; - -class ChildEmitter extends EventEmitter { - constructor(process) { +module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter { + constructor(command) { super(); - this.childProcess = process; + var cmd = command[0], + args = command.slice(1); + + this.childProcess = spawn(cmd, args); this.childProcess.on('error', function(err) { this.emit('process_error', err); @@ -45,33 +44,4 @@ class ChildEmitter extends EventEmitter { } } -function launchChild(name, command) { - var cmd = command[0], - args = command.slice(1); - - return spawn(cmd, args); -} - -exports.init = function(config) { - // Set up tag reader - var tagreader = launchChild(TAGREADER, config.tag_reader); - exports.tagreader = new ChildEmitter(tagreader); - - // set up mp3 player - var mp3player = launchChild(MP3PLAYER, config.mpg321); - exports.mp3player = new ChildEmitter(mp3player); -}; - -exports.exitHandler = function(options, err) { - console.log('[player] closing children'); - if (err) console.log(err.stack); - - module.exports.tagreader.kill(); - module.exports.mp3player.kill(); - - if (options.exit) { - process.exit(); - } -}; - // vim:set ts=2 sw=2 et: diff --git a/jukebox/media-player.js b/jukebox/media-player.js new file mode 100644 index 0000000..f0af95d --- /dev/null +++ b/jukebox/media-player.js @@ -0,0 +1,15 @@ +"use strict"; + +const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; + +class MediaPlayer extends ChildProcessEmitter { + constructor(config) { + super(config.mpg321); + } +} + +module.exports = function(config) { + return new MediaPlayer(config); +}; + +// vim:set ts=2 sw=2 et: diff --git a/jukebox/tag-reader.js b/jukebox/tag-reader.js new file mode 100644 index 0000000..c44ed73 --- /dev/null +++ b/jukebox/tag-reader.js @@ -0,0 +1,15 @@ +"use strict"; + +const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; + +class TagReader extends ChildProcessEmitter { + constructor(config) { + super(config.tag_reader); + } +} + +module.exports = function(config) { + return new TagReader(config); +}; + +// vim:set ts=2 sw=2 et: diff --git a/player.js b/player.js index af89077..4214410 100644 --- a/player.js +++ b/player.js @@ -9,7 +9,8 @@ const views = require('./jukebox/views')(__dirname + '/templates/'); const express = require('express'); const logger = require('morgan') -const children = require('./jukebox/children'); +const MediaPlayer = require('./jukebox/media-player')(config); +const TagReader = require('./jukebox/tag-reader')(config); var app = express(); var server = require('http').createServer(); @@ -20,10 +21,20 @@ var wss = new WebSocketServer({server: server}); var play_log = []; -children.init(config); +function exitHandler(options, err) { + console.log('[player] closing children'); + if (err) console.log(err.stack); -process.on('exit', children.exitHandler.bind(null)); -process.on('SIGINT', children.exitHandler.bind(null, {exit:true})); + TagReader.kill(); + MediaPlayer.kill(); + + if (options.exit) { + process.exit(); + } +}; + +process.on('exit', exitHandler.bind(null)); +process.on('SIGINT', exitHandler.bind(null, {exit:true})); function setupChildLogging(name, child) { child.on('process_error', (err) => { @@ -39,16 +50,16 @@ function setupChildLogging(name, child) { }); } -setupChildLogging('tagreader', children.tagreader); -setupChildLogging('mp3player', children.mp3player); +setupChildLogging('tagreader', TagReader); +setupChildLogging('mediaplayer', MediaPlayer); -children.tagreader.on('message', (data) => { +TagReader.on('message', (data) => { processLine(data); }); if (config.debug) { - children.mp3player.on('message', (data) => { - console.log(`[mp3player] stdout: ${data}`); + MediaPlayer.on('message', (data) => { + console.log(`[mediaplayer] stdout: ${data}`); }); } @@ -103,7 +114,7 @@ function processLine(data) { debounce_until = Date.now() / 1000 + wait_sec; if (action) { console.log(`[action] [${action}] for [${last_tag}]`); - children.mp3player.send(action + "\n"); + MediaPlayer.send(action + "\n"); } }); }