Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[major] comment

  • Loading branch information...
commit 4d395effd575f3ae97bfbbc17eb61b91b1fbb3fa 1 parent ab75176
@ecto authored
Showing with 44 additions and 9 deletions.
  1. +14 −9 lib/peer.js
  2. +9 −0 nucleus.js
  3. +21 −0 transports/mdns.js
View
23 lib/peer.js
@@ -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;
@@ -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;
View
9 nucleus.js
@@ -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);
@@ -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());
@@ -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) {
View
21 transports/mdns.js
@@ -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) {
@@ -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) {
@@ -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 = {
@@ -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]._;
@@ -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'),
Please sign in to comment.
Something went wrong with that request. Please try again.