Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit dad6c21f3b814e58ab964bc3716cb75280a7208b @emilisto committed Dec 21, 2011
Showing with 186 additions and 0 deletions.
  1. +8 −0 LICENSE.md
  2. +21 −0 README.md
  3. +12 −0 bin/test-start.js
  4. +118 −0 lib/mdns.js
  5. +27 −0 package.json
@@ -0,0 +1,8 @@
+# MIT License
+Copyright (c) 2011 Emil Stenqvist
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
+# hook.io-mdns - Advertise and discover hooks nearby through mDNS
+
+Half the purpose of a distributed event bus (i.e. Hook.io) is to
+distribute over multiple computers, and not just multiple processes.
+hook.io-mdns wraps .listen(), .connect() and .start() of the standard hook.io
+`Hook` object so that they "browse" and "advertise" hooks automatically.
+
+In real life, this means you can use hook.io just as usual, but it will
+now discover hooks on nearby machines. Sweet isn't it?
+
+## Installation
+
+ git clone git@github.com:emsten/hook.io-mdns.git
+ cd hook.io-mdns
+ npm install
+ node bin/start
+
+Run this on machine #1, and then on another machine #2. Notice how #2
+discovers and connects to #1. To see the magic, close both and run
+`node bin/start` in opposite order and see how #2 now takes the role
+as listener and #1 connects to #2.
@@ -0,0 +1,12 @@
+var mDNSHook = require('./mDNSHook').mDNSHook;
+
+var hook = new mDNSHook({
+ name: 'sample-mdns-hook'
+});
+
+hook.start({
+ 'hook-port': 30000,
+ 'hook-host': "0.0.0.0",
+ 'mdns-name': 'Bonjour Madame',
+ 'mdns-regtype': 'hookio'
+});
@@ -0,0 +1,118 @@
+var mdns = require('mdns'),
+ util = require('util'),
+ Hook = require('hook.io').Hook;
+
+var defaults = {
+ "mdns-regtype": 'hookio',
+ "mdns-listen-timeout": 500
+};
+
+var mDNSHook = exports.mDNSHook = function(options) {
+ Hook.call(this);
+};
+util.inherits(mDNSHook, Hook);
+
+// Override of listen that accepts mdns parameters and
+// advertises hook.io instance upon successful listening.
+var listen = Hook.prototype.listen;
+mDNSHook.prototype.listen = function(options, callback) {
+ var self = this;
+
+ listen.call(this, options, function(err) {
+
+ if(err) {
+ // FIXME: this should prolly be handled differently
+ throw new Error("Listening failed: " + util.inspect(err));
+ }
+
+ options = options || {};
+
+ var regtype = options["mdns-regtype"] || defaults["mdns-regtype"];
+
+ var _options = {};
+ if(options['mdns-name']) _options["name"] = options["mdns-name"];
+ if(options['mdns-domain']) _options["domain"] = options["mdns-domain"];
+ if(options['mdns-host']) _options["host"] = options["mdns-host"];
+
+ var ad = mdns.createAdvertisement(
+ regtype, port, options
+ );
+ ad.start();
+
+ // FIXME: Check so that advertisement was successful
+
+ if(callback) callback.apply(null, arguments);
+ });
+
+};
+
+//
+// Browses mDNS for hookio services with matching regtype and port
+// and connects to it if found. Gives up after options
+// 'mdns-listen-timeout' ms.
+//
+// TODO: implement mdns-domain to specify search area to non-local (I think)
+// TODO: instead of connecting to first found, make it possible to chose
+// from multiple ones through some sort of prioritization.
+//
+var connect = Hook.prototype.connect;
+mDNSHook.prototype.connect = function(options, callback) {
+ var self = this;
+ var browser = mdns.createBrowser(defaults.regtype);
+
+ var timer = setTimeout(function() {
+ // Stop listening for services, important for app
+ // to not enter an infinite loop
+ browser.stop();
+
+ if(callback) callback('no-server-found');
+ }, defaults['mdns-listen-timeout']);
+
+
+ var _connect = function(host, port) {
+ browser.stop();
+
+ options = options || {};
+ options['hook-host'] = host;
+ options['hook-port'] = port;
+
+ connect.call(self, options, callback);
+ clearTimeout(timer);
+ };
+
+ browser.on('serviceUp', function(info, flags) {
+ var host = info.addresses[0],
+ port = info.port;
+
+ if(options.port) {
+ if(options.port === port) {
+ _connect(host, port);
+ }
+ } else {
+ // If no port is not specified, connect to any hook found
+ _connect(host, port);
+ }
+ });
+ browser.on('serviceDown', function(info, flags) {
+ // TODO: use when implementing support for multiple services
+ });
+
+ browser.start();
+};
+
+//
+// Reversed version of the default start:
+// tries to connect first, and then falls back on listen
+mDNSHook.prototype.start = function (options, callback) {
+ var self = this;
+
+ self.connect(options, function(err) {
+ if(!err) {
+ // Connect to existing
+ if(callback) callback.apply(null, arguments);
+ } else {
+ // ... or start your own show
+ self.listen.call(self, options, callback);
+ }
+ });
+};
@@ -0,0 +1,27 @@
+{
+ "name": "hook.io-mdns",
+ "description": "Advertise and discover your hook.io's through mDNS",
+ "version": "0.0.1",
+ "author": "Emil Stenqvist <emsten@gmail.com>",
+ "contributors": [
+ {
+ "name" : "Emil Stenqvist",
+ "email" : "emsten@gmail.com"
+ }
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/emilisto/hook.io-mdns"
+ },
+ "bin": {
+ "hookio-irc": "bin/start"
+ },
+ "main": "./lib/mdns",
+ "dependencies": {
+ "hook.io": "0.8.x",
+ "mdns": "0.0.3"
+ },
+ "engine": [
+ "node 0.4.x"
+ ]
+}

0 comments on commit dad6c21

Please sign in to comment.