Repository: mafintosh/airswarm Branch: master Commit: 3a9acb5017f9 Files: 8 Total size: 5.7 KB Directory structure: gitextract_6zr_6dzb/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - "0.10" - '0.12' - 'iojs' ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Mathias Buus 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. ================================================ FILE: README.md ================================================ # airswarm Network swarm that automagically discovers other peers on the network using multicast dns ``` npm install airswarm ``` [![build status](http://img.shields.io/travis/mafintosh/airswarm.svg?style=flat)](http://travis-ci.org/mafintosh/airswarm) ## Usage ``` js var airswarm = require('airswarm') airswarm('testing', function (sock) { sock.write('hello world (' + process.pid + ')\n') sock.pipe(process.stdout) }) ``` If you run the above program in a couple of processes on the same local network the swarms should start connecting to each other and write hello world ## API #### `swarm = airswarm(name, [options], [onpeer])` Create a new swarm. The `swarm` will emit `peer` everytime a new peer is connected. Optionally you can pass a `peer` listener as the second argument. The `peer` will be a tcp stream to another swarm. Options include ``` js { limit: maxPeersToConnectTo // defaults to Infinity } ``` #### `swarm.peers` An array containing all the currently connected peers ## License MIT ================================================ FILE: example.js ================================================ var airswarm = require('airswarm') airswarm('testing', function (sock) { sock.write('hello world (' + process.pid + ')\n') sock.pipe(process.stdout) }) ================================================ FILE: index.js ================================================ var multicastdns = require('multicast-dns') var net = require('net') var addr = require('network-address') module.exports = function airswarm (name, opts, fn) { if (typeof opts === 'function') return airswarm(name, null, opts) if (!opts) opts = {} var limit = opts.limit || Infinity var mdns = multicastdns() var connections = {} var server = net.createServer(function (sock) { sock.on('error', function (err) { sock.destroy(err) }) track(sock) }) server.peers = [] function track (sock) { if (server.peers.length >= limit) return sock.destroy() server.peers.push(sock) sock.on('close', function () { server.peers.splice(server.peers.indexOf(sock), 1) }) server.emit('peer', sock) } server.on('listening', function () { var host = addr() var port = server.address().port var id = host + ':' + port mdns.on('query', function (q) { for (var i = 0; i < q.questions.length; i++) { var qs = q.questions[i] if (qs.name === name && qs.type === 'SRV') return respond() } }) mdns.on('response', function (r) { for (var i = 0; i < r.answers.length; i++) { var a = r.answers[i] if (a.name === name && a.type === 'SRV') connect(a.data.target, a.data.port) } }) update() var interval = setInterval(update, 3000) server.on('close', function () { clearInterval(interval) }) function respond () { mdns.response([{ name: name, type: 'SRV', data: { port: port, weigth: 0, priority: 10, target: host } }]) } function update () { if (server.peers.length < limit) mdns.query([{name: name, type: 'SRV'}]) } function connect (host, port) { var remoteId = host + ':' + port if (remoteId === id) return if (connections[remoteId]) return if (remoteId < id) return respond() var sock = connections[remoteId] = net.connect(port, host) sock.on('error', function () { sock.destroy() }) sock.on('close', function () { delete connections[remoteId] }) track(sock) } }) if (fn) server.on('peer', fn) server.listen(0) return server } ================================================ FILE: package.json ================================================ { "name": "airswarm", "version": "1.1.0", "description": "Network swarm that automagically discovers other peers on the network using multicast dns", "main": "index.js", "dependencies": { "multicast-dns": "^3.0.0", "network-address": "^1.0.0" }, "devDependencies": { "standard": "^5.2.1", "tape": "^4.2.0" }, "scripts": { "test": "standard && tape test.js" }, "repository": { "type": "git", "url": "https://github.com/mafintosh/airswarm.git" }, "author": "Mathias Buus (@mafintosh)", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/airswarm/issues" }, "homepage": "https://github.com/mafintosh/airswarm" } ================================================ FILE: test.js ================================================ var tape = require('tape') var airswarm = require('./') tape('connects', function (t) { t.plan(3) var once1 = true var once2 = true airswarm(process.pid + '-testing--', function (sock) { t.ok(once1, 'got socket') once1 = false sock.on('data', function (data) { t.same(data.toString(), '+') }) }) airswarm(process.pid + '-testing--', function (sock) { t.ok(once2, 'got socket') once2 = false sock.write('+') }) }) tape('exits', function (t) { t.end() process.exit(0) })