<commit_before>// Created by leiko on 29/10/14 14:57
var util        = require('util'),
    events      = require('events'),
    WebSocket   = require('ws'),
    chalk       = require('chalk'),
    unregister  = require('./actions/server/unregister');

/**
 *
 * @param port
 * @param path
 * @constructor
 */
function Server(port, path) {
    this.ttl = 1000*60*60; // 1 hour
    this.id2ws = {};
    this.ws2id = {};
    this.ack2ws = {};
    this.ws2ack = {};
    this.ack2timeout = {};

    path = path || '';
    if (path.substr(0, 1) !== '/') { path = '/' + path; }

    this.wss = new WebSocket.Server({ port: port, path: path });
    this.wss.on('connection', function (ws) {
        this._connectionHandler(ws);
        this.emit('connection');
    }.bind(this));
}

util.inherits(Server, events.EventEmitter);

Server.prototype.close = function () {
    try {
        this.wss.close();
    } catch (ignore) {
        /* no one cares */
    } finally {
        this.wss = null;
    }
};

/**
 *
 * @param ws
 * @private
 */
Server.prototype._connectionHandler = function (ws) {
    ws.on('message', function (msg) {
        this._messageHandler(ws, msg);
    }.bind(this));

    ws.on('close', function () {
        this._closeHandler(ws);
    }.bind(this));
};

/**
 *
 * @param ws
 * @param data
 * @private
 */
Server.prototype._messageHandler = function (ws, data) {
    var msg = data;
    if (typeof data === 'object') { msg = data.data; }

    try {
        msg = JSON.parse(msg);
        console.log(chalk.bold.gray('server'));
        console.log(JSON.stringify(msg, null, 2));
        var action = require('./actions/server/'+msg.action);
        if (action instanceof Function) {
            try {
                action(this, ws, msg);
            } catch (err) {
                this.emit('error', err);
            }
        } else {
            this.emit('error', new Error('"action" module must be Functions (action "'+msg.action+'" is a '+typeof action+')'));
            //this.log.error(this.toString(), '"'+this.getName()+'" malformed action "'+msg.action+'"');
        }
    } catch (err) {
        if (err.code === 'MODULE_NOT_FOUND') {<commit_msg>clean log<commit_after>// Created by leiko on 29/10/14 14:57
var util        = require('util'),
    events      = require('events'),
    WebSocket   = require('ws'),
    chalk       = require('chalk'),
    unregister  = require('./actions/server/unregister');

/**
 *
 * @param port
 * @param path
 * @constructor
 */
function Server(port, path) {
    this.ttl = 1000*60*60; // 1 hour
    this.id2ws = {};
    this.ws2id = {};
    this.ack2ws = {};
    this.ws2ack = {};
    this.ack2timeout = {};

    path = path || '';
    if (path.substr(0, 1) !== '/') { path = '/' + path; }

    this.wss = new WebSocket.Server({ port: port, path: path });
    this.wss.on('connection', function (ws) {
        this._connectionHandler(ws);
        this.emit('connection');
    }.bind(this));
}

util.inherits(Server, events.EventEmitter);

Server.prototype.close = function () {
    try {
        this.wss.close();
    } catch (ignore) {
        /* no one cares */
    } finally {
        this.wss = null;
    }
};

/**
 *
 * @param ws
 * @private
 */
Server.prototype._connectionHandler = function (ws) {
    ws.on('message', function (msg) {
        this._messageHandler(ws, msg);
    }.bind(this));

    ws.on('close', function () {
        this._closeHandler(ws);
    }.bind(this));
};

/**
 *
 * @param ws
 * @param data
 * @private
 */
Server.prototype._messageHandler = function (ws, data) {
    var msg = data;
    if (typeof data === 'object') { msg = data.data; }

    try {
        msg = JSON.parse(msg);
        //console.log(chalk.bold.gray('server'));
        //console.log(JSON.stringify(msg, null, 2));
        var action = require('./actions/server/'+msg.action);
        if (action instanceof Function) {
            try {
                action(this, ws, msg);
            } catch (err) {
                this.emit('error', err);
            }
        } else {
            this.emit('error', new Error('"action" module must be Functions (action "'+msg.action+'" is a '+typeof action+')'));
            //this.log.error(this.toString(), '"'+this.getName()+'" malformed action "'+msg.action+'"');
        }
    } catch (err) {
        if (err.code === 'MODULE_NOT_FOUND') {
{'fraction_non_alphanumeric': 0.18632400183570444, 'fraction_numerical': 0.012391005048187242, 'mean_word_length': 2.2098674521354935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6148112', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1383, 'n_words': 299}