Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle local name collision and avahi-daemon restart #1294

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 37 additions & 32 deletions src/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,39 @@ const debug = require('debug')('signalk-server:mdns')
const dnssd = require('dnssd2')
const ports = require('./ports')

module.exports = function mdnsResponder(app) {
const config = app.config
const registerWithRetries=function(mdns,type,retries){
let ad;
for (let i=0;i<retries;i++) {
if (i > 0) {
type.options.name = type.host + "-" + i;
console.log("retrying advertisement for " + type.type + " with name " + type.options.name);
}
else {
console.log("mdns register for "+type.type+":"+type.port);
}
try {
ad = new mdns.Advertisement(type.type, type.port, type.options)
ad.on('error', err => {
console.error("advertisement error for "+type.type+":"+type.port+": "+err+", retrying")
setTimeout(function(){
try{
ad.stop();
}catch (e){}
registerWithRetries(mdns,type,retries);
},3000)
})
ad.start()
break;
} catch (e) {
console.log("error in advertising", e);
}
}
}



module.exports = function mdnsResponder(app) {
const config = app.config;
let mdns = dnssd

try {
Expand All @@ -38,7 +68,6 @@ module.exports = function mdnsResponder(app) {
debug('Mdns disabled by configuration')
return
}

let txtRecord = {
txtvers: '1',
swname: config.name,
Expand Down Expand Up @@ -94,36 +123,12 @@ module.exports = function mdnsResponder(app) {
if (host !== require('os').hostname()) {
options.host = host
}

debug(options)

const ads = []
// tslint:disable-next-line: forin
for (const i in types) {
const type = types[i]
debug(
'Starting mDNS ad: ' +
type.type +
' ' +
app.config.getExternalHostname() +
':' +
type.port
)
const ad = new mdns.Advertisement(type.type, type.port, options)
ad.on('error', err => {
console.log(type.type.name)
console.error(err)
})
ad.start()
ads.push(ad)
for (const i in types){
types[i].options=Object.assign({},options);
types[i].host=host;
}

return {
stop: function() {
ads.forEach(function(ad) {
debug('Stopping mDNS advertisement...')
ad.stop()
})
}
for (const i in types){
registerWithRetries(mdns,types[i],20);
}
}