<commit_before>/**
 * @module gateway
 */

var hubiquitus = require('hubiquitus-core');
var logger = hubiquitus.logger('hubiquitus:addons:gateway');
var sockjs = require('sockjs');
var http = require('http');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var tv4 = require('tv4');
var schemas = require('./schemas');

var events = new EventEmitter();
events.setMaxListeners(0);

var defaultPort = 8888;
var defaultPath = '/hubiquitus';
var loginTimeout = 30000;

/**
 * Create a gateway
 * @param [session] {function} session actor
 * @param [login] {function} loginentication function
 * @returns {Gateway}
 */
exports.createGateway = function (session, login) {
  if (!_.isFunction(session)) {
    logger.trace('session actor not provided or invalid; default implementation is used instead');
    session = null;
  }
  if (!_.isFunction(login)) {
    logger.trace('login procedure not provided or invalid; default implementation is used instead');
    login = null;
  }
  return new Gateway(login, session);
};

/**
 * Gateway constructor
 * @param [login] {function} loginentication function
 * @param [session] {function} session actor
 * @constructor
 */
function Gateway(session, login) {
  EventEmitter.call(this);

  var _this = this;

  login = login || basicLogin;
  session = session || basicSession;

  _this.sock = sockjs.createServer();

  _this.sock.on('connection', function (sock) {
    logger.debug('connection from ' + sock.remoteAddress + ' (using ' + sock.protocol + ')');

    sock.loginTimeout = setTimeout(function () {
      logger.warn('authentication delay timeout !');
      logout(sock);
    }, loginTimeout);

    sock.on('data', function (data) {
      var msg = decode(data);
      if (msg && msg.type) {
        switch (msg.type) {
          case 'req':
            sock.identity ? processReq(sock, msg) : enqueueReq(sock, msg);
            break;
          case 'res':
            sock.identity && processRes(sock, msg);
            break;
          case 'login':
            processLogin(sock, msg.authData);
            break;
          default:
            logger.warn('received unknown message type', msg);
        }
      } else {
        logger.warn('received malformat data', msg);
      }
    });<commit_msg>overload sockjs logger<commit_after>/**
 * @module gateway
 */

var hubiquitus = require('hubiquitus-core');
var logger = hubiquitus.logger('hubiquitus:addons:gateway');
var sockjsLogger = hubiquitus.logger('hubiquitus:addons:gateway:sockjs');
var sockjs = require('sockjs');
var http = require('http');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var tv4 = require('tv4');
var schemas = require('./schemas');

var events = new EventEmitter();
events.setMaxListeners(0);

var defaultPort = 8888;
var defaultPath = '/hubiquitus';
var loginTimeout = 30000;

/**
 * Create a gateway
 * @param [session] {function} session actor
 * @param [login] {function} loginentication function
 * @returns {Gateway}
 */
exports.createGateway = function (session, login) {
  if (!_.isFunction(session)) {
    logger.trace('session actor not provided or invalid; default implementation is used instead');
    session = null;
  }
  if (!_.isFunction(login)) {
    logger.trace('login procedure not provided or invalid; default implementation is used instead');
    login = null;
  }
  return new Gateway(login, session);
};

/**
 * Gateway constructor
 * @param [login] {function} loginentication function
 * @param [session] {function} session actor
 * @constructor
 */
function Gateway(session, login) {
  EventEmitter.call(this);

  var _this = this;

  login = login || basicLogin;
  session = session || basicSession;

  var sockOptions = {
    log: function (level, message) {
      sockjsLogger[level](message);
    }
  };
  _this.sock = sockjs.createServer(sockOptions);

  _this.sock.on('connection', function (sock) {
    logger.debug('connection from ' + sock.remoteAddress + ' (using ' + sock.protocol + ')');

    sock.loginTimeout = setTimeout(function () {
      logger.warn('authentication delay timeout !');
      logout(sock);
    }, loginTimeout);

    sock.on('data', function (data) {
      var msg = decode(data);
      if (msg && msg.type) {
        switch (msg.type) {
          case 'req':
            sock.identity ? processReq(sock, msg) : enqueueReq(sock, msg);
            break;
          case 'res':
            sock.identity && processRes(sock, msg);
            break;
          case 'login':
            processLogin(sock, msg.authData);
            break;
          default:
            logger.warn('received unknown message type', msg);
        }
      } else {
        logger.warn('received malformat data', msg);
      }
    });
{'fraction_non_alphanumeric': 0.1400414937759336, 'fraction_numerical': 0.004979253112033195, 'mean_word_length': 3.2289473684210526, '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': '1249438', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1374, 'n_words': 372}