<commit_before>var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('node-uuid');
var Logger = require('./logger');
var WebSocket = require('./web_socket');

function calculatePeerUrl(url, name){
 var wsUrl = url.replace(/^http/, 'ws');
  var peerPath = '/peers/' + name;
  if(wsUrl.indexOf('/', wsUrl.length - 1) === -1)  {
    wsUrl = wsUrl + peerPath;
  } else {
    wsUrl = wsUrl.slice(0, wsUrl.length - 1) + peerPath;
  } 
  return wsUrl;
}


var PeerClient = module.exports = function(url, server) {
  var wsUrl = calculatePeerUrl(url, server._name); 
  this.reconnect = {
    min: 100,
    max: 30000, // max amount of time allowed to backoff
    maxRandomOffset: 1000, // max amount of time
  };

  this.url = wsUrl;

  this.server = server.httpServer.spdyServer;
  this.connected = false;
  this.retryCount = 0;
  this.log = server.log || new Logger();
  this._backoffTimer = null;
  this._stopped = false;
  this._isClosed = false;

  // create a unique connection id peer connection, used to associate initiaion request
  this.connectionId = null;
  this.ws = new WebSocket(this._createNewUrl(), {});

  EventEmitter.call(this);
};
util.inherits(PeerClient, EventEmitter);

PeerClient.calculatePeerUrl = calculatePeerUrl;

PeerClient.prototype._createNewUrl = function() {
  this.connectionId = uuid.v4();
  return this.url + '?connectionId=' + this.connectionId;
};

PeerClient.prototype.properties = function() {
  return {
    url: this.url,
    connectionId: this.connectionId,
  };
};

PeerClient.prototype.start = function() {
  this._createSocket();
};

PeerClient.prototype.close = function() {
  if (this._isClosed) {
    return;
  }

  this._isClosed = true;

  clearTimeout(this._backoffTimer);
  this._stopped = true;
  this.ws.close();

  this.emit('closed');
};

PeerClient.prototype._createSocket = function() {
  var self = this;

  if (this.backoffTimer) {
    clearTimeout(this.backoffTimer);
  }

  // once peer is closed dont create new socket
  if (this._stopped) {
    return;
  }
  
  var backoff = this.generateBackoff(this.retryCount);
  this._backoffTimer = setTimeout(function(){
    // create a new connection id
    self.ws.setAddress(self._createNewUrl());
    if (self.retryCount === 0) {
      self.ws.on('open', function onOpen(socket) {
        self.checkServerReq();
        self.emit('connecting');  
        self.server.emit('connection', socket);
        self.log.emit('log', 'peer-client', 'WebSocket to peer established (' + self.url + ')');
      });

      self.ws.on('error', function onError(err) {
        self.connected = false;
        self.log.emit('log', 'peer-client', 'Peer connection error (' + self.url + '): ' + err);
        if (!self._stopped) {
          self.close();
        }
        reconnect(err);
      });

      self.ws.on('close', function(code, message) {
        //if (self.retryCount > 0) throw new Error('wtf');
        self.connected = false;
        self.log.emit('log', 'peer-client', 'Peer connection closed (' + self.url + '): ' + code + ' - ' + message);

        if (!self._stopped) {
          self.close();
        }
        reconnect();
      });
    }

    self.ws.start();
  }, backoff);

  function reconnect(err) {
    self.retryCount++;
    self._stopped = false;
    self._createSocket();
  }
};<commit_msg>Removed isClosed flag in PeerClient, method .stop() will stop peerClient from reconnecting<commit_after>var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('node-uuid');
var Logger = require('./logger');
var WebSocket = require('./web_socket');

function calculatePeerUrl(url, name){
 var wsUrl = url.replace(/^http/, 'ws');
  var peerPath = '/peers/' + name;
  if(wsUrl.indexOf('/', wsUrl.length - 1) === -1)  {
    wsUrl = wsUrl + peerPath;
  } else {
    wsUrl = wsUrl.slice(0, wsUrl.length - 1) + peerPath;
  } 
  return wsUrl;
}


var PeerClient = module.exports = function(url, server) {
  var wsUrl = calculatePeerUrl(url, server._name); 
  this.reconnect = {
    min: 100,
    max: 30000, // max amount of time allowed to backoff
    maxRandomOffset: 1000, // max amount of time
  };

  this.url = wsUrl;

  this.server = server.httpServer.spdyServer;
  this.connected = false;
  this.retryCount = 0;
  this.log = server.log || new Logger();
  this._backoffTimer = null;
  this._stopped = false;

  // create a unique connection id peer connection, used to associate initiaion request
  this.connectionId = null;
  this.ws = new WebSocket(this._createNewUrl(), {});

  EventEmitter.call(this);
};
util.inherits(PeerClient, EventEmitter);

PeerClient.calculatePeerUrl = calculatePeerUrl;

PeerClient.prototype._createNewUrl = function() {
  this.connectionId = uuid.v4();
  return this.url + '?connectionId=' + this.connectionId;
};

PeerClient.prototype.properties = function() {
  return {
    url: this.url,
    connectionId: this.connectionId,
  };
};

PeerClient.prototype.start = function() {
  this._stopped = false; // If previously closed, reset stopped flag
  this._createSocket();
};

// Close and stop reconnecting
PeerClient.prototype.close = function() {
  clearTimeout(this._backoffTimer);
  this._stopped = true;
  this.ws.close();
};

PeerClient.prototype._createSocket = function() {
  var self = this;

  if (this.backoffTimer) {
    clearTimeout(this.backoffTimer);
  }

  // once peer is closed dont create new socket
  if (this._stopped) {
    return;
  }
  
  var backoff = this.generateBackoff(this.retryCount);
  this._backoffTimer = setTimeout(function(){
    // create a new connection id
    self.ws.setAddress(self._createNewUrl());
    if (self.retryCount === 0) {
      self.ws.on('open', function onOpen(socket) {
        self.checkServerReq();
        self.emit('connecting');  
        self.server.emit('connection', socket);
        self.log.emit('log', 'peer-client', 'WebSocket to peer established (' + self.url + ')');
      });

      self.ws.on('error', function onError(err) {
        self.connected = false;
        self.log.emit('log', 'peer-client', 'Peer connection error (' + self.url + '): ' + err);
        self.emit('closed');
        reconnect(err);
      });

      self.ws.on('close', function(code, message) {
        //if (self.retryCount > 0) throw new Error('wtf');
        self.connected = false;
        self.log.emit('log', 'peer-client', 'Peer connection closed (' + self.url + '): ' + code + ' - ' + message);
        self.emit('closed');
        reconnect();
      });
    }

    self.ws.start();
  }, backoff);

  function reconnect(err) {
    self.retryCount++;
    self._createSocket();
  }
};
{'fraction_non_alphanumeric': 0.15470518165574748, 'fraction_numerical': 0.005955926146515783, 'mean_word_length': 3.1335384615384614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6229612', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2127, 'n_words': 479}