<commit_before>// NodeJS net module
const net = require('net');
// trace routines
const Trace = require('atm-trace');

function Network(ipc, log) {
  this.trace = new Trace();
  //this.client = new net.Socket();
  this.isConnected = false;

  /**
   * [getOutgoingMessageLength get the Message Header Length]
   * @param  {[type]} data [binary data]
   * @return {[type]}      [two-bytes length, e.g. '\x00\x0a' for 10-byte data]
   */
  this.getOutgoingMessageLength = function (data){
    return String.fromCharCode(data.length / 256) + String.fromCharCode(data.length % 256);
  };

  /**
   * [send description]
   * @param  {[type]} data [description]
   * @return {[type]}      [description]
   */
  this.send = function (data){
    let binary_data = Buffer(this.getOutgoingMessageLength(data) + data, 'binary');
    this.client.write(binary_data);
    this.trace.trace(binary_data, ' >> ' + binary_data.length + ' bytes sent:');
  };

  /**
   * [toggleConnect description]
   * @param  {[type]} host [description]
   * @param  {[type]} port [description]
   * @return {[type]}      [description]
   */
  this.toggleConnect = function(host, port){
    if(!this.isConnected){
      this.trace.trace('', ' >> Connecting to ' + host + ':' + port );

      this.client = net.createConnection({ host: host, port: port }, () => {
        this.isConnected = true;
        this.trace.trace('', ' >> Connected' );
        ipc.send('network-connection-status-change', this.isConnected);
      });

      this.client.on('error', (e) => {
        log.error(e);
      });

      this.client.on('end', _ => {
        this.isConnected = false;
        ipc.send('network-connection-status-change', this.isConnected);
        log.warn('Connection closed');
      });

      this.client.on('data', (data) => {
        this.trace.trace(data, ' << ' + data.length + ' bytes received:');
        ipc.send('network-data-received', data);
      });
    }else{
      this.client.end();
    }
  };
}

/**
 * [send description]
 * @param  {[type]} message [description]
 * @return {[type]}      [description]
 */
Network.prototype.send = function(message){
  this.send(message);
};

module.exports = Network;

<commit_msg>class Network<commit_after>const net = require('net');
const Trace = require('atm-trace');

class Network {
  constructor(ipc, log) {
    this.ipc = ipc;
    this.log = log;

    this.trace = new Trace();
    this.isConnected = false;
  }

  /**
   * [getOutgoingMessageLength get the Message Header Length]
   * @param  {[type]} data [binary data]
   * @return {[type]}      [two-bytes length, e.g. '\x00\x0a' for 10-byte data]
   */
  getOutgoingMessageLength(data){
    return String.fromCharCode(data.length / 256) + String.fromCharCode(data.length % 256);
  }

  /**
   * [send description]
   * @param  {[type]} data [description]
   * @return {[type]}      [description]
   */
  send(data){
    let binary_data = Buffer(this.getOutgoingMessageLength(data) + data, 'binary');
    this.client.write(binary_data);
    this.trace.trace(binary_data, ' >> ' + binary_data.length + ' bytes sent:');
  }


  /**
   * [toggleConnect description]
   * @param  {[type]} host [description]
   * @param  {[type]} port [description]
   * @return {[type]}      [description]
   */
  toggleConnect(host, port){
    if(!this.isConnected){
      this.trace.trace('', ' >> Connecting to ' + host + ':' + port );

      this.client = net.createConnection({ host: host, port: port }, () => {
        this.isConnected = true;
        this.trace.trace('', ' >> Connected' );
        this.ipc.send('network-connection-status-change', this.isConnected);
      });

      this.client.on('error', (e) => {
        this.log.error(e);
      });

      this.client.on('end', _ => {
        this.isConnected = false;
        this.ipc.send('network-connection-status-change', this.isConnected);
        this.log.warn('Connection closed');
      });

      this.client.on('data', (data) => {
        this.trace.trace(data, ' << ' + data.length + ' bytes received:');
        this.ipc.send('network-data-received', data);
      });
    }else{
      this.client.end();
    }
  }
}

module.exports = Network;


{'fraction_non_alphanumeric': 0.18025340664594788, 'fraction_numerical': 0.005259383217786278, 'mean_word_length': 2.7864253393665157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '4121015', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1347, 'n_words': 262}