Skip to content

Commit e675341

Browse files
committed
OcaConnectionBroker: add deviceUpdated event type
1 parent 3651055 commit e675341

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Examples/OCABrokerTest/BrokerTest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public enum BrokerTest {
4040
print("failed to connect to \(event.deviceIdentifier): \(error)")
4141
}
4242
}
43+
case .deviceUpdated:
44+
print("device updated: \(event.deviceIdentifier)")
4345
default:
4446
break
4547
}

Sources/SwiftOCA/OCA/Browsing/ConnectionBroker.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public actor OcaConnectionBroker {
120120
case deviceAdded
121121
/// A device has been removed from the network or is no longer available
122122
case deviceRemoved
123+
/// An existing device has been updated (e.g. renamed via DNS-SD)
124+
case deviceUpdated
123125
/// The connection state of a device has changed
124126
case connectionStateChanged(Ocp1ConnectionState)
125127
}
@@ -333,10 +335,12 @@ public actor OcaConnectionBroker {
333335
return
334336
}
335337

336-
let event = Event(eventType: .deviceAdded, deviceIdentifier: deviceIdentifier)
337338
let device = DeviceInfo(deviceIdentifier: deviceIdentifier, serviceInfo: serviceInfo)
339+
let isExistingDevice =
340+
_devices[deviceIdentifier] != nil || _connections[deviceIdentifier] != nil
338341

339342
if let existingDevice = _devices[deviceIdentifier] {
343+
// Device still in _devices (add arrived before remove, or address change)
340344
try await withDeviceConnection(deviceIdentifier) { existingConnection in
341345
if (try? existingDevice.addresses) != (try? device.addresses),
342346
let mutableConnection = existingConnection as? Ocp1MutableConnection
@@ -351,9 +355,22 @@ public actor OcaConnectionBroker {
351355
try await existingConnection.set(options: _connectionOptions)
352356
}
353357
}
358+
} else if _connections[deviceIdentifier] != nil {
359+
// Device removed from _devices (e.g. DNS-SD name change) but connection persists;
360+
// re-enable automatic reconnect
361+
try? await withDeviceConnection(deviceIdentifier) { existingConnection in
362+
if _connectionOptions.flags.contains(.automaticReconnect),
363+
await !existingConnection.options.flags.contains(.automaticReconnect)
364+
{
365+
try await existingConnection.set(options: _connectionOptions)
366+
}
367+
}
354368
}
355369

356370
_devices[deviceIdentifier] = device
371+
372+
let eventType: EventType = isExistingDevice ? .deviceUpdated : .deviceAdded
373+
let event = Event(eventType: eventType, deviceIdentifier: deviceIdentifier)
357374
_eventsContinuation.yield(event)
358375
}
359376

0 commit comments

Comments
 (0)