<commit_before>"use strict";

var request = require('request')
  , crypto = require('crypto')
  , querystring = require('querystring')
  , url = require('url');

var PROTO = 1; //Version of Beebotte Protocol

/** @constructor */
function Connection (bbt) {
  this.bbt = bbt;
  this.connection = null;
  this.connected;
}

Connection.prototype.onConnection = function() {
  for(var c in this.bbt.subscriptions) {
    this.do_subscribe(this.bbt.subscriptions[c]);
  }
}

Connection.prototype.getWsUrl = function() {
  var p = (this.bbt.transport.ssl === true)? this.bbt.transport.sport : this.bbt.transport.port;
  return ((this.bbt.transport.ssl === true)? 'https://' : 'http://' ) + this.bbt.transport.ws_host + ':' + p;
}

Connection.prototype.connect = function () {
  if( this.connection && this.connection.connected ) return this;

  var self = this;
  if( self.bbt.transport.token ) {
    var query =  'token=' + self.bbt.transport.token + '&username=' + (self.bbt.userinfo.username || '');
  } else {
    var query =  'key=' + self.bbt.transport.key + '&username=' + (self.bbt.userinfo.username || '');
  }
  this.connection = require('socket.io-client')(self.getWsUrl(), {query: query });
  this.connection.on('connect', function () {
    if(self.connected === false) {
      self.connected = true;
      self.onConnection();
      self.bbt.emit('reconnected');    
    }else {
      self.connected = true;
      self.bbt.emit('connected');
    }
  });

  this.connection.on('error', function (err) {
    self.bbt.emit('error', err);
  });

  this.connection.on('disconnect', function () {
    self.connected = false;
    self.bbt.emit('disconnected');
  });

  this.connection.on('message', function (msg) {
    if(msg.channel && msg.resource) {
      var subscription = self.bbt.getAnySubscription(msg.channel, msg.resource);
      if(subscription) {
        subscription.fct(msg);
      }else {
        //console.log('Warning! non subscribed message: ' + JSON.stringify(msg));
      }
    } else {
      //console.log('Warning! non conform message: ' + JSON.stringify(msg));
    }
  });
  
  return self.bbt;
}

Connection.prototype.disconnect = function () {
  if(this.connection.connected) {
    this.connection.disconnect();
    this.connection.removeAllListeners();
  };
  return this.bbt;
}

//for internal use only
Connection.prototype.send_auth = function(sig, source) {
  var self = this;
  if(self.send('control', 'authenticate', {auth: sig.auth, source: source})) {
    this.authenticated = true;
    return true;
  }else {
    this.authenticated = false
    return false;
  }
}

Connection.prototype.subscribe = function(args, callback) {
  var subscription = this.bbt.getSubscription(args.channel, args.resource);

  if(subscription) {
    if(subscription.update(args)) this.do_subscribe(subscription);
  }else {
    subscription = this.bbt.addSubscription(args, callback);<commit_msg>Fix #21: enable reconnection after disconnect<commit_after>"use strict";

var request = require('request')
  , crypto = require('crypto')
  , querystring = require('querystring')
  , url = require('url');

var PROTO = 1; //Version of Beebotte Protocol

/** @constructor */
function Connection (bbt) {
  this.bbt = bbt;
  this.connection = null;
  this.connected;
}

Connection.prototype.onConnection = function() {
  for(var c in this.bbt.subscriptions) {
    this.do_subscribe(this.bbt.subscriptions[c]);
  }
}

Connection.prototype.getWsUrl = function() {
  var p = (this.bbt.transport.ssl === true)? this.bbt.transport.sport : this.bbt.transport.port;
  return ((this.bbt.transport.ssl === true)? 'https://' : 'http://' ) + this.bbt.transport.ws_host + ':' + p;
}

Connection.prototype.connect = function () {

  var self = this;

  // already connected, return
  if (self.connection && self.connection.connected) {
    return self;
  }

  if (self.connection) {
    self.connection.connect()
  } else {
    if( self.bbt.transport.token ) {
      var query =  'token=' + self.bbt.transport.token + '&username=' + (self.bbt.userinfo.username || '');
    } else {
      var query =  'key=' + self.bbt.transport.key + '&username=' + (self.bbt.userinfo.username || '');
    }
    self.connection = require('socket.io-client')(self.getWsUrl(), {query: query });
  }
  self.connection.on('connect', function () {
    if(self.connected === false) {
      self.connected = true;
      self.onConnection();
      self.bbt.emit('reconnected');    
    } else {
      self.connected = true;
      self.bbt.emit('connected');
    }
  });

  self.connection.on('error', function (err) {
    self.bbt.emit('error', err);
  });

  self.connection.on('disconnect', function () {
    self.connected = false;
    self.bbt.emit('disconnected');
  });

  self.connection.on('message', function (msg) {
    if(msg.channel && msg.resource) {
      var subscription = self.bbt.getAnySubscription(msg.channel, msg.resource);
      if(subscription) {
        subscription.fct(msg);
      }else {
        //console.log('Warning! non subscribed message: ' + JSON.stringify(msg));
      }
    } else {
      //console.log('Warning! non conform message: ' + JSON.stringify(msg));
    }
  });

  self.connection.on('getsid', function (sid) {
    self._id = sid
  })

  return self.bbt;
}

Connection.prototype.disconnect = function () {
  if(this.connection.connected) {
    this.connection.disconnect();
    this.connection.removeAllListeners();
  };
  return this.bbt;
}

//for internal use only
Connection.prototype.send_auth = function(sig, source) {
  var self = this;
  if(self.send('control', 'authenticate', {auth: sig.auth, source: source})) {
    this.authenticated = true;
    return true;
  }else {
    this.authenticated = false
    return false;
  }
}

Connection.prototype.subscribe = function(args, callback) {
  var subscription = this.bbt.getSubscription(args.channel, args.resource);

  if(subscription) {
    if(subscription.update(args)) this.do_subscribe(subscription);
  }else {
    subscription = this.bbt.addSubscription(args, callback);
{'fraction_non_alphanumeric': 0.16534260178748758, 'fraction_numerical': 0.0006620324395895399, 'mean_word_length': 3.3885257806826434, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1251718', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1940, 'n_words': 358}