<commit_before>//
//  STReachability.swift
//  SwiftlyReachable
//
//  Created by <NAME> on 2/4/15.
//  Copyright (c) 2015 SwiftTime. All rights reserved.
//

import Foundation
import SystemConfiguration


public class STReachability {

    // MARK: - data structures
    
    public enum STReachabilityStatus {
        case Unknown
        case UnReachable
        case ViaWiFi
        case ViaCellData
    }
    
    public struct STReachabilityFlags {
        enum ReachabilityFlags:UInt32 {
            case Unknown = 0
            case TransientConnection = 1 // 1<<0
            case Reachable = 2 // 1<<1
            case ConnectionRequired = 4 // 1<<2
            case ConnectionOnTraffic, ConnectionAutomatic = 8 // 1<<3
            case InterventionRequired = 16 // 1<<4
            case ConnectionOnDemand = 32 // 1<<5
            case IsLocalAddress = 65_536 // 1<<16
            case IsDirect = 131_072 // 1<<17
            case IsWWAN = 262_144 // 1<<18
        }
        
        var flags:UInt32 = 0
        
        func isSet(flag:ReachabilityFlags) -> Bool {
            return flags & flag.rawValue > 0
        }
    }
    
    // MARK: - properties
    
    public   var changedBlock:((STReachabilityStatus)->())?
    
    internal var status:STReachabilityStatus = .Unknown
    private  let reachability:SCNetworkReachability
    
    private var bridge:STReachabilityBridge!
    
    private var flags:STReachabilityFlags = STReachabilityFlags() {
        didSet {
            if flags.flags != oldValue.flags {
                updateStatus()
                
                if let block = changedBlock {
                    dispatch_async(dispatch_get_main_queue()) {block(self.status)}
                }
            }
        }
    }
    
    // MARK: - lifecycle
    
    public convenience init() {
        self.init(nodeName: "www.apple.com")
    }
    
    public init(nodeName:String) {
        /*
        // alternate to node name method
        
        var address = sockaddr(sa_len: UInt8(sizeof(sockaddr)),
        sa_family: sa_family_t(AF_INET),
        sa_data: (0,0,0,0,0,0,0,0,0,0,0,0,0,0))
        
        reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, &address).takeRetainedValue()
        */
        
        
        reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, nodeName).takeRetainedValue()
        
        bridge = STReachabilityBridge(target:reachability, andBlock:{[weak self] newFlags
            in
            self?.flags.flags = newFlags
            
            return Void()
        })

        updateFlags()
    }
    
    deinit {
        
    }
    
    // MARK: - functions
    
    public func startMonitor() -> Bool {
        return bridge.startMonitoring()
    }
    
    public func stopMonitor() {
        bridge.stopMonitoring()
    }
    
    public func statusHasUpdated(newFlags: UInt32) {
        flags.flags = newFlags
    }

    private func updateStatus() {
        if flags.isSet(.IsWWAN) {
            status = .ViaCellData
        } else if flags.isSet(.Reachable) {
            status = .ViaWiFi
        } else {
            status = .UnReachable
        }
    }

    private func updateFlags() {
        flags.flags = bridge.getStatus();
    }
    
}<commit_msg>added demo app display flags property<commit_after>//
//  STReachability.swift
//  SwiftlyReachable
//
//  Created by <NAME> on 2/4/15.
//  Copyright (c) 2015 SwiftTime. All rights reserved.
//

import Foundation
import SystemConfiguration


public class STReachability {

    // MARK: - data structures
    
    public enum STReachabilityStatus {
        case Unknown
        case UnReachable
        case ViaWiFi
        case ViaCellData
    }
    
    public struct STReachabilityFlags {
        enum ReachabilityFlags:UInt32 {
            case Unknown = 0
            case TransientConnection = 1 // 1<<0
            case Reachable = 2 // 1<<1
            case ConnectionRequired = 4 // 1<<2
            case ConnectionOnTraffic, ConnectionAutomatic = 8 // 1<<3
            case InterventionRequired = 16 // 1<<4
            case ConnectionOnDemand = 32 // 1<<5
            case IsLocalAddress = 65_536 // 1<<16
            case IsDirect = 131_072 // 1<<17
            case IsWWAN = 262_144 // 1<<18
        }
        
        var flags:UInt32 = 0
        
        func isSet(flag:ReachabilityFlags) -> Bool {
            return flags & flag.rawValue > 0
        }
    }
    
    // MARK: - properties
    
    public   var changedBlock:((STReachabilityStatus)->())?
    
    internal var status:STReachabilityStatus = .Unknown
    private  let reachability:SCNetworkReachability
    
    private var bridge:STReachabilityBridge!
    
    private var flags:STReachabilityFlags = STReachabilityFlags() {
        didSet {
            if flags.flags != oldValue.flags {
                updateStatus()
                
                if let block = changedBlock {
                    dispatch_async(dispatch_get_main_queue()) {block(self.status)}
                }
            }
        }
    }
    
    // MARK: - demo property
    
    public var binaryFlags:String {
        var fmtString = ""
        var fmtFlags = flags.flags
        
        do {
            let temp = fmtFlags & 1 > 0 ? "1" : "0"
            fmtString = temp + fmtString
            fmtFlags /= 2;
        } while fmtFlags > 0
        
        return fmtString
    }
    
    // MARK: - lifecycle
    
    public convenience init() {
        self.init(nodeName: "www.apple.com")
    }
    
    public init(nodeName:String) {
        /*
        // alternate to node name method
        
        var address = sockaddr(sa_len: UInt8(sizeof(sockaddr)),
        sa_family: sa_family_t(AF_INET),
        sa_data: (0,0,0,0,0,0,0,0,0,0,0,0,0,0))
        
        reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, &address).takeRetainedValue()
        */
        
        
        reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, nodeName).takeRetainedValue()
        
        bridge = STReachabilityBridge(target:reachability, andBlock:{[weak self] newFlags
            in
            self?.flags.flags = newFlags
            
            return Void()
        })

        updateFlags()
    }
    
    deinit {
        
    }
    
    // MARK: - functions
    
    public func startMonitor() -> Bool {
        return bridge.startMonitoring()
    }
    
    public func stopMonitor() {
        bridge.stopMonitoring()
    }
    
    public func statusHasUpdated(newFlags: UInt32) {
        flags.flags = newFlags
    }

    private func updateStatus() {
        if flags.isSet(.IsWWAN) {
            status = .ViaCellData
        } else if flags.isSet(.Reachable) {
            status = .ViaWiFi
        } else {
            status = .UnReachable
        }
    }

    private func updateFlags() {
        flags.flags = bridge.getStatus();
    }
    
}
{'fraction_non_alphanumeric': 0.09003446295232626, 'fraction_numerical': 0.023262492820218264, 'mean_word_length': 1.634266263237519, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7572592', 'n_tokens_mistral': 2120, 'n_tokens_neox': 1967, 'n_words': 432}