<commit_before>//
//  Requester.swift
//  RequestSwift
//
//  Created by <NAME> on 7/12/17.
//  Copyright © 2017 BiAtoms. All rights reserved.
//

import Foundation
import SocketSwift

public typealias ResponseHandler = (Response?, Error?) -> Void
open class Requester {
    var timeout = 10
    open var request: Request
    open var response: Response?
    open var error: Error?
    open var handler: ResponseHandler? = nil
    
    public init(request: Request) {
        self.request = request
    }
    
    open func start() {
        do {
            let socket = try Socket(.inet)
            let (hostname, portString) = request.hostnameAndPort
            var port: SocketSwift.Port = 80
            if let p = portString {
                port = Port(p)!
            }
            
            let address = try hostToIp(hostname)
            
            try socket.connect(port: port, address: address)
            
            let bytes = RequestWriter.write(request: request)
            try socket.write(bytes)
            
            try wait(socket)
            
            self.response = try ResponseParser.parse(socket: socket)
        } catch {
            self.error = error
        }
        handler?(response, error)
    }
    
    open func response(_ handler: @escaping ResponseHandler) {
        self.handler = handler
        if response != nil || error != nil {
            handler(response, error)
        }
    }
    
    
    func hostToIp( _ hostname: String) throws -> String {
        if let a = gethostbyname(hostname) {
            let b = withUnsafePointer(to: &(a.pointee.h_addr_list.pointee)) {
                UnsafePointer<UnsafePointer<in_addr>>(OpaquePointer($0)).pointee.pointee
            }
            let c = inet_ntoa(b)!
            
            return String(cString: c)
        }
        
        try ing { -1 } //thorws :)
        return ""
    }
    func wait(_ socket: Socket) throws {
        var fd = pollfd()
        memset(&fd, 0, MemoryLayout<pollfd>.stride)
        fd.fd = socket.fileDescriptor
        fd.events = Int16(POLLOUT)
        try ing { poll(&fd, 1, Int32(timeout * 1000)) }
    }
}<commit_msg>Fixed waiting socket for writing instead of reading<commit_after>//
//  Requester.swift
//  RequestSwift
//
//  Created by <NAME> on 7/12/17.
//  Copyright © 2017 BiAtoms. All rights reserved.
//

import Foundation
import SocketSwift

public typealias ResponseHandler = (Response?, Error?) -> Void
open class Requester {
    var timeout = 10
    open var request: Request
    open var response: Response?
    open var error: Error?
    open var handler: ResponseHandler? = nil
    
    public init(request: Request) {
        self.request = request
    }
    
    open func start() {
        do {
            let socket = try Socket(.inet)
            let (hostname, portString) = request.hostnameAndPort
            var port: SocketSwift.Port = 80
            if let p = portString {
                port = Port(p)!
            }
            
            let address = try hostToIp(hostname)
            
            try socket.connect(port: port, address: address)
            
            let bytes = RequestWriter.write(request: request)
            try socket.write(bytes)
            
            try wait(socket)
            
            self.response = try ResponseParser.parse(socket: socket)
        } catch {
            self.error = error
        }
        handler?(response, error)
    }
    
    open func response(_ handler: @escaping ResponseHandler) {
        self.handler = handler
        if response != nil || error != nil {
            handler(response, error)
        }
    }
    
    
    func hostToIp( _ hostname: String) throws -> String {
        if let a = gethostbyname(hostname) {
            let b = withUnsafePointer(to: &(a.pointee.h_addr_list.pointee)) {
                UnsafePointer<UnsafePointer<in_addr>>(OpaquePointer($0)).pointee.pointee
            }
            let c = inet_ntoa(b)!
            
            return String(cString: c)
        }
        
        try ing { -1 } //thorws :)
        return ""
    }
    func wait(_ socket: Socket) throws {
        var fd = pollfd()
        memset(&fd, 0, MemoryLayout<pollfd>.stride)
        fd.fd = socket.fileDescriptor
        fd.events = Int16(POLLIN)
        try ing { poll(&fd, 1, Int32(timeout * 1000)) }
    }
}
{'fraction_non_alphanumeric': 0.09723807349920109, 'fraction_numerical': 0.011412919424788861, 'mean_word_length': 1.6589805825242718, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5359431', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1238, 'n_words': 333}