Skip to content

Commit

Permalink
[major] comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Cam Pedersen committed Feb 21, 2012
1 parent ab75176 commit 4d395ef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/peer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var net = require('net');

/*
* Construct a Peer object
* Connect to the discovered node
* Handle reconnections
*/
var Peer = function (options) {
options = options || {};
this._ = options;
Expand All @@ -13,20 +18,20 @@ var Peer = function (options) {
);
}

Peer.prototype.send = function (data) {
this.$.write(JSON.stringify(data) + '\r\n');
}

/*
* Handle a successful connection
*/
Peer.prototype.handleConnection = function (client) {
var that = this;
this._.parent.ee.emit('peer.connection', client);
this.$.on('data', function () {
that.handleIncomingMessage(arguments);
});
}

Peer.prototype.handleIncomingMessage = function () {
console.log(arguments);
/*
* Send data via Peer connection
* Should intelligently cast `data`
*/
Peer.prototype.send = function (data) {
this.$.write(JSON.stringify(data) + '\r\n');
}

module.exports = Peer;
9 changes: 9 additions & 0 deletions nucleus.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Nucleus.prototype.buildLocalAddresses = function () {
}
}

/*
* Set up a TCP server to listen for Peer clients
*/
Nucleus.prototype.createServer = function () {
var that = this;
this.$ = net.createServer(this.connectionHandler);
Expand All @@ -72,6 +75,9 @@ Nucleus.prototype.createServer = function () {
});
}

/*
* Handle a Peer client connecting to TCP server
*/
Nucleus.prototype.connectionHandler = function (socket) {
socket.on('data', function (data) {
console.log(data.toString());
Expand All @@ -83,6 +89,9 @@ Nucleus.prototype.connectionHandler = function (socket) {
});
}

/*
* Write to all TCP Peer clients
*/
Nucleus.prototype.broadcast = function (data) {
for (var i in this.peers) {
if (this.peers[i].$.writable) {
Expand Down
21 changes: 21 additions & 0 deletions transports/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ mDNSTransport.browse = function (parent) {
mdns.tcp('nucleus')
);

/*
* Determine if a service originates from localhost
* Depends on Nucleus#buildLocalAddresses firing before this
* mDNS doesn't have a filter for this :(
*/
var isSelf = function (service) {
var found = false;
for (var i in service.addresses) {
Expand All @@ -23,6 +28,10 @@ mDNSTransport.browse = function (parent) {
return found;
}

/*
* Return the index of given `service`
* inside Nucleus#peers
*/
var locatePeer = function (service) {
var found = false;
for (var i in parent.peers) {
Expand All @@ -34,6 +43,9 @@ mDNSTransport.browse = function (parent) {
return found && i;
}

/*
* Respond to a discovered Peer
*/
_.on('serviceUp', function(service) {
if (!isSelf(service)) {
var peer = {
Expand All @@ -47,6 +59,11 @@ mDNSTransport.browse = function (parent) {
}
});

/*
* Handle a Peer going down
* Hopefully happens after connection
* and peer construction
*/
_.on('serviceDown', function(service) {
var i = locatePeer(service);
var p = parent.peers[i]._;
Expand All @@ -57,6 +74,10 @@ mDNSTransport.browse = function (parent) {
_.start();
}

/*
* Trigger an mDNS advertisement as a "nucleus" service
* Always running
*/
mDNSTransport.advertise = function (parent) {
var _ = parent._.ad = mdns.createAdvertisement(
mdns.tcp('nucleus'),
Expand Down

0 comments on commit 4d395ef

Please sign in to comment.