<commit_before>'use strict';

const EventEmitter = require('events');
const dgram = require('dgram');

const MULTICAST_ADDRESS = '192.168.3.11';
const MULTICAST_PORT = 4321;

/**
 * Local Developer API for the gateway. Used to read data from the gateway
 * and connected devices.
 *
 * TODO: Retries for discovery
 */
module.exports = class DeveloperApi extends EventEmitter {
	constructor(parent, address) {
		super();

		this.address = address;

		// Bind a custom function instead of using directly to skip resolving debug before id is set
		this.debug = function() { parent.debug.apply(parent, arguments); };

		this.socket = dgram.createSocket({
			type: 'udp4',
			reuseAddr: true
		});

		this.socket.on('message', this._onMessage.bind(this));
		this.socket.on('listening', () => {
			this.socket.addMembership(MULTICAST_ADDRESS);

			this.emit('ready');
		});
		this.socket.bind(9898);

		this.devices = [];
		this.ready = false;
	}

	destroy() {
		this.socket.destroy();
	}

	send(data) {
		const json = JSON.stringify(data);
		this.debug('DEV ->', json);
		this.socket.send(json, 0, json.length, this.port || MULTICAST_PORT, this.address);
	}

	_onMessage(msg) {
		let data;
		try {
			this.debug('DEV <-', msg.toString());
			data = JSON.parse(msg.toString());
		} catch(ex) {
			this.emit('error', ex);
			return;
		}

		switch(data.cmd) {
			case 'heartbeat':
			case 'report': {
				if(! this.sid && data.model === 'gateway') {
					this.sid = data.sid;<commit_msg>Exclusive bind to support use in cluster mode<commit_after>'use strict';

const EventEmitter = require('events');
const dgram = require('dgram');

const MULTICAST_ADDRESS = '192.168.3.11';
const MULTICAST_PORT = 4321;

/**
 * Local Developer API for the gateway. Used to read data from the gateway
 * and connected devices.
 *
 * TODO: Retries for discovery
 */
module.exports = class DeveloperApi extends EventEmitter {
	constructor(parent, address) {
		super();

		this.address = address;

		// Bind a custom function instead of using directly to skip resolving debug before id is set
		this.debug = function() { parent.debug.apply(parent, arguments); };

		this.socket = dgram.createSocket({
			type: 'udp4',
			reuseAddr: true
		});

		this.socket.on('message', this._onMessage.bind(this));
		this.socket.on('listening', () => {
			this.socket.addMembership(MULTICAST_ADDRESS);

			this.emit('ready');
		});
		this.socket.bind({
			port: 9898,
			exclusive: true
		});

		this.devices = [];
		this.ready = false;
	}

	destroy() {
		this.socket.destroy();
	}

	send(data) {
		const json = JSON.stringify(data);
		this.debug('DEV ->', json);
		this.socket.send(json, 0, json.length, this.port || MULTICAST_PORT, this.address);
	}

	_onMessage(msg) {
		let data;
		try {
			this.debug('DEV <-', msg.toString());
			data = JSON.parse(msg.toString());
		} catch(ex) {
			this.emit('error', ex);
			return;
		}

		switch(data.cmd) {
			case 'heartbeat':
			case 'report': {
				if(! this.sid && data.model === 'gateway') {
					this.sid = data.sid;
{'fraction_non_alphanumeric': 0.1590082644628099, 'fraction_numerical': 0.01256198347107438, 'mean_word_length': 3.5918057663125946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2254109', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1002, 'n_words': 242}