Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server state to the CM delegate #55

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Example/DownloadToGo/TestTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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]?
Expand Down
17 changes: 17 additions & 0 deletions Example/DownloadToGo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -590,6 +603,10 @@ extension ViewController: ContentManagerDelegate {
self.statusLabel.text = newState.asString()
}
}

func serverDidChangeState(_ state: DTGServerState) {
print("serverDidChangeState: \(state)")
}
}

/************************************************************/
Expand Down
24 changes: 24 additions & 0 deletions Sources/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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.
Expand Down Expand Up @@ -260,6 +268,22 @@ public enum LogLevel {
}

public typealias DTGRequestParams = (url: URL, headers: [String:String])

public protocol DTGRequestParamsAdapter: class {
func adapt(_ params: DTGRequestParams) -> DTGRequestParams
}

/// 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?)
}
8 changes: 8 additions & 0 deletions Sources/ContentManager+Validate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// ContentManager+Validate.swift
// DownloadToGo
//
// Created by Noam Tamim on 29/03/2020.
//

import Foundation
20 changes: 20 additions & 0 deletions Sources/ContentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,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))
}
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public enum DTGError: LocalizedError {
case insufficientDiskSpace(freeSpaceInMegabytes: Int)
/// Network timeout
case networkTimeout(url: String)
/// Invalid URL
case invalidUrl(url: String)
/// Server down
case internalServerDown

public var errorDescription: String? {
switch self {
Expand All @@ -184,6 +188,10 @@ public enum DTGError: LocalizedError {
return "insufficient disk space to start or continue the download, only have \(freeSpaceInMegabytes)MB free..."
case .networkTimeout:
return "Network timeout"
case .invalidUrl:
return "Invalid URL"
case .internalServerDown:
return "Internal Server is Down"
}
}
}
Expand Down