2019-06-21 11:20:03 -07:00
|
|
|
//
|
|
|
|
|
// Server.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 6/20/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Network
|
|
|
|
|
|
|
|
|
|
enum ConnectionError: LocalizedError
|
|
|
|
|
{
|
|
|
|
|
case serverNotFound
|
|
|
|
|
case connectionFailed
|
|
|
|
|
case connectionDropped
|
|
|
|
|
|
2020-05-15 10:55:18 -07:00
|
|
|
var failureReason: String? {
|
2019-06-21 11:20:03 -07:00
|
|
|
switch self
|
|
|
|
|
{
|
|
|
|
|
case .serverNotFound: return NSLocalizedString("Could not find AltServer.", comment: "")
|
|
|
|
|
case .connectionFailed: return NSLocalizedString("Could not connect to AltServer.", comment: "")
|
|
|
|
|
case .connectionDropped: return NSLocalizedString("The connection to AltServer was dropped.", comment: "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 19:53:10 -07:00
|
|
|
extension Server
|
|
|
|
|
{
|
|
|
|
|
enum ConnectionType
|
|
|
|
|
{
|
|
|
|
|
case wireless
|
|
|
|
|
case wired
|
|
|
|
|
case local
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
struct Server: Equatable
|
|
|
|
|
{
|
2020-01-13 10:17:30 -08:00
|
|
|
var identifier: String? = nil
|
|
|
|
|
var service: NetService? = nil
|
2019-06-21 11:20:03 -07:00
|
|
|
|
2019-09-03 23:47:47 -07:00
|
|
|
var isPreferred = false
|
2020-06-04 19:53:10 -07:00
|
|
|
var connectionType: ConnectionType = .wireless
|
2020-01-13 10:17:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Server
|
|
|
|
|
{
|
|
|
|
|
// Defined in extension so we can still use the automatically synthesized initializer.
|
2019-09-03 23:47:47 -07:00
|
|
|
init?(service: NetService, txtData: Data)
|
2020-01-13 10:17:30 -08:00
|
|
|
{
|
2019-09-03 23:47:47 -07:00
|
|
|
let txtDictionary = NetService.dictionary(fromTXTRecord: txtData)
|
|
|
|
|
guard let identifierData = txtDictionary["serverID"], let identifier = String(data: identifierData, encoding: .utf8) else { return nil }
|
|
|
|
|
|
|
|
|
|
self.service = service
|
2020-01-13 10:17:30 -08:00
|
|
|
self.identifier = identifier
|
2019-09-03 23:47:47 -07:00
|
|
|
}
|
2019-06-21 11:20:03 -07:00
|
|
|
}
|