Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Add tag #74

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ libp2p-mdns JavaScript implementation
```JavaScript
const MDNS = require('libp2p-mdns')

const mdns = new MDNS(peerInfo, options)
const mdns = new MDNS(options)

mdns.on('peer', (peerInfo) => {
console.log('Found a peer in the local network', peerInfo.id.toB58String())
Expand All @@ -28,9 +28,10 @@ mdns.start(() => setTimeout(() => mdns.stop(() => {}), 20 * 1000))
```

- options
- `peerInfo` - PeerInfo to announce
- `broadcast` - (true/false) announce our presence through mDNS, default false
- `interval` - query interval, default 10 * 1000 (10 seconds)
- `serviceTag` - name of the service announcedm, default 'ipfs.local`
- `serviceTag` - name of the service announce , default 'ipfs.local`

# MDNS messages

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-mdns",
"devDependencies": {
"aegir": "^13.0.6",
"async": "^2.6.0",
"aegir": "^14.0.0",
"async": "^2.6.1",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
},
"dependencies": {
"libp2p-tcp": "~0.12.0",
"multiaddr": "^4.0.0",
"multiaddr": "^5.0.0",
"multicast-dns": "^7.0.0",
"peer-id": "~0.10.7",
"peer-info": "~0.14.0"
"peer-info": "~0.14.1"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

const multicastDNS = require('multicast-dns')
const EventEmitter = require('events').EventEmitter
const assert = require('assert')
const debug = require('debug')
const log = debug('libp2p:mdns')
const query = require('./query')

class MulticastDNS extends EventEmitter {
constructor (peerInfo, options) {
constructor (options) {
super()
options = options || {}
assert(options.peerInfo, 'needs a PeerInfo to work')

this.broadcast = options.broadcast !== false
this.interval = options.interval || (1e3 * 10)
this.serviceTag = options.serviceTag || 'ipfs.local'
this.port = options.port || 5353
this.peerInfo = peerInfo
this.peerInfo = options.peerInfo
this._queryInterval = null
}

Expand Down Expand Up @@ -56,7 +57,8 @@ class MulticastDNS extends EventEmitter {
}
}

module.exports = MulticastDNS
exports = module.exports = MulticastDNS
exports.tag = 'mdns'

/* for reference

Expand Down
54 changes: 33 additions & 21 deletions test/multicast-dns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('MulticastDNS', () => {

before(function (done) {
this.timeout(80 * 1000)

parallel([
(cb) => {
PeerInfo.create((err, peer) => {
Expand Down Expand Up @@ -63,14 +64,16 @@ describe('MulticastDNS', () => {
it('find another peer', function (done) {
this.timeout(40 * 1000)

const options = {
port: 50001 // port must be the same
}
const mdnsA = new MulticastDNS(pA, {
const mdnsA = new MulticastDNS({
peerInfo: pA,
broadcast: false, // do not talk to ourself
port: 50001
})
const mdnsB = new MulticastDNS(pB, options)

const mdnsB = new MulticastDNS({
peerInfo: pB,
port: 50001 // port must be the same
})

parallel([
(cb) => mdnsA.start(cb),
Expand All @@ -91,16 +94,19 @@ describe('MulticastDNS', () => {
it('only announce TCP multiaddrs', function (done) {
this.timeout(40 * 1000)

const options = {
port: 50003 // port must be the same
}

const mdnsA = new MulticastDNS(pA, {
const mdnsA = new MulticastDNS({
peerInfo: pA,
broadcast: false, // do not talk to ourself
port: 50003
})
const mdnsC = new MulticastDNS(pC, options)
const mdnsD = new MulticastDNS(pD, options)
const mdnsC = new MulticastDNS({
peerInfo: pC,
port: 50003 // port must be the same
})
const mdnsD = new MulticastDNS({
peerInfo: pD,
port: 50003 // port must be the same
})

parallel([
(cb) => mdnsA.start(cb),
Expand All @@ -125,14 +131,16 @@ describe('MulticastDNS', () => {
it('announces IP6 addresses', function (done) {
this.timeout(40 * 1000)

const options = {
port: 50001 // port must be the same
}
const mdnsA = new MulticastDNS(pA, {
const mdnsA = new MulticastDNS({
peerInfo: pA,
broadcast: false, // do not talk to ourself
port: 50001
})
const mdnsB = new MulticastDNS(pB, options)

const mdnsB = new MulticastDNS({
peerInfo: pB,
port: 50001
})

series([
(cb) => mdnsB.start(cb),
Expand All @@ -154,11 +162,15 @@ describe('MulticastDNS', () => {
it('doesn\'t emit peers after stop', function (done) {
this.timeout(40 * 1000)

const options = {
const mdnsA = new MulticastDNS({
peerInfo: pA,
port: 50004 // port must be the same
}
const mdnsA = new MulticastDNS(pA, options)
const mdnsC = new MulticastDNS(pC, options)
})

const mdnsC = new MulticastDNS({
peerInfo: pC,
port: 50004
})

series([
(cb) => mdnsA.start(cb),
Expand Down