Created
August 22, 2011 20:17
-
-
Save Filirom1/1163413 to your computer and use it in GitHub Desktop.
Dnode cluster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dnode = require('dnode'); | |
var Cluster = function(options) { /* Options : local port, host, remote nodes, ... */ | |
var _self = this; | |
this.options = options; | |
/* the remote server connected with this server */ | |
this.connectedPeers = []; | |
/* Exposed functions */ | |
this._server = dnode({ | |
/** | |
* Method called by other nodes to say hello | |
* and transmit their respective connectedPeers in order to merge | |
* the cluster. | |
*/ | |
__hello: function(remote, remoteConnectedPeers) { | |
//TODO try to share the connection remote to me === me to remote | |
_self._connect(remote.host, remote.port); | |
remoteConnectedPeers.forEach(function(peer) { | |
_self._connect(peer.host, peer.port); | |
}); | |
} | |
}); | |
/** | |
* Start listening + try to join the other cluster members | |
* every `options.refresh` ms | |
*/ | |
this.start = function() { | |
_self._listen(); | |
setInterval(function() { | |
_self.join.apply(_self); | |
}, _self.options.refresh); | |
}; | |
/** Start listening */ | |
this._listen = function() { | |
console.log(_self.toString() + " listening"); | |
_self._server.listen(_self.options.local.port); | |
}; | |
/** | |
* Connect to a remote server and send my connected peers to merge | |
* the cluster. | |
*/ | |
this._connect = function(host, port) { | |
if (!_self._isMe(host, port)) { | |
if (!_self._isPeerConnected(host, port)) { | |
console.log(_self.toString() + " tring to connect on " + host + ":" + port); | |
dnode.connect(port, function(remote) { | |
console.log(_self.toString() + " connected with " + host + ":" + port + " !"); | |
_self.connectedPeers.push({ | |
host: host, | |
port: port | |
}); | |
remote.__hello(_self.options.local, _self.connectedPeers); | |
}); | |
} | |
} | |
}; | |
/** return true if this server is connected with this host:port */ | |
this._isPeerConnected = function(host, port) { | |
var returnValue = false; | |
_self.connectedPeers.forEach(function(peer){ | |
if (peer.host == host && peer.port == port) { | |
returnValue = true; | |
} | |
}); | |
return returnValue; | |
}; | |
/** return true if this server is connected with this host:port */ | |
this._isMe = function(host, port) { | |
if (host == _self.options.local.host && port == _self.options.local.port) { | |
return true; | |
} | |
return false; | |
}; | |
/** Join the other nodes of the cluster */ | |
this.join = function() { | |
_self.options.remote.forEach(function(remote) { | |
_self._connect(remote.host, remote.port); | |
}); | |
}; | |
/** return your-ip:port */ | |
this.toString = function() { | |
return _self.options.local.host + ":" + _self.options.local.port; | |
}; | |
}; | |
module.exports = Cluster; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cluster = require('./cluster'); | |
console.log(cluster); | |
var a = new cluster({ | |
local: { | |
host: '127.0.0.1', | |
port: 3333 | |
}, | |
remote: [{ | |
host: '127.0.0.1', | |
port: 4444 | |
}, { | |
host: '127.0.0.1', | |
port: 5555 | |
}], | |
refresh: 1000 | |
}); | |
a.start(); | |
var b = new cluster({ | |
local: { | |
host: '127.0.0.1', | |
port: 4444 | |
}, | |
remote: [], | |
refresh: 1000 | |
}); | |
b.start(); | |
var c = new cluster({ | |
local: { | |
host: '127.0.0.1', | |
port: 5555 | |
}, | |
remote: [], | |
refresh: 1000 | |
}); | |
c.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output is :
127.0.0.1:3333 listening
127.0.0.1:4444 listening
127.0.0.1:5555 listening
127.0.0.1:3333 tring to connect on 127.0.0.1:4444
127.0.0.1:3333 tring to connect on 127.0.0.1:5555
127.0.0.1:3333 connected with 127.0.0.1:5555 !
127.0.0.1:3333 connected with 127.0.0.1:4444 !
127.0.0.1:4444 tring to connect on 127.0.0.1:3333
127.0.0.1:4444 tring to connect on 127.0.0.1:5555
127.0.0.1:5555 tring to connect on 127.0.0.1:3333
127.0.0.1:5555 connected with 127.0.0.1:3333 !
127.0.0.1:4444 connected with 127.0.0.1:5555 !
127.0.0.1:4444 connected with 127.0.0.1:3333 !
127.0.0.1:5555 tring to connect on 127.0.0.1:4444
127.0.0.1:5555 connected with 127.0.0.1:4444 !