2019-09-10 12:19:46 -07:00
|
|
|
//
|
|
|
|
|
// FindServerOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 9/8/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2020-01-13 10:17:30 -08:00
|
|
|
import AltKit
|
2019-09-10 12:19:46 -07:00
|
|
|
import Roxas
|
|
|
|
|
|
2020-01-13 10:17:30 -08:00
|
|
|
private extension Notification.Name
|
|
|
|
|
{
|
|
|
|
|
static let didReceiveWiredServerConnectionResponse = Notification.Name("io.altstore.didReceiveWiredServerConnectionResponse")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private let ReceivedWiredServerConnectionResponse: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void =
|
|
|
|
|
{ (center, observer, name, object, userInfo) in
|
|
|
|
|
NotificationCenter.default.post(name: .didReceiveWiredServerConnectionResponse, object: nil)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 12:19:46 -07:00
|
|
|
@objc(FindServerOperation)
|
|
|
|
|
class FindServerOperation: ResultOperation<Server>
|
|
|
|
|
{
|
2020-03-06 17:08:35 -08:00
|
|
|
let context: OperationContext
|
2019-09-10 12:19:46 -07:00
|
|
|
|
2020-01-13 10:17:30 -08:00
|
|
|
private var isWiredServerConnectionAvailable = false
|
2020-03-06 17:08:35 -08:00
|
|
|
|
|
|
|
|
init(context: OperationContext = OperationContext())
|
2019-09-10 12:19:46 -07:00
|
|
|
{
|
2020-03-06 17:08:35 -08:00
|
|
|
self.context = context
|
2019-09-10 12:19:46 -07:00
|
|
|
}
|
2020-03-06 17:08:35 -08:00
|
|
|
|
2019-09-10 12:19:46 -07:00
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
if let error = self.context.error
|
2019-09-10 12:19:46 -07:00
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
if let server = self.context.server
|
|
|
|
|
{
|
|
|
|
|
self.finish(.success(server))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 10:17:30 -08:00
|
|
|
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
|
|
|
|
|
|
|
|
|
|
// Prepare observers to receive callback from wired server (if connected).
|
|
|
|
|
CFNotificationCenterAddObserver(notificationCenter, nil, ReceivedWiredServerConnectionResponse, CFNotificationName.wiredServerConnectionAvailableResponse.rawValue, nil, .deliverImmediately)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(FindServerOperation.didReceiveWiredServerConnectionResponse(_:)), name: .didReceiveWiredServerConnectionResponse, object: nil)
|
|
|
|
|
|
|
|
|
|
// Post notification.
|
|
|
|
|
CFNotificationCenterPostNotification(notificationCenter, .wiredServerConnectionAvailableRequest, nil, nil, true)
|
|
|
|
|
|
|
|
|
|
// Wait for either callback or timeout.
|
|
|
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
|
|
|
|
|
if self.isWiredServerConnectionAvailable
|
|
|
|
|
{
|
|
|
|
|
let server = Server(isWiredConnection: true)
|
|
|
|
|
self.finish(.success(server))
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if let server = ServerManager.shared.discoveredServers.first(where: { $0.isPreferred })
|
|
|
|
|
{
|
|
|
|
|
// Preferred server.
|
|
|
|
|
self.finish(.success(server))
|
|
|
|
|
}
|
|
|
|
|
else if let server = ServerManager.shared.discoveredServers.first
|
|
|
|
|
{
|
|
|
|
|
// Any available server.
|
|
|
|
|
self.finish(.success(server))
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// No servers.
|
|
|
|
|
self.finish(.failure(ConnectionError.serverNotFound))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-10 12:19:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 10:17:30 -08:00
|
|
|
private extension FindServerOperation
|
|
|
|
|
{
|
|
|
|
|
@objc func didReceiveWiredServerConnectionResponse(_ notification: Notification)
|
|
|
|
|
{
|
|
|
|
|
self.isWiredServerConnectionAvailable = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|