<commit_before>import Foundation
import Dispatch
import COrvibo

#if os(Linux)
    import Glibc
#else
    import Darwin
#endif

guard CommandLine.arguments.count == 2 else {
    print("Usage: \(CommandLine.arguments.first!) <mac>")
    exit(EXIT_FAILURE)
}

let mac = CommandLine.arguments.last!
guard let socket = Orvibo(mac) else {
    fatalError("Cannot create socket for \(mac)")
}

socket.onStateChange {
    print($1 ? "On" : "Off")
}

socket.onDiscovery() {
    print("Discovered \(mac) at \($0.ip)")
    $0.subscribe()
}

socket.onSubscription() { socket, _ in
    print("Subscribed, state = \(socket.state.rawValue)")
    let mainQueue = DispatchQueue.main
    mainQueue.async {
        var done = false
        var line = readLine()
        while let cmd = line, !done {
            switch cmd.lowercased() {
            case "q":
                fallthrough
            case "quit":
                done = true
                continue
            case "on":
                socket.on = true
            case "off":
                socket.on = false
            default:
                print("Unknown command '\(cmd)'")
            }
            line = readLine()
        }
        socket.unsubscribe()
        mainQueue.asyncAfter(deadline: .now() + .milliseconds(100)) {
            socket.destroy()
            orvibo_stop()
            exit(EXIT_SUCCESS)
        }
    }
}

socket.onUnsubscription() {
    print("Unsubscribed from \($0).")
}

socket.discover()

dispatchMain()<commit_msg>Flush stdout<commit_after>import Foundation
import Dispatch
import COrvibo

#if os(Linux)
    import Glibc
#else
    import Darwin
#endif

guard CommandLine.arguments.count == 2 else {
    print("Usage: \(CommandLine.arguments.first!) <mac>")
    exit(EXIT_FAILURE)
}

let mac = CommandLine.arguments.last!
guard let socket = Orvibo(mac) else {
    fatalError("Cannot create socket for \(mac)")
}

socket.onStateChange {
    let status = $1 ? "On\n" : "Off\n"
    fputs(status, stdout)
    fflush(stdout)
}

socket.onDiscovery() {
    fputs("Discovered \(mac) at \($0.ip)\n", stdout)
    fflush(stdout)
    $0.subscribe()
}

socket.onSubscription() { socket, _ in
    fputs("Subscribed, state = \(socket.state.rawValue)\n", stdout)
    fflush(stdout)
    let mainQueue = DispatchQueue.main
    mainQueue.async {
        var done = false
        var line = readLine()
        while let cmd = line, !done {
            switch cmd.lowercased() {
            case "q":
                fallthrough
            case "quit":
                done = true
                continue
            case "on":
                socket.on = true
            case "off":
                socket.on = false
            default:
                fputs("Unknown command '\(cmd)'", stderr)
                fflush(stderr)
            }
            line = readLine()
        }
        socket.unsubscribe()
        mainQueue.asyncAfter(deadline: .now() + .milliseconds(100)) {
            socket.destroy()
            orvibo_stop()
            exit(EXIT_SUCCESS)
        }
    }
}

socket.onUnsubscription() {
    fputs("Unsubscribed from \($0).\n", stdout)
    fflush(stdout)
}

socket.discover()

dispatchMain()
{'fraction_non_alphanumeric': 0.12327478042659976, 'fraction_numerical': 0.005018820577164366, 'mean_word_length': 1.9283746556473829, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, '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': '3347550', 'n_tokens_mistral': 985, 'n_tokens_neox': 971, 'n_words': 219}