Skip to content

Commit

Permalink
Merge pull request #2 from godsflaw/master
Browse files Browse the repository at this point in the history
Added support for node-gossip to be run from within a project's directory
  • Loading branch information
bpot committed Aug 30, 2012
2 parents 63abc94 + 372a007 commit 1943c11
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.sw?
node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Check out the the scripts in the simulations/ directory for some examples.

Gossiper methods:

allPeeers()
allPeers()
livePeers()
deadPeers()
peerValue(peer, key)
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is just added for convenience so this repository can be
// directly checked out (submodule) into a project
module.exports = require('./lib/gossiper');
27 changes: 14 additions & 13 deletions lib/gossiper.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
var PeerState = require('peer_state').PeerState,
Scuttle = require('scuttle').Scuttle,
EventEmitter = require('events').EventEmitter,
net = require('net'),
sys = require('sys'),
var PeerState = require('./peer_state').PeerState,
Scuttle = require('./scuttle').Scuttle,
EventEmitter = require('events').EventEmitter,
net = require('net'),
util = require('util'),
child_process = require('child_process'),
dns = require('dns'),
msgpack = require('msgpack');
dns = require('dns'),
msgpack = require('msgpack');

var Gossiper = exports.Gossiper = function(port, seeds, ip_to_bind) {
EventEmitter.call(this);
this.peers = {};
this.ip_to_bind = ip_to_bind;
this.port = port;
this.seeds = seeds;
this.my_state = new PeerState();
this.scuttle = new Scuttle(this.peers, this.my_state);

this.handleNewPeers(seeds);
}

sys.inherits(Gossiper, EventEmitter);
util.inherits(Gossiper, EventEmitter);

Gossiper.prototype.start = function(callback) {
var self = this;
Expand Down Expand Up @@ -63,12 +64,12 @@ Gossiper.prototype.stop = function() {
}


// The method of choosing whice peer(s) to gossip to is borrowed from Cassandra.
// The method of choosing which peer(s) to gossip to is borrowed from Cassandra.
// They seemed to have worked out all of the edge cases
// http://wiki.apache.org/cassandra/ArchitectureGossip
Gossiper.prototype.gossip = function() {
// Find a live peer to gossip to
if(this.livePeers() > 0) {
if(this.livePeers().length > 0) {
var live_peer = this.chooseRandom(this.livePeers());
this.gossipToPeer(live_peer);
}
Expand Down Expand Up @@ -112,7 +113,7 @@ Gossiper.prototype.gossipToPeer = function(peer) {
mp_stream.send(self.requestMessage());
});
gosipee.on('error', function(exception) {
// console.log(self.peer_name + " received " + sys.inspect(exception));
// console.log(self.peer_name + " received " + util.inspect(exception));
});
}

Expand Down Expand Up @@ -219,12 +220,12 @@ Gossiper.prototype.allPeers = function() {

Gossiper.prototype.livePeers = function() {
var keys = [];
for(var k in this.peers) { if(k.alive) { keys.push(k)} };
for(var k in this.peers) { if(this.peers[k].alive) { keys.push(k)} };
return keys;
}

Gossiper.prototype.deadPeers = function() {
var keys = [];
for(var k in this.peers) { if(!k.alive) { keys.push(k) } };
for(var k in this.peers) { if(!this.peers[k].alive) { keys.push(k) } };
return keys;
}
8 changes: 4 additions & 4 deletions lib/peer_state.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var AccrualFailureDetector = require('./accrual_failure_detector').AccrualFailureDetector,
EventEmitter = require('events').EventEmitter,
sys = require('sys');
var AccrualFailureDetector = require('./accrual_failure_detector').AccrualFailureDetector,
EventEmitter = require('events').EventEmitter,
util = require('util');

var PeerState = exports.PeerState = function(name) {
EventEmitter.call(this);
Expand All @@ -13,7 +13,7 @@ var PeerState = exports.PeerState = function(name) {
this.name = name;
};

sys.inherits(PeerState, EventEmitter);
util.inherits(PeerState, EventEmitter);

PeerState.prototype.updateWithDelta = function(k,v,n) {
// It's possibly to get the same updates more than once if we're gossiping with multiple peers at once
Expand Down
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gossiper",
"description": "node-gossip implements a gossip protocol",
"version": "0.0.3",
"author": "Bob Potter <[email protected]>",
"contributors": [
{
"name": "Christopher Mooney",
"email": "[email protected]"
}
],
"repository": {
"type" : "git",
"url" : "git://github.com/bpot/node-gossip.git"
},
"keywords": ["gossip"],
"dependencies": {
"msgpack" : ">= 0.0.0"
},
"main": "index.js",
"scripts": { "test": "expresso -I lib test/*" },
"engines": { "node": "0.4.x || 0.5.x || 0.6.x || 0.7.x || 0.8.x || 0.9.x" }
}
1 change: 0 additions & 1 deletion simulation/s3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Gossiper = require('gossiper').Gossiper;
var sys = require('sys');

var seed1 = new Gossiper(9000, []);
seed1.start();
Expand Down

0 comments on commit 1943c11

Please sign in to comment.