<commit_before>
(function(port){
	//create the websocket server and start running.
	var WebSocketServer = require('ws').Server;
	var wss = new WebSocketServer({port: port});

	//include filesystem now, used later
	var fs=require('fs');
	var shell = require('child_process');

	//initialize client id, counter
	var client=0; 

	var folderName=function(i){
		return 'client_'+('0000000'+i).slice(-7);
	};


	//remove all folders that where created from any previous server instances.
	var cleanup=function(){

		var i=0, recurse=false;
		if(arguments.length){
			i=arguments[0];
		}else{
			recurse=true;
		}

		var folder=folderName(i);

		fs.exists(folder, (function(folder){ 
			return function (exists) {
				if(exists){
					fs.readdir(folder,function(err, files){
						if(err){
							console.log('Error reading dir: '+folder);
						}else{
							files.forEach(function(f){
								fs.unlink(folder+'/'+f,function(err){
									if(err){
										throw err;
									}else{
										//console.log('Deleted: '+folder+'/'+f);
										//too much logging...
									}
								});
							});

							fs.rmdir(folder,function(err){
								if(err){
									throw err;
								}else{
									console.log((recurse?'':(i+': '))+'Deleted: '+folder+'/');
								}
							});
							if(recurse===true){
								cleanup(i+1);
							}
						}
					});

				}
			}; 
		})(folder));

	};
	cleanup(); //need to empty folders

	var clients={};
	var getClientParam=function(cid, param, defaultValue){

		if(clients["_"+cid]){
			var c=clients["_"+cid].config();
			if(c[param]){
				return c[param];
			}
		}

		return defaultValue;
	};

	wss.on('connection', function(ws) {


		var clientMode=['command']; 
		var clientConfig=[{}];

		var mode=function(){
			return clientMode[clientMode.length-1];
		};


		var config=function(){
			return clientConfig[clientConfig.length-1];
		};

		var emptyHandler=function(data){
			console.log((cid)+': discarded binary data, empty handler');
		};




		var dataHandler=emptyHandler;

		var cid=client; //this is the current connections id.
		client++; //increment for other connections

		clients['_'+cid]={

				config:config,
				mode:mode,
				ws:ws

		};

		var clientsfolder=folderName(cid);

		fs.mkdir(clientsfolder);

		console.log('Connected Client: '+(cid)+', with folder: '+clientsfolder+'. mode: '+mode());


		var process=function(data, flags){
			if(flags){
				if(flags.binary){  

					dataHandler(data);

				}else{
					console.log((cid)+': '+data);

					if(mode()==='command'){

						//basically want to support a number of data stream types, 
						//for html5 it is not possible(?) to stream encoded video data, so support
						//for image frames is necessary

						if(data.indexOf('begin captureimageframes')===0){

							//arguments should be parsed from data, eg data might be: 'begin captureimageframes -fps 10 -mime png'

							clientMode.push('captureimageframes');
							clientConfig.push({
								ext:"png"
							});
							dataHandler=(function(){ 
								var i=1;
								return function(data){
									var opts=config();
									fs.writeFile(clientsfolder+'/f_'+('000000'+(i++)).slice(-6)+'.'+opts.ext, data, function (err) {
										if (err) throw err;
									});	
								}; 
							})();

							console.log((cid)+': mode: '+mode());

						}else if(data.indexOf('begin audioupload')===0){

							//arguments should be parsed from data, eg data might be: 'begin captureimageframes -fps 10 -mime png'

							clientMode.push('audioupload');
							clientConfig.push({
								ext:"wav"
							});
							dataHandler=(function(){ 
								var i=1;
								return function(data){
									var opts=config();
									fs.writeFile(clientsfolder+'/a_'+('000'+(i++)).slice(-3)+'.'+opts.ext, data, function (err) {<commit_msg>add support for jpg encoded image frames<commit_after>
(function(port){
	//create the websocket server and start running.
	var WebSocketServer = require('ws').Server;
	var wss = new WebSocketServer({port: port});

	//include filesystem now, used later
	var fs=require('fs');
	var shell = require('child_process');

	//initialize client id, counter
	var client=0; 

	var folderName=function(i){
		return 'client_'+('0000000'+i).slice(-7);
	};


	//remove all folders that where created from any previous server instances.
	var cleanup=function(){

		var i=0, recurse=false;
		if(arguments.length){
			i=arguments[0];
		}else{
			recurse=true;
		}

		var folder=folderName(i);

		fs.exists(folder, (function(folder){ 
			return function (exists) {
				if(exists){
					fs.readdir(folder,function(err, files){
						if(err){
							console.log('Error reading dir: '+folder);
						}else{
							files.forEach(function(f){
								fs.unlink(folder+'/'+f,function(err){
									if(err){
										throw err;
									}else{
										//console.log('Deleted: '+folder+'/'+f);
										//too much logging...
									}
								});
							});

							fs.rmdir(folder,function(err){
								if(err){
									throw err;
								}else{
									console.log((recurse?'':(i+': '))+'Deleted: '+folder+'/');
								}
							});
							if(recurse===true){
								cleanup(i+1);
							}
						}
					});

				}
			}; 
		})(folder));

	};
	cleanup(); //need to empty folders

	var clients={};
	var getClientParam=function(cid, param, defaultValue){

		if(clients["_"+cid]){
			var c=clients["_"+cid].config();
			if(c[param]){
				return c[param];
			}
		}

		return defaultValue;
	};

	wss.on('connection', function(ws) {


		var clientMode=['command']; 
		var clientConfig=[{}];

		var mode=function(){
			return clientMode[clientMode.length-1];
		};


		var config=function(){
			return clientConfig[clientConfig.length-1];
		};

		var emptyHandler=function(data){
			console.log((cid)+': discarded binary data, empty handler');
		};




		var dataHandler=emptyHandler;

		var cid=client; //this is the current connections id.
		client++; //increment for other connections

		clients['_'+cid]={

				config:config,
				mode:mode,
				ws:ws

		};

		var clientsfolder=folderName(cid);

		fs.mkdir(clientsfolder);

		console.log('Connected Client: '+(cid)+', with folder: '+clientsfolder+'. mode: '+mode());


		var process=function(data, flags){
			if(flags){
				if(flags.binary){  

					dataHandler(data);

				}else{
					console.log((cid)+': '+data);

					if(mode()==='command'){

						//basically want to support a number of data stream types, 
						//for html5 it is not possible(?) to stream encoded video data, so support
						//for image frames is necessary

						if(data.indexOf('begin captureimageframes')===0){

							//arguments should be parsed from data, eg data might be: 'begin captureimageframes -mime png'
							var d={
								ext:'png'
							};
							data.split(' -').forEach(function(s){
								
								if(s.indexOf('mime ')===0){
									var m=s.split(' ');
									if(m.length==2&&(['png', 'jpg']).indexOf(m[1])>=0){
										d.ext=m[1];
									}
								}
								
							});

							clientMode.push('captureimageframes');
							clientConfig.push(d);
							dataHandler=(function(){ 
								var i=1;
								return function(data){
									var opts=config();
									fs.writeFile(clientsfolder+'/f_'+('000000'+(i++)).slice(-6)+'.'+opts.ext, data, function (err) {
										if (err) throw err;
									});	
								}; 
							})();

							console.log((cid)+': mode: '+mode());

						}else if(data.indexOf('begin audioupload')===0){

							//arguments should be parsed from data, eg data might be: 'begin captureimageframes -fps 10 -mime png'

							clientMode.push('audioupload');
							clientConfig.push({
								ext:"wav"
							});
							dataHandler=(function(){ 
								var i=1;
								return function(data){
									var opts=config();
									fs.writeFile(clientsfolder+'/a_'+('000'+(i++)).slice(-3)+'.'+opts.ext, data, function (err) {
{'fraction_non_alphanumeric': 0.1697061803444782, 'fraction_numerical': 0.008991894630192503, 'mean_word_length': 2.6374942422846614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7009422', 'n_tokens_mistral': 3610, 'n_tokens_neox': 2501, 'n_words': 567}