diff --git a/lib/units/device/plugins/screen/stream.js b/lib/units/device/plugins/screen/stream.js index 36b231da..e819c45a 100644 --- a/lib/units/device/plugins/screen/stream.js +++ b/lib/units/device/plugins/screen/stream.js @@ -34,6 +34,7 @@ module.exports = syrup.serial() this.desiredState = new StateQueue() this.output = null this.socket = null + this.pid = -1 this.banner = null this.parser = null this.frameConfig = config @@ -77,6 +78,9 @@ module.exports = syrup.serial() .on('unexpectedEnd', this._outputEnded.bind(this)) return this._readOutput(this.output.stream) }) + .then(function() { + return this._waitForPid() + }) .then(function() { return this._connectService() }) @@ -242,8 +246,29 @@ module.exports = syrup.serial() return lifecycle.fatal() } + var match + if ((match = /^PID: (\d+)$/.exec(line))) { + this.pid = +match[1] + this.emit('pid', this.pid) + } + log.info('minicap says: "%s"', line) - }) + }.bind(this)) + } + + FrameProducer.prototype._waitForPid = function() { + if (this.pid > 0) { + return Promise.resolve(this.pid) + } + + var pidListener + return new Promise(function(resolve/*, reject*/) { + this.on('pid', pidListener = resolve) + }.bind(this)).bind(this) + .timeout(2000) + .finally(function() { + this.removeListener('pid', pidListener) + }) } FrameProducer.prototype._connectService = function() { @@ -288,6 +313,7 @@ module.exports = syrup.serial() .finally(function() { this.output = null this.socket = null + this.pid = -1 this.banner = null this.parser = null }) @@ -323,7 +349,7 @@ module.exports = syrup.serial() return Promise.resolve(true) } - var pid = this.banner ? this.banner.pid : -1 + var pid = this.pid function kill(signal) { if (pid <= 0) {