<commit_before>/**
 * var connections = new Connections({ tag:             'droonga',
 *                                     defaultDataset:  'example',
 *                                     hostNames:       ['127.0.0.1', ...],
 *                                     port:            24224,
 *                                     receiveHostName: '127.0.0.1',
 *                                     receivePort:     10030 });
 */

var Connection = require('./connection').Connection;
var ConsoleLogger = require('../console-logger').ConsoleLogger;

function Connections(params) {
  this._params = params || {};

  if (!this._params.logger)
    this._params.logger = new ConsoleLogger();

  var hostNames = this._params.hostNames ||
                    this._params.hostName ||
                    Connection.DEFAULT_FLUENT_HOST_NAME;
  if (!Array.isArray(hostNames))
    this._params.hostNames = [hostNames];

  var uniqueHostNames = {};
  this._params.hostNames = this._params.hostNames.filter(function(hostName) {
    if (hostName in uniqueHostNames) {
      return false;
    }
    else {
      uniqueHostNames[hostName] = true;
      return true;
    }
  });

  if (this._params.hostNames.length == 0)
    throw new Error('Connections: you must give one or more host name(s)!');

  this._connections = {};
  this._instantiate();
}

Connections.prototype = {
  _instantiate: function() {
    this._params.hostNames.forEach(function(hostName) {
      if (this._connections[hostName])
        return;

      this._connections[hostName] = new Connection({
        tag:             this._params.tag
        defaultDataset:  this._params.defaultDataset
        hostName:        hostName
        port:            this._params.port
        receiveHostName: this._params.receiveHostName
        receivePort:     this._params.receivePort
      });
    }, this);

    this._nextIndex = 0;
  },

  get: function() {
    var hostName = this._params.hostNames[this._nextIndex];

    this._nextIndex++;
    if (this._nextIndex == this._params.hostNames.length)
      this._nextIndex = 0;

    return this._connections[hostName];
  },

  closeAll: function() {
    this._params.hostNames.forEach(function(hostName) {
      var connection = this._connections[hostName];
      connection.close();
    }, this);
  }
};

exports.Connections = Connections;<commit_msg>Add a property to get the number of instantiated connections<commit_after>/**
 * var connections = new Connections({ tag:             'droonga',
 *                                     defaultDataset:  'example',
 *                                     hostNames:       ['127.0.0.1', ...],
 *                                     port:            24224,
 *                                     receiveHostName: '127.0.0.1',
 *                                     receivePort:     10030 });
 */

var Connection = require('./connection').Connection;
var ConsoleLogger = require('../console-logger').ConsoleLogger;

function Connections(params) {
  this._params = params || {};

  if (!this._params.logger)
    this._params.logger = new ConsoleLogger();

  var hostNames = this._params.hostNames ||
                    this._params.hostName ||
                    Connection.DEFAULT_FLUENT_HOST_NAME;
  if (!Array.isArray(hostNames))
    this._params.hostNames = [hostNames];

  var uniqueHostNames = {};
  this._params.hostNames = this._params.hostNames.filter(function(hostName) {
    if (hostName in uniqueHostNames) {
      return false;
    }
    else {
      uniqueHostNames[hostName] = true;
      return true;
    }
  });

  if (this._params.hostNames.length == 0)
    throw new Error('Connections: you must give one or more host name(s)!');

  this._connections = {};
  this._instantiate();
}

Connections.prototype = {
  _instantiate: function() {
    this._params.hostNames.forEach(function(hostName) {
      if (this._connections[hostName])
        return;

      this._connections[hostName] = new Connection({
        tag:             this._params.tag
        defaultDataset:  this._params.defaultDataset
        hostName:        hostName
        port:            this._params.port
        receiveHostName: this._params.receiveHostName
        receivePort:     this._params.receivePort
      });
    }, this);

    this._nextIndex = 0;
  },

  get: function() {
    var hostName = this._params.hostNames[this._nextIndex];

    this._nextIndex++;
    if (this._nextIndex == this._params.hostNames.length)
      this._nextIndex = 0;

    return this._connections[hostName];
  },

  closeAll: function() {
    this._params.hostNames.forEach(function(hostName) {
      var connection = this._connections[hostName];
      connection.close();
    }, this);
  },

  get count() {
    return this._params.hostNames.length;
  }
};

exports.Connections = Connections;
{'fraction_non_alphanumeric': 0.12066458982346832, 'fraction_numerical': 0.010384215991692628, 'mean_word_length': 2.083226632522407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5507427', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1308, 'n_words': 252}