From 8f3b8cbc78c58f3a90568c7367365d50ad897785 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Tue, 10 Mar 2020 16:21:53 +0200 Subject: [PATCH 1/2] Add server state to the CM delegate --- Example/DownloadToGo/ViewController.swift | 4 ++++ Sources/API.swift | 23 +++++++++++++++++++++++ Sources/ContentManager.swift | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Example/DownloadToGo/ViewController.swift b/Example/DownloadToGo/ViewController.swift index 519f50d..ee4dab0 100644 --- a/Example/DownloadToGo/ViewController.swift +++ b/Example/DownloadToGo/ViewController.swift @@ -590,6 +590,10 @@ extension ViewController: ContentManagerDelegate { self.statusLabel.text = newState.asString() } } + + func serverDidChangeState(_ state: DTGServerState) { + print("serverDidChangeState: \(state)") + } } /************************************************************/ diff --git a/Sources/API.swift b/Sources/API.swift index 4c02be8..0adffc8 100644 --- a/Sources/API.swift +++ b/Sources/API.swift @@ -126,6 +126,14 @@ public protocol ContentManagerDelegate: class { /// Item has changed state. in case state will be failed, the error will be provided (interupted state could also provide error). func item(id: String, didChangeToState newState: DTGItemState, error: Error?) + + /// The state of the internal web server has changed. + func serverDidChangeState(_ state: DTGServerState) +} + +// Default implementation of serverDidChangeState() to make it optional. +public extension ContentManagerDelegate { + func serverDidChangeState(_ state: DTGServerState) {} } /// A downloadable item. @@ -255,3 +263,18 @@ public enum LogLevel { } } } + +/// The state of the internal web server. +public enum DTGServerState { + /// Server is started and accepting connections. + case started(serverUrl: URL?) + + /// Server is stopped. + case stopped + + /// Server is handling requests. + case connected(serverUrl: URL?) + + /// Server has finished handling requests (back to idle). + case disconnected(serverUrl: URL?) +} diff --git a/Sources/ContentManager.swift b/Sources/ContentManager.swift index f7f94a7..b449968 100644 --- a/Sources/ContentManager.swift +++ b/Sources/ContentManager.swift @@ -568,9 +568,29 @@ public class ContentManager: NSObject, DTGContentManager { extension ContentManager: GCDWebServerDelegate { + private func report(_ state: DTGServerState) { + log.debug("Web server state=\(state), app state=\(UIApplication.shared.applicationState)") + + delegate?.serverDidChangeState(state) + } + + public func webServerDidConnect(_ server: GCDWebServer) { + report(.connected(serverUrl: server.serverURL)) + } + + public func webServerDidDisconnect(_ server: GCDWebServer) { + report(.disconnected(serverUrl: server.serverURL)) + } + + public func webServerDidStop(_ server: GCDWebServer) { + report(.stopped) + } + public func webServerDidStart(_ server: GCDWebServer) { self.startCompletionHandler?() self.startCompletionHandler = nil + + report(.started(serverUrl: server.serverURL)) } } From 03d427ac5ee164192192b0a7c073dca9c96cd7e1 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Sun, 15 Mar 2020 12:34:33 +0200 Subject: [PATCH 2/2] Test: show JSON template --- Example/DownloadToGo/TestTools.swift | 8 ++++---- Example/DownloadToGo/ViewController.swift | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Example/DownloadToGo/TestTools.swift b/Example/DownloadToGo/TestTools.swift index 016165f..a380739 100644 --- a/Example/DownloadToGo/TestTools.swift +++ b/Example/DownloadToGo/TestTools.swift @@ -16,11 +16,11 @@ let setSmallerOfflineDRMExpirationMinutes: Int? = nil let defaultEnv = "http://cdnapi.kaltura.com" -struct ItemOTTParamsJSON: Decodable { +struct ItemOTTParamsJSON: Codable { let format: String? } -struct ItemJSON: Decodable { +struct ItemJSON: Codable { let id: String let title: String? let partnerId: Int? @@ -37,14 +37,14 @@ struct ItemJSON: Decodable { let ottParams: ItemOTTParamsJSON? } -struct ExpectedValues: Decodable { +struct ExpectedValues: Codable { let estimatedSize: Int64? let downloadedSize: Int64? let audioLangs: [String]? let textLangs: [String]? } -struct OptionsJSON: Decodable { +struct OptionsJSON: Codable { let audioLangs: [String]? let allAudioLangs: Bool? let textLangs: [String]? diff --git a/Example/DownloadToGo/ViewController.swift b/Example/DownloadToGo/ViewController.swift index ee4dab0..3bcfb46 100644 --- a/Example/DownloadToGo/ViewController.swift +++ b/Example/DownloadToGo/ViewController.swift @@ -105,6 +105,19 @@ class Item { } +func showJsonTemplate() { + let json = try! JSONEncoder().encode([ + ItemJSON(id: "ID", title: "TITLE", partnerId: 1, ks: nil, env: "http://example.com", url: "http://example.com/master.m3u8", + options: OptionsJSON(audioLangs: nil, allAudioLangs: false, textLangs: nil, allTextLangs: false, + videoCodecs: nil, audioCodecs: nil, videoWidth: nil, videoHeight: nil, videoBitrates: nil, + allowInefficientCodecs: nil), expected: nil, ott: true, + ottParams: ItemOTTParamsJSON(format: "FORMAT"))]) + + print("JSON Template:", String(data: json, encoding: .utf8)) + +} + + class ViewController: UIViewController { let dummyFileName = "dummyfile" let videoViewControllerSegueIdentifier = "videoViewController"