Switch to protocol buffers for communication.

This commit is contained in:
Simo Kinnunen
2014-01-22 16:34:07 +09:00
parent f60cf2008b
commit 90e405a341
8 changed files with 354 additions and 36 deletions

View File

@@ -0,0 +1,51 @@
var events = require('events')
var util = require('util')
function ChannelManager() {
events.EventEmitter.call(this)
this.channels = Object.create(null)
}
util.inherits(ChannelManager, events.EventEmitter)
ChannelManager.prototype.register = function(id, timeout) {
this.channels[id] = {
timeout: timeout
, lastActivity: Date.now()
, timer: null
}
// Set timer with initial check
this.check(id)
}
ChannelManager.prototype.unregister = function(id) {
var channel = this.channels[id]
delete this.channels[id]
clearInterval(channel.timer)
}
ChannelManager.prototype.keepalive = function(id) {
var channel = this.channels[id]
if (channel) {
channel.lastActivity = Date.now()
}
}
ChannelManager.prototype.check = function(id) {
var channel = this.channels[id]
, inactivePeriod = Date.now() - channel.lastActivity
if (inactivePeriod >= channel.timeout) {
this.unregister(id)
this.emit('timeout', id)
}
else if (channel.timeout < Infinity) {
channel.timer = setTimeout(
this.check.bind(this, id)
, channel.timeout - inactivePeriod
)
}
}
module.exports = ChannelManager