Skip to content

Commit

Permalink
adding Bonjour docs
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Aug 23, 2024
1 parent 93b06fa commit 9dcc20d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 61 deletions.
29 changes: 25 additions & 4 deletions Sources/SublimationBonjour/BindingConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
//
// Untitled.swift
// BindingConfiguration.swift
// SublimationBonjour
//
// Created by Leo Dion on 8/23/24.
// 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.
//

extension BindingConfiguration {

/// Information to advertise how to connect to the server.
///
/// ```
Expand All @@ -27,7 +48,7 @@ extension BindingConfiguration {
/// - port: The port number of the server.
/// - isSecure: Whether to use https or http.
///
public init (hosts: [String], port: Int = 8080, isSecure: Bool = false) {
public init(hosts: [String], port: Int = 8080, isSecure: Bool = false) {
self.init()
self.hosts = hosts
self.isSecure = isSecure
Expand Down
49 changes: 22 additions & 27 deletions Sources/SublimationBonjour/BonjourSublimatory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,25 @@

public import Logging

public struct BonjourSublimatory: Sublimatory {
public init(
bindingConfiguration: BindingConfiguration,
logger: Logger,
listener: NWListener,
name: String = Self.defaultName,
type: String = Self.defaultHttpTCPServiceType,
listenerQueue: DispatchQueue = .global(),
connectionQueue: DispatchQueue = .global()
) {
self.bindingConfiguration = bindingConfiguration
self.logger = logger
self.listener = listener
self.name = name
self.type = type
self.listenerQueue = listenerQueue
self.connectionQueue = connectionQueue
}

public struct BonjourSublimatory: Sublimatory {
public init(
bindingConfiguration: BindingConfiguration,
logger: Logger,
listener: NWListener,
name: String = Self.defaultName,
type: String = Self.defaultHttpTCPServiceType,
listenerQueue: DispatchQueue = .global(),
connectionQueue: DispatchQueue = .global()
) {
self.bindingConfiguration = bindingConfiguration
self.logger = logger
self.listener = listener
self.name = name
self.type = type
self.listenerQueue = listenerQueue
self.connectionQueue = connectionQueue
}


public init(
bindingConfiguration: BindingConfiguration,
logger: Logger,
Expand All @@ -78,10 +76,9 @@ public struct BonjourSublimatory: Sublimatory {
connectionQueue: connectionQueue
)
}

let bindingConfiguration: BindingConfiguration
let logger: Logger
let listener: NWListener
let bindingConfiguration: BindingConfiguration
let logger: Logger
let listener: NWListener
let name: String
let type: String
let listenerQueue: DispatchQueue
Expand Down Expand Up @@ -131,9 +128,7 @@ public struct BonjourSublimatory: Sublimatory {
// return addresses
// }

public func shutdown() {
listener.cancel()
}
public func shutdown() { listener.cancel() }
public func run() async throws {
let data = try self.bindingConfiguration.serializedData()
let txtRecordValues = data.base64EncodedString().splitByMaxLength(199)
Expand Down
14 changes: 6 additions & 8 deletions Sources/SublimationBonjour/Client/BonjourClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@
public import Logging
#endif

public struct URLDefaultConfiguration {
public init(isSecure: Bool = false, port: Int = 8080) {
self.isSecure = isSecure
self.port = port
}
public let isSecure: Bool
public let port: Int
}


/// Client for fetching the url of the host server.
/// ```
/// let depositor = BonjourClient(logger: app.logger)
/// let hostURL = await depositor.first()
/// ```
public actor BonjourClient {
private let browser: NWBrowser
private let streams = StreamManager<UUID, URL>()
Expand Down
16 changes: 16 additions & 0 deletions Sources/SublimationBonjour/Client/Untitled.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Untitled.swift
// SublimationBonjour
//
// Created by Leo Dion on 8/23/24.
//


public struct URLDefaultConfiguration {
public init(isSecure: Bool = false, port: Int = 8080) {
self.isSecure = isSecure
self.port = port
}
public let isSecure: Bool
public let port: Int
}
28 changes: 8 additions & 20 deletions Sources/SublimationBonjour/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,17 @@

Use Bonjour for automatic discovery of your Swift Server.

## Overview
![SublimationBonjour Diagram](SublimationBonjour.svg)

```mermaid
sequenceDiagram
participant Server as Hummingbird/Vapor Server
participant BonjourSub as BonjourSublimatory
participant NWListener as NWListener
participant Network as Local Network
participant BonjourClient as BonjourClient
participant App as iOS/watchOS App
Server->>BonjourSub: Start server, provide IP addresses,<br/>hostnames, port, and protocol (http/https)
BonjourSub->>NWListener: Configure with server information
NWListener->>Network: Advertise service:<br/>1. Send encoded server data<br/>2. Use Text Record for additional info
App->>BonjourClient: Request server URL
BonjourClient->>Network: Search for advertised services
Network-->>BonjourClient: Return advertised service information
BonjourClient->>BonjourClient: 1. Receive and decode server data<br/>2. Parse Text Record
BonjourClient-->>App: Return AsyncStream<URL><br/>or first available URL
App->>Server: Connect to server using discovered URL
```
## Overview

When the Swift Server begins it will tell Sublimation the ip addresses or host names which are available to access the server from (including the port number and whether to use https or http). This is called a `BonjourSublimatory`. The `BonjourSublimatory` then uses `NWListener` to advertise this information both by send the data encoded using Protocol Buffers as well as inside the Text Record advertised.

The iPhone or Apple Watch then uses a `BonjourClient` to fetch either an `AsyncStream` of `URL` or simply get the `first` one available.

### Setting up your Server


```
let bindingConfiguration = BindingConfiguration(
host: ["Leo's-Mac.local", "192.168.1.10"],
Expand All @@ -45,6 +28,11 @@ let sublimation = Sublimation(sublimatory : bonjour)

### Setting up your Client

```
let depositor = BonjourClient(logger: app.logger)
let hostURL = await depositor.first()
```

## Topics

### <!--@START_MENU_TOKEN@-->Group<!--@END_MENU_TOKEN@-->
Expand Down
3 changes: 1 addition & 2 deletions Sources/SublimationBonjour/Sublimation+Bonjour.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

extension Sublimation {
/// Initializes a `Sublimation` instance with the provided parameters.
///
///
/// - Parameters:
/// - bindingConfiguration: A configuration with addresses, port and tls configuration.
/// - name: Service name.
Expand Down Expand Up @@ -64,7 +64,6 @@
)
self.init(sublimatory: sublimatory)
}

public convenience init(
bindingConfiguration: BindingConfiguration,
logger: Logger,
Expand Down

0 comments on commit 9dcc20d

Please sign in to comment.