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

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
vasco-santos and jacobheun committed Apr 21, 2020
1 parent 5c6daaf commit 2b52761
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"chai": "^4.2.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"libp2p-interfaces": "libp2p/js-interfaces#v0.3.x",
"libp2p-interfaces": "^0.3.0",
"p-defer": "^3.0.0"
},
"dependencies": {
Expand Down
19 changes: 14 additions & 5 deletions src/compat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const Responder = require('./responder')
const Querier = require('./querier')

class GoMulticastDNS extends EE {
constructor (peerId, multiaddrs) {
constructor ({ peerId, multiaddrs, queryPeriod, queryInterval }) {
super()
this._started = false
this._peerId = peerId
this._multiaddrs = multiaddrs
this._queryPeriod = queryPeriod
this._queryInterval = queryInterval
this._onPeer = this._onPeer.bind(this)
}

Expand All @@ -21,8 +23,15 @@ class GoMulticastDNS extends EE {
}

this._started = true
this._responder = new Responder(this._peerId, this._multiaddrs)
this._querier = new Querier(this._peerId)
this._responder = new Responder({
peerId: this._peerId,
multiaddrs: this._multiaddrs
})
this._querier = new Querier({
peerId: this._peerId,
queryInterval: this._queryInterval,
queryPeriod: this._queryPeriod
})

this._querier.on('peer', this._onPeer)

Expand All @@ -32,8 +41,8 @@ class GoMulticastDNS extends EE {
])
}

_onPeer (peerInfo) {
this.emit('peer', peerInfo)
_onPeer (peerData) {
this.emit('peer', peerData)
}

stop () {
Expand Down
22 changes: 11 additions & 11 deletions src/compat/querier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ log.error = debug('libp2p:mdns:compat:querier:error')
const { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } = require('./constants')

class Querier extends EE {
constructor (peerId, options) {
constructor ({ peerId, queryInterval = 60000, queryPeriod }) {
super()

if (!peerId) {
throw new Error('missing peerId parameter')
}

options = options || {}
this._peerIdStr = peerId.toB58String()
// Re-query every 60s, in leu of network change detection
options.queryInterval = options.queryInterval || 60000
// Time for which the MDNS server will stay alive waiting for responses
// Must be less than options.queryInterval!
options.queryPeriod = Math.min(
options.queryInterval,
options.queryPeriod == null ? 5000 : options.queryPeriod
)
this._options = options
this._options = {
// Re-query in leu of network change detection (every 60s by default)
queryInterval: queryInterval,
// Time for which the MDNS server will stay alive waiting for responses
// Must be less than options.queryInterval!
queryPeriod: Math.min(
queryInterval,
queryPeriod == null ? 5000 : queryPeriod
)
}
this._onResponse = this._onResponse.bind(this)
}

Expand Down
2 changes: 1 addition & 1 deletion src/compat/responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const log = require('debug')('libp2p:mdns:compat:responder')
const { SERVICE_TAG_LOCAL } = require('./constants')

class Responder {
constructor (peerId, multiaddrs) {
constructor ({ peerId, multiaddrs }) {
if (!peerId) {
throw new Error('missing peerId parameter')
}
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class MulticastDNS extends EventEmitter {
this._onPeer = this._onPeer.bind(this)

if (options.compat !== false) {
this._goMdns = new GoMulticastDNS(options.peerId, this.peerMultiaddrs, {
this._goMdns = new GoMulticastDNS({
multiaddrs: this.peerMultiaddrs,
peerId: options.peerId,
queryPeriod: options.compatQueryPeriod,
queryInterval: options.compatQueryInterval
})
Expand Down Expand Up @@ -56,9 +58,9 @@ class MulticastDNS extends EventEmitter {
query.gotQuery(event, this.mdns, this.peerId, this.peerMultiaddrs, this.serviceTag, this.broadcast)
}

async _onMdnsResponse (event) {
_onMdnsResponse (event) {
try {
const foundPeer = await query.gotResponse(event, this.peerId, this.serviceTag)
const foundPeer = query.gotResponse(event, this.peerId, this.serviceTag)

if (foundPeer) {
this.emit('peer', foundPeer)
Expand All @@ -68,8 +70,8 @@ class MulticastDNS extends EventEmitter {
}
}

_onPeer (peerInfo) {
this.emit('peer', peerInfo)
_onPeer (peerData) {
this.emit('peer', peerData)
}

/**
Expand Down
27 changes: 21 additions & 6 deletions test/compat/go-multicast-dns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ describe('GoMulticastDNS', () => {
})

it('should start and stop', async () => {
const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]])
const mdns = new GoMulticastDNS({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})

await mdns.start()
return mdns.stop()
})

it('should ignore multiple start calls', async () => {
const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]])
const mdns = new GoMulticastDNS({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})

await mdns.start()
await mdns.start()
Expand All @@ -43,13 +49,22 @@ describe('GoMulticastDNS', () => {
})

it('should ignore unnecessary stop calls', async () => {
const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]])
const mdns = new GoMulticastDNS({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})
await mdns.stop()
})

it('should emit peer info when peer is discovered', async () => {
const mdnsA = new GoMulticastDNS(peerIds[0], [peerAddrs[0]])
const mdnsB = new GoMulticastDNS(peerIds[1], [peerAddrs[1]])
it('should emit peer data when peer is discovered', async () => {
const mdnsA = new GoMulticastDNS({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})
const mdnsB = new GoMulticastDNS({
peerId: peerIds[1],
multiaddrs: [peerAddrs[1]]
})
const defer = pDefer()

mdnsA.on('peer', ({ id, multiaddrs }) => {
Expand Down
10 changes: 5 additions & 5 deletions test/compat/querier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ describe('Querier', () => {
})

it('should start and stop', async () => {
const querier = new Querier(peerIds[0])
const querier = new Querier({ peerId: peerIds[0] })

await querier.start()
await querier.stop()
})

it('should query on interval', async () => {
querier = new Querier(peerIds[0], { queryPeriod: 0, queryInterval: 10 })
querier = new Querier({ peerId: peerIds[0], queryPeriod: 0, queryInterval: 10 })
mdns = MDNS()

let queryCount = 0
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('Querier', () => {
* @param {Function} getResponse Given a query, construct a response to test the querier
*/
async function ensurePeer (getResponse) {
querier = new Querier(peerIds[0])
querier = new Querier({ peerId: peerIds[0] })
mdns = MDNS()

mdns.on('query', (event, info) => {
Expand All @@ -266,11 +266,11 @@ describe('Querier', () => {
}

/**
* Ensure none of peerInfos are emitted from `querier`
* Ensure none of peerIds are emitted from `querier`
* @param {Function} getResponse Given a query, construct a response to test the querier
*/
async function ensureNoPeer (getResponse) {
querier = new Querier(peerIds[0])
querier = new Querier({ peerId: peerIds[0] })
mdns = MDNS()

mdns.on('query', (event, info) => {
Expand Down
20 changes: 16 additions & 4 deletions test/compat/responder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ describe('Responder', () => {
})

it('should start and stop', async () => {
const responder = new Responder(peerIds[0], [peerAddrs[0]])
const responder = new Responder({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})

await responder.start()
await responder.stop()
})

it('should not respond to a query if no TCP addresses', async () => {
const peerId = await PeerId.create()
responder = new Responder(peerId, [])
responder = new Responder({
peerId,
multiaddrs: []
})
mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 })

await responder.start()
Expand All @@ -72,7 +78,10 @@ describe('Responder', () => {
})

it('should not respond to a query with non matching service tag', async () => {
responder = new Responder(peerIds[0], [peerAddrs[0]])
responder = new Responder({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})
mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 })

await responder.start()
Expand Down Expand Up @@ -100,7 +109,10 @@ describe('Responder', () => {
})

it('should respond correctly', async () => {
responder = new Responder(peerIds[0], [peerAddrs[0]])
responder = new Responder({
peerId: peerIds[0],
multiaddrs: [peerAddrs[0]]
})
mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 })

await responder.start()
Expand Down
8 changes: 4 additions & 4 deletions test/multicast-dns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ describe('MulticastDNS', () => {
await mdns.start()

return new Promise((resolve, reject) => {
mdns.on('peer', (peerInfo) => {
if (!peerInfo) {
reject(new Error('peerInfo was not set'))
mdns.on('peer', (peerData) => {
if (!peerData) {
reject(new Error('peerData was not set'))
}
})

mdns.mdns.on('response', () => {
// query.gotResponse is async - we'll bail from that method when
// comparing the senders PeerId to our own but it'll happen later
// so allow enough time for the test to have failed if we emit
// empty PeerInfo objects
// empty peerData objects
setTimeout(() => {
resolve()
}, 100)
Expand Down

0 comments on commit 2b52761

Please sign in to comment.