Skip to content

Commit

Permalink
feature: mdns retry with indexed names
Browse files Browse the repository at this point in the history
Add mdns advertisement retry with instance names that
have the retry count appended to the name. Retries
9 times and gives up.
  • Loading branch information
tkurki committed Apr 7, 2021
1 parent d21ad32 commit a456cd8
Showing 1 changed file with 56 additions and 24 deletions.
80 changes: 56 additions & 24 deletions src/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,7 @@ module.exports = function mdnsResponder(app) {

const ads = []
types.forEach((type, i) => {
debug(
'Starting mDNS ad: ' +
type.type +
' ' +
app.config.getExternalHostname() +
':' +
type.port
)
let name
if (instanceName) {
name = toUtfMaxLength(i === 0 ? `SK ${instanceName}` : instanceName)
}
const optionsForType = { name, ...options }
debug(optionsForType)
const ad = new mdns.Advertisement(type.type, type.port, optionsForType)
ad.on('error', err => {
console.log(type.type.name)
console.error(err)
})
ad.start()
ads.push(ad)
startAdWithNamedRetry(mdns, type, i, options, instanceName, ads, 0)
})

return {
Expand All @@ -131,19 +111,71 @@ module.exports = function mdnsResponder(app) {
}
}

const MAX_RETRIES = 9

function startAdWithNamedRetry(
mdns,
type,
i,
options,
instanceName,
ads,
retryIndex
) {
if (retryIndex > MAX_RETRIES) {
return
}

debug(`Starting mDNS ad: ${type.type} ${type.host} ${type.port}`)

let name
if (instanceName) {
name = toLengthCappedIndexedName(
i === 0 ? `SK ${instanceName}` : instanceName,
retryIndex
)
}
const optionsForType = { name, ...options }
debug(optionsForType)
const ad = new mdns.Advertisement(type.type, type.port, optionsForType)
ad.on('error', err => {
console.log(type.type.name)
console.error(err)
try {
ad.stop()
} catch (e) {
console.error(e)
}
startAdWithNamedRetry(
mdns,
type,
i,
options,
instanceName,
ads,
retryIndex + 1
)
})
ad.start()
ads[i] = ad
}

const AD_NAME_MAX_UTF_LENGTH = 63 - 3 //allow prefix 'SK ' for http

function getInstanceName(signalk) {
const full = signalk.retrieve()
return _.get(full, `${_.get(full, 'self')}.name`)
}

function toUtfMaxLength(s) {
// return the string with utf length capped to 60
// with retry count appended as -n for n >0
function toLengthCappedIndexedName(s, retryIndex) {
let result = s
while (utfLength(result) > AD_NAME_MAX_UTF_LENGTH) {
const maxLength = AD_NAME_MAX_UTF_LENGTH - (retryIndex > 0 ? '-X'.length : 0)
while (utfLength(result) > maxLength) {
result = result.slice(0, result.length - 1)
}
return result
return result + (retryIndex > 0 ? `-${retryIndex}` : '')
}

function utfLength(s) {
Expand Down

0 comments on commit a456cd8

Please sign in to comment.