<commit_before>#!/usr/bin/node

var fs   = require( 'fs' );
var net  = require( 'net' );
var util = require( 'util' );
var http = require( 'http' );
var tls  = require( 'tls' );

// listens on 12003-12005 and reports counts

function PortCounter( port, show ) {

	var svr = null;
	var ctr = 0;

	var connHandler = function( sk ) {

		var lns, buf = '', cct = 0;

		var remote = sk.remoteAddress + ':' + sk.remotePort;

		console.log( 'New connection on port ' + port + ' from ' + remote );
		sk.setEncoding( 'utf8' );
		sk.on( 'data', function( d ) {

			buf += d.toString( );
			lns  = buf.split( '\n' );
			buf  = lns.pop( );
			ctr += lns.length;
			cct += lns.length;

			if( show ) {
				process.stdout.write( d );
			}
		});
		sk.on( 'close', function( ) {
			console.log( 'Connection from ' + remote + ' closed after ' + cct + ' lines.' );
		});
	};

	this.count = function( ) {
		return ctr;
	};

	if( port > 13000 ) {
		var opts = {
			key:	fs.readFileSync( '../dist/tls/key.pem', 'utf8' ),
			cert:	fs.readFileSync( '../dist/tls/cert.pem', 'utf8' ),
			passphrase: '<PASSWORD>',
		};
		svr = tls.createServer( opts,connHandler );
	} else {
		svr = net.createServer( connHandler );
	}
	svr.listen( port, '0.0.0.0', function( ) {
		console.log( 'Server listening on port ' + port + ( show ? ' (echoing).' : '.' ) );
	});
}


function PostHandler( port )
{
	var server = null;

	var handler = function( req, res ) {

		var obj, text = '';

		if( req.method !== 'POST' ) {
			res.writeHead( 405 );
			res.end( );
			return;
		}

		req.on( 'data', function(chunk) {
			text += chunk.toString( );
		});

		req.on( 'error', function( err ) {
			console.log( 'Request error: ' + err.toString( ) );
			res.writeHead( 501 );
			res.end( );
			return;
		});

		req.on( 'end', function( ) {
			try {
				obj = JSON.parse( text );
			} catch( err ) {
				res.writeHead( 400 );
				res.end( );
				return;
			}

			console.log( JSON.stringify( obj ) );
		});
	};

	server = http.createServer( handler );
	server.listen( port, '0.0.0.0', function( ) {
		console.log( 'Listening for HTTP connections on port ' + port + '.' );
	});
}


var ctrs = { };
var prts = [ 12003, 12004, 12005, 13003 ];
var totl = 0;
var show = false;

for( var i = 0; i < prts.length; i++ ) {
	if( prts[i] === 13003 ) {
		show = true;
	} else {
		show = false;
	}
	ctrs[prts[i]] = {
		ctr:	new PortCounter( prts[i], show ),
		sum:	0,
		prv:    0,
		lps:    0,
		prt:	prts[i],
	};
}

var postServer = new PostHandler( 13000 );

var printf = function( ) {
	console.log( util.format.apply( util, arguments ) );
};


var rate = function( prv, cur ) {
	return (( cur - prv ) / 10).toFixed( 3 );
};

var reporter = function( ) {
<commit_msg>minor change - receiver echo port selector<commit_after>#!/usr/bin/node

var fs   = require( 'fs' );
var net  = require( 'net' );
var util = require( 'util' );
var http = require( 'http' );
var tls  = require( 'tls' );

// listens on 12003-12005 and reports counts

function PortCounter( port, show ) {

	var svr = null;
	var ctr = 0;

	var connHandler = function( sk ) {

		var lns, buf = '', cct = 0;

		var remote = sk.remoteAddress + ':' + sk.remotePort;

		console.log( 'New connection on port ' + port + ' from ' + remote );
		sk.setEncoding( 'utf8' );
		sk.on( 'data', function( d ) {

			buf += d.toString( );
			lns  = buf.split( '\n' );
			buf  = lns.pop( );
			ctr += lns.length;
			cct += lns.length;

			if( show ) {
				process.stdout.write( d );
			}
		});
		sk.on( 'close', function( ) {
			console.log( 'Connection from ' + remote + ' closed after ' + cct + ' lines.' );
		});
	};

	this.count = function( ) {
		return ctr;
	};

	if( port > 13000 ) {
		var opts = {
			key:	fs.readFileSync( '../dist/tls/key.pem', 'utf8' ),
			cert:	fs.readFileSync( '../dist/tls/cert.pem', 'utf8' ),
			passphrase: '<PASSWORD>',
		};
		svr = tls.createServer( opts,connHandler );
	} else {
		svr = net.createServer( connHandler );
	}
	svr.listen( port, '0.0.0.0', function( ) {
		console.log( 'Server listening on port ' + port + ( show ? ' (echoing).' : '.' ) );
	});
}


function PostHandler( port )
{
	var server = null;

	var handler = function( req, res ) {

		var obj, text = '';

		if( req.method !== 'POST' ) {
			res.writeHead( 405 );
			res.end( );
			return;
		}

		req.on( 'data', function(chunk) {
			text += chunk.toString( );
		});

		req.on( 'error', function( err ) {
			console.log( 'Request error: ' + err.toString( ) );
			res.writeHead( 501 );
			res.end( );
			return;
		});

		req.on( 'end', function( ) {
			try {
				obj = JSON.parse( text );
			} catch( err ) {
				res.writeHead( 400 );
				res.end( );
				return;
			}

			console.log( JSON.stringify( obj ) );
		});
	};

	server = http.createServer( handler );
	server.listen( port, '0.0.0.0', function( ) {
		console.log( 'Listening for HTTP connections on port ' + port + '.' );
	});
}


var ctrs = { };
var prts = [ 12003, 12004, 12005, 13003 ];
var totl = 0;
var show = false;

var echoport = parseInt( process.argv[2] || '13003', 10 );

for( var i = 0; i < prts.length; i++ ) {
	show = ( prts[i] === echoport );
	ctrs[prts[i]] = {
		ctr:	new PortCounter( prts[i], show ),
		sum:	0,
		prv:    0,
		lps:    0,
		prt:	prts[i],
	};
}

var postServer = new PostHandler( 13000 );

var printf = function( ) {
	console.log( util.format.apply( util, arguments ) );
};


var rate = function( prv, cur ) {
	return (( cur - prv ) / 10).toFixed( 3 );
};

var reporter = function( ) {

{'fraction_non_alphanumeric': 0.1935897435897436, 'fraction_numerical': 0.028021978021978023, 'mean_word_length': 2.6309840425531914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7178557', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2108, 'n_words': 536}