Add owners to groups.

This commit is contained in:
Simo Kinnunen
2014-02-04 20:33:53 +09:00
parent 3e7d91cb91
commit 37303c5d92
8 changed files with 98 additions and 70 deletions

View File

@@ -25,8 +25,8 @@ module.exports = function(options) {
, identity = Object.create(null)
, display = Object.create(null)
, vendor = Object.create(null)
, owner = null
, solo = wireutil.makePrivateChannel()
, group = null
, channels = new ChannelManager()
, vitals = new Vitals()
, ports = {
@@ -83,7 +83,7 @@ module.exports = function(options) {
// Unsubscribe from temporary channels when they timeout
channels.on('timeout', function(channel) {
log.info('Channel "%s" timed out', channel)
if (channel === group) {
if (channel === owner.group) {
leaveGroup()
}
})
@@ -342,7 +342,7 @@ module.exports = function(options) {
.on(wire.GroupMessage, function(channel, message) {
if (!isGrouped() &&
devutil.matchesRequirements(identity, message.requirements)) {
joinGroup(message.channel, message.timeout)
joinGroup(message.owner, message.timeout)
}
channels.keepalive(channel)
})
@@ -436,23 +436,37 @@ module.exports = function(options) {
}
function isGrouped() {
return !!group
return !!owner
}
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 joinGroup(newOwner, timeout) {
log.info('Now owned by "%s"', newOwner.email)
log.info('Subscribing to group channel "%s"', newOwner.group)
channels.register(newOwner.group, timeout)
sub.subscribe(newOwner.group)
push.send([
wireutil.global
, wireutil.envelope(new wire.JoinGroupMessage(
options.serial
, newOwner
))
])
owner = newOwner
}
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
log.info('No longer owned by "%s"', owner.email)
log.info('Unsubscribing from group channel "%s"', owner.group)
channels.unregister(owner.group)
sub.unsubscribe(owner.group)
push.send([
wireutil.global
, wireutil.envelope(new wire.LeaveGroupMessage(
options.serial
, owner
))
])
owner = null
}
function selfDestruct() {