multicast-eventemitter
Advanced tools
Comparing version 0.0.6 to 0.0.8
@@ -5,7 +5,7 @@ // Copyright 2011 Thorcom Systems Ltd. All Rights Reserved. | ||
var emitter = new mee.MulticastEventEmitter(); | ||
var emitter = mee.getEmitter(); | ||
// subscribe to channelA events | ||
emitter.on('channelA', function(text, time) { | ||
console.log('message received', text, time); | ||
console.log('message received on channelA:', text, time); | ||
}); |
@@ -5,8 +5,10 @@ // Copyright 2011 Thorcom Systems Ltd. All Rights Reserved. | ||
var emitter = new mee.MulticastEventEmitter(); | ||
var emitter = mee.getEmitter(); | ||
// emit a packet everu second, | ||
setInterval(function() { | ||
console.log('emitting channelA'); | ||
emitter.emit('channelA', 'this is channel A', new Date().getTime()); | ||
console.log('emitting channelB'); | ||
emitter.emit('channelB', 'this is channel B', new Date().getTime()); | ||
}, 1000); |
@@ -13,4 +13,15 @@ // Copyright 2011 Thorcom Systems Ltd. All Rights Reserved. | ||
, 'ttl': 64 | ||
, 'overrides' : {} // should be of form "event_name: { address: 'address', port: port }" | ||
} | ||
var emitter; // singleton | ||
exports.getEmitter = getEmitter; | ||
function getEmitter() { | ||
if (!emitter) { | ||
emitter = new MulticastEventEmitter(); | ||
} | ||
return emitter; | ||
} | ||
exports.MulticastEventEmitter = MulticastEventEmitter; | ||
@@ -20,9 +31,17 @@ function MulticastEventEmitter() { | ||
this.serversByEvent = {}; // a hash of bound datagram servers | ||
this.lastSeq = {}; // a hash of last heard sequence number, by src | ||
this.src = Math.random(); // unique id of this sender | ||
this.seq = 0; | ||
} | ||
MulticastEventEmitter.prototype.addListener = function(event, listener) { | ||
console.log('addListener', event, listener); | ||
//console.log('addListener', event, listener); | ||
if (!this.listenersByEvent[event]) { // make a new multicast listener and an empty array | ||
this.listenersByEvent[event] = []; | ||
var hash = hasher.hash(event); | ||
var hash; | ||
if (options.overrides[event]) { | ||
hash = options.overrides[event]; | ||
} else { | ||
hash = hasher.hash(event); | ||
} | ||
@@ -43,5 +62,9 @@ // FIXME: add code to cope with hash collisions (should be rare as we have ~39 bits of hash space) | ||
MulticastEventEmitter.prototype.handleMessage = function(event, msg, rinfo) { | ||
console.log('handleMessage', event, msg, rinfo); | ||
//console.log('handleMessage', event, msg, rinfo); | ||
try { | ||
var message = JSON.parse(msg.toString('utf8')); | ||
//if (this.lastSeq[message.src] && CONTINUIE HERE | ||
if (message.seq) { | ||
this.lastSeq[message.src] = message.seq; | ||
} | ||
} catch(e) { | ||
@@ -52,7 +75,13 @@ console.warn('error parsing multicast message:', msg); | ||
var list = this.listenersByEvent[event]; | ||
console.log('list', list); | ||
//console.log('list', list); | ||
for (var i in list) { | ||
var listener = list[i]; | ||
// TODO: should we use nextTick here? | ||
listener.apply(undefined, message.args); | ||
if (message.args) { | ||
listener.apply(undefined, message.args); | ||
} else { | ||
// If the message doesn't follow the spec, pass it all on. | ||
// (Needed for legacy apps). | ||
listener.call(undefined, message); | ||
} | ||
} | ||
@@ -65,5 +94,10 @@ } | ||
var args = Array.prototype.slice.call(arguments); | ||
console.log('emit', args); | ||
var hash = hasher.hash(event); | ||
var message = new Buffer(JSON.stringify({ event: event, args: args })); | ||
//console.log('emit', args); | ||
var hash; | ||
if (options.overrides[event]) { | ||
hash = options.overrides[event]; | ||
} else { | ||
hash = hasher.hash(event); | ||
} | ||
var message = new Buffer(JSON.stringify({ event: event, args: args, src: this.src, seq: this.seq++ })); | ||
var socket = dgram.createSocket('udp4'); | ||
@@ -70,0 +104,0 @@ socket.addMembership(hash.address, options.multicastInterface); |
{ "name" : "multicast-eventemitter" | ||
, "description" : "LAN wide eventemitter, using multicast." | ||
, "version" : "0.0.6" | ||
, "version" : "0.0.8" | ||
, "maintainers" : | ||
@@ -5,0 +5,0 @@ [ { "name": "Chris Dew" |
8690
10
193