[
  {
    "path": ".gitignore",
    "content": "node_modules\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: node_js\nnode_js:\n  - \"0.10\"\n  - '0.12'\n  - 'iojs'\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 Mathias Buus\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# airswarm\n\nNetwork swarm that automagically discovers other peers on the network using multicast dns\n\n```\nnpm install airswarm\n```\n\n[![build status](http://img.shields.io/travis/mafintosh/airswarm.svg?style=flat)](http://travis-ci.org/mafintosh/airswarm)\n\n## Usage\n\n``` js\nvar airswarm = require('airswarm')\n\nairswarm('testing', function (sock) {\n  sock.write('hello world (' + process.pid + ')\\n')\n  sock.pipe(process.stdout)\n})\n```\n\nIf you run the above program in a couple of processes on the same local network\nthe swarms should start connecting to each other and write hello world\n\n## API\n\n#### `swarm = airswarm(name, [options], [onpeer])`\n\nCreate a new swarm. The `swarm` will emit `peer` everytime a new peer\nis connected. Optionally you can pass a `peer` listener as the second argument.\n\nThe `peer` will be a tcp stream to another swarm.\n\nOptions include\n\n``` js\n{\n  limit: maxPeersToConnectTo // defaults to Infinity\n}\n```\n\n#### `swarm.peers`\n\nAn array containing all the currently connected peers\n\n## License\n\nMIT\n"
  },
  {
    "path": "example.js",
    "content": "var airswarm = require('airswarm')\n\nairswarm('testing', function (sock) {\n  sock.write('hello world (' + process.pid + ')\\n')\n  sock.pipe(process.stdout)\n})\n"
  },
  {
    "path": "index.js",
    "content": "var multicastdns = require('multicast-dns')\nvar net = require('net')\nvar addr = require('network-address')\n\nmodule.exports = function airswarm (name, opts, fn) {\n  if (typeof opts === 'function') return airswarm(name, null, opts)\n  if (!opts) opts = {}\n\n  var limit = opts.limit || Infinity\n  var mdns = multicastdns()\n  var connections = {}\n\n  var server = net.createServer(function (sock) {\n    sock.on('error', function (err) {\n      sock.destroy(err)\n    })\n    track(sock)\n  })\n\n  server.peers = []\n\n  function track (sock) {\n    if (server.peers.length >= limit) return sock.destroy()\n    server.peers.push(sock)\n    sock.on('close', function () {\n      server.peers.splice(server.peers.indexOf(sock), 1)\n    })\n    server.emit('peer', sock)\n  }\n\n  server.on('listening', function () {\n    var host = addr()\n    var port = server.address().port\n    var id = host + ':' + port\n\n    mdns.on('query', function (q) {\n      for (var i = 0; i < q.questions.length; i++) {\n        var qs = q.questions[i]\n        if (qs.name === name && qs.type === 'SRV') return respond()\n      }\n    })\n\n    mdns.on('response', function (r) {\n      for (var i = 0; i < r.answers.length; i++) {\n        var a = r.answers[i]\n        if (a.name === name && a.type === 'SRV') connect(a.data.target, a.data.port)\n      }\n    })\n\n    update()\n    var interval = setInterval(update, 3000)\n\n    server.on('close', function () {\n      clearInterval(interval)\n    })\n\n    function respond () {\n      mdns.response([{\n        name: name,\n        type: 'SRV',\n        data: {\n          port: port,\n          weigth: 0,\n          priority: 10,\n          target: host\n        }\n      }])\n    }\n\n    function update () {\n      if (server.peers.length < limit) mdns.query([{name: name, type: 'SRV'}])\n    }\n\n    function connect (host, port) {\n      var remoteId = host + ':' + port\n      if (remoteId === id) return\n      if (connections[remoteId]) return\n      if (remoteId < id) return respond()\n\n      var sock = connections[remoteId] = net.connect(port, host)\n\n      sock.on('error', function () {\n        sock.destroy()\n      })\n\n      sock.on('close', function () {\n        delete connections[remoteId]\n      })\n\n      track(sock)\n    }\n  })\n\n  if (fn) server.on('peer', fn)\n  server.listen(0)\n\n  return server\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"airswarm\",\n  \"version\": \"1.1.0\",\n  \"description\": \"Network swarm that automagically discovers other peers on the network using multicast dns\",\n  \"main\": \"index.js\",\n  \"dependencies\": {\n    \"multicast-dns\": \"^3.0.0\",\n    \"network-address\": \"^1.0.0\"\n  },\n  \"devDependencies\": {\n    \"standard\": \"^5.2.1\",\n    \"tape\": \"^4.2.0\"\n  },\n  \"scripts\": {\n    \"test\": \"standard && tape test.js\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"https://github.com/mafintosh/airswarm.git\"\n  },\n  \"author\": \"Mathias Buus (@mafintosh)\",\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/mafintosh/airswarm/issues\"\n  },\n  \"homepage\": \"https://github.com/mafintosh/airswarm\"\n}\n"
  },
  {
    "path": "test.js",
    "content": "var tape = require('tape')\nvar airswarm = require('./')\n\ntape('connects', function (t) {\n  t.plan(3)\n\n  var once1 = true\n  var once2 = true\n\n  airswarm(process.pid + '-testing--', function (sock) {\n    t.ok(once1, 'got socket')\n    once1 = false\n    sock.on('data', function (data) {\n      t.same(data.toString(), '+')\n    })\n  })\n\n  airswarm(process.pid + '-testing--', function (sock) {\n    t.ok(once2, 'got socket')\n    once2 = false\n    sock.write('+')\n  })\n})\n\ntape('exits', function (t) {\n  t.end()\n  process.exit(0)\n})\n"
  }
]