<commit_before>var net =           require('net');
var util =          require('util');
var EventEmitter =  require('events').EventEmitter;

var Lirc = function (config) {

    if (!(this instanceof Lirc)) return new Lirc(config);

    var that = this;

    config = config || {};
    if(!config.host && !config.path) {
        config.host = '127.0.0.1';
    }
    if(config.host) {
        config.port = config.port || 8765;
    }
    config.reconnect = config.reconnect || 5000;

    var client;
    var lircConnected;

    var callbacks = {};
    var timeouts = {};

    var incoming = [];
    var isIncoming;

    var delayed = [];

    this.cmd = function (/* cmd, variable number of cmd arguments, optional callback*/) {

        var args = Array.prototype.slice.call(arguments);
        var callback;

        if (Object.keys(timeouts).length) {
            delayed.push(args);
            return;
        }

        if (args.length > 1 && typeof args[args.length - 1] === 'function') {
            callback = args.pop();
        }

        if (!lircConnected) {
            if (callback) callback('not connected');
            return;
        }

        var data = args.join(' ');

        timeouts[data] = setTimeout(function () {
            that.emit('error', 'response timeout: ' + data);
            if (callback) delete callbacks[data];
            delete timeouts[data];
        }, 2500);

        client.write(data + '\n', function () {
            if (callback) callbacks[data] = callback;
        });

    };

    function parseResponse(data) {

        if (data[0] !== 'BEGIN' || data[data.length - 1] !== 'END') {
            return;
        }

        var cmd = data[1];

        clearTimeout(timeouts[cmd]);
        delete timeouts[cmd];

        var res;
        var payload;

        if (data.length > 3) {
            res = data[2];
        } else {
            that.emit(cmd); // this is used by LIRC for SIGHUP Broadcast
            return;
        }

        if (callbacks[cmd]) {

            if (data[3] === 'DATA') {
                payload = data.slice(5, -1);
            }

            if (res === 'SUCCESS') {
                callbacks[cmd](null, payload);
            } else if (res === 'ERROR') {
                callbacks[cmd](payload[0]);
            }

            delete callbacks[cmd];
        }

        if (delayed.length) {
            that.cmd.apply(that, delayed.shift());
        }

    }

    function lircConnect() {
        if (!lircConnected) {

            client = net.connect(config, function() {
                lircConnected = true;
                that.emit('connect');
            });

            client.on('error', function (data) {
                that.emit('error', data.toString());
            });

            client.on('end', function () {
                that.emit('error', 'end');
                lircConnected = false;
                //lircConnect();
            });

            client.on('timeout', function () {
                that.emit('error', 'timeout');
                lircConnected = false;
                //lircConnect();
            });

            client.on('close', function () {
                lircConnected = false;
                that.emit('disconnect');
                setTimeout(lircConnect, config.reconnect);
            });

            client.on('data', function (data) {
                data = data.toString();<commit_msg>add close method<commit_after>var net =           require('net');
var util =          require('util');
var EventEmitter =  require('events').EventEmitter;

var Lirc = function (config) {

    if (!(this instanceof Lirc)) return new Lirc(config);

    var that = this;

    config = config || {};
    if(!config.host && !config.path) {
        config.host = '127.0.0.1';
    }
    if(config.host) {
        config.port = config.port || 8765;
    }
    config.reconnect = config.reconnect || 5000;

    var client;
    var lircConnected;
    var closing;

    var callbacks = {};
    var timeouts = {};

    var incoming = [];
    var isIncoming;

    var delayed = [];

    this.cmd = function (/* cmd, variable number of cmd arguments, optional callback*/) {

        var args = Array.prototype.slice.call(arguments);
        var callback;

        if (Object.keys(timeouts).length) {
            delayed.push(args);
            return;
        }

        if (args.length > 1 && typeof args[args.length - 1] === 'function') {
            callback = args.pop();
        }

        if (!lircConnected) {
            if (callback) callback('not connected');
            return;
        }

        var data = args.join(' ');

        timeouts[data] = setTimeout(function () {
            that.emit('error', 'response timeout: ' + data);
            if (callback) delete callbacks[data];
            delete timeouts[data];
        }, 2500);

        client.write(data + '\n', function () {
            if (callback) callbacks[data] = callback;
        });

    };

    this.close = function() {
        closing = true;
        client.end();
    };

    function parseResponse(data) {

        if (data[0] !== 'BEGIN' || data[data.length - 1] !== 'END') {
            return;
        }

        var cmd = data[1];

        clearTimeout(timeouts[cmd]);
        delete timeouts[cmd];

        var res;
        var payload;

        if (data.length > 3) {
            res = data[2];
        } else {
            that.emit(cmd); // this is used by LIRC for SIGHUP Broadcast
            return;
        }

        if (callbacks[cmd]) {

            if (data[3] === 'DATA') {
                payload = data.slice(5, -1);
            }

            if (res === 'SUCCESS') {
                callbacks[cmd](null, payload);
            } else if (res === 'ERROR') {
                callbacks[cmd](payload[0]);
            }

            delete callbacks[cmd];
        }

        if (delayed.length) {
            that.cmd.apply(that, delayed.shift());
        }

    }

    function reConnect() {
        if(closing) { return; }
        setTimeout(lircConnect, config.reconnect);
    }

    function lircConnect() {
        if (!lircConnected) {

            client = net.connect(config, function() {
                lircConnected = true;
                that.emit('connect');
            });

            client.on('error', function (data) {
                that.emit('error', data.toString());
            });

            client.on('end', function () {
                if(!closing) {
                    that.emit('error', 'end');
                }
                lircConnected = false;
                reConnect();
            });

            client.on('timeout', function () {
                that.emit('error', 'timeout');
                lircConnected = false;
                reConnect();
            });

            client.on('close', function () {
                lircConnected = false;
                that.emit('disconnect');
                reConnect();
            });

            client.on('data', function (data) {
                data = data.toString();
{'fraction_non_alphanumeric': 0.1422316384180791, 'fraction_numerical': 0.008192090395480226, 'mean_word_length': 1.4672473867595819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6040751', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1902, 'n_words': 430}