1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

Allow only a single group per device.

This commit is contained in:
Simo Kinnunen 2014-02-03 02:28:58 +09:00
parent 6141bdb5ce
commit 6a7e10882f
2 changed files with 34 additions and 18 deletions

View file

@ -26,6 +26,7 @@ module.exports = function(options) {
, display = Object.create(null) , display = Object.create(null)
, vendor = Object.create(null) , vendor = Object.create(null)
, solo = wireutil.makePrivateChannel() , solo = wireutil.makePrivateChannel()
, group = null
, channels = new ChannelManager() , channels = new ChannelManager()
, vitals = new Vitals() , vitals = new Vitals()
, ports = { , ports = {
@ -82,8 +83,9 @@ module.exports = function(options) {
// Unsubscribe from temporary channels when they timeout // Unsubscribe from temporary channels when they timeout
channels.on('timeout', function(channel) { channels.on('timeout', function(channel) {
log.info('Channel "%s" timed out', channel) log.info('Channel "%s" timed out', channel)
sub.unsubscribe(channel) if (channel === group) {
push.send([channel, wireutil.makeLeaveGroupMessage(options.serial)]) leaveGroup()
}
}) })
// Closure of vital functionality // Closure of vital functionality
@ -334,24 +336,16 @@ module.exports = function(options) {
channels.keepalive(channel) channels.keepalive(channel)
}) })
.on(wire.GroupMessage, function(channel, message) { .on(wire.GroupMessage, function(channel, message) {
var groupChannel = message.channel if (!isGrouped() &&
if (devutil.matchesRequirements(identity, message.requirements)) { devutil.matchesRequirements(identity, message.requirements)) {
channels.register(groupChannel, message.timeout) joinGroup(message.channel, message.timeout)
log.info('Subscribing to group channel "%s"', groupChannel)
sub.subscribe(groupChannel)
push.send([groupChannel,
wireutil.makeJoinGroupMessage(options.serial)])
} }
channels.keepalive(channel) channels.keepalive(channel)
}) })
.on(wire.UngroupMessage, function(channel, message) { .on(wire.UngroupMessage, function(channel, message) {
var groupChannel = message.channel if (isGrouped() &&
if (devutil.matchesRequirements(identity, message.requirements)) { devutil.matchesRequirements(identity, message.requirements)) {
channels.unregister(groupChannel) leaveGroup()
log.info('Unsubscribing from group channel "%s"', groupChannel)
sub.unsubscribe(groupChannel)
push.send([groupChannel,
wireutil.makeLeaveGroupMessage(options.serial)])
} }
channels.keepalive(channel) channels.keepalive(channel)
}) })
@ -413,6 +407,26 @@ module.exports = function(options) {
wireutil.makeDevicePokeMessage(options.serial, solo)]) wireutil.makeDevicePokeMessage(options.serial, solo)])
} }
function isGrouped() {
return !!group
}
function joinGroup(channel, timeout) {
log.info('Subscribing to group channel "%s"', channel)
channels.register(channel, timeout)
sub.subscribe(channel)
push.send([channel, wireutil.makeJoinGroupMessage(options.serial)])
group = channel
}
function leaveGroup() {
log.info('Unsubscribing from group channel "%s"', group)
channels.unregister(group)
sub.unsubscribe(group)
push.send([group, wireutil.makeLeaveGroupMessage(options.serial)])
group = null
}
function selfDestruct() { function selfDestruct() {
process.exit(1) process.exit(1)
} }

View file

@ -21,8 +21,10 @@ ChannelManager.prototype.register = function(id, timeout) {
ChannelManager.prototype.unregister = function(id) { ChannelManager.prototype.unregister = function(id) {
var channel = this.channels[id] var channel = this.channels[id]
delete this.channels[id] if (channel) {
clearTimeout(channel.timer) delete this.channels[id]
clearTimeout(channel.timer)
}
} }
ChannelManager.prototype.keepalive = function(id) { ChannelManager.prototype.keepalive = function(id) {