Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions lib/network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var dgram = require('dgram'),
crypto = require('crypto'),
os = require('os'),
broadcastAddress = require("broadcast-address"),
EventEmitter = require('events').EventEmitter,
util = require('util'),
uuid = require('uuid/v4'),
Expand All @@ -9,8 +10,8 @@ var dgram = require('dgram'),
var procUuid = uuid();
var hostName = process.env.DISCOVERY_HOSTNAME || os.hostname();

module.exports = Network;
module.exports = Network;

function Network (options) {
if (!(this instanceof Network)) {
return new Network(options, callback);
Expand Down Expand Up @@ -96,7 +97,29 @@ Network.prototype.start = function (callback) {
self.socket.setBroadcast(true);

//TODO: get the default broadcast address from os.networkInterfaces() (not currently returned)
self.destination = [self.broadcast || "255.255.255.255"];

if (self.broadcast) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wankdanker this handles if the user DOES manually define broadcast as an option, to use what they supplied. Also expands the API to support passing an array instead of a single value.

The new code below only activates if the user did not manually declare broadcast.

self.destination = Array.isArray( self.broadcast) ? self.broadcast.slice(0) : [self.broadcast];
} else {
self.destination = [];
var networkInterfaces = os.networkInterfaces();
for (var interfaceName in networkInterfaces) {
if (!networkInterfaces.hasOwnProperty(interfaceName)) {
continue;
}
for (var i = 0; i < networkInterfaces[interfaceName].length; i++) {
var iface = networkInterfaces[interfaceName][i];
if (iface.internal || iface.family !== "IPv4") {
continue;
}

var broadcast = broadcastAddress(interfaceName, iface.address);
if (broadcast) {
self.destination.push(broadcast);
}
}
}
}
}
else {
try {
Expand Down Expand Up @@ -149,7 +172,7 @@ Network.prototype.send = function (event) {
}

var msg = Buffer.from(contents);

self.destination.forEach(function (destination) {
self.socket.send(
msg
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"node": ">=0.4.1 <0.5.0 || >=0.6.9"
},
"dependencies": {
"broadcast-address": "^1.0.2",
"uuid": "^3.3.2"
},
"bugs": {
Expand Down