<commit_before>//
//  DefaultTransportFactory.swift
//  SignalRClient
//
//  Created by <NAME> on 8/22/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import Foundation

internal class DefaultTransportFactory: TransportFactory {
    let logger: Logger
    let permittedTransportTypes: TransportType
    var orderOfPreference: [TransportType] = [.webSockets, .longPolling]
        
    init(logger: Logger, permittedTransportTypes: TransportType = .all) {
        self.logger = logger
        self.permittedTransportTypes = permittedTransportTypes
    }
    
    func createTransport(availableTransports: [TransportDescription]) throws -> Transport {
        let choices = determineAvailableTypes(availableTransports: availableTransports)
        let chosenType = chooseType(choices: choices, orderOfPreference: orderOfPreference)
        recordChoice(chosenType)
        guard let transport = buildTransport(type: chosenType) else {
            throw SignalRError.noSupportedTransportAvailable
        }
        return transport
    }
    
    /// Builds the set of available transport types from the list of TransportDescriptions
    private func determineAvailableTypes(availableTransports: [TransportDescription]) -> TransportType {
        var choices: TransportType = .init()
        for transport in availableTransports {
            choices.formUnion(transport.transportType)
        }
        choices.formIntersection(permittedTransportTypes)
        return choices
    }
    
    /// Chooses a transport type from the supplied set of choices according to the supplied order of preference
    private func chooseType(choices: TransportType, orderOfPreference: [TransportType]) -> TransportType? {
        var chosen: TransportType? = nil
        for type in orderOfPreference {
            if choices.contains(type) {
                chosen = type
                break
            }
        }
        return chosen
    }
    
    /// Sets the chosen type to have lowest priority for future reconnect attempts, to allow fallback when a transport does not work properly, e.g. due to network conditions.
    private func recordChoice(_ choice: TransportType?) {
        if let choice = choice {
            orderOfPreference.removeAll(where: { $0 == choice })
            orderOfPreference.append(choice)
        }
    }
    
    /// Creates a Transport instance for the given (singular) transport type
    private func buildTransport(type: TransportType?) -> Transport? {
        if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
            if type == .webSockets {
                logger.log(logLevel: .info, message: "Selected WebSockets transport")
                return WebsocketsTransport(logger: logger)
            }
        }
        if type == .longPolling {
            logger.log(logLevel: .info, message: "Selected LongPolling transport")
            return LongPollingTransport(logger: logger)
        }
        return nil
    }<commit_msg>Remove rotating transports<commit_after>//
//  DefaultTransportFactory.swift
//  SignalRClient
//
//  Created by <NAME> on 8/22/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import Foundation

internal class DefaultTransportFactory: TransportFactory {
    let logger: Logger
    let permittedTransportTypes: TransportType
    var orderOfPreference: [TransportType] = [.webSockets, .longPolling]
        
    init(logger: Logger, permittedTransportTypes: TransportType = .all) {
        self.logger = logger
        self.permittedTransportTypes = permittedTransportTypes
    }
    
    func createTransport(availableTransports: [TransportDescription]) throws -> Transport {
        let choices = determineAvailableTypes(availableTransports: availableTransports)
        let chosenType = chooseType(choices: choices, orderOfPreference: orderOfPreference)
        guard let transport = buildTransport(type: chosenType) else {
            throw SignalRError.noSupportedTransportAvailable
        }
        return transport
    }
    
    /// Builds the set of available transport types from the list of TransportDescriptions
    private func determineAvailableTypes(availableTransports: [TransportDescription]) -> TransportType {
        var choices: TransportType = .init()
        for transport in availableTransports {
            choices.formUnion(transport.transportType)
        }
        choices.formIntersection(permittedTransportTypes)
        return choices
    }
    
    /// Chooses a transport type from the supplied set of choices according to the supplied order of preference
    private func chooseType(choices: TransportType, orderOfPreference: [TransportType]) -> TransportType? {
        var chosen: TransportType? = nil
        for type in orderOfPreference {
            if choices.contains(type) {
                chosen = type
                break
            }
        }
        return chosen
    }
    
    /// Creates a Transport instance for the given (singular) transport type
    private func buildTransport(type: TransportType?) -> Transport? {
        if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
            if type == .webSockets {
                logger.log(logLevel: .info, message: "Selected WebSockets transport")
                return WebsocketsTransport(logger: logger)
            }
        }
        if type == .longPolling {
            logger.log(logLevel: .info, message: "Selected LongPolling transport")
            return LongPollingTransport(logger: logger)
        }
        return nil
    }
{'fraction_non_alphanumeric': 0.07542754275427543, 'fraction_numerical': 0.0077407740774077406, 'mean_word_length': 2.570694087403599, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3828014', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1337, 'n_words': 411}