Difference between revisions of "Talk:RoboRumble/Development"

From Robowiki
Jump to navigation Jump to search
m (fix code formatting)
m (Using <syntaxhighlight>.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Background Uploader==
+
==Background Uploader -- Deprecated==
Test N°1:
+
 
<pre>
+
This script is no longer necessary due to a server's upload speed-up.
 +
 
 +
This code, if run with a script in background after every iteration of roborumble's client, will upload your result while your client is fighting a new iteration, reducing the iteration time of 25% in the best case.
 +
 
 +
I'm actually patching roborumble 1.6.0 code to include this script's functionally (simply threading it and modifying the logging system for multi thread) in the hope this will be included in the next official roborumble's client
 +
 
 +
see after the code for installation instruction
 +
----
 +
ATTENTION: you MUST set roborumble.txt UPLOAD=NOT, ITERATION=NOT, and this code WORKS ONLY FOR 1VS1!!
 +
This script is no longer necessary due to a server's upload speed-up.
 +
----
 +
 
 +
<syntaxhighlight>
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
 +
import java.io.BufferedWriter;
 
import java.io.File;
 
import java.io.File;
import java.io.FileOutputStream;
 
 
import java.io.FileReader;
 
import java.io.FileReader;
 +
import java.io.FileWriter;
 
import java.io.InputStreamReader;
 
import java.io.InputStreamReader;
 
import java.io.OutputStreamWriter;
 
import java.io.OutputStreamWriter;
import java.io.PrintStream;
 
 
import java.net.HttpURLConnection;
 
import java.net.HttpURLConnection;
 
import java.net.URL;
 
import java.net.URL;
Line 15: Line 27:
 
public class UploaderMain {
 
public class UploaderMain {
  
 +
static String infoThread;
 
String clientVersion = "1";
 
String clientVersion = "1";
 
 
Line 23: Line 36:
 
String infoFilePath = "./roborumble/roborumble.txt";
 
String infoFilePath = "./roborumble/roborumble.txt";
 
 
if (args.length != 0)
+
switch (args.length){
if (args.length == 1)
+
case 2:
infoFilePath = args[0];
+
infoFilePath = args[1];
else{
+
//whitout break;
System.out.println("Usage: java UploaderMain [option]\nOption: Path to roborumble settings file");
+
case 1:
 +
infoThread = args[0];
 +
break;
 +
case 0:
 +
infoThread = "";
 +
break;
 +
default:
 +
System.out.println("Usage: java UploaderMain [id] [path]\nid: name of this thread\npath: Path to roborumble settings file");
 
return;
 
return;
}
+
}
 
 
 
File test = new File(infoFilePath);
 
File test = new File(infoFilePath);
 
if ( !test.exists() ){
 
if ( !test.exists() ){
+
System.err.println("Roborumble setting file not found, try to specify his pah by default");
 +
return;
 
}
 
}
 
 
Line 40: Line 61:
 
 
 
public UploaderMain(String infoFilePath){
 
public UploaderMain(String infoFilePath){
 +
 +
logOut("Starting Uploader");
 
 
 
// read info from file
 
// read info from file
ArrayList<String> allInfo = read( infoFilePath );
+
ArrayList<String> allInfo = readFIle( infoFilePath );
 
// extact info
 
// extact info
 
String[] info = findInfo(allInfo);
 
String[] info = findInfo(allInfo);
Line 56: Line 79:
 
 
 
// read battle from file
 
// read battle from file
ArrayList<String> battles = read( battleFilePath );
+
ArrayList<String> battles = readFIle( battleFilePath );
 
 
 
if (battles == null){
 
if (battles == null){
Line 63: Line 86:
 
}
 
}
 
 
// now delete the battles
+
// delete old battle file
File battleFileOld = new File(battleFilePath);
+
File oldBattle = new File( battleFilePath );
if (!battleFileOld.delete())
+
oldBattle.delete();
throw new IllegalArgumentException("Delete: deletion failed");
 
 
 
 
// prepare data to be uploaded (only 1vs1 supported)
 
// prepare data to be uploaded (only 1vs1 supported)
Line 72: Line 94:
 
 
 
ArrayList<String> battlesNotSend;
 
ArrayList<String> battlesNotSend;
long countTry = 10;
+
long countTry = 0;
while ((battles.size() > 0) || (countTry > maxTry)){
+
while ((battles.size() > 0) && (countTry < maxTry)){
 
battlesNotSend = new ArrayList<String>();
 
battlesNotSend = new ArrayList<String>();
 
for (String battle : battles){
 
for (String battle : battles){
Line 80: Line 102:
 
}
 
}
 
battles = battlesNotSend;  
 
battles = battlesNotSend;  
 +
countTry++;
 
}
 
}
 +
logOut("Stopping Uploader, number of not uploaded battle: "+battles.size());
 +
if (battles.size() > 0)
 +
for (String battle:battles)
 +
write(battle+"\n", "results.txt");
 
}
 
}
  
Line 95: Line 122:
 
for (String ris : allInfo){
 
for (String ris : allInfo){
 
if (ris.indexOf("RESULTSURL=") >= 0){
 
if (ris.indexOf("RESULTSURL=") >= 0){
logOut(ris.substring(ris.indexOf("=")+1));
 
 
info[0] = ris.substring(ris.indexOf("=")+1);
 
info[0] = ris.substring(ris.indexOf("=")+1);
 
}
 
}
 
if (ris.indexOf("OUTPUT=") >= 0){
 
if (ris.indexOf("OUTPUT=") >= 0){
logOut(ris.substring(ris.indexOf("=")+1));
 
 
info[1] = ris.substring(ris.indexOf("=")+1);
 
info[1] = ris.substring(ris.indexOf("=")+1);
 
}
 
}
Line 183: Line 208:
 
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
 
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
 
httpURLConnection.setRequestMethod( "POST" ) ;
 
httpURLConnection.setRequestMethod( "POST" ) ;
 +
//httpURLConnection.setDoInput(true);
 +
httpURLConnection.setDoOutput(true);
 
httpURLConnection.connect() ;
 
httpURLConnection.connect() ;
 
 
Line 193: Line 220:
 
BufferedReader dataInputStream = new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream() ) );
 
BufferedReader dataInputStream = new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream() ) );
 
String response = new String();
 
String response = new String();
 +
boolean okUpload = false;
 
while(true){
 
while(true){
 
response = dataInputStream.readLine();
 
response = dataInputStream.readLine();
 
if ( response == null )
 
if ( response == null )
 
break;
 
break;
 +
if ( response.indexOf("OK.") == 0 )
 +
okUpload = true;
 
logOut("risposta: "+response);
 
logOut("risposta: "+response);
 
}
 
}
 
dataInputStream.close();
 
dataInputStream.close();
 +
return okUpload;
 
 
 
}catch(Exception e){
 
}catch(Exception e){
Line 216: Line 247:
  
 
private void log(String string, String fileName) {
 
private void log(String string, String fileName) {
FileOutputStream file;
+
write(infoThread+": "+string+"\n", fileName);
 +
}
 +
 +
private void write(String string, String fileName) {
 
try {
 
try {
file = new FileOutputStream(fileName);
+
BufferedWriter output = new BufferedWriter(new FileWriter(fileName, true));
PrintStream output = new PrintStream(file);
+
output.write(string);
output.println(string);
 
 
output.close();
 
output.close();
 
} catch (Exception e) {
 
} catch (Exception e) {
// TODO Auto-generated catch block
 
 
e.printStackTrace();
 
e.printStackTrace();
 
}
 
}
 
 
}
 
}
  
private ArrayList<String> read( String filePath ) {
+
private ArrayList<String> readFIle( String filePath ) {
 
 
 
try{
 
try{
Line 253: Line 284:
 
}
 
}
 
}
 
}
 +
</syntaxhighlight>
 +
thank to [[User:Rednaxela|Rednaxela]] for code formatting
 +
 +
INSTALLATION
 +
copy and compile the code in the robocode home directory(where there is roborumble.bat and roborumble.sh), then if you are windows user create a new file "nameAsYouWant.bat" and write on it (script is not tested!):
 +
<pre>
 +
:inizio
 +
 +
CALL roborumble.bat
 +
 +
START java UploaderMain
 +
GOTO inizio
 +
 +
</pre>
 +
if you are under UNIX create a new directory "log" new file "nameAsYouWant.sh" and write on it the following code. This script is more advanced: in the log directory you will find the log of all client's iteration, with at the end the execution time. you can find also 2 log file of the uploader:
 +
#errUpdaters.txt created and filled with the uploader's error (no error no file)
 +
#outUploader.txt filled with server response and live time of uploaders
 +
#results.txt
 +
this last 2 file have to be deleted sometimes
 +
<pre>
 +
#!/bin/bash
 +
echo
 +
count=0
 +
while [ "$var1" != "fine" ]    #  while test "$var1" != "fine", forever
 +
do
 +
let "count=count+1"
 +
date
 +
echo "battle n: " $count
 +
time ./roborumble.sh &> ./log/temp$count.txt
 +
java UploaderMain $count &
 +
echo
 +
done 
 +
 +
exit 0
 
</pre>
 
</pre>
 +
WARNING: in the robocode's home directory there will be created a file called results.txt with all unloaded battle; delete it sometimes, with errUpdaters.txt and outUpdaters.txt if you're using unix's script.
 +
--[[User:Lestofante|lestofante]] 00:09, 6 December 2008 (UTC)
  
--[[User:Lestofante|lestofante]] 21:16, 4 December 2008 (UTC)
+
Hmm, when implementing this functionality as a patch, could you make it an option? Or alternatively, at least also make an option for a delay between iterations? I don't like how hot my laptop CPU gets if it doesn't get any breaks ;) --[[User:Rednaxela|Rednaxela]] 02:41, 6 December 2008 (UTC)

Latest revision as of 10:35, 1 July 2010

Background Uploader -- Deprecated

This script is no longer necessary due to a server's upload speed-up.

This code, if run with a script in background after every iteration of roborumble's client, will upload your result while your client is fighting a new iteration, reducing the iteration time of 25% in the best case.

I'm actually patching roborumble 1.6.0 code to include this script's functionally (simply threading it and modifying the logging system for multi thread) in the hope this will be included in the next official roborumble's client

see after the code for installation instruction


ATTENTION: you MUST set roborumble.txt UPLOAD=NOT, ITERATION=NOT, and this code WORKS ONLY FOR 1VS1!! This script is no longer necessary due to a server's upload speed-up.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class UploaderMain {

	static String infoThread;
	String clientVersion = "1";
	
	long maxTry = 10;
	
	public static void main(String args[]){
		
		String infoFilePath = "./roborumble/roborumble.txt";
		
		switch (args.length){
			case 2:
				infoFilePath = args[1];
				//whitout break;
			case 1:
				infoThread = args[0];
				break;
			case 0:
				infoThread = "";
				break;
			default:
				System.out.println("Usage: java UploaderMain [id] [path]\nid: name of this thread\npath: Path to roborumble settings file");
				return;
		}
		
		File test = new File(infoFilePath);
		if ( !test.exists() ){
			System.err.println("Roborumble setting file not found, try to specify his pah by default");
			return;
		}
		
		new UploaderMain(infoFilePath);
	}
	
	public UploaderMain(String infoFilePath){
		
		logOut("Starting Uploader");
		
//		read info from file
		ArrayList<String> allInfo = readFIle( infoFilePath );
//		extact info
		String[] info = findInfo(allInfo);
		
		if ((info == null) || (info.length != 2)){
			logErr("Roborumble settings file invalid");
			return;
		}
		
//		set info
		String uploadServerUrl = info[0];
		String battleFilePath = info[1];
		
//		read battle from file
		ArrayList<String> battles = readFIle( battleFilePath );
		
		if (battles == null){
			logErr("Battle files format invalid");
			return;
		}
		
//		delete old battle file
		File oldBattle = new File( battleFilePath );
		oldBattle.delete();
		
//		prepare data to be uploaded (only 1vs1 supported)
		battles = prepareToUpload1VS1(battles);
		
		ArrayList<String> battlesNotSend;
		long countTry = 0;
		while ((battles.size() > 0) && (countTry < maxTry)){
			battlesNotSend = new ArrayList<String>();
			for (String battle : battles){
				if ( !sendBattle(battle, uploadServerUrl) )
					battlesNotSend.add(battle);
			}
			battles = battlesNotSend; 
			countTry++;
		}
		logOut("Stopping Uploader, number of not uploaded battle: "+battles.size());
		if (battles.size() > 0)
			for (String battle:battles)
				write(battle+"\n", "results.txt");
	}

	private String[] findInfo(ArrayList<String> allInfo) {
		/*
		 * FIND INFO:
		 * at [0]: uploadServerUrl - RESULTSURL=
		 * at [1]: battleFilePath - OUTPUT=
		 */
		String[] info = new String[2];
		info[0] = null;
		info[1] = null;
		
		for (String ris : allInfo){
			if (ris.indexOf("RESULTSURL=") >= 0){
				info[0] = ris.substring(ris.indexOf("=")+1);
			}
			if (ris.indexOf("OUTPUT=") >= 0){
				info[1] = ris.substring(ris.indexOf("=")+1);
			}	
		}
		
		if ((info[0] != null)&&(info[1] != null))
			return info;
		
		return null;
	}

	private ArrayList<String> prepareToUpload1VS1(ArrayList<String> battles) {
		
		ArrayList<String> preparedBattles = new ArrayList<String>();
		
		String[] parametres = new String[6];
		String preparingBattle;
		
		for(int i = 0; i < battles.size(); i++){
			
			//extrapolate battlefield information
			parametres = battles.get(i).split(",");
			
			if ( parametres.length != 6 ){
				logErr( "invalid battlefield: "+battles.get(i) );
				continue;
			}
				
			
//			preparing string to upload
			preparingBattle = new String();
			preparingBattle += "version="+clientVersion;
			preparingBattle += "&game="+parametres[0];
			preparingBattle += "&rounds="+parametres[1];
			preparingBattle += "&field="+parametres[2];
			preparingBattle += "&user="+parametres[3];
			preparingBattle += "&time="+parametres[4];


				
//			extrapolate robot 1
			i++;
			parametres = battles.get(i).split(",");
				
//			if invalid information
			if ( parametres.length != 4 ){
				logErr( "invalid robot: "+battles.get(i) );
				continue;
			}
				
//			preparing string to upload
			preparingBattle += "&fname="+parametres[0];
			preparingBattle += "&fscore="+parametres[1];
			preparingBattle += "&fbulletd="+parametres[2];
			preparingBattle += "&fsurvival="+parametres[3];

//			extrapolate robot 2
			i++;
			parametres = battles.get(i).split(",");
				
//			if invalid information
			if ( parametres.length != 4 ){
				logErr( "invalid robot: "+battles.get(i) );
				continue;
			}
				
//			preparing string to upload
			preparingBattle += "&sname="+parametres[0];
			preparingBattle += "&sscore="+parametres[1];
			preparingBattle += "&sbulletd="+parametres[2];
			preparingBattle += "&ssurvival="+parametres[3];
			preparedBattles.add(preparingBattle);
		}
		
		return preparedBattles;
	}
	
	private boolean sendBattle(String battle, String uploadServerURL){
//		crea l'URL e la connessione HTTP con la servlet
		try{
			
			URL url = new URL(uploadServerURL);
			HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
			httpURLConnection.setRequestMethod( "POST" ) ;
			//httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			httpURLConnection.connect() ;
			

			OutputStreamWriter dataOutputStream = new OutputStreamWriter(httpURLConnection.getOutputStream());
			dataOutputStream.write( battle );
			dataOutputStream.flush();
			dataOutputStream.close();
			
			BufferedReader dataInputStream = new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream() ) );
			String response = new String();
			boolean okUpload = false; 
			while(true){
				response = dataInputStream.readLine();
				if ( response == null )
					break;
				if ( response.indexOf("OK.") == 0 )
					okUpload = true;
				logOut("risposta: "+response);
			}
			dataInputStream.close();
			return okUpload;
			
		}catch(Exception e){
			logErr("error: "+e);
		}
		return false;
	}

	private void logOut(String string) {
		log(string, "./log/outUploader");
	}
	
	private void logErr(String string) {
		log(string, "./log/errUploader");
	}

	private void log(String string, String fileName) {
		write(infoThread+": "+string+"\n", fileName);
	}
	
	private void write(String string, String fileName) {
		try {
			BufferedWriter output = new BufferedWriter(new FileWriter(fileName, true));
			output.write(string);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private ArrayList<String> readFIle( String filePath ) {
		
		try{
			
			ArrayList<String> battles = new ArrayList<String>();
			FileReader battleFile = new FileReader( filePath );
			BufferedReader battleReader = new BufferedReader(battleFile);
			String battle = null;
			while(true){
				battle = battleReader.readLine();
				if (battle==null)
					break;
				battles.add(battle);
			}
			battleReader.close();
			
			return battles;
			
		}catch(Exception e){
			System.out.println("Error: "+e);
			return null;
		}
	}
}

thank to Rednaxela for code formatting

INSTALLATION copy and compile the code in the robocode home directory(where there is roborumble.bat and roborumble.sh), then if you are windows user create a new file "nameAsYouWant.bat" and write on it (script is not tested!):

:inizio

CALL roborumble.bat

START java UploaderMain
GOTO inizio

if you are under UNIX create a new directory "log" new file "nameAsYouWant.sh" and write on it the following code. This script is more advanced: in the log directory you will find the log of all client's iteration, with at the end the execution time. you can find also 2 log file of the uploader:

  1. errUpdaters.txt created and filled with the uploader's error (no error no file)
  2. outUploader.txt filled with server response and live time of uploaders
  3. results.txt

this last 2 file have to be deleted sometimes

#!/bin/bash
echo
count=0
while [ "$var1" != "fine" ]     #  while test "$var1" != "fine", forever
do
	let "count=count+1"
	date
	echo "battle n: " $count
	time ./roborumble.sh &> ./log/temp$count.txt
	java UploaderMain $count &
	echo
done  

exit 0

WARNING: in the robocode's home directory there will be created a file called results.txt with all unloaded battle; delete it sometimes, with errUpdaters.txt and outUpdaters.txt if you're using unix's script. --lestofante 00:09, 6 December 2008 (UTC)

Hmm, when implementing this functionality as a patch, could you make it an option? Or alternatively, at least also make an option for a delay between iterations? I don't like how hot my laptop CPU gets if it doesn't get any breaks ;) --Rednaxela 02:41, 6 December 2008 (UTC)