<commit_before>/* jshint globalstrict: true */
"use strict";

const EventEmitter = require('tenshi/vendor-js/EventEmitter');
const ubjson = require('tenshi/common/ubjson');
const buffer = require('sdk/io/buffer');
const emcc_tools = require('tenshi/common/emcc_tools');
const xbee = require('tenshi/common/xbee');
const typpo = require('tenshi/common/typpo');

const MAX_XBEE_PAYLOAD_SIZE = 256;
const SEND_INTERVAL = 100;

const UBJSON_PORT = typpo.get_const('NDL3_UBJSON_PORT');
const STRING_PORT = typpo.get_const('NDL3_STRING_PORT');
const CODE_PORT = typpo.get_const('NDL3_CODE_PORT');

try {
  var ndl3 = require('tenshi/vendor-js/ndl3');
} catch (_) {
  console.warn('Could not load ndl3.js. Did you run build.sh?');
}

function call(name) {
  var args = Array.prototype.slice.call(arguments, 1);
  var arg_ts = [];
  for (var i = 0; i < args.length; i++) {
    arg_ts.push('number');
  }
  return ndl3.ccall(name, 'number', arg_ts, args);
}

var Radio = function(address, serportObj) {
  EventEmitter.apply(this);
  this.connected = false;
  this.net = call('NDL3_new', 0, 0, 0);
  if (this.net === 0) {
    throw 'Could not allocate NDL3.';
  }
  call('NDL3_open', this.net, UBJSON_PORT);
  call('NDL3_open', this.net, STRING_PORT);
  call('NDL3_open', this.net, CODE_PORT);

  this._tick = send_L2.bind(this);
  setInterval(this._tick, SEND_INTERVAL);

  this.on('send_object', send_object.bind(this));
  this.on('send_string', send_string.bind(this));
  this.on('send_code', send_code.bind(this));
  this.on('data', recv_L2.bind(this));

  if (address !== undefined &&
      serportObj !== undefined) {
    this.connectXBee(address, serportObj);
  } else {
    this.address = null;
    this.serportObj = null;
  }

  // Print out every message in or out of the radio, for debugging
  // purposes.
  var events = ['data', 'object', 'string', 'code'];
  function make_evt_callback(evt) {
    return function (data) {
      console.log('radio: ', evt, ':', data);
    };
  }
  for (var e = 0; e < events.length; e++) {
    var evt = events[e];
    this.on(evt, make_evt_callback(evt));
    this.on('send_' + evt, make_evt_callback('send_' + evt));
  }
};

Radio.prototype = Object.create(EventEmitter.prototype);

Radio.prototype.isConnected = function () {
  return this.connected;
};

Radio.prototype.connectXBee = function (address, serportObj) {
  if (this.connected) {
    throw 'XBee has already been connected to radio.';
  }
  this.address = address;
  this.serportObj = serportObj;
  serportObj.setReadHandler(read_handler.bind(this));
  serportObj.setReadHandler = function () {
    throw 'Radio already attached to serial port.';
  };
  this._send_data = send_data.bind(this);
  this.on('send_data', this._send_data);
};

Radio.prototype.disconnectXBee = function () {
  this.off('send_data', this._send_data);
  delete this.serportObj.setReadHandler;
  this.serportObj.setReadHandler(null);
  this.address = null;
  this.serportObj = null;
};

function send_L2 () {
  /* jshint validthis: true */
  var out_size_ptr = call('malloc', emcc_tools.PTR_SIZE);
  var send_block = call('malloc', MAX_XBEE_PAYLOAD_SIZE);
  if (out_size_ptr === 0 || send_block === 0) {
    throw 'malloc failed';
  }
  call('NDL3_elapse_time', this.net, SEND_INTERVAL);
  call('NDL3_L2_pop', this.net, send_block, MAX_XBEE_PAYLOAD_SIZE, out_size_ptr);
  var length = emcc_tools.get_ptr(ndl3, out_size_ptr);
  // 1 + makes room for ident byte.
  var send_buf = buffer.Buffer(1 + length);
  send_buf[0] = typpo.get_const('NDL3_IDENT');
  emcc_tools.ptr_to_buffer(ndl3, send_block, length, send_buf, 1);
  call('free', out_size_ptr);
  call('free', send_block);
  var xbee_packet = xbee.createPacket(send_buf, this.address);
  this.emit('send_data', xbee_packet);
  throw_on_NDL3_error(this.net);
}

function read_handler(data) {
  /* jshint validthis: true */
  this.emit('data', data);
}

function recv_L2 (evt) {
  /* jshint validthis: true */
  var rxbuf = evt.data;
  var ptr = emcc_tools.buffer_to_ptr(ndl3, rxbuf);
  call('NDL3_L2_push', this.net, ptr, rxbuf.length);
  call('free', ptr);

  check_L3.apply(this);
  // Ignore errors due to bad packets.
  call('NDL3_pop_error', this.net);<commit_msg>Get data from event in the radio read handler.<commit_after>/* jshint globalstrict: true */
"use strict";

const EventEmitter = require('tenshi/vendor-js/EventEmitter');
const ubjson = require('tenshi/common/ubjson');
const buffer = require('sdk/io/buffer');
const emcc_tools = require('tenshi/common/emcc_tools');
const xbee = require('tenshi/common/xbee');
const typpo = require('tenshi/common/typpo');

const MAX_XBEE_PAYLOAD_SIZE = 256;
const SEND_INTERVAL = 100;

const UBJSON_PORT = typpo.get_const('NDL3_UBJSON_PORT');
const STRING_PORT = typpo.get_const('NDL3_STRING_PORT');
const CODE_PORT = typpo.get_const('NDL3_CODE_PORT');

try {
  var ndl3 = require('tenshi/vendor-js/ndl3');
} catch (_) {
  console.warn('Could not load ndl3.js. Did you run build.sh?');
}

function call(name) {
  var args = Array.prototype.slice.call(arguments, 1);
  var arg_ts = [];
  for (var i = 0; i < args.length; i++) {
    arg_ts.push('number');
  }
  return ndl3.ccall(name, 'number', arg_ts, args);
}

var Radio = function(address, serportObj) {
  EventEmitter.apply(this);
  this.connected = false;
  this.net = call('NDL3_new', 0, 0, 0);
  if (this.net === 0) {
    throw 'Could not allocate NDL3.';
  }
  call('NDL3_open', this.net, UBJSON_PORT);
  call('NDL3_open', this.net, STRING_PORT);
  call('NDL3_open', this.net, CODE_PORT);

  this._tick = send_L2.bind(this);
  setInterval(this._tick, SEND_INTERVAL);

  this.on('send_object', send_object.bind(this));
  this.on('send_string', send_string.bind(this));
  this.on('send_code', send_code.bind(this));
  this.on('data', recv_L2.bind(this));

  if (address !== undefined &&
      serportObj !== undefined) {
    this.connectXBee(address, serportObj);
  } else {
    this.address = null;
    this.serportObj = null;
  }

  // Print out every message in or out of the radio, for debugging
  // purposes.
  var events = ['data', 'object', 'string', 'code'];
  function make_evt_callback(evt) {
    return function (data) {
      console.log('radio: ', evt, ':', data);
    };
  }
  for (var e = 0; e < events.length; e++) {
    var evt = events[e];
    this.on(evt, make_evt_callback(evt));
    this.on('send_' + evt, make_evt_callback('send_' + evt));
  }
};

Radio.prototype = Object.create(EventEmitter.prototype);

Radio.prototype.isConnected = function () {
  return this.connected;
};

Radio.prototype.connectXBee = function (address, serportObj) {
  if (this.connected) {
    throw 'XBee has already been connected to radio.';
  }
  this.address = address;
  this.serportObj = serportObj;
  serportObj.setReadHandler(read_handler.bind(this));
  serportObj.setReadHandler = function () {
    throw 'Radio already attached to serial port.';
  };
  this._send_data = send_data.bind(this);
  this.on('send_data', this._send_data);
};

Radio.prototype.disconnectXBee = function () {
  this.off('send_data', this._send_data);
  delete this.serportObj.setReadHandler;
  this.serportObj.setReadHandler(null);
  this.address = null;
  this.serportObj = null;
};

function send_L2 () {
  /* jshint validthis: true */
  var out_size_ptr = call('malloc', emcc_tools.PTR_SIZE);
  var send_block = call('malloc', MAX_XBEE_PAYLOAD_SIZE);
  if (out_size_ptr === 0 || send_block === 0) {
    throw 'malloc failed';
  }
  call('NDL3_elapse_time', this.net, SEND_INTERVAL);
  call('NDL3_L2_pop', this.net, send_block, MAX_XBEE_PAYLOAD_SIZE, out_size_ptr);
  var length = emcc_tools.get_ptr(ndl3, out_size_ptr);
  // 1 + makes room for ident byte.
  var send_buf = buffer.Buffer(1 + length);
  send_buf[0] = typpo.get_const('NDL3_IDENT');
  emcc_tools.ptr_to_buffer(ndl3, send_block, length, send_buf, 1);
  call('free', out_size_ptr);
  call('free', send_block);
  var xbee_packet = xbee.createPacket(send_buf, this.address);
  this.emit('send_data', xbee_packet);
  throw_on_NDL3_error(this.net);
}

function read_handler(evt) {
  /* jshint validthis: true */
  this.emit('data', evt.data);
}

function recv_L2 (rxbuf) {
  /* jshint validthis: true */
  var ptr = emcc_tools.buffer_to_ptr(ndl3, rxbuf);
  call('NDL3_L2_push', this.net, ptr, rxbuf.length);
  call('free', ptr);

  check_L3.apply(this);
  // Ignore errors due to bad packets.
  call('NDL3_pop_error', this.net);
{'fraction_non_alphanumeric': 0.1548977650974798, 'fraction_numerical': 0.011174512601046125, 'mean_word_length': 4.406812339331619, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1207832', 'n_tokens_mistral': 3172, 'n_tokens_neox': 3157, 'n_words': 632}