<commit_before>var net = require('net');

var hex = require("hex");

var re = /^(\w+\s?\w+)\s*:\s(.+)\n?$/;


var ApcAccess = function() {
  this._waitForResponse = false;
  this._socket = new net.Socket();
  this._requests = [];
  this._receiveBuffer = Buffer.allocUnsafe(0);
  this._socket.on('data', (data) => {
    var req = this._requests[0];
    if(req && this._waitForResponse) {
      this._receiveBuffer = Buffer.concat([this._receiveBuffer, data], this._receiveBuffer.length + data.length);
      while(this._receiveBuffer.length > 2) {
        var length = this._receiveBuffer.readUInt16BE(0);
        req.res += this._receiveBuffer.toString('ascii',2,length+2);
        this._receiveBuffer = this._receiveBuffer.slice(length+2);
      }
      if(this._receiveBuffer.length === 2 && this._receiveBuffer.readUInt16BE(0) === 0) {
        var req = this._requests.shift();
        this._waitForResponse = false;
        this._receiveBuffer = Buffer.allocUnsafe(0);
        process.nextTick(function() {
          req.handler(req.res);
        });
        this._flush();
      }
    }
  });
};

ApcAccess.prototype = {
  connect: function() {
    this._socket.connect(3551, 'obelix', () => {
      console.log('Connected');
    });
  },
  disconnect: function() {
    this._socket.end();
    console.log('disconnected');
  },
  getStatus: function() {
    this._requests.push({
      handler: this._statusHandler,
      cmd: 'status',
      res: ''
    });
    this._flush();
  },
  getEvents: function() {
    this._requests.push({
      handler: this._eventsHandler,
      cmd: 'events',
      res: ''
    });
    this._flush();
  },
  _statusHandler: function(data) {
    console.log(data);
  },
  _eventsHandler: function(data) {
    console.log(data);
  },
  _flush: function() {
    var req = this._requests[0];
    if(req && !this._waitForResponse) {
      this._waitForResponse = true;
      var buffer = Buffer.allocUnsafe(req.cmd.length + 2);
      buffer.writeUInt16BE(req.cmd.length, 0);
      buffer.write(req.cmd,2);
      this._socket.write(buffer);
    }
  }
};

var NisClient = function() {
  this._client = new net.Socket();
  this._client.on('data', (data) => {
    this._dataReceived(data);
  });
  this._buffer = Buffer.allocUnsafe(0);
  this._busy = false;
  this._response = {};
};

NisClient.prototype.connect = function() {
  this._client.connect(3551, 'obelix', () => {
    console.log('Connected');
  });
};

NisClient.prototype._sendCmd = function(cmd) {
  if (!(typeof cmd === 'string' || cmd instanceof String)) throw new Error('Cmd has to be a string');
  var buf = Buffer.allocUnsafe(cmd.length + 2);
  buf.writeUInt16BE(cmd.length, 0);
  buf.write(cmd,2);
  this._client.write(buf);
};

NisClient.prototype.requestStatus = function() {
  if (this._busy === true) {
    throw new Error('Client busy and waiting for server response');
  }
  this._busy = true;
  this._requestType = 'status';
  this._response = {};
  this._sendCmd('status');
};

NisClient.prototype.requestEvents = function() {
  if (this._busy === true) {
    throw new Error('Client busy and waiting for server response');
  }
  this._busy = true;
  this._requestType = 'events';
  this._response = '';
  this._sendCmd('events');
};

NisClient.prototype._dataReceived = function(data) {
  console.log(this._requestType);
  if(this._requestType === 'events') {
    this._handleEventsReply(data);
  } else if (this._requestType === 'status') {
    this._handleStatusReply(data);
  }
};

NisClient.prototype._handleEventsReply = function(data) {
  this._buffer = Buffer.concat([this._buffer, data], this._buffer.length + data.length);
  while(this._buffer.length > 2) {
    var length = this._buffer.readUInt16BE(0);
    var content = this._buffer.toString('ascii',2,length+2);
    this._response += content;
    this._buffer = this._buffer.slice(length+2);
  }
  if(this._buffer.length === 2 && this._buffer.readUInt16BE(0) === 0) {
    console.log("Message received:");
    console.log(this._response);
    this._buffer = Buffer.allocUnsafe(0);
    this._busy = false;
  }
};

NisClient.prototype._handleStatusReply = function(data) {
  this._buffer = Buffer.concat([this._buffer, data], this._buffer.length + data.length);
  while(this._buffer.length > 2) {
    var length = this._buffer.readUInt16BE(0);
    var content = this._buffer.toString('ascii',2,length+2);
    var match = content.match(re);
    this._response[match[1]] = match[2];
    this._buffer = this._buffer.slice(length+2);
  }
  if(this._buffer.length === 2 && this._buffer.readUInt16BE(0) === 0) {
    console.log("Message received:");
    console.log(this._response);
    this._buffer = Buffer.allocUnsafe(0);
    this._busy = false;
  }
};

var apcaccess = new ApcAccess();
apcaccess.connect();

apcaccess.getStatus();

apcaccess.getStatus();

apcaccess.getEvents();

//apcaccess.disconnect();

// var client = new NisClient();
// client.connect();
//
// function loopSt() {
//   client.requestStatus();
//   //client.requestEvents();
//   setTimeout(loopEv, 1000);
// }
//
// function loopEv() {
//   client.requestEvents();
//   //client.requestEvents();
//   setTimeout(loopSt, 1000);
// }
//
// loopEv();<commit_msg>Promisfied and Json output<commit_after>var net = require('net');

var ApcAccess = function() {
  this._waitForResponse = false;
  this._socket = new net.Socket();
  this._requests = [];
  this._receiveBuffer = Buffer.allocUnsafe(0);
  this._socket.on('data', (data) => {
    var req = this._requests[0];
    if(req && this._waitForResponse) {
      this._receiveBuffer = Buffer.concat([this._receiveBuffer, data], this._receiveBuffer.length + data.length);
      while(this._receiveBuffer.length > 2) {
        var length = this._receiveBuffer.readUInt16BE(0);
        req.res += this._receiveBuffer.toString('ascii',2,length+2);
        this._receiveBuffer = this._receiveBuffer.slice(length+2);
      }
      if(this._receiveBuffer.length === 2 && this._receiveBuffer.readUInt16BE(0) === 0) {
        var req = this._requests.shift();
        this._waitForResponse = false;
        this._receiveBuffer = Buffer.allocUnsafe(0);
        process.nextTick(function() {
          req.fulfill(req.res);
        });
        this._flush();
      }
    }
  });
};

ApcAccess.prototype = {
  connect: function() {
    return new Promise((fulfill, reject) => {
      this._socket.connect(3551, 'obelix');
      this._socket.on('connect', fulfill);
      this._socket.on('error', reject);
    });
  },
  disconnect: function() {
    return new Promise((fulfill, reject) => {
      this._socket.end();
      this._socket.on('close', fulfill);
      this._socket.on('error', reject);
    });
  },
  getStatus: function() {
    return new Promise((fulfill, reject) => {
      this._requests.push({
        fulfill: fulfill,
        reject: reject,
        cmd: 'status',
        res: ''
      });
      this._flush();
    });
  },
  getStatusJson: function() {
    return new Promise((fulfill, reject) => {
      this.getStatus().then((result) => {
        var re = /(\w+\s?\w+)\s*:\s(.+)?\n/g;
        var matches = {};
        var match = re.exec(result);
        while (match != null) {
            matches[match[1]] = match[2];
            match = re.exec(result);
        }
        fulfill(matches);
      }).catch((err) => {
        reject(err);
      });
    });
  },
  getEvents: function() {
    return new Promise((fulfill, reject) => {
      this._requests.push({
        fulfill: fulfill,
        reject: reject,
        cmd: 'events',
        res: ''
      });
      this._flush();
    });
  },
  _flush: function() {
    var req = this._requests[0];
    if(req && !this._waitForResponse) {
      this._waitForResponse = true;
      var buffer = Buffer.allocUnsafe(req.cmd.length + 2);
      buffer.writeUInt16BE(req.cmd.length, 0);
      buffer.write(req.cmd,2);
      this._socket.write(buffer);
    }
  }
};

var apcaccess = new ApcAccess();
apcaccess.connect().then(function() {
  console.log('Connected!');
  return apcaccess.getStatus();
}).then(function(result) {
  console.log(result);
  return apcaccess.getStatusJson();
}).then(function(result) {
  console.log(result);
  return apcaccess.disconnect();
}).then(function() {
  console.log('Disconnected!');
}).catch(function(err) {
  console.log(err);
});
{'fraction_non_alphanumeric': 0.17478647900878141, 'fraction_numerical': 0.011788764585588836, 'mean_word_length': 3.0437743190661477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '269508', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2686, 'n_words': 458}