<commit_before>/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

var mn = require('../../common/mobilenetwork.js'),
    log = require('../../common/logger.js'),
    connector_ws = require('./connector_ws.js'),
    connector_udp = require('./connector_udp.js');

function Connector() {
  this.nodesConnectors = {};
}

Connector.prototype = {
  /**
   * Create and return a connector object based on the data received
   */
  getConnector: function(data, connection, callback) {
    if (this.nodesConnectors[data.uaid]) {
      return callback(null, this.nodesConnectors[data.uaid]);
    } else if (data.wakeup_hostport && data.wakeup_hostport.ip && data.wakeup_hostport.port &&
       data.mobilenetwork && data.mobilenetwork.mcc && data.mobilenetwork.mnc) {
      mn.getNetwork(data.mobilenetwork.mcc, data.mobilenetwork.mnc, function(error, op) {
        if (error) {
          log.error('UDP::queue::onNewMessage --> Error getting the operator from the DB: ' + error);
          return;
        }
        if (!op || !op.wakeup) {
          log.debug('UDP::queue::onNewMessage --> No WakeUp server found for MCC=' +
                     data.mobilenetwork.mcc + ' and MNC=' + data.mobilenetwork.mnc);
          var connector = new connector_ws(data, connection);
          this.nodesConnectors[data.uaid] = connector;
          callback(null, connector);
          return;
        }
        var connector = null;
        log.debug('getConnector: UDP WakeUp server for ' + op.operator + ': ' + op.wakeup);
        connector = new connector_udp(data, connection);
        this.nodesConnectors[data.uaid] = connector;
        callback(null, connector);
      }.bind(this));
    } else {
      //Fallback
      var connector = new connector_ws(data, connection);
      this.nodesConnectors[data.uaid] = connector;
      callback(null, connector);
    }
  },

  getConnectorForUAID: function(uaid) {
    return this.nodesConnectors[uaid];
  },

  getUAtokenForConnection: function(connection) {
    Object.keys(this.nodesConnectors).forEach(function(elem) {
      if (this.nodesConnectors[elem] === connection);
      return this.nodesConnectors[elem];
    });
  },

  unregisterUAID: function(uaid) {
    if (this.nodesConnectors[uaid]) {
      this.nodesConnectors[uaid].getConnection().close();
      delete this.nodesConnectors[uaid];
    }
  }
};

var connector = new Connector();
function getConnector() {
  return connector;
}

exports.getConnector = getConnector;
<commit_msg>When checking for wakeup mode, we also check if it has a valid IP address and valid port number<commit_after>/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

var mn = require('../../common/mobilenetwork.js'),
    log = require('../../common/logger.js'),
    net = require('net'),
    connector_ws = require('./connector_ws.js'),
    connector_udp = require('./connector_udp.js');

function Connector() {
  this.nodesConnectors = {};
}

Connector.prototype = {
  /**
   * Create and return a connector object based on the data received
   */
  getConnector: function(data, connection, callback) {
    if (this.nodesConnectors[data.uaid]) {
      return callback(null, this.nodesConnectors[data.uaid]);
    } else if (data.wakeup_hostport && data.wakeup_hostport.ip && data.wakeup_hostport.port &&
        data.mobilenetwork && data.mobilenetwork.mcc && data.mobilenetwork.mnc &&
        net.isIP(data.wakeup_hostport.ip) &&                                // Is a valid IP address
        !isNaN(parseInt(data.wakeup_hostport.port)) &&                      // The port is a Number
        data.wakeup_hostport.port > 0 && data.wakeup_hostport.port <= 65535 // The port has a valid value
      ) {
      mn.getNetwork(data.mobilenetwork.mcc, data.mobilenetwork.mnc, function(error, op) {
        if (error) {
          log.error('UDP::queue::onNewMessage --> Error getting the operator from the DB: ' + error);
          return;
        }
        if (!op || !op.wakeup) {
          log.debug('UDP::queue::onNewMessage --> No WakeUp server found for MCC=' +
                     data.mobilenetwork.mcc + ' and MNC=' + data.mobilenetwork.mnc);
          var connector = new connector_ws(data, connection);
          this.nodesConnectors[data.uaid] = connector;
          callback(null, connector);
          return;
        }
        var connector = null;
        log.debug('getConnector: UDP WakeUp server for ' + op.operator + ': ' + op.wakeup);
        connector = new connector_udp(data, connection);
        this.nodesConnectors[data.uaid] = connector;
        callback(null, connector);
      }.bind(this));
    } else {
      //Fallback
      var connector = new connector_ws(data, connection);
      this.nodesConnectors[data.uaid] = connector;
      callback(null, connector);
    }
  },

  getConnectorForUAID: function(uaid) {
    return this.nodesConnectors[uaid];
  },

  getUAtokenForConnection: function(connection) {
    Object.keys(this.nodesConnectors).forEach(function(elem) {
      if (this.nodesConnectors[elem] === connection);
      return this.nodesConnectors[elem];
    });
  },

  unregisterUAID: function(uaid) {
    if (this.nodesConnectors[uaid]) {
      this.nodesConnectors[uaid].getConnection().close();
      delete this.nodesConnectors[uaid];
    }
  }
};

var connector = new Connector();
function getConnector() {
  return connector;
}

exports.getConnector = getConnector;

{'fraction_non_alphanumeric': 0.13586378148279532, 'fraction_numerical': 0.002837885775097552, 'mean_word_length': 2.8889655172413793, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3377925', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1729, 'n_words': 366}