<commit_before>#!/usr/bin/env node

var fs =	require('fs');
var path =	require('path');
var dgram = require('dgram');
var http =	require('http');
var os =	require('os');

var Logger = require('./util/logger');
var Rpc_udp = require('./rpc_udp');
var Rpc_http = require('./rpc_http');
var Http_request_handler = require('./util/http_request_handler');
var Config = require('./config');
var User_util = require('./util/user');
var Device = require('./device');
var Nrf = require('./nrf24l01');
var Nrf_scheduler = require('./util/nrf-task-scheduler');
var Packet = require('./util/packet.js').Packet;
var Packet_queue = require('./util/packet.js').Packet_queue;

function OHC()
{
	this.logger = new Logger('OHC');
	this.logger.set_devel(Logger.level.info);
	this.tokens = new Array();
	this.token_length = 32;
	this.devices = new Object();
	this.device_id_by_index = new Array();
	this.num_devices = 0;
	var conf_dir = path.join(__dirname, 'config');
	if(!fs.existsSync(conf_dir)) //Create config dir if not exists
	{
		fs.mkdirSync(conf_dir, 0750);
	}
	this.conffile_devices = path.join(conf_dir, 'devices.json');
	this.conffile_users = path.join(conf_dir, 'users.json');
	this.conffile = path.join(conf_dir, 'config.json');
	this.load_config();
	this.user_util = new User_util(this);
	this.rf_address = new Buffer(this.config.config.rf.address);
	this.init_rf();
	this.nrf_busy = false;
}

OHC.prototype.check_tx_queue = function() //Helper function to resume queue if interrupt is missed for some reason
{
	if(this.packet_queue.queue.length > 0 && !this.nrf_busy)
	{
		this.rf_send_packet(this.packet_queue.next());
	}
	setTimeout(function(ohc) {
		return function() {
			ohc.check_tx_queue();
		}
	}(this), 50);
}

OHC.prototype.init_nw_udp = function() //Sets up udp listener for broadcasts and json rpcs
{
	var interfaces = os.networkInterfaces();

	var addr = null;

	Object.keys(interfaces).forEach(function (ifname) {
		interfaces[ifname].forEach(function (iface) {
			if (iface.family != 'IPv4' || iface.internal)
			{
				return;
			}
			addr = iface.address; //Get address of ethernet interface
		});
	});

	this.logger.log("I'm @ " + addr, Logger.level.info);

	var port = this.config.config.network.port_udp;
	var rpc_udp = new Rpc_udp(this, addr, port);
	var socket = dgram.createSocket('udp4');

	var ohc = this;
	socket.on('message', function(msg, sender) { //Listen for udp packets
		(function(msg, sender) {
			this.logger.log('RX BCAST:', Logger.level.debug);
			this.logger.log('	Message: ' + msg, Logger.level.debug);
			this.logger.log('	Sender: ' + sender.address, Logger.level.debug);
			var json;
			try
			{
				json = JSON.parse(msg);
			}
			catch(ex) //Prevent gateway from crashing due to invalid input
			{
				this.logger.log('	Invalid json: ' + ex);
				return;
			}
			if(typeof json == 'undefined' || json == null)
				return;
			//Check if response port is valid
			if(typeof json.rport !== 'number' || json.rport < 1 || json.rport > 65535)
				return;
			var resp = this.exec_json_rpc(json, rpc_udp);
			if(typeof resp !== 'object')
				return;
			resp = JSON.stringify(resp);
			this.logger.log('	Response: ' + resp, Logger.level.debug);
			resp = new Buffer(resp);
			socket.send(resp, 0, resp.length, json.rport, sender.address, function(err, data) {
				(function(err, data)
				{
					if(err)
						this.logger.log("[SOCKET_TX] ERROR: " + err);<commit_msg>Small modification for new client version<commit_after>#!/usr/bin/env node

var fs =	require('fs');
var path =	require('path');
var dgram = require('dgram');
var http =	require('http');
var os =	require('os');

var Logger = require('./util/logger');
var Rpc_udp = require('./rpc_udp');
var Rpc_http = require('./rpc_http');
var Http_request_handler = require('./util/http_request_handler');
var Config = require('./config');
var User_util = require('./util/user');
var Device = require('./device');
var Nrf = require('./nrf24l01');
var Nrf_scheduler = require('./util/nrf-task-scheduler');
var Packet = require('./util/packet.js').Packet;
var Packet_queue = require('./util/packet.js').Packet_queue;

function OHC()
{
	this.logger = new Logger('OHC');
	this.logger.set_devel(Logger.level.info);
	this.tokens = new Array();
	this.token_length = 32;
	this.devices = new Object();
	this.device_id_by_index = new Array();
	this.num_devices = 0;
	var conf_dir = path.join(__dirname, 'config');
	if(!fs.existsSync(conf_dir)) //Create config dir if not exists
	{
		fs.mkdirSync(conf_dir, 0750);
	}
	this.conffile_devices = path.join(conf_dir, 'devices.json');
	this.conffile_users = path.join(conf_dir, 'users.json');
	this.conffile = path.join(conf_dir, 'config.json');
	this.load_config();
	this.user_util = new User_util(this);
	this.rf_address = new Buffer(this.config.config.rf.address);
	this.init_rf();
	this.nrf_busy = false;
}

OHC.prototype.check_tx_queue = function() //Helper function to resume queue if interrupt is missed for some reason
{
	if(this.packet_queue.queue.length > 0 && !this.nrf_busy)
	{
		this.rf_send_packet(this.packet_queue.next());
	}
	setTimeout(function(ohc) {
		return function() {
			ohc.check_tx_queue();
		}
	}(this), 50);
}

OHC.prototype.init_nw_udp = function() //Sets up udp listener for broadcasts and json rpcs
{
	var interfaces = os.networkInterfaces();

	var addr = null;

	Object.keys(interfaces).forEach(function (ifname) {
		interfaces[ifname].forEach(function (iface) {
			if (iface.family != 'IPv4' || iface.internal)
			{
				return;
			}
			addr = iface.address; //Get address of ethernet interface
		});
	});

	this.logger.log("I'm @ " + addr, Logger.level.info);

	var port = this.config.config.network.port_udp;
	var rpc_udp = new Rpc_udp(this, addr, port);
	var socket = dgram.createSocket('udp4');

	var ohc = this;
	socket.on('message', function(msg, sender) { //Listen for udp packets
		(function(msg, sender) {
			this.logger.log('RX BCAST:', Logger.level.debug);
			this.logger.log('	Message: ' + msg, Logger.level.debug);
			this.logger.log('	Sender: ' + sender.address, Logger.level.debug);
			var json;
			try
			{
				json = JSON.parse(msg);
			}
			catch(ex) //Prevent gateway from crashing due to invalid input
			{
				this.logger.log('	Invalid json: ' + ex);
				return;
			}
			if(typeof json == 'undefined' || json == null)
				return;
			//Check if response port is valid
			if(typeof json.rport !== 'number' || json.rport < 1 || json.rport > 65535)
				return;
			var resp = this.exec_json_rpc(json, rpc_udp);
			if(typeof resp !== 'object')
				return;
			resp.type = json.type;
			resp = JSON.stringify(resp);
			this.logger.log('	Response: ' + resp, Logger.level.debug);
			resp = new Buffer(resp);
			socket.send(resp, 0, resp.length, json.rport, sender.address, function(err, data) {
				(function(err, data)
				{
					if(err)
						this.logger.log("[SOCKET_TX] ERROR: " + err);
{'fraction_non_alphanumeric': 0.16501168224299065, 'fraction_numerical': 0.006717289719626168, 'mean_word_length': 4.006578947368421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7398354', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2399, 'n_words': 536}