Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wankdanker committed Sep 29, 2011
0 parents commit 53071c5
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 0 deletions.
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
node-discover
-------------

Automatically discover your nodejs instances using UDP broadcast with support for automatic single master


Why?
----

So, you have a whole bunch of node processes running but you have no way within each process to
determine where the other processes are or what they can do. This module aims to make discovery of new
processes as simple as possible. Additionally, what if you want one process to be in charge of a cluster
of processes? This module also has automatic master process selection.


Example
-------

var Discover = require('../lib/discover.js');

var d = new Discover();

d.on("promotion", function () {
/*
* Launch things this master process should do.
*
* For example:
* - Monitior your redis servers and handle failover by issuing slaveof commands then notify
* other node instances to use the new master
* - Make sure there are a certain number of nodes in the cluster and launch new ones if there
* are not enough
* - whatever
*
*/
console.log("I was promoted to a master.");
});

d.on("demotion", function () {
/*
* End all master specific functions or whatever you might like.
*
*/
console.log("I was demoted from being a master.");
});

d.on("added", function (obj) {
console.log("A new node has been added.");
});

d.on("removed", function (obj) {
console.log("A node has been removed.");
});

d.on("master", function (obj) {
/*
* A new master process has been selected
*
* Things we might want to do:
* - Review what the new master is advertising use its services
* - Kill all connections to the old master
*/
console.log("A new master is in control");
});


API
---

Constructor
-----------

new Discover({
helloInterval : How often to broadcast a hello packet in milliseconds; Default: 1000
checkInterval : How often to to check for missing nodes in milliseconds; Default: 2000
nodeTimeout : Consider a node dead if not seen in this many milliseconds; Default: 2000
masterTimeout : Consider a master node dead if not seen in this many milliseconds; Default: 2000
address : Address to bind to; Default: '0.0.0.0'
port : Port to bind to and broadcast to: Default: 12345
destination : Destination ip address; Default: '255.255.255.255'
key : Encryption key if your broadcast packets should be encrypted; Default: null (that means no encryption);
});

Attributes
----------

nodes

Methods
-------

promote
demote
join --not implemented
leave --not implemented
advertise
start
stop
eachNode(fn)

Events
------

promotion
demotion
added
removed
master





LICENSE

(MIT License)

Copyright (c) 2011 Dan VerWeire [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/discover.js');
127 changes: 127 additions & 0 deletions lib/broadcast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
var dgram = require('dgram'),
crypto = require('crypto');

var procUuid = uuid();

var Broadcast = module.exports = function (options) {
var self = this, options = options || {};

self.address = options.address || '0.0.0.0';
self.port = options.port || 12345;
self.destination = options.destination || "255.255.255.255";
self.key = options.key || null;

self.socket = dgram.createSocket('udp4');
self.socket.setBroadcast(true);
self.socket.bind(self.port, self.address);

self.instanceUuid = uuid();
self.processUuid = procUuid;

self.socket.on("message", function ( data, rinfo ) {
self.decode(data, function (err, obj) {
if (err) {
self.emit("error", err);
}
else if (obj.pid == procUuid) {
return false;
}
else if (obj.event && obj.data) {

self.emit(obj.event, obj.data, obj, rinfo);
}
else {
self.emit("message", obj)
}
});
});

self.on("error", function (err) { /* do nothing */});
};

Broadcast.prototype = new process.EventEmitter();

Broadcast.prototype.send = function (event) {
var self = this;

var obj = { event : event, pid : procUuid, iid : self.instanceUuid };

if (arguments.length == 2) {
obj.data = arguments[1];
}
else {
//TODO: splice the arguments array and remove the first element setting data to the result array
}

self.encode(obj, function (err, contents) {
if (err) {
return false;
}

var msg = new Buffer(contents);
self.socket.send(msg, 0, msg.length, self.port, self.destination);
});
};

Broadcast.prototype.encode = function (data, callback) {
var self = this;

try {
return callback(null, (self.key) ? encrypt(JSON.stringify(data),self.key) : JSON.stringify(data));
}
catch (e) {
return callback(e, null);
}
};

Broadcast.prototype.decode = function (data, callback) {
var self = this;

try {
if (self.key) {
return callback(null, JSON.parse(decrypt(data.toString(),self.key)));
}
else {
return callback(null, JSON.parse(data));
}
}
catch (e) {
return callback(e, null);
}
};


//TODO: this may need to be improved
function uuid() {
var str = require('os').hostname() + ":" + process.pid + ":" + (+new Date) + ":" + (Math.floor(Math.random() * 100000000000)) + "" + (Math.floor(Math.random() * 100000000000))

return md5(str);
}

function md5 (str) {
var hash = crypto.createHash('md5');

hash.update(str);

return hash.digest('hex');
};

function encrypt (str, key) {
var buf = [];
var cipher = crypto.createCipher('aes256', key);

buf.push(cipher.update(str, 'utf8', 'binary'));
buf.push(cipher.final('binary'));

return buf.join('');
};

function decrypt (str, key) {
var buf = [];
var decipher = crypto.createDecipher('aes256', key);

buf.push(decipher.update(str, 'binary', 'utf8'));
buf.push(decipher.final('utf8'));

return buf.join('');
};
Loading

0 comments on commit 53071c5

Please sign in to comment.