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

[WIP] feature: use vessel name as mdns instanceName #1300

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
99 changes: 77 additions & 22 deletions src/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,17 @@ module.exports = function mdnsResponder(app) {
txt: txtRecord
}

const host = app.config.getExternalHostname()
const instanceName = getInstanceName(app.signalk)

const host = app.config.getExternalHostname()
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)
}
types.forEach((type, i) => {
startAdWithNamedRetry(mdns, type, i, options, instanceName, ads, 0)
})

return {
stop: function() {
Expand All @@ -127,3 +110,75 @@ 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`)
}

// 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
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 + (retryIndex > 0 ? `-${retryIndex}` : '')
}

function utfLength(s) {
// tslint:disable-next-line:no-bitwise
return ~-encodeURI(s).split(/%..|./).length
}