Skip to content

Commit

Permalink
adds basic wrapper to announce services with avahi
Browse files Browse the repository at this point in the history
  • Loading branch information
pardo-bsso committed Mar 7, 2014
1 parent ff39f30 commit 7c5d510
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
59 changes: 59 additions & 0 deletions avahi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var mdns = require('mdns2'),
uuid = require('node-uuid'),
logger = require("../logger")().addLogger('avahi')
;

/*
* Basic helper to announce services using mdns / avahi.
*
* client_id is an unique token used to identify this particular instance. If
* not given, an uuid is created.
* namespace is the prefix used to distinguish the services. Defaults to 'MBC'
*/
var avahi = exports = module.exports = function (client_id, namespace) {
var self = this;

self.client_id = client_id || uuid.v4();
self.namespace = namespace || 'MBC';

self.services = {};

return self;
};

/*
* Announces a service on the network.
*
* The only required parameters are 'service_name' and 'port'.
* The parameter 'iface' is optional and can be either something like 'eth0'
* or also an address like '192.168.1.7'.
*/
avahi.prototype.announce = function (service_name, port, iface) {
var self = this;

if (!port || !service_name) {
logger.error('announce(): service_name and port are required parameters');
return;
}

var txtRecord = {
namespace: self.namespace,
name: service_name,
uuid: self.client_id
};

var options = {
txtRecord: txtRecord,
name: service_name + '.' + self.client_id,
};

if (iface) {
options.networkInterface = iface;
}

var ann = mdns.createAdvertisement(mdns.tcp(self.namespace), port, options);
ann.start();

var ann = mdns.createAdvertisement(mdns.tcp('http'), port, options);
ann.start();
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ exports = module.exports = {
logger: require('./logger'),
iobackends: require('./models/iobackends'),
utils: require('./utils'),
avahi: require('./avahi')
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"backbone-relational" : "git://github.com/inaes-tic/Backbone-relational.git#pageable-compat",
"backbone.io" : "git://github.com/inaes-tic/backbone.io",
"winston" : "0.7.x",
"mdns2" : "2.1.3",
"moment" : "1.7.x"
},
"license": "agplv3",
Expand Down

0 comments on commit 7c5d510

Please sign in to comment.