cree.js
function(){
		console.log("Last user has finished the experiment!");
		
		var userdata = new Object();
		for(var key in cloak.getUsers()) {
			cloak.getUsers()[key].data.device = parser.setUA(cloak.getUsers()[key].data._device).getResult(); // Parse the useragent before saving data.
			userdata[cloak.getUsers()[key].id] = cloak.getUsers()[key].data; 
		}
		
		// Alternatively consider using async.series or RSVP.Promise.
		var saveDataSuccess = saveExperimentSuccess = saveLogSucess = saveIPSucess= false;
		var successSave = function (){
			if(saveDataSuccess && saveExperimentSuccess && saveLogSucess && saveIPSucess) {
				
				// quit proccess
				console.log(typeof cloak);
				cloak.stop(function(){
					console.log(" -- server closed -- ");
					process.exit(0); // quit node.js process with succes
				})
				
			}
		}
		
		// Save user IPs if requireUniqueIP == TRUE, such that when running continues experiments (forever start xxx.js) users can only participate once.
		// TO-DO
		/*if(requireUniqueIP) {
			var experimentConfig = JSON.parse(fs.readFileSync(experimentURL[0], experimentURL[1]));
			for(var key in cloak.getUsers()) {
				experimentConfig.ExcludeIPs.push(cloak.getUsers()[key].data.ip);
			}
			fs.writeFileSync(experimentURL[0], JSON.stringify(experimentConfig, null, '\t'));
			console.log(experimentURL[0]);
			saveIPSucess = true;
			successSave();
		} else {
			saveIPSucess = true;
			successSave();
		}*/
		saveIPSucess = true;
		
		// Save user data.
		fs.writeFile('data/data_'+initTime.valueOf()+'_user.json', JSON.stringify(userdata,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_user.json');
			saveDataSuccess = true;
			successSave();
		});
		
		// Save additional experiment data.
		experiment.initTime = initTime.valueOf();
		experiment.timeFinalGlobal = timeGlobal.getValue();
		var experimentdata = experiment;
		fs.writeFile('data/data_'+initTime.valueOf()+'_experiment.json', JSON.stringify(experimentdata,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_experiment.json');
			saveExperimentSuccess = true;
			successSave();
		});
		
		// Save log of all messages send by the users.
		fs.writeFile('data/data_'+initTime.valueOf()+'_messages.json', JSON.stringify(storeMessage,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_message.json');
			saveLogSucess = true;
			successSave();
		});
		
		

		
	}
function(){
		console.log("Last user has finished the experiment!");
		
		var userdata = new Object();
		for(var key in cloak.getUsers()) {
			cloak.getUsers()[key].data.device = parser.setUA(cloak.getUsers()[key].data._device).getResult(); // Parse the useragent before saving data.
			userdata[cloak.getUsers()[key].id] = cloak.getUsers()[key].data; 
		}
		
		// Alternatively consider using async.series or RSVP.Promise.
		var saveDataSuccess = saveExperimentSuccess = saveLogSucess = saveIPSucess= false;
		var successSave = function (){
			if(saveDataSuccess && saveExperimentSuccess && saveLogSucess && saveIPSucess) {
				
				// quit proccess
				console.log(typeof cloak);
				cloak.stop(function(){
					console.log(" -- server closed -- ");
					process.exit(0); // quit node.js process with succes
				})
				
			}
		}
		
		// Save user IPs if requireUniqueIP == TRUE, such that when running continues experiments (forever start xxx.js) users can only participate once.
		// TO-DO
		/*if(requireUniqueIP) {
			var experimentConfig = JSON.parse(fs.readFileSync(experimentURL[0], experimentURL[1]));
			for(var key in cloak.getUsers()) {
				experimentConfig.ExcludeIPs.push(cloak.getUsers()[key].data.ip);
			}
			fs.writeFileSync(experimentURL[0], JSON.stringify(experimentConfig, null, '\t'));
			console.log(experimentURL[0]);
			saveIPSucess = true;
			successSave();
		} else {
			saveIPSucess = true;
			successSave();
		}*/
		saveIPSucess = true;
		
		// Save user data.
		fs.writeFile('data/data_'+initTime.valueOf()+'_user.json', JSON.stringify(userdata,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_user.json');
			saveDataSuccess = true;
			successSave();
		});
		
		// Save additional experiment data.
		experiment.initTime = initTime.valueOf();
		experiment.timeFinalGlobal = timeGlobal.getValue();
		var experimentdata = experiment;
		fs.writeFile('data/data_'+initTime.valueOf()+'_experiment.json', JSON.stringify(experimentdata,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_experiment.json');
			saveExperimentSuccess = true;
			successSave();
		});
		
		// Save log of all messages send by the users.
		fs.writeFile('data/data_'+initTime.valueOf()+'_messages.json', JSON.stringify(storeMessage,null,'\t'), function (err) {
			if (err) return console.log(err);
			console.log('data_'+initTime.valueOf()+'_message.json');
			saveLogSucess = true;
			successSave();
		});
		
		

		
	}
function ( ) { console . log ( "Last user has finished the experiment!" ) ; var userdata = new Object ( ) ; for ( var key in cloak . getUsers ( ) ) { cloak . getUsers ( ) [ key ] . data . device = parser . setUA ( cloak . getUsers ( ) [ key ] . data . _device ) . getResult ( ) ; // Parse the useragent before saving data. userdata [ cloak . getUsers ( ) [ key ] . id ] = cloak . getUsers ( ) [ key ] . data ; } // Alternatively consider using async.series or RSVP.Promise. var saveDataSuccess = saveExperimentSuccess = saveLogSucess = saveIPSucess = false ; var successSave = function ( ) { if ( saveDataSuccess && saveExperimentSuccess && saveLogSucess && saveIPSucess ) { // quit proccess console . log ( typeof cloak ) ; cloak . stop ( function ( ) { console . log ( " -- server closed -- " ) ; process . exit ( 0 ) ; // quit node.js process with succes } ) } } // Save user IPs if requireUniqueIP == TRUE, such that when running continues experiments (forever start xxx.js) users can only participate once. // TO-DO /*if(requireUniqueIP) {
			var experimentConfig = JSON.parse(fs.readFileSync(experimentURL[0], experimentURL[1]));
			for(var key in cloak.getUsers()) {
				experimentConfig.ExcludeIPs.push(cloak.getUsers()[key].data.ip);
			}
			fs.writeFileSync(experimentURL[0], JSON.stringify(experimentConfig, null, '\t'));
			console.log(experimentURL[0]);
			saveIPSucess = true;
			successSave();
		} else {
			saveIPSucess = true;
			successSave();
		}*/ saveIPSucess = true ; // Save user data. fs . writeFile ( 'data/data_' + initTime . valueOf ( ) + '_user.json' , JSON . stringify ( userdata , null , '\t' ) , function ( err ) { if ( err ) return console . log ( err ) ; console . log ( 'data_' + initTime . valueOf ( ) + '_user.json' ) ; saveDataSuccess = true ; successSave ( ) ; } ) ; // Save additional experiment data. experiment . initTime = initTime . valueOf ( ) ; experiment . timeFinalGlobal = timeGlobal . getValue ( ) ; var experimentdata = experiment ; fs . writeFile ( 'data/data_' + initTime . valueOf ( ) + '_experiment.json' , JSON . stringify ( experimentdata , null , '\t' ) , function ( err ) { if ( err ) return console . log ( err ) ; console . log ( 'data_' + initTime . valueOf ( ) + '_experiment.json' ) ; saveExperimentSuccess = true ; successSave ( ) ; } ) ; // Save log of all messages send by the users. fs . writeFile ( 'data/data_' + initTime . valueOf ( ) + '_messages.json' , JSON . stringify ( storeMessage , null , '\t' ) , function ( err ) { if ( err ) return console . log ( err ) ; console . log ( 'data_' + initTime . valueOf ( ) + '_message.json' ) ; saveLogSucess = true ; successSave ( ) ; } ) ; }
Finalize the experiment
Finalize the experiment