From 1369a621ad5bab309acf79fb82abb6e51c991747 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 10 Nov 2023 15:01:37 +0100 Subject: [PATCH] Attempt socket bugfix --- Sources/App/DeviceManager.swift | 31 ++++++++++++++----------------- Sources/App/configure.swift | 7 ++----- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Sources/App/DeviceManager.swift b/Sources/App/DeviceManager.swift index 4de97bb..456e15a 100644 --- a/Sources/App/DeviceManager.swift +++ b/Sources/App/DeviceManager.swift @@ -35,8 +35,6 @@ final class DeviceManager { private let messagesToDeviceMetric: Metric - private let scheduler: AsyncScheduler - var deviceState: DeviceState { guard let connection, !connection.isClosed else { return .disconnected @@ -55,22 +53,21 @@ final class DeviceManager { /// A promise to finish the request once the device responds or times out private var requestInProgress: CheckedContinuation? - init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64, scheduler: AsyncScheduler) { + init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64) { self.deviceKey = deviceKey self.remoteKey = remoteKey self.deviceTimeout = deviceTimeout self.deviceStateMetric = .init( "sesame.device", - name: "Device connected", - description: "Shows if the device is connected via WebSocket") + name: "Device status", + description: "Shows if the device is connected and authenticated via WebSocket") self.messagesToDeviceMetric = .init( "sesame.messages", name: "Forwarded Messages", description: "The number of messages transmitted to the device") - self.scheduler = scheduler } - private func updateDeviceConnectionMetric() async { + func updateDeviceConnectionMetric() async { _ = try? await deviceStateMetric.update(deviceState) } @@ -166,18 +163,18 @@ final class DeviceManager { func createNewDeviceConnection(_ socket: WebSocket) async { await removeDeviceConnection() + socket.eventLoop.execute { + socket.onBinary { [weak self] _, data in + self?.processDeviceResponse(data) + } + socket.onText { [weak self] _, text async in + await self?.authenticateDevice(hash: text) + } - socket.onBinary { [weak self] _, data in - self?.processDeviceResponse(data) + _ = socket.onClose.always { [weak self] _ in + self?.didCloseDeviceSocket() + } } - socket.onText { [weak self] _, text async in - await self?.authenticateDevice(hash: text) - } - - _ = socket.onClose.always { [weak self] _ in - self?.didCloseDeviceSocket() - } - connection = socket await updateDeviceConnectionMetric() } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 218dddb..4278251 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -35,11 +35,7 @@ public func configure(_ app: Application) throws { let keyFile = storageFolder.appendingPathComponent(config.keyFileName) let (deviceKey, remoteKey) = try loadKeys(at: keyFile) - deviceManager = DeviceManager( - deviceKey: deviceKey, - remoteKey: remoteKey, - deviceTimeout: config.deviceTimeout, - scheduler: asyncScheduler) + deviceManager = DeviceManager(deviceKey: deviceKey, remoteKey: remoteKey, deviceTimeout: config.deviceTimeout) try routes(app) @@ -49,6 +45,7 @@ public func configure(_ app: Application) throws { asyncScheduler.schedule { _ = try await status.update(.nominal) + await deviceManager.updateDeviceConnectionMetric() } let df = DateFormatter()