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

function callback(cb, err, value) {
  if (cb) process.nextTick(function () {
    cb(err, value);
  });
}

exports.Reservation = Reservation;
function Reservation(service, protocol, port) {
  this.service = service;
  this.protocol = protocol;
  this.port = +port;
  return this;
}
Reservation.stringify = function (reservation) {
  return reservation.service + (reservation.protocol ? '/' + reservation.protocol : '') + ' ' + reservation.port;
}
Reservation.parse = function (line) {
  var parts = /^([0-9a-zA-Z_\-]+)(?:[/](tcp|udp))?\s*(\d+)$/g.exec(String(line));
  if (!parts) {
    return null;
  }
  var service = parts[1];
  var protocol = parts[2];
  var port = parts[3];
  return new Reservation(service, protocol, port);
}

exports.Jar = Jar;
function Jar(min, max) {
  EventEmitter.call(this);
  this.services = Object.create(null);
  this.minPort = min == null ? 1025 : +min;
  this.maxPort = max == null ? MAX_PORT : +max;

  this._occupied = Object.create(null);
  this._base_port = this.minPort;
  return this;
}
util.inherits(Jar, EventEmitter);
Jar.prototype._findUnusedPort = function (protocol, preferred, attempts, value, cb) {
  var current_port = preferred ? +preferred : this._base_port;
  var first_port = current_port;
  if (current_port != current_port) {
    cb(new Error('preferred port must be a number'))
  }
  var togo = attempts ? +attempts : Infinity;
  togo--;
  while (this._occupied[current_port]) {
    this._base_port = current_port;
    if (togo <= 0) {
      cb(new Error('unable to find port in specified number of attempts'));
      return;
    }
    current_port = (current_port + 1) % (this.maxPort + 1);
    if (current_port === 0) current_port = this.minPort;
    if (current_port === first_port) {
      cb(new Error('wrapped around and found no ports'));
      return;
    }
    togo--;
  }
  this._occupied[current_port] = value;
  this.emit('updated');
  cb(null, current_port);
}
Jar.prototype.reservations = function (service, cb) {
  var spec = this.services[service];
  if (!spec) {
    callback(cb, new Error('no service with that name'));
  }
  else {
    callback(cb, null, spec);
  }
}
Jar.prototype._reserveLine = function (line, cb) {
  var jar = this;<commit_msg>actually check the port when allocating<commit_after>var util = require('util');
var EventEmitter = require('events').EventEmitter;
var net = require('net');
var dgram = require('dgram');
var MAX_PORT = 65535;

function callback(cb, err, value) {
  if (cb) process.nextTick(function () {
    cb(err, value);
  });
}

exports.Reservation = Reservation;
function Reservation(service, protocol, port) {
  this.service = service;
  this.protocol = protocol;
  this.port = +port;
  return this;
}
Reservation.stringify = function (reservation) {
  return reservation.service + (reservation.protocol ? '/' + reservation.protocol : '') + ' ' + reservation.port;
}
Reservation.parse = function (line) {
  var parts = /^([0-9a-zA-Z_\-]+)(?:[/](tcp|udp))?\s*(\d+)$/g.exec(String(line));
  if (!parts) {
    return null;
  }
  var service = parts[1];
  var protocol = parts[2];
  var port = parts[3];
  return new Reservation(service, protocol, port);
}

exports.Jar = Jar;
function Jar(min, max) {
  EventEmitter.call(this);
  this.services = Object.create(null);
  this.minPort = min == null ? 1025 : +min;
  this.maxPort = max == null ? MAX_PORT : +max;

  this._occupied = Object.create(null);
  this._base_port = this.minPort;
  return this;
}
util.inherits(Jar, EventEmitter);
Jar.prototype._findUnusedPort = function (protocol, preferred, attempts, value, cb) {
  var jar = this;
  var current_port = preferred ? +preferred : this._base_port;
  var first_port = current_port;
  if (current_port != current_port) {
    cb(new Error('preferred port must be a number'))
  }
  var togo = attempts ? +attempts : Infinity;

  var attempt = function attempt() {
    togo--;
    while (jar._occupied[current_port]) {
      jar._base_port = current_port;
      if (togo <= 0) {
        cb(new Error('unable to find port in specified number of attempts'));
        return;
      }
      current_port = (current_port + 1) % (jar.maxPort + 1);
      if (current_port === 0) current_port = jar.minPort;
      if (current_port === first_port) {
        cb(new Error('wrapped around and found no ports'));
        return;
      }
      togo--;
    }
  
    jar._occupied[current_port] = value;
    grabTCP(current_port, function (err, port, server) {
      // leave it occupied
      if (err) {
        attempt();
        return;
      }
      server.close(function () {
        jar.emit('updated');
        cb(null, port);
      });
    });
  }
  attempt();
}
Jar.prototype.reservations = function (service, cb) {
  var spec = this.services[service];
  if (!spec) {
    callback(cb, new Error('no service with that name'));
  }
  else {
    callback(cb, null, spec);
  }
}
Jar.prototype._reserveLine = function (line, cb) {
  var jar = this;
{'fraction_non_alphanumeric': 0.15159210783356125, 'fraction_numerical': 0.007032623559288923, 'mean_word_length': 3.4521739130434783, 'pattern_counts': {'":': 0, '<': 5, '<?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': '543646', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1690, 'n_words': 423}