Skip to content

Commit

Permalink
avahi: emit signals when services come up or dissapear.
Browse files Browse the repository at this point in the history
  • Loading branch information
pardo-bsso committed Mar 7, 2014
1 parent 7c5d510 commit f0a2634
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions avahi/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var mdns = require('mdns2'),
uuid = require('node-uuid'),
logger = require("../logger")().addLogger('avahi')
var mdns = require('mdns2'),
uuid = require('node-uuid'),
logger = require("../logger")().addLogger('avahi'),
Backbone = require("backbone"),
_ = require("underscore")
;

/*
Expand All @@ -9,15 +11,39 @@ var mdns = require('mdns2'),
* 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'
*
* Properties:
* services
* A hash that maps from a service name to its properties.
*
* Signals:
* serviceUp(service):
* 'service' is an object with keys for host, port, name, addresses and txtRecord.
*
* serviceDown(service_name)
*/
var avahi = exports = module.exports = function (client_id, namespace) {
var self = this;
_.extend(self, Backbone.Events);

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

self.services = {};

self.browser = mdns.createBrowser(mdns.tcp(self.namespace));
self.browser.on('serviceUp', function(service) {
var ret = _.pick(service, 'host', 'port', 'name', 'addresses', 'txtRecord');
self.services[ret.name] = ret;
self.trigger('serviceUp', ret)
});
self.browser.on('serviceDown', function(service) {
delete self.services[service.name];
self.trigger('serviceDown', service.name)
});

self.browser.start();

return self;
};

Expand Down

0 comments on commit f0a2634

Please sign in to comment.