Skip to content

Commit

Permalink
feat: mdns announce
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jul 20, 2024
1 parent edd44c3 commit 8bf2ab8
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions satellite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"@elgato-stream-deck/node": "^6.2.2",
"@julusian/bonjour-service": "^1.3.0-0",
"@julusian/image-rs": "^0.4.0",
"@julusian/jpeg-turbo": "^2.2.0-1",
"@loupedeck/node": "^1.1.2",
Expand Down
7 changes: 7 additions & 0 deletions satellite/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface SatelliteConfig {

restEnabled: boolean
restPort: number

mdnsEnabled: boolean
}

export const satelliteConfigSchema: Schema<SatelliteConfig> = {
Expand Down Expand Up @@ -35,6 +37,11 @@ export const satelliteConfigSchema: Schema<SatelliteConfig> = {
maximum: 65535,
default: 9999,
},
mdnsEnabled: {
type: 'boolean',
description: 'Enable mDNS announcement',
default: true,
},
}

export function ensureFieldsPopulated(store: Conf<SatelliteConfig>): void {
Expand Down
3 changes: 3 additions & 0 deletions satellite/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RestServer } from './rest.js'
import { SatelliteConfig, ensureFieldsPopulated } from './config.js'
import { ApiConfigData, ApiStatusResponse, compileConfig, compileStatus, updateConfig } from './apiTypes.js'
import { fileURLToPath } from 'url'
import { MdnsAnnouncer } from './mdnsAnnouncer.js'

const appConfig = new electronStore<SatelliteConfig>({
// schema: satelliteConfigSchema,
Expand All @@ -33,6 +34,7 @@ const webRoot = fileURLToPath(new URL(app.isPackaged ? '../../webui' : '../../we
const client = new CompanionSatelliteClient({ debug: true })
const devices = new DeviceManager(client)
const server = new RestServer(webRoot, appConfig, client, devices)
const mdnsAnnouncer = new MdnsAnnouncer(appConfig)

appConfig.onDidChange('remoteIp', () => tryConnect())
appConfig.onDidChange('remotePort', () => tryConnect())
Expand Down Expand Up @@ -122,6 +124,7 @@ app.whenReady()

tryConnect()
restartRestApi()
mdnsAnnouncer.start()

let trayImagePath = new URL('../assets/tray.png', import.meta.url)
let trayImageOfflinePath = new URL('../assets/tray-offline.png', import.meta.url)
Expand Down
3 changes: 3 additions & 0 deletions satellite/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DEFAULT_PORT } from './lib.js'
import { RestServer } from './rest.js'
import { openHeadlessConfig } from './config.js'
import { fileURLToPath } from 'url'
import { MdnsAnnouncer } from './mdnsAnnouncer.js'

const rawConfigPath = process.argv[2]
if (!rawConfigPath) {
Expand All @@ -29,6 +30,7 @@ const webRoot = fileURLToPath(new URL('../../webui/dist', import.meta.url))
const client = new CompanionSatelliteClient({ debug: true })
const devices = new DeviceManager(client)
const server = new RestServer(webRoot, appConfig, client, devices)
const mdnsAnnouncer = new MdnsAnnouncer(appConfig)

client.on('log', (l) => console.log(l))
client.on('error', (e) => console.error(e))
Expand All @@ -51,3 +53,4 @@ appConfig.onDidChange('remotePort', () => tryConnect())

tryConnect()
server.open()
mdnsAnnouncer.start()
53 changes: 53 additions & 0 deletions satellite/src/mdnsAnnouncer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type Conf from 'conf'
import type { SatelliteConfig } from './config.js'
import { Bonjour, Service } from '@julusian/bonjour-service'

export class MdnsAnnouncer {
readonly #appConfig: Conf<SatelliteConfig>
readonly #bonjour = new Bonjour()

#bonjourService: Service | null = null

constructor(appConfig: Conf<SatelliteConfig>) {
this.#appConfig = appConfig

this.#appConfig.onDidChange('mdnsEnabled', () => this.restart())
this.#appConfig.onDidChange('restPort', () => this.restart())
this.#appConfig.onDidChange('restEnabled', () => this.restart())
}

restart() {
this.stop()
this.start()
}

start() {
if (!this.#appConfig.get('mdnsEnabled')) return
if (this.#bonjourService) return

try {
const restEnabled = this.#appConfig.get('restEnabled')
const restPort = this.#appConfig.get('restPort')

this.#bonjourService = this.#bonjour.publish({
name: 'Companion Satellite', // TODO - something customisable?
type: 'companion-satellite',
protocol: 'tcp',
port: this.#appConfig.get('restPort') || 9999,
txt: {
restEnabled: restEnabled,
restPort: restPort,
},
})
} catch (e) {
console.error('Failed to setup mdns publisher', e)
}
}

stop() {
if (this.#bonjourService) {
this.#bonjourService.stop?.()
this.#bonjourService = null
}
}
}
46 changes: 46 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,16 @@ __metadata:
languageName: node
linkType: hard

"@julusian/bonjour-service@npm:^1.3.0-0":
version: 1.3.0-0
resolution: "@julusian/bonjour-service@npm:1.3.0-0"
dependencies:
fast-deep-equal: "npm:^3.1.3"
multicast-dns: "npm:^7.2.5"
checksum: 10c0/eda196405331c2932a762a687c50174cdc0b6090866b89b538150275a6905c3e0d1f473038bea6ebf6c19f261177003bdc5af14f4413c32a345877a42352c068
languageName: node
linkType: hard

"@julusian/image-rs@npm:^0.4.0":
version: 0.4.0
resolution: "@julusian/image-rs@npm:0.4.0"
Expand All @@ -719,6 +729,13 @@ __metadata:
languageName: node
linkType: hard

"@leichtgewicht/ip-codec@npm:^2.0.1":
version: 2.0.5
resolution: "@leichtgewicht/ip-codec@npm:2.0.5"
checksum: 10c0/14a0112bd59615eef9e3446fea018045720cd3da85a98f801a685a818b0d96ef2a1f7227e8d271def546b2e2a0fe91ef915ba9dc912ab7967d2317b1a051d66b
languageName: node
linkType: hard

"@loupedeck/core@npm:1.1.2":
version: 1.1.2
resolution: "@loupedeck/core@npm:1.1.2"
Expand Down Expand Up @@ -3037,6 +3054,15 @@ __metadata:
languageName: node
linkType: hard

"dns-packet@npm:^5.2.2":
version: 5.6.1
resolution: "dns-packet@npm:5.6.1"
dependencies:
"@leichtgewicht/ip-codec": "npm:^2.0.1"
checksum: 10c0/8948d3d03063fb68e04a1e386875f8c3bcc398fc375f535f2b438fad8f41bf1afa6f5e70893ba44f4ae884c089247e0a31045722fa6ff0f01d228da103f1811d
languageName: node
linkType: hard

"doctrine@npm:^3.0.0":
version: 3.0.0
resolution: "doctrine@npm:3.0.0"
Expand Down Expand Up @@ -5338,6 +5364,18 @@ __metadata:
languageName: node
linkType: hard

"multicast-dns@npm:^7.2.5":
version: 7.2.5
resolution: "multicast-dns@npm:7.2.5"
dependencies:
dns-packet: "npm:^5.2.2"
thunky: "npm:^1.0.2"
bin:
multicast-dns: cli.js
checksum: 10c0/5120171d4bdb1577764c5afa96e413353bff530d1b37081cb29cccc747f989eb1baf40574fe8e27060fc1aef72b59c042f72b9b208413de33bcf411343c69057
languageName: node
linkType: hard

"nanoid@npm:^3.3.7":
version: 3.3.7
resolution: "nanoid@npm:3.3.7"
Expand Down Expand Up @@ -6371,6 +6409,7 @@ __metadata:
dependencies:
"@electron/notarize": "npm:^2.3.2"
"@elgato-stream-deck/node": "npm:^6.2.2"
"@julusian/bonjour-service": "npm:^1.3.0-0"
"@julusian/image-rs": "npm:^0.4.0"
"@julusian/jpeg-turbo": "npm:^2.2.0-1"
"@loupedeck/node": "npm:^1.1.2"
Expand Down Expand Up @@ -6884,6 +6923,13 @@ __metadata:
languageName: node
linkType: hard

"thunky@npm:^1.0.2":
version: 1.1.0
resolution: "thunky@npm:1.1.0"
checksum: 10c0/369764f39de1ce1de2ba2fa922db4a3f92e9c7f33bcc9a713241bc1f4a5238b484c17e0d36d1d533c625efb00e9e82c3e45f80b47586945557b45abb890156d2
languageName: node
linkType: hard

"tmp-promise@npm:^3.0.2":
version: 3.0.3
resolution: "tmp-promise@npm:3.0.3"
Expand Down

0 comments on commit 8bf2ab8

Please sign in to comment.