<commit_before>"use strict";

// *********************************
// Libraries
// *********************************

var U               = require('util');
var Base            = require('./base');
var C               = require('./common').common();
var RS              = require('./remote_send');

function _connect_to_server( str, is_reconnect, on_connect_func ) {
    // XXX this is a 'strict violation' in jslint - find & fix
    var config = this.config_object().config;

    // only show reconnects with trace, but always show initial connects
    if( config.trace ) {
        var pre = is_reconnect ? "Re-" : "";
        C._trace( U.format( "%sConnecting to remote %s", pre, str ) );
    } else if ( !is_reconnect ) {
        C._log( "Connecting to remote " + str );
    }

    var remote = (function( str ) {
        //                  type :// connection_string
        // XXX jslint complains about: Insecure '.'.
        var m = str.match(/^(\w+):\/\/(.+)$/);

        if( m && m[0] ) {
            // it might be a host:port combo
            var conn  = m[2];
            var parts = conn.split(':', 2);
            var host  = parts[0];
            var port  = parts[1];

            switch( m[1] ) {
                case 'socket':
                    return new RS.Stream(str, conn, conn, is_reconnect, on_connect_func);
                case 'tcp':
                    return new RS.Stream(str, port, host, is_reconnect, on_connect_func);
                case 'udp':
                    return new RS.UDP(str, port, host, is_reconnect, on_connect_func);
                case 'file':
                    throw( "TODO: No support for file:// yet" );
                default:
                    throw( U.format( "Unknown server type '%s'", m[1] ) );
            }
        } else {
            // if we get here, we don't know the format
            throw( U.format( "Can not parse connection string '%s'", str ) );
        }
    }( str ));

    return remote;
}


var RemotePool = exports.RemotePool =
    function( servers, reconnect_interval, monitor_interval ) {

    // Some basic properties, for later inspection
    var obj                 = new Base.BaseObject();
    var config              = obj.config_object().config;
    obj.servers             = servers;
    obj.reconnect_interval  = reconnect_interval || config.reconnect_interval;
    obj.monitor_interval    = monitor_interval   || config.monitor_interval;

    // When the remote is connected, an event will be emitted. we'll use
    // that to signal the health monitoring to begin.
    obj.on_connect_func     = function( rs ) {
        C._trace( "Installing health checks & reconnect monitors" );

        // Install a periodic health check that updates the list of healthy
        // servers periodicaly. These are the servers that will be used from
        // the local listeners
        // We do that inside this function, so we don't get 'connection refused'
        // before the connection even happened.
        // XXX this still seems to fast on my laptop. Better way to do this so
        // the startup message doesn't say 'all unhealthy' followed by 'all is
        // well'?
        if( !this.periodic_monitor ) {
            this.periodic_monitor = setInterval( function() {
                this.state_object().current_servers = this.health_check();
            }.bind(this), this.monitor_interval );
        }

        // Install a periodic reconnect that tries to reestablish connections
        // that have been dropped, gone down or otherwise were in trouble.
        if( !this.periodic_reconnect ) {
            this.periodic_reconnect = setInterval(
                this.reconnect_to_servers, this.reconnect_interval );
        }

    }.bind(obj);

    // Loop over the servers and establish an initial connection.
    obj.connect_to_servers = function (func) {<commit_msg>* break up the diagnostic<commit_after>"use strict";

// *********************************
// Libraries
// *********************************

var U               = require('util');
var Base            = require('./base');
var C               = require('./common').common();
var RS              = require('./remote_send');

function _connect_to_server( str, is_reconnect, on_connect_func ) {
    // XXX this is a 'strict violation' in jslint - find & fix
    var config = this.config_object().config;

    // only show reconnects with trace, but always show initial connects
    if( config.trace ) {
        var pre = is_reconnect ? "Re-" : "";
        C._trace( U.format( "%sConnecting to remote %s", pre, str ) );
    } else if ( !is_reconnect ) {
        C._log( "Connecting to remote " + str );
    }

    var remote = (function( str ) {
        //                  type :// connection_string
        // XXX jslint complains about: Insecure '.'.
        var m = str.match(/^(\w+):\/\/(.+)$/);

        if( m && m[0] ) {
            // it might be a host:port combo
            var conn  = m[2];
            var parts = conn.split(':', 2);
            var host  = parts[0];
            var port  = parts[1];

            switch( m[1] ) {
                case 'socket':
                    return new RS.Stream(str, conn, conn, is_reconnect, on_connect_func);
                case 'tcp':
                    return new RS.Stream(str, port, host, is_reconnect, on_connect_func);
                case 'udp':
                    return new RS.UDP(str, port, host, is_reconnect, on_connect_func);
                case 'file':
                    throw( "TODO: No support for file:// yet" );
                default:
                    throw( U.format( "Unknown server type '%s'", m[1] ) );
            }
        } else {
            // if we get here, we don't know the format
            throw( U.format( "Can not parse connection string '%s'", str ) );
        }
    }( str ));

    return remote;
}


var RemotePool = exports.RemotePool =
    function( servers, reconnect_interval, monitor_interval ) {

    // Some basic properties, for later inspection
    var obj                 = new Base.BaseObject();
    var config              = obj.config_object().config;
    obj.servers             = servers;
    obj.reconnect_interval  = reconnect_interval || config.reconnect_interval;
    obj.monitor_interval    = monitor_interval   || config.monitor_interval;

    // When the remote is connected, an event will be emitted. we'll use
    // that to signal the health monitoring to begin.
    obj.on_connect_func     = function( rs ) {

        // Install a periodic health check that updates the list of healthy
        // servers periodicaly. These are the servers that will be used from
        // the local listeners
        // We do that inside this function, so we don't get 'connection refused'
        // before the connection even happened.
        // XXX this still seems to fast on my laptop. Better way to do this so
        // the startup message doesn't say 'all unhealthy' followed by 'all is
        // well'?
        if( !this.periodic_monitor ) {
            C._trace( "Installing health checks" );

            this.periodic_monitor = setInterval( function() {
                this.state_object().current_servers = this.health_check();
            }.bind(this), this.monitor_interval );
        }

        // Install a periodic reconnect that tries to reestablish connections
        // that have been dropped, gone down or otherwise were in trouble.
        if( !this.periodic_reconnect ) {
            C._trace( "Installing reconnect monitor" );

            this.periodic_reconnect = setInterval(
                this.reconnect_to_servers, this.reconnect_interval );
        }

    }.bind(obj);

    // Loop over the servers and establish an initial connection.
    obj.connect_to_servers = function (func) {
{'fraction_non_alphanumeric': 0.12370341913177103, 'fraction_numerical': 0.0017928031758227686, 'mean_word_length': 1.8925925925925926, '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': '1633028', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2038, 'n_words': 692}