From 3ea41c1a4e5df4dd4269387bd255132fbb77029f Mon Sep 17 00:00:00 2001 From: Simo Kinnunen Date: Mon, 2 Jun 2014 21:03:21 +0900 Subject: [PATCH] Fix direct device control not extending owner timeout. --- lib/roles/device/plugins/group.js | 13 +++++++++---- lib/roles/device/plugins/solo.js | 4 +--- lib/roles/device/support/sub.js | 4 +--- lib/wire/channelmanager.js | 23 ++++++++++++++++++++--- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/roles/device/plugins/group.js b/lib/roles/device/plugins/group.js index 7359fea2..5ebcc471 100644 --- a/lib/roles/device/plugins/group.js +++ b/lib/roles/device/plugins/group.js @@ -10,13 +10,14 @@ var grouputil = require('../../../util/grouputil') var lifecycle = require('../../../util/lifecycle') module.exports = syrup.serial() + .dependency(require('./solo')) .dependency(require('./identity')) .dependency(require('./service')) .dependency(require('../support/router')) .dependency(require('../support/push')) .dependency(require('../support/sub')) .dependency(require('../support/channels')) - .define(function(options, identity, service, router, push, sub, channels) { + .define(function(options, solo, ident, service, router, push, sub, channels) { var log = logger.createLogger('device:plugins:group') , currentGroup = null , plugin = new events.EventEmitter() @@ -44,7 +45,11 @@ module.exports = syrup.serial() log.important('Now owned by "%s"', currentGroup.email) log.info('Subscribing to group channel "%s"', currentGroup.group) - channels.register(currentGroup.group, timeout || options.groupTimeout) + channels.register(currentGroup.group, { + timeout: timeout || options.groupTimeout + , alias: solo.channel + }) + sub.subscribe(currentGroup.group) push.send([ @@ -98,7 +103,7 @@ module.exports = syrup.serial() router .on(wire.GroupMessage, function(channel, message) { var reply = wireutil.reply(options.serial) - grouputil.match(identity, message.requirements) + grouputil.match(ident, message.requirements) .then(function() { return plugin.join(message.owner, message.timeout) }) @@ -123,7 +128,7 @@ module.exports = syrup.serial() }) .on(wire.UngroupMessage, function(channel, message) { var reply = wireutil.reply(options.serial) - grouputil.match(identity, message.requirements) + grouputil.match(ident, message.requirements) .then(function() { return plugin.leave() }) diff --git a/lib/roles/device/plugins/solo.js b/lib/roles/device/plugins/solo.js index 6728d291..309894b8 100644 --- a/lib/roles/device/plugins/solo.js +++ b/lib/roles/device/plugins/solo.js @@ -7,16 +7,14 @@ var wireutil = require('../../../wire/util') module.exports = syrup.serial() .dependency(require('../support/sub')) .dependency(require('../support/push')) - .dependency(require('../support/channels')) .dependency(require('../support/router')) .dependency(require('./identity')) - .define(function(options, sub, push, channels, router, identity) { + .define(function(options, sub, push, router, identity) { var log = logger.createLogger('device:plugins:solo') var channel = wireutil.makePrivateChannel() log.info('Subscribing to permanent channel "%s"', channel) sub.subscribe(channel) - channels.register(channel, Infinity) router.on(wire.ProbeMessage, function() { push.send([ diff --git a/lib/roles/device/support/sub.js b/lib/roles/device/support/sub.js index 5677d1f7..a4d4d220 100644 --- a/lib/roles/device/support/sub.js +++ b/lib/roles/device/support/sub.js @@ -6,8 +6,7 @@ var logger = require('../../../util/logger') var wireutil = require('../../../wire/util') module.exports = syrup.serial() - .dependency(require('./channels')) - .define(function(options, channels) { + .define(function(options) { var log = logger.createLogger('device:support:sub') // Input @@ -21,7 +20,6 @@ module.exports = syrup.serial() ;[wireutil.global].forEach(function(channel) { log.info('Subscribing to permanent channel "%s"', channel) sub.subscribe(channel) - channels.register(channel, Infinity) }) return sub diff --git a/lib/wire/channelmanager.js b/lib/wire/channelmanager.js index c96c4972..0fb15b78 100644 --- a/lib/wire/channelmanager.js +++ b/lib/wire/channelmanager.js @@ -8,13 +8,27 @@ function ChannelManager() { util.inherits(ChannelManager, events.EventEmitter) -ChannelManager.prototype.register = function(id, timeout) { - this.channels[id] = { - timeout: timeout +ChannelManager.prototype.register = function(id, options) { + var channel = this.channels[id] = { + timeout: options.timeout + , alias: options.alias , lastActivity: Date.now() , timer: null } + if (channel.alias) { + // The alias can only be active for a single channel at a time + if (this.channels[channel.alias]) { + throw new Error(util.format( + 'Cannot create alias "%s" for "%s"; the channel already exists' + , channel.alias + , id + )) + } + + this.channels[channel.alias] = channel + } + // Set timer with initial check this.check(id) } @@ -24,6 +38,9 @@ ChannelManager.prototype.unregister = function(id) { if (channel) { delete this.channels[id] clearTimeout(channel.timer) + if (channel.alias) { + delete this.channels[channel.alias] + } } }