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

Commit

Permalink
feat: service names (#68)
Browse files Browse the repository at this point in the history
* fix: use 'ipfs.local' as service name

* test: must cope with async

* fix: support IPv6

* feat: more debug logging

* fix: immediately start a query, then do it every interval

* test: working stop test
  • Loading branch information
richardschneider authored and daviddias committed Feb 1, 2018
1 parent 0642fc4 commit fa8fe22
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ mdns.start(() => setTimeout(() => mdns.stop(() => {}), 20 * 1000))
```

- options
- `broadcast` - (true/false) announce our presence through mDNS
- `interval` - query interval
- `serviceTag` - name of the service announced
- `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`
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MulticastDNS extends EventEmitter {

this.broadcast = options.broadcast !== false
this.interval = options.interval || (1e3 * 10)
this.serviceTag = options.serviceTag || '_ipfs-discovery._udp'
this.serviceTag = options.serviceTag || 'ipfs.local'
this.port = options.port || 5353
this.peerInfo = peerInfo
this._queryInterval = null
Expand Down
15 changes: 11 additions & 4 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ const tcp = new TCP()
module.exports = {

queryLAN: function (mdns, serviceTag, interval) {
return setInterval(() => {
const query = () => {
log('query', serviceTag)
mdns.query({
questions: [{
name: serviceTag,
type: 'PTR'
}]
})
}, interval)
}

// Immediately start a query, then do it every interval.
query()
return setInterval(query, interval)
},

gotResponse: function (rsp, peerInfo, serviceTag, callback) {
Expand Down Expand Up @@ -55,8 +60,9 @@ module.exports = {
answers.a.forEach((a) => {
multiaddrs.push(new Multiaddr('/ip4/' + a.data + '/tcp/' + port))
})

// TODO Create multiaddrs from AAAA (IPv6) records as well
answers.aaaa.forEach((a) => {
multiaddrs.push(new Multiaddr('/ip6/' + a.data + '/tcp/' + port))
})

if (peerInfo.id.toB58String() === b58Id) {
return // replied to myself, ignore
Expand Down Expand Up @@ -141,6 +147,7 @@ module.exports = {
}
})

log('responding to query')
mdns.respond(answers)
}
}
Expand Down
67 changes: 53 additions & 14 deletions test/multicast-dns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const parallel = require('async/parallel')
const series = require('async/series')

const MulticastDNS = require('./../src')

Expand All @@ -18,7 +19,7 @@ describe('MulticastDNS', () => {
let pD

before(function (done) {
this.timeout(40 * 1000)
this.timeout(80 * 1000)
parallel([
(cb) => {
PeerInfo.create((err, peer) => {
Expand All @@ -35,6 +36,7 @@ describe('MulticastDNS', () => {

pB = peer
pB.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/20002'))
pB.multiaddrs.add(multiaddr('/ip6/::1/tcp/20002'))
cb()
})
},
Expand Down Expand Up @@ -64,7 +66,10 @@ describe('MulticastDNS', () => {
const options = {
port: 50001 // port must be the same
}
const mdnsA = new MulticastDNS(pA, options)
const mdnsA = new MulticastDNS(pA, {
broadcast: false, // do not talk to ourself
port: 50001
})
const mdnsB = new MulticastDNS(pB, options)

parallel([
Expand All @@ -73,7 +78,10 @@ describe('MulticastDNS', () => {
], () => {
mdnsA.once('peer', (peerInfo) => {
expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String())
done()
parallel([
(cb) => mdnsA.stop(cb),
(cb) => mdnsB.stop(cb)
], done)
})

mdnsB.once('peer', (peerInfo) => {})
Expand All @@ -87,7 +95,10 @@ describe('MulticastDNS', () => {
port: 50003 // port must be the same
}

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

Expand All @@ -100,13 +111,46 @@ describe('MulticastDNS', () => {
mdnsA.once('peer', (peerInfo) => {
expect(pC.id.toB58String()).to.eql(peerInfo.id.toB58String())
expect(peerInfo.multiaddrs.size).to.equal(1)
done()
parallel([
(cb) => mdnsA.stop(cb),
(cb) => mdnsC.stop(cb),
(cb) => mdnsD.stop(cb)
], done)
})

mdnsC.once('peer', (peerInfo) => {})
})
})

it('announces IP6 addresses', function (done) {
this.timeout(40 * 1000)

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

series([
(cb) => mdnsB.start(cb),
(cb) => mdnsA.start(cb)
], () => {
mdnsA.once('peer', (peerInfo) => {
expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String())
expect(peerInfo.multiaddrs.size).to.equal(2)
parallel([
(cb) => mdnsA.stop(cb),
(cb) => mdnsB.stop(cb)
], done)
})

mdnsB.once('peer', (peerInfo) => {})
})
})

it('doesn\'t emit peers after stop', function (done) {
this.timeout(40 * 1000)

Expand All @@ -116,18 +160,13 @@ describe('MulticastDNS', () => {
const mdnsA = new MulticastDNS(pA, options)
const mdnsC = new MulticastDNS(pC, options)

setTimeout(done, 15000)

parallel([
series([
(cb) => mdnsA.start(cb),
(cb) => setTimeout(cb, 1000),
(cb) => mdnsA.stop(cb),
(cb) => mdnsC.start(cb)
], () => {
mdnsA.stop((err) => {
if (err) {
return done(err)
}
})

setTimeout(() => mdnsC.stop(done), 5000)
mdnsC.once('peer', (peerInfo) => {
done(new Error('Should not receive new peer.'))
})
Expand Down

0 comments on commit fa8fe22

Please sign in to comment.