diff --git a/Package.resolved b/Package.resolved index dce7b23..09d6329 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,22 +1,13 @@ { - "originHash" : "2b9f814946e4952387f6691929b3d5d9a95ddbf99f1704f4beaab390f06560d6", + "originHash" : "310cf77e37f4fb0934d2f2d16691a6155cb61148b115825c6dace23cf8e569f5", "pins" : [ { "identity" : "sublimation", "kind" : "remoteSourceControl", - "location" : "https://github.com/brightdigit/Sublimation", + "location" : "https://github.com/brightdigit/Sublimation.git", "state" : { - "revision" : "99d0da9f907d27deb180582b274c5f83e6732921", - "version" : "2.0.0-beta.1" - } - }, - { - "identity" : "swift-log", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-log.git", - "state" : { - "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", - "version" : "1.6.1" + "revision" : "593020a9e76039b21cb07114b67ae5abb84a7ffa", + "version" : "2.0.1" } }, { @@ -24,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-protobuf.git", "state" : { - "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", - "version" : "1.27.1" + "revision" : "ebc7251dd5b37f627c93698e4374084d98409633", + "version" : "1.28.2" } } ], diff --git a/Package.swift b/Package.swift index 68d25d6..b930fcf 100644 --- a/Package.swift +++ b/Package.swift @@ -41,9 +41,8 @@ let package = Package( .library(name: "SublimationBonjour", targets: ["SublimationBonjour"]) ], dependencies: [ - .package(url: "https://github.com/brightdigit/Sublimation", from: "2.0.0-alpha.5"), - .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0"), - .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0") + .package(url: "https://github.com/brightdigit/Sublimation.git", from: "2.0.1"), + .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0") ], targets: [ .target( @@ -51,8 +50,7 @@ let package = Package( dependencies: [ .product(name: "Sublimation", package: "Sublimation"), .product(name: "SublimationCore", package: "Sublimation"), - .product(name: "SwiftProtobuf", package: "swift-protobuf"), - .product(name: "Logging", package: "swift-log") + .product(name: "SwiftProtobuf", package: "swift-protobuf") ], swiftSettings: swiftSettings ), diff --git a/Sources/SublimationBonjour/Client/BonjourClient.swift b/Sources/SublimationBonjour/Client/BonjourClient.swift index 26297bc..fe4d4cb 100644 --- a/Sources/SublimationBonjour/Client/BonjourClient.swift +++ b/Sources/SublimationBonjour/Client/BonjourClient.swift @@ -29,8 +29,7 @@ #if canImport(Network) public import Foundation - - import Network + internal import Network #if canImport(os) public import os @@ -46,9 +45,10 @@ /// let hostURL = await depositor.first() /// ``` public actor BonjourClient { + private let browser: NWBrowser private let streams = StreamManager() - private let logger: Logger? + private let logger: LoggerType? private let defaultURLConfiguration: URLDefaultConfiguration /// AsyncStream of `URL` from the network. @@ -76,7 +76,10 @@ /// - Parameters: /// - logger: Logger /// - defaultURLConfiguration: default ``URL`` configuration for missing properties. - public init(logger: Logger? = nil, defaultURLConfiguration: URLDefaultConfiguration = .init()) { + public init( + logger: LoggerType? = nil, + defaultURLConfiguration: URLDefaultConfiguration = .init() + ) { assert(logger != nil) let descriptor: NWBrowser.Descriptor descriptor = .bonjourWithTXTRecord(type: "_sublimation._tcp", domain: nil) diff --git a/Sources/SublimationBonjour/Client/LoggerType.swift b/Sources/SublimationBonjour/Client/LoggerType.swift new file mode 100644 index 0000000..d82056a --- /dev/null +++ b/Sources/SublimationBonjour/Client/LoggerType.swift @@ -0,0 +1,41 @@ +// +// LoggerType.swift +// SublimationBonjour +// +// Created by Leo Dion. +// Copyright © 2024 BrightDigit. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +#if canImport(os) + public import os +#elseif canImport(Logging) + public import Logging +#endif + +#if canImport(os) || canImport(Logging) + public typealias LoggerType = Logger +#else + public typealias LoggerType = any NilLoggerType + public protocol NilLoggerType { func debug(_ message: String) } +#endif diff --git a/Sources/SublimationBonjour/Client/StreamManager.swift b/Sources/SublimationBonjour/Client/StreamManager.swift index ad92ad5..61582e6 100644 --- a/Sources/SublimationBonjour/Client/StreamManager.swift +++ b/Sources/SublimationBonjour/Client/StreamManager.swift @@ -29,12 +29,6 @@ internal import Foundation -#if canImport(os) - internal import os -#elseif canImport(Logging) - internal import Logging -#endif - internal actor StreamManager { private var streamContinuations: [Key: AsyncStream.Continuation] = [:] @@ -44,7 +38,7 @@ internal actor StreamManager { internal init(newID: @escaping @Sendable () -> Key) { self.newID = newID } - internal func yield(_ urls: [Value], logger: Logger?) { + internal func yield(_ urls: [Value], logger: LoggerType?) { if streamContinuations.isEmpty { logger?.debug("Missing Continuations.") } for streamContinuation in streamContinuations { for url in urls { streamContinuation.value.yield(url) } diff --git a/Sources/SublimationBonjour/Extensions/Dictionary.swift b/Sources/SublimationBonjour/Extensions/Dictionary.swift index 0b3feaa..82d8f28 100644 --- a/Sources/SublimationBonjour/Extensions/Dictionary.swift +++ b/Sources/SublimationBonjour/Extensions/Dictionary.swift @@ -27,7 +27,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // -import Foundation +internal import Foundation extension Dictionary where Key == String, Value == String { internal init( diff --git a/Sources/SublimationBonjour/Extensions/NWConnection.swift b/Sources/SublimationBonjour/Extensions/NWConnection.swift new file mode 100644 index 0000000..35a01ed --- /dev/null +++ b/Sources/SublimationBonjour/Extensions/NWConnection.swift @@ -0,0 +1,46 @@ +// +// NWConnection.swift +// SublimationBonjour +// +// Created by Leo Dion. +// Copyright © 2024 BrightDigit. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +#if canImport(Network) + import Foundation + public import Network + + extension NWConnection.State: @retroactive CustomDebugStringConvertible { + @_documentation(visibility: internal) public var debugDescription: String { + switch self { case .setup: return "setup" case .waiting(let error): + return "waiting: \(error.debugDescription)" + case .preparing: return "preparing" + case .ready: return "ready" + case .failed(let error): return "failed: \(error.debugDescription)" + case .cancelled: return "cancelled" + @unknown default: return "unknown state" + } + } + } +#endif diff --git a/Sources/SublimationBonjour/Extensions/String.swift b/Sources/SublimationBonjour/Extensions/String.swift index 7763b82..78f190d 100644 --- a/Sources/SublimationBonjour/Extensions/String.swift +++ b/Sources/SublimationBonjour/Extensions/String.swift @@ -27,7 +27,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // -import Foundation +internal import Foundation extension String { internal func isLocalhost() -> Bool { diff --git a/Sources/SublimationBonjour/Extensions/URL.swift b/Sources/SublimationBonjour/Extensions/URL.swift index 6a03a98..12e948f 100644 --- a/Sources/SublimationBonjour/Extensions/URL.swift +++ b/Sources/SublimationBonjour/Extensions/URL.swift @@ -27,7 +27,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // -import Foundation +internal import Foundation extension URL { internal init?(scheme: String, host: String, port: Int) { diff --git a/Sources/SublimationBonjour/Server/BonjourSublimatory.swift b/Sources/SublimationBonjour/Server/BonjourSublimatory.swift index fdf8c8c..15e5d54 100644 --- a/Sources/SublimationBonjour/Server/BonjourSublimatory.swift +++ b/Sources/SublimationBonjour/Server/BonjourSublimatory.swift @@ -27,14 +27,13 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#if canImport(Network) +#if canImport(Network) && canImport(Logging) internal import Foundation public import Network public import SublimationCore - public import Logging /// Sublimatory for using Bonjour auto-discovery. @@ -165,26 +164,25 @@ let txtRecord = NWTXTRecord(dictionary) assert(listener.service == nil) listener.service = .init(name: name, type: type, txtRecord: txtRecord) - + let logger = self.logger listener.newConnectionHandler = { connection in connection.stateUpdateHandler = { state in switch state { case .waiting(let error): - self.logger.warning("Connection Waiting error: \(error)") + logger.debug("Connection Waiting error: \(error.localizedDescription)") case .ready: - self.logger.debug("Connection Ready") - self.logger.debug("Sending data \(data.count) bytes") + logger.debug("Connection Ready") + logger.debug("Sending data \(data.count) bytes") connection.send( content: data, completion: .contentProcessed { error in - if let error { self.logger.warning("Connection Send error: \(error)") } + if let error { self.logger.error("Connection Send error: \(error)") } connection.cancel() } ) - case .failed(let error): self.logger.error("Connection Failure: \(error)") - - default: self.logger.debug("Connection state updated: \(state)") + case .failed(let error): logger.debug("Connection Failure: \(error)") + default: logger.debug("Connection state updated: \(state.debugDescription)") } } connection.start(queue: connectionQueue) @@ -202,7 +200,7 @@ self.logger.error("Listener Failure: \(error)") continuation.resume(throwing: error) case .cancelled: continuation.resume() - default: self.logger.debug("Listener state updated: \(state)") + default: self.logger.debug("Listener state updated: \(state.debugDescription)") } } } diff --git a/Sources/SublimationBonjour/Server/Sublimation+Bonjour.swift b/Sources/SublimationBonjour/Server/Sublimation+Bonjour.swift index 6f54660..f6b0945 100644 --- a/Sources/SublimationBonjour/Server/Sublimation+Bonjour.swift +++ b/Sources/SublimationBonjour/Server/Sublimation+Bonjour.swift @@ -27,7 +27,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#if canImport(Network) +#if canImport(Network) && canImport(Logging) public import Network public import Sublimation public import Logging